Docker常用指令

帮助命令

1
2
3
docker version     	# 显示版本信息
docker info # 显示系统信息,包括镜像和容器数量
docker 命令 --help # 帮助命令

帮助文档的地址:https://docs.docker.com/get-started/

镜像命令

docker images:查看所有本地的主机上的镜像

1
2
3
4
5
6
7
8
9
10
11
12
13
14
REPOSITORY                                               TAG       IMAGE ID       CREATED        SIZE
tensorflow/serving latest 784e871ab5a4 4 weeks ago 298MB

# 解释
REPOSITORY 镜像的仓库名
TAG 镜像的标签
IMAGE ID 镜像的ID
CREATED 镜像创建时间
SIZE 镜像的大小

# 常用可选项
Options:
-a, --all # 列出所有的镜像
-q, --quiet # 只显示镜像ID

docker search:搜索镜像

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
(base) zzy@zzydeMacBook-Pro ~ % docker search mysql
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
mysql MySQL is a widely used, open-source relation… 10565 [OK]
mariadb MariaDB Server is a high performing open sou… 3947 [OK]
mysql/mysql-server Optimized MySQL Server Docker images. Create… 774 [OK]
percona Percona Server is a fork of the MySQL relati… 527 [OK]

# 解释
STARS 收藏

# 常用可选项
--filter=STARS=3000 搜索出来的镜像STARS大于3000

(base) zzy@zzydeMacBook-Pro ~ % docker search mysql --filter=STARS=3000
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
mysql MySQL is a widely used, open-source relation… 10565 [OK]
mariadb MariaDB Server is a high performing open sou… 3947 [OK]

docker pull:下载镜像

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# 下载镜像 docker pull 镜像名[:tag]

(base) zzy@zzydeMacBook-Pro ~ % docker pull mysql
Using default tag: latest #如果不指定tag,默认是最新的
latest: Pulling from library/mysql
45b42c59be33: Pull complete #分层下载,docker images的核心
b4f790bd91da: Pull complete
325ae51788e9: Pull complete
adcb9439d751: Pull complete
174c7fe16c78: Pull complete
698058ef136c: Pull complete
4690143a669e: Pull complete
f7599a246fd6: Pull complete
35a55bf0c196: Pull complete
790ac54f4c47: Pull complete
b0ddd5d1b543: Pull complete
1aefd67cb33d: Pull complete
Digest: sha256:7706e4c382be813b58ef514f2bdac747cd463a6866c6c81165d42a1d0e4fe947
Status: Downloaded newer image for mysql:latest
docker.io/library/mysql:latest #真实地址

# 等价的, 版本必须来自docker hub
docker pull mysql
docker pull mysql:lastest

docker rmi:删除镜像

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# 删除指定容器
docker rm -f 容器id
# 删除所有
docker rmi -f $(docker images -aq)


(base) zzy@zzydeMacBook-Pro ~ % docker rmi -f 8457e9155715
Untagged: mysql:latest
Untagged: mysql@sha256:7706e4c382be813b58ef514f2bdac747cd463a6866c6c81165d42a1d0e4fe947
Deleted: sha256:8457e9155715d4e1c80c9e048d94c9b47b5b733fa927756280382dd326403644
Deleted: sha256:f0d02d3f5fc5a0f745bf3a97ec0b26c6b2d8b05288d98d954eeb87c4a6d47146
Deleted: sha256:bf1129a8799d8beaafa396d6333a3ba6eac9d0d7f606491f9794c470fb2dd311
Deleted: sha256:4386f82820992c927b924177ed3e4c2ffd477d4db7a63539ac76fd09ee36cd89
Deleted: sha256:d7494c9168a11444d8b13558068409ace7393452f08f878686eec45122ee56c1
Deleted: sha256:08dbcab3fe630e39bbabaa9f0ae72ec6d100bf1e400ebb4b7f04151b18bca89c
Deleted: sha256:c3f78dcd6bcc4c156554296323e0eed74a4d2d93b304be15f55c1ef62dd06e0a
Deleted: sha256:f89b66495a65489290c8edb71e0dbf9e3d0d6213b82cebc2554b271599f2f99d
Deleted: sha256:1918839317d9988ff5e0168e336717e32820af1e77c3121297efc73a387ecdc5
Deleted: sha256:1d2bcd52664a92805e5f49d94d3649323dd0f5682ae3e1380fa07b7a54d6ceb0
Deleted: sha256:787de05fee96c7ba99e49f17d72aec68769a7373a8881a27917bdbf83dca58e8
Deleted: sha256:eb82f9a2fbd7a4a0fdfbe40b5e77a995ccf73ab91364d90f4db820fd59dbf63b
Deleted: sha256:9eb82f04c782ef3f5ca25911e60d75e441ce0fe82e49f0dbf02c81a3161d1300

容器命令

说明:有了镜像才可以创建容器,这里下载一个centos镜像进行尝试

新建容器并启动

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
docker run [参数] image /bin/bash

# 参数说明
--name="Name" 容器名字,用来区分容器
-d 后台方式运行
-it 使用交互方式运行,进入容器查看内容
-p 指定容器的端口
-p 主机端口:容器端口
-p 容器端口
容器端口
-P 随机指定端口

# 启动并进入容器
docker run -it centos /bin/bash

列出所有运行的容器

1
2
3
4
5
docker ps	# 列出当前正在运行的容器
-a # 列出当前正在运行的容器+历史运行容器
-n=? # 显示最近创建的容器
-q # 只显示容器编号

退出容器

1
2
exit		# 直接停止容器并退出
Ctrl + P + Q # 容器不停止,但是退出

删除容器

1
2
3
docker rm 容器id			# 删除指定容器,不能删除正在运行中的容器
docker rm -f $(docker ps -aq) # 删除所有容器
docker ps -a -q|xargs docker rm # 删除所有容器(linux版)

启动和停止

1
2
3
4
docker start 容器id	# 启动容器
docker restart 容器id # 重启容器
docker stop 容器id # 停止当前正在运行的容器
docker kill 容器id # 强制停止当前容器