删除全部停止的容器
docker container prune [OPTIONS] Options: --filter filter Provide filter values (e.g. 'until=<timestamp>') -f, --force Do not prompt for confirmation docker container prune WARNING! This will remove all stopped containers. Are you sure you want to continue? [y/N] y Total reclaimed space: 0B删除的时候会提示是否继续,如果不想提示可以加-f参数,最终会输出删除结果。
rename重命名一个容器
docker container rename CONTAINER NEW_NAME or docker rename CONTAINER NEW_NAME查看容器
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 30c33d778412 4b "catalina.sh run" 23 minutes ago Up 23 minutes 0.0.0.0:8089->8080/tcp, :::8089->8080/tcp elated_northcutt 8efed1c23ff5 4b "catalina.sh run" 30 minutes ago Up 18 minutes 0.0.0.0:8088->8080/tcp, :::8088->8080/tcp charming_pike将容器30c33d778412重命名
docker rename 30c33d778412 test_name CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 30c33d778412 4b "catalina.sh run" 24 minutes ago Up 24 minutes 0.0.0.0:8089->8080/tcp, :::8089->8080/tcp test_name 8efed1c23ff5 4b "catalina.sh run" 31 minutes ago Up 18 minutes 0.0.0.0:8088->8080/tcp, :::8088->8080/tcp charming_pike从结果的NAMES看到已经修改成test_name的容器名了。
restart重启一个或多个容器
docker container restart [OPTIONS] CONTAINER [CONTAINER...] or docker restart [OPTIONS] CONTAINER [CONTAINER...] Options: -t, --time int Seconds to wait for stop before killing the container (default 10)重启上面列出的两个容器30c33d778412,8efed1c23ff5。
docker restart 30c33d778412 8efed1c23ff5 30c33d778412 8efed1c23ff5如果重启容器想隔一会再重启,可以增加参数-t,单位:秒。
rm删除一个或多个容器
docker container rm [OPTIONS] CONTAINER [CONTAINER...] or docker rm [OPTIONS] CONTAINER [CONTAINER...] Options: -f, --force Force the removal of a running container (uses SIGKILL) -l, --link Remove the specified link -v, --volumes Remove anonymous volumes associated with the container删除容器时,容器必须是未运行的。
docker rm 30c33d778412 Error response from daemon: You cannot remove a running container 30c33d7784121fc46beba6a3097ac9dda9a47e37c5bc6ccc4f2c2b7eb9b5b0cf. Stop the container before attempting removal or force remove如果实在是想删除,可以添加-f参数,强制删除运行的容器。
run运行一个新的容器,使用方式与创建容器差不多,参数也差不多。
docker container run [OPTIONS] IMAGE [COMMAND] [ARG...] or docker run [OPTIONS] IMAGE [COMMAND] [ARG...]运行tomcat容器,后台运行并将端口映射为8087
docker run -d -p 8087:8080 4b6a6e0cf6c1 start启动一个或多个停止的容器
docker container start [OPTIONS] CONTAINER [CONTAINER...] or docker start [OPTIONS] CONTAINER [CONTAINER...] Options: -a, --attach Attach STDOUT/STDERR and forward signals --detach-keys string Override the key sequence for detaching a container -i, --interactive Attach container's STDIN启动容器30c33d778412
docker start 30c33d778412 30c33d778412如果想看到启动的过程,可以增加-a参数,不过退出启动过程,容器也会跟着停止。
stats显示容器资源使用统计信息的实时流
docker container stats [OPTIONS] [CONTAINER...] or ocker stats [OPTIONS] [CONTAINER...] Options: -a, --all Show all containers (default shows just running) --format string Pretty-print images using a Go template --no-stream Disable streaming stats and only pull the first result --no-trunc Do not truncate output如果不跟任何容器,则输出所有容器的实时信息
docker stats CONTAINER ID NAME CPU % MEM USAGE / LIMIT MEM % NET I/O BLOCK I/O PIDS 30c33d778412 test_name 0.10% 102.7MiB / 3.67GiB 2.73% 866B / 0B 0B / 0B 33 8efed1c23ff5 charming_pike 0.07% 97.89MiB / 3.67GiB 2.61% 1.01kB / 0B 0B / 0B 33 stop停止一个或多个正在运行的容器
docker container stop [OPTIONS] CONTAINER [CONTAINER...] docker stop [OPTIONS] CONTAINER [CONTAINER...] Options: -t, --time int Seconds to wait for stop before killing it (default 10)从参数上看,停止命令与重启命令一样,可以设置时间。
docker stop 30c33d778412 docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 30c33d778412 4b "catalina.sh run" 46 minutes ago Exited (143) 13 seconds ago test_name 8efed1c23ff5 4b "catalina.sh run" 53 minutes ago Up 18 minutes 0.0.0.0:8088->8080/tcp, :::8088->8080/tcp charming_pike从结果看出PORTS列,停止掉的容器已经是空的,表示容器已经停止成功。
update更新一个或多个容器配置
docker container update [OPTIONS] CONTAINER [CONTAINER...] or docker update [OPTIONS] CONTAINER [CONTAINER...] Options: --blkio-weight uint16 Block IO (relative weight), between 10 and 1000, or 0 to disable (default 0) --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 the CPU real-time period in microseconds --cpu-rt-runtime int Limit the 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) --kernel-memory bytes Kernel memory limit -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 --pids-limit int Tune container pids limit (set -1 for unlimited) --restart string Restart policy to apply when a container exits从参数上看,可以更新容器的cpu、内存配置,需要注意的是,--kernel-memory自Docker 20.10起,该选项已被弃用。
wait等待到一个或多个容器停止,然后打印容器的退出代码
docker container wait CONTAINER [CONTAINER...] or docker wait CONTAINER [CONTAINER...]如果是一个已经停止的容器,则直接输出退出的代码
docker wait 30c33d778412 143指定一个正在运行的容器,会一直等待容器停止,然后输出退出代码。
docker wait 8efed1c23ff5启动另外一个窗口,停止8efed1c23ff5容器,这个时候之前wait命令的窗口就输出143的容器退出代码。