1
2
3
4
5
6
7
8
# 查看docker版本信息
docker version
# 查看 docker 信息
docker info
# docker 万能命令
docker --help
# docker 官方文档命令地址
https://docs.docker.com/reference/

镜像命令

  • Docker images 查看所有在本地主机上的镜像
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
[root@test001 ~]# docker images 
REPOSITORY TAG IMAGE ID CREATED SIZE
mariadb 10.2 36837551a279 3 weeks ago 340MB
hello-world latest bf756fb1ae65 9 months ago 13.3kB

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

# docker images 的可选项 可以使用 --help打开可选项帮助

Options:
-a, --all Show all images (default hides intermediate images) #查看所有的镜像
--digests Show digests #摘要
-f, --filter filter Filter output based on conditions provided #根据条件过滤
--format string Pretty-print images using a Go template #格式化
--no-trunc Don't truncate output #截断
-q, --quiet Only show numeric IDs # 只显示镜像的ID

Docker搜索镜像 docker search 搜索的镜像信息

1
2
3
4
5
6
7
8
9
10
Search the Docker Hub for images

Options:
-f, --filter filter Filter output based on conditions provided #根据过滤信息搜索
--format string Pretty-print search using a Go template #格式化
--limit int Max number of search results (default 25) #对搜索的结果进行限制
--no-trunc Don't truncate output #不对结果进行截断

#例如:要搜索被Stars 3000以上的
docker search mysql -f=STARS=3000

下载镜像

Docker 下载根据层级下载,避免了重复下载,实现共用相同的镜像,节省内存,联合文件系统

docker pull [OPTIONS] NAME[:TAG|@DIGEST]

Docker pull 参数选项解析

命令description描述
–all-tags , -aDownload all tagged images in the repository下载存储库中所有标记的镜像
–disable-content-trustSkip image verification跳过镜像校验
–platformexperimental (daemon)API 1.32+Set platform if server is multi-platform capable实验(守护程序)API 1.32 + Set平台(如果服务器具有多平台功能)
–quiet , -qSuppress verbose output禁止详细输出
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#下载MySQL  如果后面不指定版本信息,默认使用的**最新版**
[root@test001 ~]# docker pull mysql
Using **default tag: latest**
latest: Pulling from library/mysql
bb79b6b2107f: Pull complete
49e22f6fb9f7: Pull complete
842b1255668c: Pull complete
9f48d1f43000: Pull complete
c693f0615bce: Pull complete
8a621b9dbed2: Pull complete
0807d32aef13: Pull complete
a56aca0feb17: Pull complete
de9d45fd0f07: Pull complete
1d68a49161cc: Pull complete
47834b5a7c81: Pull complete
7b0764b0009c: Pull complete
Digest: sha256:b30e3c13ab71f51c7951120826671d56586afb8d9e1988c480b8673c8570eb74
Status: Downloaded newer image for mysql:latest

**docker.io/library/mysql:latest #真实的下载MySQL的地址**

#等价
**docker pull mysql
docker pull docker.io/libary/mysql:latest**

#指定版本下载 MySQL docker pull [OPTIONS] MySQL[:**版本信息**|@DIGEST] 指定的MySQL版本要在官方镜像中存在

[root@test001 ~]# docker pull mysql:5.7
5.7: Pulling from library/mysql
bb79b6b2107f: Already exists
49e22f6fb9f7: Already exists
842b1255668c: Already exists
9f48d1f43000: Already exists
c693f0615bce: Already exists
8a621b9dbed2: Already exists
0807d32aef13: Already exists
f15d42f48bd9: Pull complete
098ceecc0c8d: Pull complete
0918bc006cab: Pull complete
2e0006443ed3: Pull complete
Digest: sha256:8f084c4e1cbf80b1975344271c9ee4b4c0019f8d057c1d179644dd89eef46c0f
Status: Downloaded newer image for mysql:5.7
docker.io/library/mysql:5.7
****

Docker删除镜像 命令⇒ docker rmi [OPTIONS] IMAGE [IMAGE…]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#用法 Usage  docker rmi [OPTIONS] IMAGE [IMAGE...]
# 删除镜像 可以通过 镜像名称来删除还可以通过 镜像ID删除,还可以根据指定参数 比如 -f 强力删除

