容器命令
新建容器并启动 docker run image
[root@bogon ~]# 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 ...
[root@bogon ~]# docker run -it centos /bin/bash [root@0f499f177381 /]# [root@0f499f177381 /]# ls bin dev etc home lib lib64 lost+found media
[root@0f499f177381 /]# exit exit
查看运行记录docker ps
[root@bogon ~]# docker ps --help Usage: docker ps [OPTIONS] List containers Options: -a, --all Show all containers (default shows just running) -f, --filter filter Filter output based on conditions provided --format string Pretty-print containers using a Go template -n, --last int Show n last created containers (includes all states) (default -1) -l, --latest Show the latest created container (includes all states) --no-trunc Don't truncate output -q, --quiet Only display container IDs -s, --size Display total file sizes
[root@bogon ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES [root@bogon ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 0f499f177381 centos "/bin/bash" 3 minutes ago Exited (0) 11 seconds ago mystifying_noyce 399f59e81314 centos "/bin/bash" 5 minutes ago Exited (0) 5 minutes ago zen_nightingale 29503dde5cc6 centos "/bin/bash" 18 hours ago Exited (0) 18 hours ago sleepy_hermann 4de5bcf0a027 hello-world "/hello" 3 days ago Exited (0) 3 days ago sad_shaw [root@bogon ~]# docker ps -aq 0f499f177381 399f59e81314 29503dde5cc6 4de5bcf0a027
退出容器 exit ; ctrl+p+q
[root@bogon ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES fd820dce0b42 centos "/bin/bash" 28 seconds ago Up 20 seconds quirky_banach
启动已经停止的容器docker start
[root@bogon ~]# docker start --help Usage: docker start [OPTIONS] CONTAINER [CONTAINER...] Start one or more stopped containers 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
启动指定的已经停止了的容器
[root@bogon ~]# docker start 0f499f177381 0f499f177381 [root@bogon ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES fd820dce0b42 centos "/bin/bash" 6 minutes ago Up 5 minutes quirky_banach 0f499f177381 centos "/bin/bash" 14 minutes ago Up 12 seconds mystifying_noyce [root@bogon ~]#
停止容器docker stop
[root@bogon ~]# docker stop --help Usage: docker stop [OPTIONS] CONTAINER [CONTAINER...] Stop one or more running containers Options: -t, --time int Seconds to wait for stop before killing it (default 10)
[root@bogon ~]# docker stop 0f499f177381 0f499f177381 [root@bogon ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES fd820dce0b42 centos "/bin/bash" 8 minutes ago Up 8 minutes quirky_banach [root@bogon ~]#
重启容器 docker restart
[root@bogon ~]# docker restart --help Usage: docker restart [OPTIONS] CONTAINER [CONTAINER...] Restart one or more containers Options: -t, --time int Seconds to wait for stop before killing the container (default 10)
[root@bogon ~]# docker restart 0f499f177381 0f499f177381 [root@bogon ~]# docker ps -q fd820dce0b42 0f499f177381 [root@bogon ~]#
强停容器 docker kill
[root@bogon ~]# docker kill --help Usage: docker kill [OPTIONS] CONTAINER [CONTAINER...] Kill one or more running containers Options: -s, --signal string Signal to send to the container (default "KILL")
[root@bogon ~]# docker kill fd820dce0b42 fd820dce0b42
删除容器 docker rm
[root@bogon ~]# docker rm --help
Usage: docker rm [OPTIONS] CONTAINER [CONTAINER...] Remove one or more containers 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
[root@bogon ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES fd820dce0b42 centos "/bin/bash" 15 minutes ago Up 15 minutes quirky_banach 0f499f177381 centos "/bin/bash" 23 minutes ago Up 4 minutes mystifying_noyce 399f59e81314 centos "/bin/bash" 25 minutes ago Exited (0) 25 minutes ago zen_nightingale 29503dde5cc6 centos "/bin/bash" 18 hours ago Exited (0) 18 hours ago sleepy_hermann 4de5bcf0a027 hello-world "/hello" 3 days ago Exited (0) 3 days ago sad_shaw [root@bogon ~]# docker rm 399f59e81314 ## 正常移除了已经停止的容器 399f59e81314 [root@bogon ~]# docker rm 0f499f177381 ## 无法移除了在运行的容器 Error response from daemon: You cannot remove a running container 0f499f177381d1fe7bc2cfdc302bfff99052c96eeb140e7c4932fb584a5053ed. Stop the container before attempting removal or force remove [root@bogon ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES fd820dce0b42 centos "/bin/bash" 15 minutes ago Up 15 minutes quirky_banach 0f499f177381 centos "/bin/bash" 24 minutes ago Up 5 minutes mystifying_noyce 29503dde5cc6 centos "/bin/bash" 18 hours ago Exited (0) 18 hours ago sleepy_hermann 4de5bcf0a027 hello-world "/hello" 3 days ago Exited (0) 3 days ago sad_shaw ## 强制移除 [root@bogon ~]# docker rm -f 0f499f177381 0f499f177381
删除所有容器
[root@bogon ~]# docker rm -f $(docker ps -a)
[root@bogon ~]# docker ps -aq |xargs docker rm
cab139f64b92
学习参考视频:【狂神说Java】Docker最新超详细版教程通俗易懂_哔哩哔哩_bilibili