Docker容器管理
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
[root@docker01 docker]# docker container --help Usage: docker container COMMAND Manage containers Commands: attach Attach local standard input, output, and error streams to a running container commit Create a new image from a container's changes cp Copy files/folders between a container and the local filesystem create Create a new container diff Inspect changes to files or directories on a container's filesystem exec Run a command in a running container export Export a container's filesystem as a tar archive inspect Display detailed information on one or more containers kill Kill one or more running containers logs Fetch the logs of a container ls List containers pause Pause all processes within one or more containers port List port mappings or a specific mapping for the container prune Remove all stopped containers rename Rename a container restart Restart one or more containers rm Remove one or more containers run Run a command in a new container start Start one or more stopped containers stats Display a live stream of container(s) resource usage statistics stop Stop one or more running containers top Display the running processes of a container unpause Unpause all processes within one or more containers update Update configuration of one or more containers wait Block until one or more containers stop, then print their exit codes Run 'docker container COMMAND --help' for more information on a command. [root@docker01 docker]#
1.创建容器常用选项
创建一个后台运行的容器,并查看容器和容器的详细配置
root@docker01 docker]# docker container run --help Usage: docker container run [OPTIONS] IMAGE [COMMAND] [ARG...] 或者 Usage: docker run [OPTIONS] IMAGE [COMMAND] [ARG...] [root@docker01 docker]# docker container run -d nginx:1.15 fb2c59dca1a1e6e6882258738ac53f509936a892449ff9d1f23508a176276e4a [root@docker01 docker]# docker ps -a #查看所有的容器 CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES fb2c59dca1a1 nginx:1.15 "nginx -g 'daemon of…" 29 seconds ago Up 19 seconds 80/tcp modest_ritchie [root@docker01 docker]# [root@docker01 docker]# docker ps -l #查看最新创建的一个容器 CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 965ba0ce94b9 nginx:1.15 "nginx -g 'daemon of…" 2 hours ago Up 2 hours 0.0.0.0:88->80/tcp nginx [root@docker01 docker]#
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
[root@docker01 docker]# docker container inspect fb2c59dca1a1 [ { "Id": "fb2c59dca1a1e6e6882258738ac53f509936a892449ff9d1f23508a176276e4a", "Created": "2020-05-02T04:53:56.71395164Z", "Path": "nginx", "Args": [ "-g", "daemon off;" ], "State": { "Status": "running", "Running": true, "Paused": false, "Restarting": false, "OOMKilled": false, "Dead": false, "Pid": 8103, "ExitCode": 0, "Error": "", "StartedAt": "2020-05-02T04:54:05.208314648Z", "FinishedAt": "0001-01-01T00:00:00Z" }, "Image": "sha256:53f3fd8007f76bd23bf663ad5f5009c8941f63828ae458cef584b5f85dc0a7bf", #挂载了dns hostname hosts log 这几个文件 "ResolvConfPath": "/var/lib/docker/containers/fb2c59dca1a1e6e6882258738ac53f509936a892449ff9d1f23508a176276e4a/resolv.conf", "HostnamePath": "/var/lib/docker/containers/fb2c59dca1a1e6e6882258738ac53f509936a892449ff9d1f23508a176276e4a/hostname", "HostsPath": "/var/lib/docker/containers/fb2c59dca1a1e6e6882258738ac53f509936a892449ff9d1f23508a176276e4a/hosts", "LogPath": "/var/lib/docker/containers/fb2c59dca1a1e6e6882258738ac53f509936a892449ff9d1f23508a176276e4a/fb2c59dca1a1e6e6882258738ac53f509936a892449ff9d1f23508a176276e4a-json.log", "Name": "/modest_ritchie", "RestartCount": 0, "Driver": "overlay2", "Platform": "linux", "MountLabel": "", "ProcessLabel": "", "AppArmorProfile": "", "ExecIDs": null, "HostConfig": { "Binds": null, "ContainerIDFile": "", "LogConfig": { "Type": "json-file", "Config": {} }, "NetworkMode": "default", "PortBindings": {}, "RestartPolicy": { "Name": "no", "MaximumRetryCount": 0 }, "AutoRemove": false, "VolumeDriver": "", "VolumesFrom": null, "CapAdd": null, "CapDrop": null, "Capabilities": null, "Dns": [], "DnsOptions": [], "DnsSearch": [], "ExtraHosts": null, "GroupAdd": null, "IpcMode": "private", "Cgroup": "", "Links": null, "OomScoreAdj": 0, "PidMode": "", "Privileged": false, "PublishAllPorts": false, "ReadonlyRootfs": false, "SecurityOpt": null, "UTSMode": "", "UsernsMode": "", "ShmSize": 67108864, "Runtime": "runc", "ConsoleSize": [ 0, 0 ], "Isolation": "", "CpuShares": 0, "Memory": 0, "NanoCpus": 0, "CgroupParent": "", "BlkioWeight": 0, "BlkioWeightDevice": [], "BlkioDeviceReadBps": null, "BlkioDeviceWriteBps": null, "BlkioDeviceReadIOps": null, "BlkioDeviceWriteIOps": null, "CpuPeriod": 0, "CpuQuota": 0, "CpuRealtimePeriod": 0, "CpuRealtimeRuntime": 0, "CpusetCpus": "", "CpusetMems": "", "Devices": [], "DeviceCgroupRules": null, "DeviceRequests": null, "KernelMemory": 0, "KernelMemoryTCP": 0, "MemoryReservation": 0, "MemorySwap": 0, "MemorySwappiness": null, "OomKillDisable": false, "PidsLimit": null, "Ulimits": null, "CpuCount": 0, "CpuPercent": 0, "IOMaximumIOps": 0, "IOMaximumBandwidth": 0, "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/da0e7d1011d83bc771a6a597d1457c0dd1624539c0cca7972136868b7c10105e-init/diff:/var/lib/docker/overlay2/1986c1a8b826f43b0860a0df31294f7b6381ca5338d0ef9683fdd3130ac5dd97/diff:/var/lib/docker/overlay2/527520c21b161ef06a9ae523410c8b2c32cb8b1e41b19d542881e7ac3591708c/diff:/var/lib/docker/overlay2/c901e05dc8dd30b4940d62d94de563adfb25d04af6fba3a981939139537ece2e/diff", "MergedDir": "/var/lib/docker/overlay2/da0e7d1011d83bc771a6a597d1457c0dd1624539c0cca7972136868b7c10105e/merged", "UpperDir": "/var/lib/docker/overlay2/da0e7d1011d83bc771a6a597d1457c0dd1624539c0cca7972136868b7c10105e/diff", "WorkDir": "/var/lib/docker/overlay2/da0e7d1011d83bc771a6a597d1457c0dd1624539c0cca7972136868b7c10105e/work" }, "Name": "overlay2" }, "Mounts": [], "Config": { "Hostname": "fb2c59dca1a1", "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.15.12-1~stretch", "NJS_VERSION=1.15.12.0.3.1-1~stretch" ], "Cmd": [ "nginx", "-g", "daemon off;" ], "Image": "nginx:1.15", "Volumes": null, "WorkingDir": "", "Entrypoint": null, "OnBuild": null, "Labels": { "maintainer": "NGINX Docker Maintainers <docker-maint@nginx.com>" }, "StopSignal": "SIGTERM" }, "NetworkSettings": { "Bridge": "", "SandboxID": "251bfddef64765d24c94fcd1a781d5d8b0147056340f77bd83a61451588e4047", "HairpinMode": false, "LinkLocalIPv6Address": "", "LinkLocalIPv6PrefixLen": 0, "Ports": { "80/tcp": null }, "SandboxKey": "/var/run/docker/netns/251bfddef647", "SecondaryIPAddresses": null, "SecondaryIPv6Addresses": null, "EndpointID": "587b61ebd5fbb4fab2780081b2c90e7ee549c7daf3193e8e7d8b02053c930d6f", "Gateway": "172.17.0.1", "GlobalIPv6Address": "", "GlobalIPv6PrefixLen": 0, "IPAddress": "172.17.0.2", #为容器分配一个IP "IPPrefixLen": 16, "IPv6Gateway": "", "MacAddress": "02:42:ac:11:00:02", "Networks": { "bridge": { "IPAMConfig": null, "Links": null, "Aliases": null, "NetworkID": "dcfaf483d096e5b2c5409007b6e9ceb07bd1825d7c7b4d9265c1b881b9bf1d62", "EndpointID": "587b61ebd5fbb4fab2780081b2c90e7ee549c7daf3193e8e7d8b02053c930d6f", "Gateway": "172.17.0.1", "IPAddress": "172.17.0.2", "IPPrefixLen": 16, "IPv6Gateway": "", "GlobalIPv6Address": "", "GlobalIPv6PrefixLen": 0, "MacAddress": "02:42:ac:11:00:02", "DriverOpts": null } } } } ] [root@docker01 docker]#
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
[root@docker01 docker]# 开两个窗口测试一下 窗口一: [root@docker01 docker]# curl 172.17.0.2 <!DOCTYPE html> <html> <head> <title>Welcome to nginx!</title> <style> body { width: 35em; margin: 0 auto; font-family: Tahoma, Verdana, Arial, sans-serif; } </style> </head> <body> <h1>Welcome to nginx!</h1> <p>If you see this page, the nginx web server is successfully installed and working. Further configuration is required.</p> <p>For online documentation and support please refer to <a href="http://nginx.org/">nginx.org</a>.<br/> Commercial support is available at <a href="http://nginx.com/">nginx.com</a>.</p> <p><em>Thank you for using nginx.</em></p> </body> </html> [root@docker01 docker]# #窗口二 查看日志 [root@docker01 ~]# tail -f /var/lib/docker/containers/fb2c59dca1a1e6e6882258738ac53f509936a892449ff9d1f23508a176276e4a/fb2c59dca1a1e6e6882258738ac53f509936a892449ff9d1f23508a176276e4a-json.log {"log":"172.17.0.1 - - [02/May/2020:04:59:23 +0000] \"GET / HTTP/1.1\" 200 612 \"-\" \"curl/7.29.0\" \"-\"\n","stream":"stdout","time":"2020-05-02T04:59:23.770228241Z"}
创建一个后台运行容器暴露端口
[root@docker01 docker]# docker container run -d -p 88:80 -e ABC=123 --name=nginx -h nginx_web nginx:1.15 965ba0ce94b94923c68bc17735f45ad6c5e9039fcea500eb57ec8575d07aa886 [root@docker01 docker]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 965ba0ce94b9 nginx:1.15 "nginx -g 'daemon of…" 41 seconds ago Up 32 seconds 0.0.0.0:88->80/tcp nginx --name=nginx 容器的名字 -h nginx_web 容器的主机名 -p 88:80 发布宿主机的88端口 映射到容器80端口 -d 后台运行 -e ABC=123 为容器设置一个环境变量 [root@docker01 docker]# docker container exec -it 965ba0ce94b9 bash #进入容器 root@nginx_web:/# hostname nginx_web root@nginx_web:/# echo $ABC 123 root@nginx_web:/# [root@docker01 docker]# curl 192.168.1.10:88 <!DOCTYPE html> <html> <head> <title>Welcome to nginx!</title> <style> body { width: 35em; margin: 0 auto; font-family: Tahoma, Verdana, Arial, sans-serif; } </style> </head> <body> <h1>Welcome to nginx!</h1> <p>If you see this page, the nginx web server is successfully installed and working. Further configuration is required.</p> <p>For online documentation and support please refer to <a href="http://nginx.org/">nginx.org</a>.<br/> Commercial support is available at <a href="http://nginx.com/">nginx.com</a>.</p> <p><em>Thank you for using nginx.</em></p> </body> </html> [root@docker01 docker]# docker container logs 965ba0ce94b9 #查看日志 192.168.1.3 - - [02/May/2020:05:19:32 +0000] "GET / HTTP/1.1" 200 612 "-" "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:75.0) Gecko/20100101 Firefox/75.0" "-" 2020/05/02 05:19:32 [error] 6#6: *1 open() "/usr/share/nginx/html/favicon.ico" failed (2: No such file or directory), client: 192.168.1.3, server: localhost, request: "GET /favicon.ico HTTP/1.1", host: "192.168.1.10:88" 192.168.1.3 - - [02/May/2020:05:19:32 +0000] "GET /favicon.ico HTTP/1.1" 404 154 "-" "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:75.0) Gecko/20100101 Firefox/75.0" "-" 192.168.1.10 - - [02/May/2020:05:20:42 +0000] "GET / HTTP/1.1" 200 612 "-" "curl/7.29.0" "-" [root@docker01 docker]#
创建一个交互式容器
[root@docker01 docker]# docker container run -it nginx:1.15
2.容器资源限制
-m,–-memory 容器可以使用的最大内存量 –-memory-swap 允 许交换到磁盘的内存量 –-memory-swappiness=<0-100> 容器使用SWAP分区交换的百分比(0-100,默认为-1) –-oom-kill-disable 禁用OOM Killer #禁止kill 该docker容器 –-cpus 可以使用的CPU数量 –-cpuset-cpus 限制容器使用特定的CPU核心,如(0-3, 0,1) #指定使用某个CPU –-cpu-shares CPU共享(相对权重) 示例: 内存限额: 允许容器最多使用500M内存和100M的Swap,并禁用OOM Killer: docker run -d --name nginx03 --memory="500m" --memory-swap=“600m" --oom-kill-disable nginx CPU限额: 允许容器最多使用一个半的CPU: docker run -d --name nginx04 --cpus="1.5" nginx 允许容器最多使用50%的CPU: docker run -d --name nginx05 --cpus=".5" nginx
[root@localhost ~]# docker image ls REPOSITORY TAG IMAGE ID CREATED SIZE nginx latest 4733136e5c3c 42 hours ago 126MB mysql 5.7 e1e1680ac726 43 hours ago 373MB centos 7 9f38484d220f 5 months ago 202MB [root@localhost ~]# docker container run -d -m 1G 4733136e5c3c #限制容器内存一个G b5682943f5c5e826ebf92da81672fbb0cdcc6a46d2d5d7e07048f6aede4de2c3 [root@localhost ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES b5682943f5c5 4733136e5c3c "nginx -g 'daemon of…" 23 seconds ago Up 21 seconds 80/tcp wizardly_volhard 6e9fd6b3c136 4733136e5c3c "nginx -g 'daemon of…" 25 hours ago Exited (0) 25 hours ago nginx 5c76e5a5901b centos:7 "/bin/bash" 26 hours ago Exited (137) 25 hours ago great_dhawan 17e4c208a035 centos:7 "/bin/bash" 26 hours ago Exited (0) 26 hours ago reverent_gates 361d4b9f85a7 mysql:5.7 "docker-entrypoint.s…" 26 hours ago Exited (1) 26 hours ago competent_varahamihira [root@localhost ~]# docker stats #查看容器状态 CONTAINER ID NAME CPU % MEM USAGE / LIMIT MEM % NET I/O BLOCK I/O PIDS b5682943f5c5 wizardly_volhard 0.00% 1.391MiB / 1GiB 0.14% 1.31kB / 0B 16.4MB / 0B 2
3.管理容器常用命令
docker container commit
docker 将正在运行的容器打包为镜像 将容器打包成镜像 docker commit -a "runoob.com" -m "my apache" 容器名称或id 打包的镜像名称:标签 OPTIONS说明: -a :提交的镜像作者; -c :使用Dockerfile指令来创建镜像; -m :提交时的说明文字; -p :在commit时,将容器暂停。
[root@localhost ~]# docker container commit b5682943f5c5 nginx:v1 #将容器提交一个新的镜像 sha256:280caf95b0cdec90a4f5783378d973744b81f0ad67ad8c8ccea6c8f4261b1844 [root@localhost ~]# docker image ls REPOSITORY TAG IMAGE ID CREATED SIZE nginx v1 280caf95b0cd 9 seconds ago 126MB nginx latest 4733136e5c3c 43 hours ago 126MB mysql 5.7 e1e1680ac726 44 hours ago 373MB centos 7 9f38484d220f 5 months ago 202MB [root@localhost ~]#
docker container ps
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
docker ps : 列出容器 语法 docker ps [OPTIONS] OPTIONS说明: -a :显示所有的容器,包括未运行的。 -f :根据条件过滤显示的内容。 --format :指定返回值的模板文件。 -l :显示最近创建的容器。 -n :列出最近创建的n个容器。 --no-trunc :不截断输出。 -q :静默模式,只显示容器编号。 -s :显示总的文件大小。 实例 列出所有在运行的容器信息。 runoob@runoob:~$ docker ps CONTAINER ID IMAGE COMMAND ... PORTS NAMES 09b93464c2f7 nginx:latest "nginx -g 'daemon off" ... 80/tcp, 443/tcp myrunoob 96f7f14e99ab mysql:5.6 "docker-entrypoint.sh" ... 0.0.0.0:3306->3306/tcp mymysql 列出最近创建的5个容器信息。 runoob@runoob:~$ docker ps -n 5 CONTAINER ID IMAGE COMMAND CREATED 09b93464c2f7 nginx:latest "nginx -g 'daemon off" 2 days ago ... b8573233d675 nginx:latest "/bin/bash" 2 days ago ... b1a0703e41e7 nginx:latest "nginx -g 'daemon off" 2 days ago ... f46fb1dec520 5c6e1090e771 "/bin/sh -c 'set -x \t" 2 days ago ... a63b4a5597de 860c279d2fec "bash" 2 days ago ... 列出所有创建的容器ID。 runoob@runoob:~$ docker ps -a -q 09b93464c2f7 b8573233d675 b1a0703e41e7 f46fb1dec520 a63b4a5597de 6a4aa42e947b de7bb36e7968 43a432b73776 664a8ab1a585 ba52eb632bbd ...
#查看正在运行(up)的容器docker container ls [root@localhost ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES b5682943f5c5 4733136e5c3c "nginx -g 'daemon of…" 28 minutes ago Up 28 minutes 80/tcp wizardly_volhard 6e9fd6b3c136 4733136e5c3c "nginx -g 'daemon of…" 26 hours ago Exited (0) 25 hours ago nginx 5c76e5a5901b centos:7 "/bin/bash" 26 hours ago Exited (137) 25 hours ago great_dhawan 17e4c208a035 centos:7 "/bin/bash" 26 hours ago Exited (0) 26 hours ago reverent_gates 361d4b9f85a7 mysql:5.7 "docker-entrypoint.s…" 26 hours ago Exited (1) 26 hours ago competent_varahamihira [root@localhost ~]# docker container start 6e9fd6b3c136 6e9fd6b3c136 [root@localhost ~]# docker container ls CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES b5682943f5c5 4733136e5c3c "nginx -g 'daemon of…" 28 minutes ago Up 28 minutes 80/tcp wizardly_volhard 6e9fd6b3c136 4733136e5c3c "nginx -g 'daemon of…" 26 hours ago Up 5 seconds 0.0.0.0:88->80/tcp nginx
docker container inspect
[root@localhost ~]# docker container run -d mysql:5.7 #运行容器到后台 361d4b9f85a7c8734d3dedf2497d67f81588adcdf606dd3eac87a204dffcb22f [root@localhost ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 361d4b9f85a7 mysql:5.7 "docker-entrypoint.s…" 38 seconds ago Exited (1) 36 seconds ago competent_varahamihira
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
[root@localhost ~]# docker container inspect 361d4b9f85a7 #查看容器详细信息 [ { "Id": "361d4b9f85a7c8734d3dedf2497d67f81588adcdf606dd3eac87a204dffcb22f", "Created": "2019-08-14T23:40:12.231728222Z", "Path": "docker-entrypoint.sh", "Args": [ "mysqld" ], "State": { "Status": "exited", "Running": false, "Paused": false, "Restarting": false, "OOMKilled": false, "Dead": false, "Pid": 0, "ExitCode": 1, "Error": "", "StartedAt": "2019-08-14T23:40:13.114619629Z", "FinishedAt": "2019-08-14T23:40:13.261352398Z" }, "Image": "sha256:e1e1680ac726cf9d328fb567e4107031feadcb3904d9206ad2a258f9e472ea10", "ResolvConfPath": "/var/lib/docker/containers/361d4b9f85a7c8734d3dedf2497d67f81588adcdf606dd3eac87a204dffcb22f/resolv.conf", "HostnamePath": "/var/lib/docker/containers/361d4b9f85a7c8734d3dedf2497d67f81588adcdf606dd3eac87a204dffcb22f/hostname", "HostsPath": "/var/lib/docker/containers/361d4b9f85a7c8734d3dedf2497d67f81588adcdf606dd3eac87a204dffcb22f/hosts", "LogPath": "/var/lib/docker/containers/361d4b9f85a7c8734d3dedf2497d67f81588adcdf606dd3eac87a204dffcb22f/361d4b9f85a7c8734d3dedf2497d67f81588adcdf606dd3eac87a204dffcb22f-json.log", "Name": "/competent_varahamihira", "RestartCount": 0, "Driver": "overlay2", "Platform": "linux", "MountLabel": "", "ProcessLabel": "", "AppArmorProfile": "", "ExecIDs": null, "HostConfig": { "Binds": null, "ContainerIDFile": "", "LogConfig": { "Type": "json-file", "Config": {} }, "NetworkMode": "default", "PortBindings": {}, "RestartPolicy": { "Name": "no", "MaximumRetryCount": 0 }, "AutoRemove": false, "VolumeDriver": "", "VolumesFrom": null, "CapAdd": null, "CapDrop": null, "Capabilities": null, "Dns": [], "DnsOptions": [], "DnsSearch": [], "ExtraHosts": null, "GroupAdd": null, "IpcMode": "private", "Cgroup": "", "Links": null, "OomScoreAdj": 0, "PidMode": "", "Privileged": false, "PublishAllPorts": false, "ReadonlyRootfs": false, "SecurityOpt": null, "UTSMode": "", "UsernsMode": "", "ShmSize": 67108864, "Runtime": "runc", "ConsoleSize": [ 0, 0 ], "Isolation": "", "CpuShares": 0, "Memory": 0, "NanoCpus": 0, "CgroupParent": "", "BlkioWeight": 0, "BlkioWeightDevice": [], "BlkioDeviceReadBps": null, "BlkioDeviceWriteBps": null, "BlkioDeviceReadIOps": null, "BlkioDeviceWriteIOps": null, "CpuPeriod": 0, "CpuQuota": 0, "CpuRealtimePeriod": 0, "CpuRealtimeRuntime": 0, "CpusetCpus": "", "CpusetMems": "", "Devices": [], "DeviceCgroupRules": null, "DeviceRequests": null, "KernelMemory": 0, "KernelMemoryTCP": 0, "MemoryReservation": 0, "MemorySwap": 0, "MemorySwappiness": null, "OomKillDisable": false, "PidsLimit": null, "Ulimits": null, "CpuCount": 0, "CpuPercent": 0, "IOMaximumIOps": 0, "IOMaximumBandwidth": 0, "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/40a77a2182f5ef1d959b26e11944ab2846995ea863f968422c21bf8fa7684189-init/diff:/var/lib/docker/overlay2/af8a3291147ba2342d3d5720389ffc54dd262522ccc4e4a326440d3d09f1baba/diff:/var/lib/docker/overlay2/4744ed10484432a1738cbd9aefcac25be11f045d4fc1e3c82d28636d40f58058/diff:/var/lib/docker/overlay2/3ebb4822fc3a423dd554c228d8e434e1c43c27f98aa6618d1d8bff8a564f6e2b/diff:/var/lib/docker/overlay2/a2def021cafd4ab790649bb63958aed08637cda934e11fcec1450ce269337be5/diff:/var/lib/docker/overlay2/6884e52e5a70d1fe745892c3ce51662b3a60dbb6edadfbc457b1987654c35960/diff:/var/lib/docker/overlay2/690a7c2ca76224232c595934f4ffd9084b848755141c0392c26d39b8eb8eb900/diff:/var/lib/docker/overlay2/b27b9cfdc5fb1343f1fdb3e2a4cbdf1d157481526861bb37b1d6699bd45fbd74/diff:/var/lib/docker/overlay2/bba98036bd0aa8318aa7e7a6678f0f26e82230a05da8b359192c995837cc6294/diff:/var/lib/docker/overlay2/d944ad242a218ed572c73d6718fe92f7f30795ebd2f5bdf920c00175d0e5f81a/diff:/var/lib/docker/overlay2/722e0ca46244a4efd2d172bba070c2249a33ac57bdfacdcba2ac92a3ca13466e/diff:/var/lib/docker/overlay2/ba9a9c0ea6017f8c8bb2d1e13f9d17af75564024dc403480ad80bc792216f1ce/diff", "MergedDir": "/var/lib/docker/overlay2/40a77a2182f5ef1d959b26e11944ab2846995ea863f968422c21bf8fa7684189/merged", "UpperDir": "/var/lib/docker/overlay2/40a77a2182f5ef1d959b26e11944ab2846995ea863f968422c21bf8fa7684189/diff", "WorkDir": "/var/lib/docker/overlay2/40a77a2182f5ef1d959b26e11944ab2846995ea863f968422c21bf8fa7684189/work" }, "Name": "overlay2" }, "Mounts": [ { "Type": "volume", "Name": "1891385a520fb48c16cffcd9efe3b2b6f4ea1ba2ecbaa9b9cf196e4e9f3ea3b5", "Source": "/var/lib/docker/volumes/1891385a520fb48c16cffcd9efe3b2b6f4ea1ba2ecbaa9b9cf196e4e9f3ea3b5/_data", "Destination": "/var/lib/mysql", "Driver": "local", "Mode": "", "RW": true, "Propagation": "" } ], "Config": { "Hostname": "361d4b9f85a7", "Domainname": "", "User": "", "AttachStdin": false, "AttachStdout": false, "AttachStderr": false, "ExposedPorts": { "3306/tcp": {}, "33060/tcp": {} }, "Tty": false, "OpenStdin": false, "StdinOnce": false, "Env": [ "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin", "GOSU_VERSION=1.7", "MYSQL_MAJOR=5.7", "MYSQL_VERSION=5.7.27-1debian9" ], "Cmd": [ "mysqld" ], "Image": "mysql:5.7", "Volumes": { "/var/lib/mysql": {} }, "WorkingDir": "", "Entrypoint": [ "docker-entrypoint.sh" ], "OnBuild": null, "Labels": {} }, "NetworkSettings": { "Bridge": "", "SandboxID": "0e042880d0c8d0ec618a05ad0daaa563c422cc8d38cc0593c5ebd15673d036e5", "HairpinMode": false, "LinkLocalIPv6Address": "", "LinkLocalIPv6PrefixLen": 0, "Ports": {}, "SandboxKey": "/var/run/docker/netns/0e042880d0c8", "SecondaryIPAddresses": null, "SecondaryIPv6Addresses": null, "EndpointID": "", "Gateway": "", "GlobalIPv6Address": "", "GlobalIPv6PrefixLen": 0, "IPAddress": "", "IPPrefixLen": 0, "IPv6Gateway": "", "MacAddress": "", "Networks": { "bridge": { "IPAMConfig": null, "Links": null, "Aliases": null, "NetworkID": "5d0a1b0ef4a287323a8a0eb3eb46c842783c6daaa3602ec839ce868707352164", "EndpointID": "", "Gateway": "", "IPAddress": "", "IPPrefixLen": 0, "IPv6Gateway": "", "GlobalIPv6Address": "", "GlobalIPv6PrefixLen": 0, "MacAddress": "", "DriverOpts": null } } } } ]
docker container start
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
[root@localhost ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 5c76e5a5901b centos:7 "/bin/bash" 47 seconds ago Exited (0) 36 seconds ago great_dhawan 17e4c208a035 centos:7 "/bin/bash" 10 minutes ago Exited (0) 2 seconds ago reverent_gates 361d4b9f85a7 mysql:5.7 "docker-entrypoint.s…" 16 minutes ago Exited (1) 5 minutes ago competent_varahamihira [root@localhost ~]# docker start 5c76e5a5901b 5c76e5a5901b [root@localhost ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 5c76e5a5901b centos:7 "/bin/bash" About a minute ago Up 2 seconds great_dhawan 17e4c208a035 centos:7 "/bin/bash" 11 minutes ago Exited (0) 18 seconds ago reverent_gates 361d4b9f85a7 mysql:5.7 "docker-entrypoint.s…" 16 minutes ago Exited (1) 5 minutes ago competent_varahamihira [root@localhost ~]#
docker container exec
[root@localhost ~]# docker image ls REPOSITORY TAG IMAGE ID CREATED SIZE nginx latest 4733136e5c3c 17 hours ago 126MB mysql 5.7 e1e1680ac726 18 hours ago 373MB centos 7 9f38484d220f 5 months ago 202MB [root@localhost ~]# docker run -d -p 88:80 -e ABC=123 --name=nginx -h nginx_web 4733136e5c3c #-p发布88端口给宿主机 映射到容器80端口 6e9fd6b3c1364665978709c9cc86e88ecf75d90e3e1eeee622cd9a34b743cef0 [root@localhost ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 6e9fd6b3c136 4733136e5c3c "nginx -g 'daemon of…" 17 seconds ago Up 15 seconds 0.0.0.0:88->80/tcp nginx 5c76e5a5901b centos:7 "/bin/bash" 20 minutes ago Up 19 minutes great_dhawan 17e4c208a035 centos:7 "/bin/bash" 30 minutes ago Exited (0) 19 minutes ago reverent_gates 361d4b9f85a7 mysql:5.7 "docker-entrypoint.s…" 36 minutes ago Exited (1) 24 minutes ago competent_varahamihira [root@localhost ~]# docker container exec -it 6e9fd6b3c136 bash root@nginx_web:/# [root@localhost ~]# netstat -anptu | grep 88 tcp6 0 0 :::88 :::* LISTEN 22538/docker-proxy [root@localhost ~]# [root@localhost ~]# docker container exec b5682943f5c5 ls #在容器中执行命令 bin boot dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var
docker container cp
[root@localhost ~]# docker container cp centos7.tar b5682943f5c5:/ #拷贝宿主机的文件到容器中 [root@localhost ~]# docker container exec b5682943f5c5 ls bin boot centos7.tar dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var [root@localhost ~]#
docker container top
[root@localhost ~]# docker container top b5682943f5c5 UID PID PPID C STIME TTY TIME CMD root 19296 19277 0 09:30 ? 00:00:00 nginx: master process nginx -g daemon off; 101 19335 19296 0 09:30 ? 00:00:00 nginx: worker process [root@localhost ~]#
[root@docker01 docker]# docker container port 965ba0ce94b9 80/tcp -> 0.0.0.0:88 [root@docker01 docker]#
[root@docker01 docker]# docker stats 965ba0ce94b9 --no-stream #免交互模式 输出结果 CONTAINER ID NAME CPU % MEM USAGE / LIMIT MEM % NET I/O BLOCK I/O PIDS 965ba0ce94b9 nginx 0.00% 1.371MiB / 1.777GiB 0.08% 2.79kB / 3.32kB 0B / 0B 2 [root@docker01 docker]#
删除容器
列出所有的容器 ID docker ps -aq 停止所有的容器 docker stop $(docker ps -aq) 删除所有的容器 方法一 docker rm $(docker ps -aq) [root@db01 ~]# docker ps -aq 方法二 [root@docker01 ~]# docker rm -f $(docker ps -a | awk '{if (NR>1){print $1}}')#删除所有运行的容器
获取docker连接数的方法,或查看docker的连接
由于使用DOCKER的时候,ESTABLISHED连接不会出现在netstat中: 在运行中的docker容器中列出打开的套接字的方法 方法: 1. 查找docker的进程号 : docker inspect -f '{{.State.Pid}}' <containerid> $ docker inspect -f '{{.State.Pid}}' 49b98b2fbad2 1840 2. 查看连接: sudo nsenter -t <pid> -n netstat | grep ESTABLISHED $ nsenter -t 1840 -n netstat |grep ESTABLISHED udp 0 0 node-2:45963 10.254.0.2:domain ESTABLISHED 从上面可以看到,只有1个tcp连接
在宿主机查看docker使用cpu、内存、网络、io情况
docker stats 容器名 或 docker stats 容器id docker stats horizonstar 动态显示下列数据: CONTAINER CPU % MEM USAGE / LIMIT MEM % NET I/O BLOCK I/O PIDS horizonstar 2.01% 6.957 GB / 16.05 GB 43.36% 2.042 GB / 2.079 GB 5.012 GB / 9.396 GB 0
小栗子:Docker - 在容器中搭建运行FTP服务器(vsftpd)https://www.hangge.com/blog/cache/detail_2449.html