环境
  docker-ce-19.03.1-3.el7.x86_64
  CentOS 7

一、查找、拉取镜像、启动容器
1、查找镜像-docker search
默认查找Docker Hub上的镜像,举例:Docker安装nginx

[root@node105 ~]# docker search nginx
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
nginx Official build of Nginx. [OK]
jwilder/nginx-proxy Automated Nginx reverse proxy for docker con… [OK]
richarvey/nginx-php-fpm Container running Nginx + PHP-FPM capable of… [OK]
linuxserver/nginx An Nginx container, brought to you by LinuxS…
bitnami/nginx Bitnami nginx Docker Image [OK]
tiangolo/nginx-rtmp Docker image with Nginx using the nginx-rtmp… [OK]
nginxdemos/hello NGINX webserver that serves a simple page co… [OK]
jc21/nginx-proxy-manager Docker container for managing Nginx proxy ho…
nginx/nginx-ingress NGINX Ingress Controller for Kubernetes
jlesage/nginx-proxy-manager Docker container for Nginx Proxy Manager [OK]
schmunk42/nginx-redirect A very simple container to redirect HTTP tra… [OK]
crunchgeek/nginx-pagespeed Nginx with PageSpeed + GEO IP + VTS + more_s…
blacklabelops/nginx Dockerized Nginx Reverse Proxy Server. [OK]
centos/nginx--centos7 Platform for running nginx 1.8 or building n…
centos/nginx--centos7 Platform for running nginx 1.12 or building …
nginxinc/nginx-unprivileged Unprivileged NGINX Dockerfiles
nginx/nginx-prometheus-exporter NGINX Prometheus Exporter
sophos/nginx-vts-exporter Simple server that scrapes Nginx vts stats a… [OK]
1science/nginx Nginx Docker images that include Consul Temp… [OK]
mailu/nginx Mailu nginx frontend [OK]
pebbletech/nginx-proxy nginx-proxy sets up a container running ngin… [OK]
travix/nginx NGinx reverse proxy [OK]
centos/nginx--centos7 Platform for running nginx 1.10 or building …
wodby/nginx Generic nginx [OK]
ansibleplaybookbundle/nginx-apb An APB to deploy NGINX [OK]
[root@node105 ~]#

2、拉取镜像-docker pull

[root@node105 ~]# docker pull nginx
Using default tag: latest
latest: Pulling from library/nginx
1ab2bdfe9778: Pull complete
a17e64cfe253: Pull complete
e1288088c7a8: Pull complete
Digest: sha256:53ddb41e46de3d63376579acf46f9a41a8d7de33645db47a486de9769201fec9
Status: Downloaded newer image for nginx:latest
docker.io/library/nginx:latest
[root@node105 ~]#

3、列出本地镜像-docker images
下载完成后,在本地镜像列表里查到 REPOSITORY 为 nginx 的镜像

[root@node105 ~]# docker images nginx
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest 5a3221f0137b days ago 126MB
[root@node105 ~]#

4、根据镜像启动一个Nginx容器实例-docker run

[root@node105 ~]# docker run --name nginx-test -d -p 8081:80 nginx
db8b3e2f1c647bc2589f04c3984374625455434449ee01e37f0b21163362b052

执行成功返回一行字符串,表示容器ID

--name 为容器设置一个名字

-d 后台运行
-p 端口进行映射,将本地8081端口映射到容器内部的80端口(:左侧是本地端口 右侧是容器内端口)
-P 容器内部端口随机映射到主机的高端口

另外:docker create创建容器但不启动

eg:

按镜像java创建容器myjava3

$ docker create --name myjava3  -it java /bin/bash

启动myjava3

$ docker start myjava3

以交互方式连接myjava3容器 然后执行/bin/bash

$ docker exec -it 容器ID /bin/bash

5、查看容器运行情况-docker ps

