- 重启docker服务 service docker restart
- 关闭docker服务 service docker stop
- 开启docker服务 service docker start
- 查看当前运行的容器:docker ps
- 查询存在的容器:docker ps -a
- 删除容器:docker -rm CONTAINERID
- 强制删除容器:docker -rm -f CONTAINERID
- 不能够删除一个正在运行的容器,会报错。需要先停止容器。
- 查看镜像:docker images
- 删除镜像:docker -rmi IMAGEID
- 强制删除镜像:docker -rmi -f IMAGEID
- 利用镜像创建容器:docker run --name centos -itd centos:latest
注:-i表示让容器的标准输入打开,-t表示分配一个伪终端,-d表示后台启动,要把-i -t -d 放到镜像名字前面,--name指定容器名。
实战
1、docker run centos
首次运行如果本地没有该base image,则会从远端clone一个base images。
[root@MiWiFi-R3L-srv ~]# docker run centos Unable to find image 'centos:latest' locally latest: Pulling from library/centos 3c72a8ed6814: Pull complete Digest: sha256:76d24f3ba3317fa945743bb3746fbaf3a0b752f10b10376960de01da70685fbd Status: Downloaded newer image for centos:latest [root@MiWiFi-R3L-srv ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE centos latest 0d120b6ccaa8 2 months ago 215MB hello-world latest bf756fb1ae65 10 months ago 13.3kB [root@MiWiFi-R3L-srv ~]# docker run centos [root@MiWiFi-R3L-srv ~]#
docker run centos 执行完之后centos对应的/bin/bash 就退出了。
交互式的docker run
- -i, --interactive Keep STDIN open even if not attached
- -t, --tty Allocate a pseudo-TTY
[root@MiWiFi-R3L-srv ~]# docker run -it centos [root@de7d5f1cad4e /]# ls -l total 0 lrwxrwxrwx. 1 root root 7 May 11 2019 bin -> usr/bin drwxr-xr-x. 5 root root 360 Nov 3 14:23 dev drwxr-xr-x. 1 root root 66 Nov 3 14:23 etc drwxr-xr-x. 2 root root 6 May 11 2019 home lrwxrwxrwx. 1 root root 7 May 11 2019 lib -> usr/lib lrwxrwxrwx. 1 root root 9 May 11 2019 lib64 -> usr/lib64 drwx------. 2 root root 6 Aug 9 21:40 lost+found drwxr-xr-x. 2 root root 6 May 11 2019 media drwxr-xr-x. 2 root root 6 May 11 2019 mnt drwxr-xr-x. 2 root root 6 May 11 2019 opt dr-xr-xr-x. 308 root root 0 Nov 3 14:23 proc dr-xr-x---. 2 root root 162 Aug 9 21:40 root drwxr-xr-x. 11 root root 163 Aug 9 21:40 run lrwxrwxrwx. 1 root root 8 May 11 2019 sbin -> usr/sbin drwxr-xr-x. 2 root root 6 May 11 2019 srv dr-xr-xr-x. 13 root root 0 Oct 29 14:24 sys drwxrwxrwt. 7 root root 145 Aug 9 21:40 tmp drwxr-xr-x. 12 root root 144 Aug 9 21:40 usr drwxr-xr-x. 20 root root 262 Aug 9 21:40 var [root@de7d5f1cad4e /]#
此时使用docker container ls 可以看到正在运行的image
2、查看centos容器
[root@MiWiFi-R3L-srv ~]# docker container ls CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES [root@MiWiFi-R3L-srv ~]# [root@MiWiFi-R3L-srv ~]# [root@MiWiFi-R3L-srv ~]# docker container ls -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 7af01d5e9dda centos "/bin/bash" 13 seconds ago Exited (0) 12 seconds ago happy_sinoussi 9514cf1bd842 centos "/bin/bash" 52 seconds ago Exited (0) 51 seconds ago sweet_curie 0edb374d25bb hello-world "/hello" 24 hours ago Exited (0) 24 hours ago bold_burnell
3、删除退出的容器
docker container的命令列表,使用rm
root@MiWiFi-R3L-srv ~]# 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.
或者docker rm de7 ,默认删除的container
注意不要加上参数 i 就是 删除image。
docker rmi {id}
如何批量删除已经退出的container?
[root@MiWiFi-R3L-srv ~]# docker container ls -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 922dd27b0faf centos "/bin/bash" 10 seconds ago Exited (0) 9 seconds ago mystifying_khorana af024051dd04 centos "/bin/bash" 13 seconds ago Exited (0) 12 seconds ago inspiring_mendeleev 3c91318afd1e centos "/bin/bash" 16 seconds ago Exited (0) 15 seconds ago modest_johnson 7af01d5e9dda centos "/bin/bash" 21 minutes ago Exited (0) 21 minutes ago happy_sinoussi 9514cf1bd842 centos "/bin/bash" 21 minutes ago Exited (0) 21 minutes ago sweet_curie 0edb374d25bb hello-world "/hello" 24 hours ago Exited (0) 24 hours ago bold_burnell
使用-q参数列出containerId
root@MiWiFi-R3L-srv ~]# docker container ls -aq 922dd27b0faf af024051dd04 3c91318afd1e 7af01d5e9dda 9514cf1bd842 0edb374d25bb
如果你会使用awk,不使用这个参数完全可以,请看:
[root@MiWiFi-R3L-srv ~]# docker container ls -a|grep -v CONTAINER|awk '{print $1}' 922dd27b0faf af024051dd04 3c91318afd1e 7af01d5e9dda 9514cf1bd842 0edb374d25bb
删除所有已经退出的container,世界清静了。
[root@MiWiFi-R3L-srv ~]# docker rm $(docker container ls -aq) 922dd27b0faf af024051dd04 3c91318afd1e 7af01d5e9dda 9514cf1bd842 0edb374d25bb [root@MiWiFi-R3L-srv ~]# docker container ls -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES [root@MiWiFi-R3L-srv ~]#
这样把正在run的container也干掉了,显然是不行的。例如这样如何只干掉Exited的container呢?
-f 过滤状态,当然shell也可以实现
root@MiWiFi-R3L-srv ~]# docker container ls -f "status=exited" CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES e03dda244d80 centos "/bin/bash" 2 minutes ago Exited (0) 2 minutes ago jolly_euclid 590c5bd090d7 centos "/bin/bash" 2 minutes ago Exited (0) 2 minutes ago sweet_hertz 707300b53120 centos "/bin/bash" 2 minutes ago Exited (0) 2 minutes ago pedantic_liskov dbf30d595e76 centos "/bin/bash" 2 minutes ago Exited (0) 2 minutes ago goofy_leakey [root@MiWiFi-R3L-srv ~]# docker container ls -f "status=exited" -q e03dda244d80 590c5bd090d7 707300b53120 dbf30d595e76