Docker 命令集合
1 | [root@VM-0-13-centos ~]# docker container --help |
使用场景
容器启动
新建容器并启动
涉及命令:
docker run
示例:
启动并操作后则终止容器
1
2
3
4
5
6
7
8
9
10[root@VM-0-13-centos ~]# docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx v2 33a05755d7f3 3 weeks ago 142 MB
docker.io/nginx latest 51086ed63d8c 3 weeks ago 142 MB
docker.io/ubuntu 18.04 71cb16d32be4 3 weeks ago 63.1 MB
<none> <none> 618d482e06b4 3 months ago 52.6 MB
<none> <none> 4c0257ee9d10 3 months ago 5.53 MB
docker.io/ubuntu latest 27941809078c 4 months ago 77.8 MB
[root@VM-0-13-centos ~]# docker run docker.io/ubuntu:18.04 /bin/echo 'Hello, World!'
Hello, World!启动
bash
终端,并进行交互1
2
3
4
5
6[root@VM-0-13-centos ~]# docker run -it docker.io/ubuntu:18.04 /bin/bash
root@8c2f4c3d6f16:/# pwd
/
root@8c2f4c3d6f16:/# exit
exit
[root@VM-0-13-centos ~]#
启动已中止容器
- 涉及命令:
docker container start
- 涉及命令:
后台运行
很多场景下, 需要Docker
后台运行而不是讲命令结果输出在宿主机上, 可通过 -d
参数实现;
涉及命令:
docker run -d
示例:
不用
-d
:1
2
3
4
5
6
7[root@VM-0-13-centos ~]# docker run docker.io/ubuntu:18.04 /bin/sh -c "while true; do echo hello world; sleep 1; done"
hello world
hello world
hello world
hello world
hello world
hello world可以看到容器的输出结果
STDOUT
, 打印在了宿主机上;用
-d
:1
2[root@VM-0-13-centos ~]# docker run -d docker.io/ubuntu:18.04 /bin/sh -c "while true; do echo hello world; sleep 1; done"
86ef2217c3d59179524323c4f957bb74cb7f47d8e173eb7f0f2c66d9a7eb9128此时容器选择后台运行;
容器终止
容器进入终止状态的方式是很多的, 例如:
Docker
容器指定的应用终结时, 自动终止容器;Docker
容器内通过,exit
,Ctrl+d
退出终端后, 容器终止;Docker container stop
命令, 可以终止一个处于运行
状态的容器;示例: 将上面启动的后台运行的
Docker
容器终止掉:1
2
3
4
5
6
7[root@VM-0-13-centos ~]# docker container ls
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
86ef2217c3d5 docker.io/ubuntu:18.04 "/bin/sh -c 'while..." 9 minutes ago Up 9 minutes practical_montalcini
[root@VM-0-13-centos ~]# docker container stop 86ef2217c3d5
86ef2217c3d5
[root@VM-0-13-centos ~]# docker container ls
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES进入容器
在某些场景内, 需要进入容器,进行操作, 可以使用 docker attach
或 docker exec -it
命令, 推荐使用 docker exec -it
;
使用
docker attach
1
2
3
4
5
6
7
8
9
10[root@VM-0-13-centos ~]# docker run -dit docker.io/ubuntu:18.04
5d5cbafd10fdb777c7063444db737130768dae3c4c36a048bf0c1f66cba5656d
[root@VM-0-13-centos ~]# docker container ls
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
5d5cbafd10fd docker.io/ubuntu:18.04 "bash" 12 seconds ago Up 11 seconds brave_bohr
[root@VM-0-13-centos ~]# docker attach 5d5cbafd10fd
root@5d5cbafd10fd:/# exit
exit
[root@VM-0-13-centos ~]# docker container ls
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES可以看到 使用
exit
命令推出后, 容器变为终止状态;使用
docker exec
1
2
3
4
5
6
7
8[root@VM-0-13-centos ~]# docker run -dit docker.io/ubuntu:18.04
f170bccfbdcff331f02bbdbc5dfd163fa929caf87ebc4555dd768236cd696d19
[root@VM-0-13-centos ~]# docker exec -it f170 bash
root@f170bccfbdcf:/# exit
exit
[root@VM-0-13-centos ~]# docker container ls
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
f170bccfbdcf docker.io/ubuntu:18.04 "bash" 37 seconds ago Up 36 seconds mystifying_brattain可以看到即使使用了
exit
命令, 也没有导致容器进入终止状态;
删除容器
删除一个终止状态的容器,
docker container rm
删除所有终止状态的容器,
docker container prune
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19[root@VM-0-13-centos ~]# docker container prune
WARNING! This will remove all stopped containers.
Are you sure you want to continue? [y/N] y
Deleted Containers:
5d5cbafd10fdb777c7063444db737130768dae3c4c36a048bf0c1f66cba5656d
86ef2217c3d59179524323c4f957bb74cb7f47d8e173eb7f0f2c66d9a7eb9128
a3b8f09e5fe30f219e15a026ef52d070b204dbb12bad945fff16e4297c8c8d6e
8c2f4c3d6f164d3e32dff74eeab1562828b3bda4fdf77b3e079d41a839e64a4b
54a804e8935f520fa9915907bab8c21c5d09ea7fa064c46d01a0ed14f9f6cc6a
857e62419b36ea769c2655d1eb13fe268a3a516a731e2c5cf7848582aa7d2233
72b48ef7e44ba46b7746387c7041280bd5c065e5aa3e2672996d2affe675a005
8194230a842c3469242b4c9dbc81cb4a83cf21e23a718b6cd0e0d456215c8c2c
6be1289236e9321d86de1ca7aaded317b136b78c6174ed4192ead4a03cf44044
0dc2228117db93521c7048248eb56c2be289401d0630965010eb93e903a6aa7b
e0d023f15495e37eb3b78355cfda033eca7a1672f48c1574333157d09bab7976
a93b2d91a8c3dd59713017e423e7ad34108b87904bbbcd4b9143f9854667877e
7b44171dcfe7c63bb2aeab7df8a65e2a64afd1fe5182d07ef108b845143deb0b
Total reclaimed space: 9.85 MB删除一个运行状态的容器,
docker container rm -f
1
2
3
4
5
6
7
8[root@VM-0-13-centos ~]# docker container ls
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
f170bccfbdcf docker.io/ubuntu:18.04 "bash" 5 minutes ago Up 5 minutes mystifying_brattain
3fa97808f299 docker.io/ubuntu:18.04 "/bin/bash" 45 minutes ago Up 45 minutes upbeat_swanson
8bcaacf3fee6 nginx:v2 "/docker-entrypoin..." 3 weeks ago Up 3 weeks 0.0.0.0:81->80/tcp web2
c795eabb7c7d nginx "/docker-entrypoin..." 3 weeks ago Up 3 weeks 0.0.0.0:80->80/tcp webserver
[root@VM-0-13-centos ~]# docker rm -f upbeat_swanson
upbeat_swanson