[root@node105 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
db8b3e2f1c64 nginx "nginx -g 'daemon of…" minutes ago Up minutes 0.0.0.0:->/tcp nginx-test
[root@node105 ~]#

6、查看网络端口-docker port

[root@node105 ~]# docker port nginx-test-web
/tcp -> 0.0.0.0:
[root@node105 ~]#

6、查看网络端口-docker port

[root@node105 ~]# docker port nginx-test-web
/tcp -> 0.0.0.0:
[root@node105 ~]#

7、查看应用程序容器的进程-docker top

[root@node105 www]# docker top nginx-test-web
UID PID PPID C STIME TTY TIME CMD
root : ? :: nginx: master process nginx -g daemon off;
: ? :: nginx: worker process

8、查看应用程序日志-docker logs

[root@node105 www]# docker logs -f nginx-test-web

9、检查应用程序-docker inspect 返回一个 JSON 文件记录着 Docker 容器的配置和状态信息

[root@node105 ~]# docker inspect nginx-test-web
[
{
"Id": "398ec11f39b47674e99f4e2b774e5e28db9a2c2797f26433362936427c98b140",
"Created": "2019-08-25T02:20:55.894180986Z",
"Path": "nginx",
"Args": [
"-g",
"daemon off;"
],
"State": {
"Status": "running",
"Running": true,
"Paused": false,
"Restarting": false,
"OOMKilled": false,
"Dead": false,
"Pid": ,
"ExitCode": ,
"Error": "",
"StartedAt": "2019-08-25T02:20:56.331564845Z",
"FinishedAt": "0001-01-01T00:00:00Z"
},
"Image": "sha256:5a3221f0137beb960c34b9cf4455424b6210160fd618c5e79401a07d6e5a2ced",
"ResolvConfPath": "/var/lib/docker/containers/398ec11f39b47674e99f4e2b774e5e28db9a2c2797f26433362936427c98b140/resolv.conf",
"HostnamePath": "/var/lib/docker/containers/398ec11f39b47674e99f4e2b774e5e28db9a2c2797f26433362936427c98b140/hostname",
"HostsPath": "/var/lib/docker/containers/398ec11f39b47674e99f4e2b774e5e28db9a2c2797f26433362936427c98b140/hosts",
"LogPath": "/var/lib/docker/containers/398ec11f39b47674e99f4e2b774e5e28db9a2c2797f26433362936427c98b140/398ec11f39b47674e99f4e2b774e5e28db9a2c2797f26433362936427c98b140-json.log",
"Name": "/nginx-test-web",
"RestartCount": ,
"Driver": "overlay2",
"Platform": "linux",
"MountLabel": "",
"ProcessLabel": "",
"AppArmorProfile": "",
"ExecIDs": null,
"HostConfig": {
"Binds": [
"/root/nginx/www:/usr/share/nginx/html",
"/root/nginx/conf/nginx.conf:/etc/nginx/nginx.conf",
"/root/nginx/logs:/var/log/nginx"
],
"ContainerIDFile": "",
"LogConfig": {
"Type": "json-file",
"Config": {}
},
"NetworkMode": "default",
"PortBindings": {
"80/tcp": [
{
"HostIp": "",
"HostPort": ""
}
]
},
"RestartPolicy": {
"Name": "no",
"MaximumRetryCount":
},
"AutoRemove": false,
"VolumeDriver": "",
"VolumesFrom": null,
"CapAdd": null,
"CapDrop": null,
"Capabilities": null,
"Dns": [],
"DnsOptions": [],
"DnsSearch": [],
"ExtraHosts": null,
"GroupAdd": null,
"IpcMode": "private",
"Cgroup": "",
"Links": null,
"OomScoreAdj": ,
"PidMode": "",
"Privileged": false,
"PublishAllPorts": false,
"ReadonlyRootfs": false,
"SecurityOpt": null,
"UTSMode": "",
"UsernsMode": "",
"ShmSize": ,
"Runtime": "runc",
"ConsoleSize": [
, ],
"Isolation": "",
"CpuShares": ,
"Memory": ,
"NanoCpus": ,
"CgroupParent": "",
"BlkioWeight": ,
"BlkioWeightDevice": [],
"BlkioDeviceReadBps": null,
"BlkioDeviceWriteBps": null,
"BlkioDeviceReadIOps": null,
"BlkioDeviceWriteIOps": null,
"CpuPeriod": ,
"CpuQuota": ,
"CpuRealtimePeriod": ,
"CpuRealtimeRuntime": ,
"CpusetCpus": "",
"CpusetMems": "",
"Devices": [],
"DeviceCgroupRules": null,
"DeviceRequests": null,
"KernelMemory": ,
"KernelMemoryTCP": ,
"MemoryReservation": ,
"MemorySwap": ,
"MemorySwappiness": null,
"OomKillDisable": false,
"PidsLimit": null,
"Ulimits": null,
"CpuCount": ,
"CpuPercent": ,
"IOMaximumIOps": ,
"IOMaximumBandwidth": ,
"MaskedPaths": [
"/proc/asound",
"/proc/acpi",
"/proc/kcore",
"/proc/keys",
"/proc/latency_stats",
"/proc/timer_list",
"/proc/timer_stats",
"/proc/sched_debug",
"/proc/scsi",
"/sys/firmware"
],
"ReadonlyPaths": [
"/proc/bus",
"/proc/fs",
"/proc/irq",
"/proc/sys",
"/proc/sysrq-trigger"
]
},
"GraphDriver": {
"Data": {
"LowerDir": "/var/lib/docker/overlay2/ec3f5e1f9af11fe6b069a8deaebd664e1ffbea953e44296c99915a9b985cd197-init/diff:/var/lib/docker/overlay2/c4db9bb89c2305db1a0e7cf606393ae448936abe940f6718c49b676a39bfd960/diff:/var/lib/docker/overlay2/379e82acb36eb864e2ec8dc5df0bbce1faaac0d124b4d0f573474465713898d5/diff:/var/lib/docker/overlay2/3689b1b87fe3ce1d82df9b91396f9b6f40db29310589016185d9c1f9b118a082/diff",
"MergedDir": "/var/lib/docker/overlay2/ec3f5e1f9af11fe6b069a8deaebd664e1ffbea953e44296c99915a9b985cd197/merged",
"UpperDir": "/var/lib/docker/overlay2/ec3f5e1f9af11fe6b069a8deaebd664e1ffbea953e44296c99915a9b985cd197/diff",
"WorkDir": "/var/lib/docker/overlay2/ec3f5e1f9af11fe6b069a8deaebd664e1ffbea953e44296c99915a9b985cd197/work"
},
"Name": "overlay2"
},
"Mounts": [
{
"Type": "bind",
"Source": "/root/nginx/www",
"Destination": "/usr/share/nginx/html",
"Mode": "",
"RW": true,
"Propagation": "rprivate"
},
{
"Type": "bind",
"Source": "/root/nginx/conf/nginx.conf",
"Destination": "/etc/nginx/nginx.conf",
"Mode": "",
"RW": true,
"Propagation": "rprivate"
},
{
"Type": "bind",
"Source": "/root/nginx/logs",
"Destination": "/var/log/nginx",
"Mode": "",
"RW": true,
"Propagation": "rprivate"
}
],
"Config": {
"Hostname": "398ec11f39b4",
"Domainname": "",
"User": "",
"AttachStdin": false,
"AttachStdout": false,
"AttachStderr": false,
"ExposedPorts": {
"80/tcp": {}
},
"Tty": false,
"OpenStdin": false,
"StdinOnce": false,
"Env": [
"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
"NGINX_VERSION=1.17.3",
"NJS_VERSION=0.3.5",
"PKG_RELEASE=1~buster"
],
"Cmd": [
"nginx",
"-g",
"daemon off;"
],
"Image": "nginx",
"Volumes": null,
"WorkingDir": "",
"Entrypoint": null,
"OnBuild": null,
"Labels": {
"maintainer": "NGINX Docker Maintainers <docker-maint@nginx.com>"
},
"StopSignal": "SIGTERM"
},
"NetworkSettings": {
"Bridge": "",
"SandboxID": "b0edc9c3923fb5aa35c6809212b5d4ac5732344bbf05b94aa45dc9b10312f35d",
"HairpinMode": false,
"LinkLocalIPv6Address": "",
"LinkLocalIPv6PrefixLen": ,
"Ports": {
"80/tcp": [
{
"HostIp": "0.0.0.0",
"HostPort": ""
}
]
},
"SandboxKey": "/var/run/docker/netns/b0edc9c3923f",
"SecondaryIPAddresses": null,
"SecondaryIPv6Addresses": null,
"EndpointID": "dd1bdd3627df73ffbafe6743e59a02a52c9476f4849be086f95933e644df61bb",
"Gateway": "172.17.0.1",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": ,
"IPAddress": "172.17.0.3",
"IPPrefixLen": ,
"IPv6Gateway": "",
"MacAddress": "02:42:ac:11:00:03",
"Networks": {
"bridge": {
"IPAMConfig": null,
"Links": null,
"Aliases": null,
"NetworkID": "888aa5a2f0c6e980f85c259b7833c883f358a0a0db907e1d2f4459cffee7d551",
"EndpointID": "dd1bdd3627df73ffbafe6743e59a02a52c9476f4849be086f95933e644df61bb",
"Gateway": "172.17.0.1",
"IPAddress": "172.17.0.3",
"IPPrefixLen": ,
"IPv6Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": ,
"MacAddress": "02:42:ac:11:00:03",
"DriverOpts": null
}
}
}
}
]

10、关闭容器-docker stop

[root@node105 ~]# docker stop nginx-test
nginx-test
[root@node105 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@node105 ~]#

11、启动容器-docker start

[root@node105 ~]# docker start nginx-test
nginx-test
[root@node105 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
db8b3e2f1c64 nginx "nginx -g 'daemon of…" minutes ago Up seconds 0.0.0.0:->/tcp nginx-test
[root@node105 ~]#

12、重启容器

[root@node105 ~]# docker restart nginx-test
nginx-test
[root@node105 ~]# docker kill -s HUP nginx-test
nginx-test
[root@node105 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
db8b3e2f1c64 nginx "nginx -g 'daemon of…" minutes ago Up seconds 0.0.0.0:->/tcp nginx-test
[root@node105 ~]#

-s HUP 向容器发送HUP信号,表示重新加载

13、移除应用容器,容器必须是关闭的

[root@node105 ~]# docker rm nginx-test-web
nginx-test-web

二、容器使用

举例:nginx部署
1、将容器内文件拷贝出来

[root@node105 nginx]# docker cp db8b3e2f1c64:/etc/nginx/nginx.conf ~/nginx/conf

2、在本机新建部署nginx目录

[root@node105 ~]# mkdir -p ~/nginx/www ~/nginx/logs ~/nginx/conf
[root@node105 ~]# ll
total
-rw-------. root root Aug : anaconda-ks.cfg
drwxr-xr-x. root root Aug : nginx

www: 目录将映射为nginx容器配置的虚拟目录。

logs: 目录将映射为nginx容器的日志目录。
conf: 目录里的配置文件将映射为nginx容器的配置文件。

3、部署命令

[root@node105 ~]# docker run -d -p 8082:80 --name nginx-test-web -v ~/nginx/www:/usr/share/nginx/html -v ~/nginx/conf/nginx.conf:/etc/nginx/nginx.conf -v ~/nginx/logs:/var/log/nginx nginx
398ec11f39b47674e99f4e2b774e5e28db9a2c2797f26433362936427c98b140
[root@node105 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
398ec11f39b4 nginx "nginx -g 'daemon of…" seconds ago Up seconds 0.0.0.0:->/tcp nginx-test-web
[root@node105 ~]#

-v 将冒号左侧本机目录挂载到冒号右侧容器的目录,创建映射。

4、测试
进入 ~/nginx/www 目录,创建 index.html 文件,内容如下:

[root@node105 ~]# cd ./nginx/www
[root@node105 www]# vi index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>docker nginx部署测试</title>
</head>
<body>
<h1>我的第一个标题</h1>
<p>我的第一个段落。</p>
</body>
</html>

参考:
CentOS Docker 安装
https://www.runoob.com/docker/centos-docker-install.html
Windows Docker 安装
https://www.runoob.com/docker/windows-docker-install.html
Docker 命令大全
https://www.runoob.com/docker/docker-command-manual.html
Docker 资源汇总
https://www.runoob.com/docker/docker-resources.html

【Docker学习之三】Docker查找拉取镜像、启动容器、容器使用的更多相关文章

  1. Docker学习笔记二:Docker常用命令及提升拉取镜像的速度

    一.Docker命令: 1.docker images   //命令用来查看docker中所包含的镜像信息 2.docker ps -a    //命令用来查看docker中所包含所有容器信息(运行状 ...

  2. 使用 docker 拉取镜像和创建容器-nginx

    本文主要分享通过从docker  hub上获取nginx镜像 1.首先查找nginx镜像 [root@node1 ~]# docker search nginx NAME DESCRIPTION ST ...

  3. Docker学习之Docker Registry

    前言 前面已经学习了怎样通过Dockerfile来构建我们自己的镜像了,但是一个系统可能包含着很多个微服务即有很多个镜像,当镜像越来越多的时候,就必须得有一个地方来管理这些镜像,Docker官方提供了 ...

  4. 配置 Containerd 在 harbor 私有仓库拉取镜像

    官方文档地址:https://github.com/containerd/cri/blob/master/docs/registry.md 严格来说,这个具体可分为两部分 1.在k8s中使用Conta ...

  5. 【docker】【mysql】docker安装mysql,阿里云docker镜像加速器,docker搜索查看远程仓库上的镜像,docker拉取镜像,查看本地所有镜像,查看容器的运行状况,查看容器的详细信息

    在docker上安装mysql有两种方式 1.通过Dockerfile构建 2.直接在docker hub上拉取镜像安装 =================本篇采用方法2=============== ...

  6. 怎么解决docker pull拉取镜像速度过慢的问题

    在我们安装了docker之后,在利用docker pull下载镜像的时候,由于国内的源会出现的问题就是速度真的很慢,可以用龟速来形容,最痛苦的是当你耐心的等待几个小时之后,出现unexpected E ...

  7. docker拉取镜像报错:net/http: TLS handshake timeout.

    docker拉取镜像报错:net/http: TLS handshake timeout. 启动一个后台的busybox容器 [yunva@node1 network-scripts]$ docker ...

  8. Docker拉取镜像时错误解决办法

    拉取docker中的镜像时报错: [root@master ~]# docker pull docker.io/centos:latest Trying to pull repository dock ...

  9. Docker拉取镜像加速

    关于Docker拉取镜像加速 打开桌面 docker 小图标 选中框框 根据下图 添加国内的加速源即可 Docker加速源 #网易 http://hub-mirror.c.163.com #Docke ...

随机推荐

  1. 转载>>去除inline-block元素间间距的N种方法《重》

    一.现象描述 真正意义上的inline-block水平呈现的元素间,换行显示或空格分隔的情况下会有间距,很简单的个例子: <input /> <input type="su ...

  2. wordpress去掉自定义菜单的外层div

    wordpress调用自定义菜单时自动会在外层加一个<div class="menu-nav-container">,如下图所示,nav是后台定义的菜单名称,如果想把这 ...

  3. shell脚本中大于,大于等于,小于,小于等于、不等于的表示方法

    症状:shell中大于,大于等于,小于等于,lt,gt ,ne,ge,le 很对应. 应对方法: 大于 -gt (greater than) 小于 -lt (less than) 大于或等于 -ge ...

  4. TestAbstract

    public class TestAbstract { public static void main(String[] args) { System.out.println("Hello ...

  5. swift的类型系统

    顶级抽象:protocol 具体类型:值类型.引用类型 类型操作:扩展 其他: 范型.函数式类型:function.monand

  6. tomcat catalina

    脚本catalina用于启动和关闭tomcat服务器,是最关键的脚本 在tomcat里有此文件 写这个脚本的人为什么命名为catalina一开始我还以为是其初恋女友,后来才知道原来是一个小岛的名字.

  7. git 提交代码报错failed to push some refs to 解决笔记

    Administrator@SC- MINGW64 /e/gitrepository (master) $ git push django master To github.com:zgc137/dj ...

  8. 洛谷P4735题解

    若想要深入学习可持久化0-1Trie树,传送门. Description: 给定数列 \(\{a_n\}\) ,支持两种操作: 在数列尾添加一个数 \(x\) ,数列长度变成 \(n+1\) ; 给定 ...

  9. rancher2基础环境配置

    一.主机配置 1.配置要求 参考节点要求 2.主机名配置 因为K8S的规定,主机名只支持包含 - 和 .(中横线和点)两种特殊符号,并且主机名不能出现重复. 3.Hosts 配置每台主机的hosts( ...

  10. TF 保存模型为 .pb格式

    将网络模型,图加权值,保存为.pb文件  write.py # -*- coding: utf-8 -*- from __future__ import absolute_import, unicod ...