#根据 镜像ID删除
[root@test001 ~]# docker rmi b5c10a3624ce
Untagged: mysql:latest
Untagged: mysql@sha256:b30e3c13ab71f51c7951120826671d56586afb8d9e1988c480b8673c8570eb74
Deleted: sha256:b5c10a3624cefb120b8b43e2494aeaab8dfa69f10450a40670d9793c2f9c9564
Deleted: sha256:64a40d52dfb0e048b33f6912596d591ff6196ef49d3c2c6df43b2509f52f7a73
Deleted: sha256:36341005e0b18207409665723254779fb244fa6dbbfafd8fc22ce7bc894fb9ec
Deleted: sha256:c25bdaaaf298842706f214fd3d855ca6f215cc66d01345efb9fd7479f6ca7b5f
Deleted: sha256:64231a7235fe05862b419beb8884dec599d27cab1b1bb402f0f75e64bc4cbd19
Deleted: sha256:79bc33a7ecad0032cc5e218d8b246b645f4cfddbf639b5db8383f81d4cbb6c9b

#根据 option -f来进行强制全部删除 -f强制删除 $(docker images -qa) 查询全部镜像ID 然后根据镜像ID全部强制删除

[root@test001 ~]# docker rmi -f $(docker images -qa )
Untagged: mysql:5.7
Untagged: mysql@sha256:8f084c4e1cbf80b1975344271c9ee4b4c0019f8d057c1d179644dd89eef46c0f
Deleted: sha256:2836a03e922fadf36e2d5cff005394cd8391f9c33d8ffb9e92220564c94fdfca
Deleted: sha256:ef037e726bf99879545cdd0de116d04fcb02a41c0cb9aef796d45e00955c33c7
Deleted: sha256:808b3da01295593488144de32cc939673f055dcb02f79050b9c832d6e1a9dfcc
Deleted: sha256:2d927a6eb4cc26ba5454193a3f4abf503b6cea710293d4f0e6d061c08578b687
Deleted: sha256:b96c2c2176ca7b5223469ad39c9e17509505233dd81030878bc12b03074baef4
Deleted: sha256:33134afe9e842a2898e36966f222fcddcdb2ab42e65dcdc581a4b38b2852c2e0
Deleted: sha256:dd053ec71039c3646324372061452778609b9ba42d1501f6a72c0272c94f8965
Deleted: sha256:2d4c647f875f6256f210459e5c401aad27ad223d0fa3bada7c4088a6040f8ba4
Deleted: sha256:4bded7e9aa769cb0560e60da97421e2314fa896c719179b926318640324d7932
Deleted: sha256:5fd9447ef700dfe5975c4cba51d3c9f2e757b34782fe145c1d7a12dfbee1da2f
Deleted: sha256:5ee7cbb203f3a7e90fe67330905c28e394e595ea2f4aa23b5d3a06534a960553
Deleted: sha256:d0fe97fa8b8cefdffcef1d62b65aba51a6c87b6679628a2b50fc6a7a579f764c
Untagged: mariadb:10.2
Untagged: mariadb@sha256:9343e2d69f0a85cc022a22a5b0b28fb5aed0b1960a2644b46fdb6a2bb91ad248
Deleted: sha256:36837551a279ce8b8a30fd7cac385ad17656a7dd219f3e5bb6a29bef51acc5c3
Deleted: sha256:7d1b6c896600358059e6aaf08c209c912f03bf4149d0d81f3ad9ca96351a459a
Deleted: sha256:2dd8ee90a824aabc49da66b2cc5f8d62d468a5283896880ceddd2f52bb8964f6
Deleted: sha256:67a4a7b9afc111428a0daedb9077f0404b7f8983c3a72662286fd4567752678c
Deleted: sha256:71664e7f9269a750e2e9589802df2aa71822bc7c6359be5117a2084773869608
Deleted: sha256:3906b456ad0bbe6e6d88c1f0b54813349e7906e65763c560c082259e52f5052a
Deleted: sha256:00b27bab3074eec91d743d84db8e4f3f84d23caf83941d971d6e0bb26de1a56c
Deleted: sha256:51b9071eb7657615f752cef1e725287932a6ed9a409b6d44b1a10da575a311cb
Deleted: sha256:bbad490dc9583e822357c7e8f7ab365075386031425b9b52888d178ae099a7a4
Deleted: sha256:bf9310e45975365dc4024cdfdd14479ac375707a42c224f8982d028814c6ebb7
Deleted: sha256:1e12a1ef671a00d018ca61e597a527b0f6148eb143d183684223a28c92a49736
Deleted: sha256:423662870fb3d051ccbc52495a1ff83e3b0c136d88614618dd18ea5b5a4fda15
Deleted: sha256:dd44b56f7a8f4d7c34f8fe346f507e46defea98f198bccd13ef227a80a512f18
Deleted: sha256:80580270666742c625aecc56607a806ba343a66a8f5a7fd708e6c4e4c07a3e9b
Untagged: hello-world:latest
Untagged: hello-world@sha256:8c5aeeb6a5f3ba4883347d3747a7249f491766ca1caa47e5da5dfcf6b9b717c0
Deleted: sha256:bf756fb1ae65adf866bd8c456593cd24beb6a0a061dedf42b26a993176745f6b
命令description描述
–force , -fForce removal of the image强力删除某个镜像
–no-pruneDo not delete untagged parents不要删除未加标签的父母级

