docker容器管理命令
查看容器
docker ps #查看运行的容器
空 列出当前正在运行的容器
-a all 列出当前正在运行的容器,和历世运行过的容器,即可全部
-n=? 显示最近运行的容器
-q 只显示容器的ID
[root@localhost /]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
57634f16bc58 centos:7 "/bin/bash" 10 minutes ago Exited (0) 10 minutes ago sad_kowalevski
32f9bb257ef2 centos:7 "/bin/bash" 13 minutes ago Exited (0) 11 minutes ago quizzical_babbage
53197642feec hello-world "/hello" 2 hours ago Exited (0) 2 hours ago sharp_robinson
[root@localhost /]# docker ps -a -n=1
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
57634f16bc58 centos:7 "/bin/bash" 15 minutes ago Exited (0) 15 minutes ago sad_kowalevski
[root@localhost /]# docker ps -a -n=2
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
57634f16bc58 centos:7 "/bin/bash" 15 minutes ago Exited (0) 15 minutes ago sad_kowalevski
32f9bb257ef2 centos:7 "/bin/bash" 17 minutes ago Exited (0) 16 minutes ago quizzical_babbage
[root@localhost /]# docker ps -a -n=3
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
57634f16bc58 centos:7 "/bin/bash" 15 minutes ago Exited (0) 15 minutes ago sad_kowalevski
32f9bb257ef2 centos:7 "/bin/bash" 18 minutes ago Exited (0) 16 minutes ago quizzical_babbage
53197642feec hello-world "/hello" 2 hours ago Exited (0) 2 hours ago sharp_robinson
[root@localhost /]# docker ps -aq
57634f16bc58
32f9bb257ef2
53197642feec
有些容器会死掉,我们也想查看,就需要:
这样就列出了所有的容器,我们可以根据STATUS容器的状态,来判断哪些容器死掉,哪些还在运行。
STATUS容器的状态
- Created 已创建
- exited 退出
- Up 10 hours 类似这种都是运行中的
我们还可以使用
docker container start openvpn-server #把死掉/退出状态的容器拉起来
openvpn-server是容器的名字
运行镜像
运行镜像生成容器
命令格式:
docker run [可选参数] image
常用参数:
–name=“Name” 容器的名字
-d 后台方式运行
-it 使用交互方式运行
-p 指定容器端口,例如:-p 8080:8080
-p ip:主机端口:容器端口
-p 主机端口:容器端口
-p 容器端口
容器端口
-P 大写P随机指定端口
#测试启动并进入容器
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mysql 5.7 8cf625070931 13 days ago 448MB
mysql latest c60d96bd2b77 13 days ago 514MB
hello-world latest d1165f221234 5 months ago 13.3kB
centos 7 8652b9f0cb4c 8 months ago 204MB
[root@localhost ~]# docker run -it centos:7 /bin/bash
[root@32f9bb257ef2 /]# 主机名其实就是镜像ID
[root@32f9bb257ef2 /]# exit #退出容器,快捷键Ctrl+D
exit
#宿主机和镜像根目录对比
[root@57634f16bc58 /]# ls
anaconda-post.log dev home lib64 mnt proc run srv tmp var
bin etc lib media opt root sbin sys usr
bin boot dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var
[root@localhost /]#
#退出容器不停止
快捷键Ctrl+Q+P
[root@localhost /]# docker run -it centos:7 /bin/bash
[root@d6a03b8e3f49 /]# [root@localhost /]#
[root@localhost /]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
d6a03b8e3f49 centos:7 "/bin/bash" 44 seconds ago Up 43 seconds mystifying_carver
[root@localhost /]#
[root@localhost ~]# docker run --help
Usage: docker run [OPTIONS] IMAGE [COMMAND] [ARG...]
Run a command in a new container
Options:
--add-host list Add a custom host-to-IP mapping (host:ip)
-a, --attach list Attach to STDIN, STDOUT or STDERR
--blkio-weight uint16 Block IO (relative weight), between 10 and 1000, or 0 to
disable (default 0)
--blkio-weight-device list Block IO weight (relative device weight) (default [])
--cap-add list Add Linux capabilities
--cap-drop list Drop Linux capabilities
--cgroup-parent string Optional parent cgroup for the container
--cgroupns string Cgroup namespace to use (host|private)
'host': Run the container in the Docker host's cgroup
namespace
'private': Run the container in its own private cgroup
namespace
'': Use the cgroup namespace as configured by the
default-cgroupns-mode option on the daemon (default)
--cidfile string Write the container ID to the file
--cpu-period int Limit CPU CFS (Completely Fair Scheduler) period
--cpu-quota int Limit CPU CFS (Completely Fair Scheduler) quota
--cpu-rt-period int Limit CPU real-time period in microseconds
--cpu-rt-runtime int Limit CPU real-time runtime in microseconds
-c, --cpu-shares int CPU shares (relative weight)
--cpus decimal Number of CPUs
--cpuset-cpus string CPUs in which to allow execution (0-3, 0,1)
--cpuset-mems string MEMs in which to allow execution (0-3, 0,1)
-d, --detach Run container in background and print container ID
--detach-keys string Override the key sequence for detaching a container
--device list Add a host device to the container
--device-cgroup-rule list Add a rule to the cgroup allowed devices list
--device-read-bps list Limit read rate (bytes per second) from a device (default [])
--device-read-iops list Limit read rate (IO per second) from a device (default [])
--device-write-bps list Limit write rate (bytes per second) to a device (default [])
--device-write-iops list Limit write rate (IO per second) to a device (default [])
--disable-content-trust Skip image verification (default true)
--dns list Set custom DNS servers
--dns-option list Set DNS options
--dns-search list Set custom DNS search domains
--domainname string Container NIS domain name
--entrypoint string Overwrite the default ENTRYPOINT of the image
-e, --env list Set environment variables
--env-file list Read in a file of environment variables
--expose list Expose a port or a range of ports
--gpus gpu-request GPU devices to add to the container ('all' to pass all GPUs)
--group-add list Add additional groups to join
--health-cmd string Command to run to check health
--health-interval duration Time between running the check (ms|s|m|h) (default 0s)
--health-retries int Consecutive failures needed to report unhealthy
--health-start-period duration Start period for the container to initialize before starting
health-retries countdown (ms|s|m|h) (default 0s)
--health-timeout duration Maximum time to allow one check to run (ms|s|m|h) (default 0s)
--help Print usage
-h, --hostname string Container host name
--init Run an init inside the container that forwards signals and
reaps processes
-i, --interactive Keep STDIN open even if not attached
--ip string IPv4 address (e.g., 172.30.100.104)
--ip6 string IPv6 address (e.g., 2001:db8::33)
--ipc string IPC mode to use
--isolation string Container isolation technology
--kernel-memory bytes Kernel memory limit
-l, --label list Set meta data on a container
--label-file list Read in a line delimited file of labels
--link list Add link to another container
--link-local-ip list Container IPv4/IPv6 link-local addresses
--log-driver string Logging driver for the container
--log-opt list Log driver options
--mac-address string Container MAC address (e.g., 92:d0:c6:0a:29:33)
-m, --memory bytes Memory limit
--memory-reservation bytes Memory soft limit
--memory-swap bytes Swap limit equal to memory plus swap: '-1' to enable unlimited swap
--memory-swappiness int Tune container memory swappiness (0 to 100) (default -1)
--mount mount Attach a filesystem mount to the container
--name string Assign a name to the container
--network network Connect a container to a network
--network-alias list Add network-scoped alias for the container
--no-healthcheck Disable any container-specified HEALTHCHECK
--oom-kill-disable Disable OOM Killer
--oom-score-adj int Tune host's OOM preferences (-1000 to 1000)
--pid string PID namespace to use
--pids-limit int Tune container pids limit (set -1 for unlimited)
--platform string Set platform if server is multi-platform capable
--privileged Give extended privileges to this container
-p, --publish list Publish a container's port(s) to the host
-P, --publish-all Publish all exposed ports to random ports
--pull string Pull image before running ("always"|"missing"|"never")
(default "missing")
--read-only Mount the container's root filesystem as read only
--restart string Restart policy to apply when a container exits (default "no")
--rm Automatically remove the container when it exits
--runtime string Runtime to use for this container
--security-opt list Security Options
--shm-size bytes Size of /dev/shm
--sig-proxy Proxy received signals to the process (default true)
--stop-signal string Signal to stop a container (default "SIGTERM")
--stop-timeout int Timeout (in seconds) to stop a container
--storage-opt list Storage driver options for the container
--sysctl map Sysctl options (default map[])
--tmpfs list Mount a tmpfs directory
-t, --tty Allocate a pseudo-TTY
--ulimit ulimit Ulimit options (default [])
-u, --user string Username or UID (format: <name|uid>[:<group|gid>])
--userns string User namespace to use
--uts string UTS namespace to use
-v, --volume list Bind mount a volume
--volume-driver string Optional volume driver for the container
--volumes-from list Mount volumes from the specified container(s)
-w, --workdir string Working directory inside the container
docker run -d -p 80:80 nginx
- run 创建并运行一个容器
- -d 放在后台运行,不加会扛住
- -p 端口映射
- nginx 镜像的名字
重启docker服务后,所有的容器都会退出。再次执行是不会有冲突的。
启动/停止容器
docker start 容器ID号 #启动已经停止的容器
docker restart 容器ID号 #重启容器
docker stop 容器ID号 #停止容器
docker kill 容器ID号 #强制停止容器
最好是使用ID,如果使用名称则还需要加上版本号
docker stop $(docker ps -aq) #停止所有运行的容器
以上两个命令是等价的。
删除容器
docker rm 镜像ID/名称:版本号 #删除停止的容器,不能删除正在运行的容器
docker rm -f #强制删除容器,可以删除运行的容器
docker rm -f $(docker ps -aq) #删除所有容器
删除容器之前,必须先停止容器,否则无法删除。
批量删除容器
docker ps -aq 列出所有容器的ID
docker rm `docker ps -aq` 删除所有停止的容器,运行的不删
docker rm -f `docker ps -aq` 强制删除所有容器,包含运行的
查看容器所有信息
docker inspect 容器名称/ID
[root@pokes03 ~]# docker inspect wordpress:latest
[
{
"Id": "sha256:2c9350d16623803c9bcfa58973b9bdba18e06d3107b15ba8090c09db946c27bf",
"RepoTags": [
"wordpress:latest"
],
"RepoDigests": [
"wordpress@sha256:602ea0959040cd101e5e6923474612a15f9dd330ab45ceaec4e07dc762d6
],
"Parent": "",
"Comment": "",
"Created": "2020-06-12T00:07:55.207481684Z",
"Container": "a5b6cd410c01c96a947c5e32490970812359d7cee4baeedffbfb28c39b7a0e23",
"ContainerConfig": {
"Hostname": "a5b6cd410c01",
"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",
"PHPIZE_DEPS=autoconf \t\tdpkg-dev \t\tfile \t\tg++ \t\tgcc \t\tlibc-dev \
"PHP_INI_DIR=/usr/local/etc/php",
"APACHE_CONFDIR=/etc/apache2",
"APACHE_ENVVARS=/etc/apache2/envvars",
"PHP_EXTRA_BUILD_DEPS=apache2-dev",
"PHP_EXTRA_CONFIGURE_ARGS=--with-apxs2 --disable-cgi",
"PHP_CFLAGS=-fstack-protector-strong -fpic -fpie -O2 -D_LARGEFILE_SOURCE -
"PHP_CPPFLAGS=-fstack-protector-strong -fpic -fpie -O2 -D_LARGEFILE_SOURCE
"PHP_LDFLAGS=-Wl,-O1 -pie",
"GPG_KEYS=CBAF69F173A0FEA4B537F470D66C9593118BCCB6 F38252826ACD957EF380D39
"PHP_VERSION=7.3.19",
"PHP_URL=https://www.php.net/distributions/php-7.3.19.tar.xz",
"PHP_ASC_URL=https://www.php.net/distributions/php-7.3.19.tar.xz.asc",
"PHP_SHA256=6402faa19b1a8c4317c7612632bce985684a5bbae0980a5779a40194398824
"PHP_MD5=",
"WORDPRESS_VERSION=5.4.2",
"WORDPRESS_SHA1=e5631f812232fbd45d3431783d3db2e0d5670d2d"
],
"Cmd": [
"/bin/sh",
"-c",
"#(nop) ",
"CMD [\"apache2-foreground\"]"
],
"ArgsEscaped": true,
"Image": "sha256:c10a2278bd33b371ee332f97d8eea4b18b297e7eb6ee086e4b829ff224640
"Volumes": {
"/var/www/html": {}
},
"WorkingDir": "/var/www/html",
"Entrypoint": [
"docker-entrypoint.sh"
],
"OnBuild": null,
"Labels": {},
"StopSignal": "SIGWINCH"
},
"DockerVersion": "18.09.7",
"Author": "",
"Config": {
"Hostname": "",
"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",
"PHPIZE_DEPS=autoconf \t\tdpkg-dev \t\tfile \t\tg++ \t\tgcc \t\tlibc-dev \
"PHP_INI_DIR=/usr/local/etc/php",
"APACHE_CONFDIR=/etc/apache2",
"APACHE_ENVVARS=/etc/apache2/envvars",
"PHP_EXTRA_BUILD_DEPS=apache2-dev",
"PHP_EXTRA_CONFIGURE_ARGS=--with-apxs2 --disable-cgi",
"PHP_CFLAGS=-fstack-protector-strong -fpic -fpie -O2 -D_LARGEFILE_SOURCE -
"PHP_CPPFLAGS=-fstack-protector-strong -fpic -fpie -O2 -D_LARGEFILE_SOURCE
"PHP_LDFLAGS=-Wl,-O1 -pie",
"GPG_KEYS=CBAF69F173A0FEA4B537F470D66C9593118BCCB6 F38252826ACD957EF380D39
"PHP_VERSION=7.3.19",
"PHP_URL=https://www.php.net/distributions/php-7.3.19.tar.xz",
"PHP_ASC_URL=https://www.php.net/distributions/php-7.3.19.tar.xz.asc",
"PHP_SHA256=6402faa19b1a8c4317c7612632bce985684a5bbae0980a5779a40194398824
"PHP_MD5=",
"WORDPRESS_VERSION=5.4.2",
"WORDPRESS_SHA1=e5631f812232fbd45d3431783d3db2e0d5670d2d"
],
"Cmd": [
"apache2-foreground"
],
"ArgsEscaped": true,
"Image": "sha256:c10a2278bd33b371ee332f97d8eea4b18b297e7eb6ee086e4b829ff224640
"Volumes": {
"/var/www/html": {}
},
"WorkingDir": "/var/www/html",
"Entrypoint": [
"docker-entrypoint.sh"
],
"OnBuild": null,
"Labels": null,
"StopSignal": "SIGWINCH"
},
"Architecture": "amd64",
"Os": "linux",
"Size": 540262290,
"VirtualSize": 540262290,
"GraphDriver": {
"Data": {
"LowerDir": "/var/lib/docker/overlay2/d0d5e484c0b2d7c53a387f32034d1a76c823774e59c46666c160b89dc940c4fceef6aaf52b09ffbef0495a37895f/diff:/var/lib/docker/overlay2/7da/var/lib/docker/overlay2/d30bd98850c4db91ad8f3a2304bb2861e828c20d1f50f6b1b385ec0600f8399d/95fb7f1c62e43b13de672108a55/diff:/var/lib/docker/overlay2/63c0b97bb2a0578a66fb5c3f3eb13ae7e1177c9abc306fd151fb83c48581a7f4fbe88b82ac456efc083645d327bd/diff:/var/lib/docker/overlay2iff:/var/lib/docker/overlay2/82f426dee1fcf26b0cd754d89c4dbf7be83e7ac6a342d5796f79b8e8d9b030b9fd3ad9dd3f71a52ee9ec37eeb857/diff:/var/lib/docker/overlay2/90492d2f9575c394aaeecf40c31c34b71a96a10696c7706f05a21fe11957e1e4427e2d2f52995d78bcacc5fd71a5/diff:/var/lib/docker/over16/diff:/var/lib/docker/overlay2/94502395d936aa593d4575cebc7feaa39f361e07ac0661ef30aea379154cb345475703ded124b8343fa5fc26a564/diff:/var/lib/docker/overlay2/48ae99b670235cb9a6b05e51ay2/213e63785f45838a744391f25e3874480202e41e26faa54cc784a00a2e3330a9/diff:/var/lib/docker/4b0e70/diff:/var/lib/docker/overlay2/0d4b21ac998b9a81f0e3f9fe6ebafa41c657fb1bff36f0634555723f9f7f3e501f71ab286639f8a4979cae93e3b3/diff",
"MergedDir": "/var/lib/docker/overlay2/ccafc8b7d78d176f58b618c0744455292e7
"UpperDir": "/var/lib/docker/overlay2/ccafc8b7d78d176f58b618c0744455292e7f
"WorkDir": "/var/lib/docker/overlay2/ccafc8b7d78d176f58b618c0744455292e7fe
},
"Name": "overlay2"
},
"RootFS": {
"Type": "layers",
"Layers": [
"sha256:13cb14c2acd34e45446a50af25cb05095a17624678dbafbcc9e26086547c1d74",
"sha256:4e53c951cb3be8dd433b04b4baaf5d7e3d28b8769e8ee695f9168b256e85754f",
"sha256:a5df928da0a7242fe40ea6375f20d8434e1a5b88ad6453c3e4e1196d46a55187",
"sha256:db497de51efd83c51695b81f35d1e73e3d6d99f645cad9e26777a474abe9f660",
"sha256:fe216093409999341449fdc24c41bf40e73dd165f1df2e1616de862e6f9400a0",
"sha256:5be09852131792826711673c0b2ee7abde7d52ada63c74de1c7f812182604106",
"sha256:32e2a84acc26eaa3a3afaed80376a67c8498ddc6825622ac1c81ed7376741215",
"sha256:cd275085ca7adac1147329b75445799a169d87c19c6fa84c1c62ac7d148248d5",
"sha256:331aeb83b78f90b8d549e951d766d51b97c87c34a53d5336b9e1f1d59d4b164e",
"sha256:e4039a54cf07b15bfd65fd073911e7f0bd3b50733f7d68af2883e3abe701563d",
"sha256:4f34bd1e5854798e75cb689c46a34c33d02d27902a5741e1845fccaad22b11c9",
"sha256:fa0bdf5f6bfd63960ee85c04d1c52ba6c96778ed6bfc539aa81ae06e0ad43137",
"sha256:0838256aa71b34314a47ea271ba905b87fe6ad6f903f7c6d3aaa03254323d1ad",
"sha256:c2fa989035223ec633a53ca0a557e6272103187411a5a2bf394fe27f55cb191b",
"sha256:7483f924f7529904ea2cd84072f0ebd3bd200b974aaaf14bfb5bd547026a0ee3",
"sha256:aa70623a5747f22f4072ae0697286feeae4e59d684b934bff1ada7df1960dbee",
"sha256:8f8d6a24a5f1876a6f97b6af4c10af7302372dbb6f92216e01a4d98276386c4b",
"sha256:37659df06d3e1c46605bd735f85b1c943e61f59483ce719dcbbb2ae48bfdfe80",
"sha256:b440f9ae1d4caa4451ca6518082d85c114e1739191dc46077210a0ec3f2d198e",
"sha256:267d60806e3ed912c3418f6a9da30058667d3b7b2a22ca2f4c8c4fab4200db9f",
"sha256:b2cb1046d1ea9e0f935e9ab56241404bbfd8c4ed566da5b2bd45879bf3a8f622"
]
},
"Metadata": {
"LastTagTime": "0001-01-01T00:00:00Z"
}
}
]
查看容器日志
docker logs 容器ID
docker logs -ft --tail 10 容器ID #显示10条日志
查看容器内部的进程信息
docker top 容器ID
查看镜像的元数据
docker inspect 容器ID
进入容器
进入到容器内部
方法1:可以用docker run -it
docker run -it --name centos6 centos:6.9 /bin/bash
- -it 分配交互式终端
- –name 指定容器的名字
- /bin/bash 覆盖容器的初始命令
方法2:比较常用
docker exec -it 7752222e1e1 /bin/bash #进入容器并重新开启一个终端
-it 是重新分配一个终端
方法3
docker attach 容器ID #进入容器正在执行的终端
从容器拷贝文件到宿主机
docker cp 容器ID:容器内路径 目的的主机路径