Docker容器管理
目录
Docker容器管理
1.容器命令
docker container
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.
2.容器的启动
该命令可以启动本地没有的镜像,但必须有外网可以拉取镜像
docker run -d -p 80:80 nginx
docker run --name mydocker -t -i centos /bin/bash
2.1 交互式容器的启动
#交互式容器使用一般适用于工具或者需要部分命令就实现的容器(开发、测试、临时性的任务方向)
docker container run -it centos
[root@DogMan ~]# docker container ls
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
82b46f8f02a3 centos "/bin/bash" 2 minutes ago Up 2 minutes priceless_heisenberg
注释:
CONTAINER ID : 容器的唯一号码(自动生成)
NAMES : 容器的名字(可以自动生成,也可以手动指定)
STATUS :容器的运行状态(Exited,Up)
2.2 守护式容器
#守护式容器适用于:需要长期提供服务的应用
docker container run -d -p 80:80 nginx
本地测试
[root@DogMan ~]# curl 127.0.0.1:80
<!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>
WEB端测试访问
3. 容器详细信息查看
#语法:docker container inspect + 容器ID或者容器名称
[root@DogMan ~]# docker container inspect 25ade3bae028 (容器ID或者容器名称)
[
{
"Id": "25ade3bae0287192f4ad4fd5e6ea076fedef053b74873bae926f2dd3f1de8ce5",
"Created": "2020-12-09T15:22:44.154635276Z",
"Path": "/docker-entrypoint.sh",
"Args": [
"nginx",
"-g",
"daemon off;"
],
"State": {
"Status": "running",
"Running": true,
"Paused": false,
"Restarting": false,
"OOMKilled": false,
"Dead": false,
"Pid": 16827,
"ExitCode": 0,
"Error": "",
"StartedAt": "2020-12-09T15:22:44.555883581Z",
"FinishedAt": "0001-01-01T00:00:00Z"
},
"Image": "sha256:bc9a0695f5712dcaaa09a5adc415a3936ccba13fc2587dfd76b1b8aeea3f221c",
"ResolvConfPath": "/var/lib/docker/containers/25ade3bae0287192f4ad4fd5e6ea076fedef053b74873bae926f2dd3f1de8ce5/resolv.conf",
"HostnamePath": "/var/lib/docker/containers/25ade3bae0287192f4ad4fd5e6ea076fedef053b74873bae926f2dd3f1de8ce5/hostname",
"HostsPath": "/var/lib/docker/containers/25ade3bae0287192f4ad4fd5e6ea076fedef053b74873bae926f2dd3f1de8ce5/hosts",
"LogPath": "/var/lib/docker/containers/25ade3bae0287192f4ad4fd5e6ea076fedef053b74873bae926f2dd3f1de8ce5/25ade3bae0287192f4ad4fd5e6ea076fedef053b74873bae926f2dd3f1de8ce5-json.log",
"Name": "/romantic_sammet",
"RestartCount": 0,
"Driver": "overlay2",
"Platform": "linux",
"MountLabel": "",
"ProcessLabel": "",
"AppArmorProfile": "",
"ExecIDs": null,
"HostConfig": {
"Binds": null,
"ContainerIDFile": "",
"LogConfig": {
"Type": "json-file",
"Config": {}
},
"NetworkMode": "default",
"PortBindings": {
"80/tcp": [
{
"HostIp": "",
"HostPort": "80"
}
]
},
"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/eb830697e204c402cb8b47913da1e21a1458731c144b19336136be7e5ebc5dbb-init/diff:/var/lib/docker/overlay2/ee9e252b662916f478dfab14b6653b28ec9482a09d085ca10aed868e6d95e475/diff:/var/lib/docker/overlay2/382cbdc7de9611a11b9b3ae12351c0851eb2436bcaa4c6b5eaf567c9204dac17/diff:/var/lib/docker/overlay2/5fad396cb9e5672e38969b8ec1a0378807573a1a4ca87461733764dab7a1f1a9/diff:/var/lib/docker/overlay2/1e0cab7e91fa5c0be316d62777853ee8db8be9101db5b8b590bb35a8d2ef84d8/diff:/var/lib/docker/overlay2/6eb78b76638c8d2abdc4925b1954c620bc5880a9374064dc7ddc370085040bcc/diff",
"MergedDir": "/var/lib/docker/overlay2/eb830697e204c402cb8b47913da1e21a1458731c144b19336136be7e5ebc5dbb/merged",
"UpperDir": "/var/lib/docker/overlay2/eb830697e204c402cb8b47913da1e21a1458731c144b19336136be7e5ebc5dbb/diff",
"WorkDir": "/var/lib/docker/overlay2/eb830697e204c402cb8b47913da1e21a1458731c144b19336136be7e5ebc5dbb/work"
},
"Name": "overlay2"
},
"Mounts": [],
"Config": {
"Hostname": "25ade3bae028",
"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.19.5",
"NJS_VERSION=0.4.4",
"PKG_RELEASE=1~buster"
],
"Cmd": [
"nginx",
"-g",
"daemon off;"
],
"Image": "nginx",
"Volumes": null,
"WorkingDir": "",
"Entrypoint": [
"/docker-entrypoint.sh"
],
"OnBuild": null,
"Labels": {
"maintainer": "NGINX Docker Maintainers <docker-maint@nginx.com>"
},
"StopSignal": "SIGQUIT"
},
"NetworkSettings": {
"Bridge": "",
"SandboxID": "64a6377512e5bbe11d81b4e03395bd70a5f27b004a2b6752e56fd343b595718a",
"HairpinMode": false,
"LinkLocalIPv6Address": "",
"LinkLocalIPv6PrefixLen": 0,
"Ports": {
"80/tcp": [
{
"HostIp": "0.0.0.0",
"HostPort": "80"
}
]
},
"SandboxKey": "/var/run/docker/netns/64a6377512e5",
"SecondaryIPAddresses": null,
"SecondaryIPv6Addresses": null,
"EndpointID": "832a08fbc5ec8ef8ed4beaa019db50273c7555854c7d0b3b4d778c026c8ffb07",
"Gateway": "172.17.0.1",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"IPAddress": "172.17.0.2",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"MacAddress": "02:42:ac:11:00:02",
"Networks": {
"bridge": {
"IPAMConfig": null,
"Links": null,
"Aliases": null,
"NetworkID": "2db0abd462e047f92f2231fb978b14083f3ccd73a13993858775708835de7dcd",
"EndpointID": "832a08fbc5ec8ef8ed4beaa019db50273c7555854c7d0b3b4d778c026c8ffb07",
"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
}
}
}
}
]
4.自定义容器名称启动容器
docker container run -d --name="xcz" centos
我们在使用交互式启动并进入容器是,如果正常启动使用完容器后,我们自定义的容器依旧还在,只是空间释放了,而我们第二次还想以自定义命名的容器该怎么操作呢?
如果我们没有将原来自定义命名的容器删除,我们再次使用原来名字的容器,则会报错
[root@DogMan ~]# docker container run -it --name="xcz" centos
docker: Error response from daemon: Conflict. The container name "/xcz" is already in use by container "8868fcc9a6dd3e36ced62fbabf98c72a224a1a356451868c15f2249de0239a5a". You have to remove (or rename) that container to be able to reuse that name.
See 'docker run --help'.
为解决这类问题,我们只需要填加参数“--rm”即可:
docker container run -it --name="xxx" --rm centos
5.容器的关闭
docker stop 【容器ID】
例子:docker stop d65ed430affe
6.容器查看
docker ps
docker ps -a
docker ps -q
docker ps -aq
7. 进入容器
docker exec ---会分配一个新的终端tty
docker exec -it 容器ID /bin/bash
docker attach ---会使用同一个终端,退出时,所有窗口均会退出,谨慎使用。
docker attach 容器ID
8.容器的删除(谨慎操作)
#删除指定容器
docker rm 容器ID
#批量删除容器
docker stop $(docker ps -q)
docker rm $(docker ps -aq)
docker container rm -f `docker container ls -a -q`
9.容器进程TOP查看
[root@DogMan ~]# docker top 887dfbcfea30/zabbix-web-nginx-mysql(容器ID或者镜像名称)
UID PID PPID C STIME TTY TIME CMD
1997 7976 7960 0 22:54 pts/0 00:00:00 /usr/bin/python3 /usr/bin/supervisord -c /etc/supervisor/supervisord.conf
1997 8012 7976 0 22:54 pts/0 00:00:00 nginx: master process /usr/sbin/nginx -g daemon off;error_log /dev/stdout info; -c /etc/nginx/nginx.conf
1997 8013 7976 0 22:54 pts/0 00:00:00 php-fpm: master process (/etc/php7/php-fpm.conf)
1997 8014 8012 0 22:54 pts/0 00:00:00 nginx: worker process
1997 8015 8012 0 22:54 pts/0 00:00:00 nginx: worker process
1997 8016 8012 0 22:54 pts/0 00:00:00 nginx: worker process
1997 8017 8012 0 22:54 pts/0 00:00:00 nginx: worker process
1997 8018 8012 0 22:54 pts/0 00:00:00 nginx: worker process
1997 8019 8013 0 22:54 pts/0 00:00:00 php-fpm: pool zabbix
1997 8020 8013 0 22:54 pts/0 00:00:00 php-fpm: pool zabbix
1997 8021 8013 0 22:54 pts/0 00:00:00 php-fpm: pool zabbix
1997 8022 8013 0 22:54 pts/0 00:00:00 php-fpm: pool zabbix
1997 8023 8013 0 22:54 pts/0 00:00:00 php-fpm: pool zabbix
10.容器日志查看
[root@DogMan ~]# docker logs 887dfbcfea30
** Deploying Zabbix web-interface (Nginx) with MySQL database
** Using MYSQL_USER variable from ENV
** Using MYSQL_PASSWORD variable from ENV
********************
* DB_SERVER_HOST: mysql-server
* DB_SERVER_PORT: 3306
* DB_SERVER_DBNAME: zabbix
********************
** Adding Zabbix virtual host (HTTP)
**** Impossible to enable SSL support for Nginx. Certificates are missed.
** Preparing Zabbix frontend configuration file
########################################################
** Executing supervisord
2020-12-10 14:54:22,072 INFO Included extra file "/etc/supervisor/conf.d/supervisord_zabbix.conf" during parsing
2020-12-10 14:54:22,072 INFO Included extra file "/etc/supervisor/conf.d/supervisord_zabbix.conf" during parsing
2020-12-10 14:54:22,077 INFO RPC interface 'supervisor' initialized
2020-12-10 14:54:22,077 INFO RPC interface 'supervisor' initialized
2020-12-10 14:54:22,077 INFO supervisord started with pid 1
2020-12-10 14:54:22,077 INFO supervisord started with pid 1
2020-12-10 14:54:23,081 INFO spawned: 'nginx' with pid 14
2020-12-10 14:54:23,081 INFO spawned: 'nginx' with pid 14
2020-12-10 14:54:23,085 INFO spawned: 'php-fpm7' with pid 15
2020-12-10 14:54:23,085 INFO spawned: 'php-fpm7' with pid 15
2020/12/10 14:54:23 [notice] 14#14: using the "epoll" event method
2020/12/10 14:54:23 [notice] 14#14: nginx/1.18.0
2020/12/10 14:54:23 [notice] 14#14: OS: Linux 3.10.0-957.el7.x86_64
2020/12/10 14:54:23 [notice] 14#14: getrlimit(RLIMIT_NOFILE): 1048576:1048576
2020/12/10 14:54:23 [notice] 14#14: start worker processes
2020/12/10 14:54:23 [notice] 14#14: start worker process 16
2020/12/10 14:54:23 [notice] 14#14: start worker process 17
2020/12/10 14:54:23 [notice] 14#14: start worker process 18
2020/12/10 14:54:23 [notice] 14#14: start worker process 19
2020/12/10 14:54:23 [notice] 14#14: start worker process 20
[10-Dec-2020 14:54:23] NOTICE: fpm is running, pid 15
[10-Dec-2020 14:54:23] NOTICE: ready to handle connections
2020-12-10 14:54:25,201 INFO success: nginx entered RUNNING state, process has stayed up for > than 2 seconds (startsecs)
2020-12-10 14:54:25,201 INFO success: nginx entered RUNNING state, process has stayed up for > than 2 seconds (startsecs)
2020-12-10 14:54:25,201 INFO success: php-fpm7 entered RUNNING state, process has stayed up for > than 2 seconds (startsecs)
2020-12-10 14:54:25,201 INFO success: php-fpm7 entered RUNNING state, process has stayed up for > than 2 seconds (startsecs)
docker logs -tf 887dfbcfea30
docker logs -tf --tail 10 887dfbcfea30