容器命令

我们有了镜像才可以创建容器,Linux,下载一个centos来测试学习

新建容器并启动

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
docker run [options] image

#参数说明
--name 给容器一个名称,用于区分容器
-d 后台方式运行
-it 使用交互方式运行,进入容器查看内容
-p 指定容器的端口
-p ip:主机端口:容器端口 #根据ip来进行指定
-p 主机端口:容器端口 (常用) #让主机端口和容器端口映射起来
-p 容器端口 #指定容器端口
容器端口 #不用-p命令直接写容器端口

-P(大写P) 随机指定端口

#测试 启动并进入容器
root@again:~# docker run -it centos /bin/bash
[root@6e8937399257 /]# ls
bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var
#退出这个容器
[root@6e8937399257 /]# exit
exit
root@again:~#

#列出所有运行的容器
root@again:~# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
4b7005ca94a6 mariadb:10.2 "docker-entrypoint.s…" 6 weeks ago Up 13 days 0.0.0.0:7021->3306/tcp trojan-mariadb
#查询所有运行的容器
root@again:~# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
6e8937399257 centos "/bin/bash" 3 minutes ago Exited (0) About a minute ago festive_stonebraker
4cb852419d9d centos "/bin/bash" 3 minutes ago Exited (127) 3 minutes ago charming_varahamihira
e3dc2e75a462 hello-world "/hello" 4 minutes ago Exited (0) 4 minutes ago wonderful_archimedes
a83c0a6b6e36 hello-world "/hello" 4 days ago Exited (0) 4 days ago brave_payne
61dc28dad2a0 hello-world "/hello" 4 days ago Exited (0) 4 days ago pensive_burnell
4b7005ca94a6 mariadb:10.2 "docker-entrypoint.s…" 6 weeks ago Up 13 days 0.0.0.0:7021->3306/tcp trojan-mariadb
root@again:~#
# 根据条件显示运行的容器
root@again:~# docker ps -a -n=1
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
6e8937399257 centos "/bin/bash" 6 minutes ago Exited (0) 4 minutes ago festive_stonebraker

#只显示容器的编号
root@again:~# docker ps -aq
6e8937399257
4cb852419d9d
e3dc2e75a462
a83c0a6b6e36
61dc28dad2a0
4b7005ca94a6

退出容器

1
exit

退出容器不停止

1
使用 快捷键  CTRL+p+q

删除容器

1
2
3
4
5
6
#普通删除
docker rm 容器id
#递归删除 -f force 强制删除
docker rm -f $(docker ps -qa)
#通过管道符删除容器
docker ps -a -q | xargs docker rm

启动和停止容器的操作

1
2
3
4
5
6
7
8
# 启动容器
docker start 容器ip
# 重启容器
docker restart 容器id
# 停止容器
docker stop 容器id
# 杀掉容器
docker kill 容器id 强制停止指定的容器

修改镜像名称

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# 语法为:  docker tag 镜像名:Tag 新的镜像名:新Tag

[root@fugui001 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
sky0429/java final fa6c155ac43c 15 minutes ago 669MB
sky0429/java test fa6c155ac43c 15 minutes ago 669MB
sky0429/spark final 864cade88d71 39 minutes ago 939MB
sky0429/spark 1.0 5e6a1433e9cc About an hour ago 928MB
centos latest 300e315adb2f 3 weeks ago 209MB
[root@fugui001 ~]# docker tag sky0429/spark:1.0 sky0429/spark:2.0
[root@fugui001 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
sky0429/java final fa6c155ac43c 15 minutes ago 669MB
sky0429/java test fa6c155ac43c 15 minutes ago 669MB
sky0429/spark final 864cade88d71 39 minutes ago 939MB
sky0429/spark 1.0 5e6a1433e9cc About an hour ago 928MB
sky0429/spark 2.0 5e6a1433e9cc About an hour ago 928MB
centos latest 300e315adb2f 3 weeks ago 209MB
[root@fugui001 ~]#

修改容器名称

1
2
3
4
5
6
7
8
9
10
# 语法为: docker rename 容器ID 新的容器名

[root@fugui001 ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
3c9a8225f928 004894834ce7 "/bin/bash" 8 minutes ago Exited (127) 8 minutes ago romantic_kalam
[root@fugui001 ~]# docker rename 3c9a8225f928 test
[root@fugui001 ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
3c9a8225f928 004894834ce7 "/bin/bash" 8 minutes ago Exited (127) 8 minutes ago test
[root@fugui001 ~]#