帮助命令

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

 帮助文档的地址:Reference documentation | Docker Documentation

 docker命令导图


 

镜像命令

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

[root@iZmu0fa4hv0 ~]# docker images
REPOSITORY    TAG       IMAGE ID       CREATED        SIZE
hello-world   latest    d1165f221234   3 months ago   13.3kB

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

#可选项

-a, –all       列出所有镜像
    –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     只显示镜像ID


 

docker search:搜索镜像 

[root@iZmu0fa4hv0jwzZ ~]# docker search mysql
NAME                              DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
mysql                             MySQL is a widely used, open-source relation…   11020     [OK]       
mariadb                           MariaDB Server is a high performing open sou…   4176      [OK]

#可选项
Name, shorthand Default Description
--filter , -f   Filter output based on conditions provided
--format   Pretty-print search using a Go template
--limit 25 Max number of search results
--no-trunc   Don’t truncate output

#通过收藏来过滤
--filter=STARS=3000 搜索出来的镜像是STARS大于3000的

 

docker pull:下载镜像

 

#下载镜像 docker pull 镜像名[:TAG]

[root@iZmu0fa4hv0jwzZ ~]# docker pull mysql
Using default tag: latest  #如果写tag,默认就是latest
latest: Pulling from library/mysql
69692152171a: Pull complete #分层下载,docker image的核心
1651b0be3df3: Pull complete
951da7386bc8: Pull complete
0f86c95aa242: Pull complete
37ba2d8bd4fe: Pull complete
6d278bb05e94: Pull complete
497efbd93a3e: Pull complete
f7fddf10c2c2: Pull complete
16415d159dfb: Pull complete
0e530ffc6b73: Pull complete
b0a4a1a77178: Pull complete
cd90f92aa9ef: Pull complete
Digest: sha256:d50098d7fcb25b1fcb24e2d3247cae3fc55815d64fec640dc395840f8fa80969 #签名
Status: Downloaded newer image for mysql:latest
docker.io/library/mysql:latest #真实地址


#指定版本

[root@iZmu0fa4hv0jwzZ ~]# docker pull mysql:5.7
5.7: Pulling from library/mysql
69692152171a: Already exists
1651b0be3df3: Already exists
951da7386bc8: Already exists
0f86c95aa242: Already exists
37ba2d8bd4fe: Already exists
6d278bb05e94: Already exists
497efbd93a3e: Already exists
a023ae82eef5: Pull complete
e76c35f20ee7: Pull complete
e887524d2ef9: Pull complete
ccb65627e1c3: Pull complete
Digest: sha256:a682e3c78fc5bd941e9db080b4796c75f69a28a8cad65677c23f7a9f18ba21fa
Status: Downloaded newer image for mysql:5.7
docker.io/library/mysql:5.7

 

 


 

 

docker rmi:删除镜像

#通过ID删除(可指定删除多个容器):docker rmi -f 容器id [容器id ...]
[root@iZmu0fa4hv0jwzZ ~]# docker rmi -f 2c9028880e58
Untagged: mysql:5.7
Untagged: mysql@sha256:a682e3c78fc5bd941e9db080b4796c75f69a28a8cad65677c23f7a9f18ba21fa
Deleted: sha256:2c9028880e5814e8923c278d7e2059f9066d56608a21cd3f83a01e3337bacd68
Deleted: sha256:c49c5c776f1bc87cdfff451ef39ce16a1ef45829e10203f4d9a153a6889ec15e
Deleted: sha256:8345316eca77700e62470611446529113579712a787d356e5c8656a41c244aee
Deleted: sha256:8ae51b87111404bd3e3bde4115ea2fe3fd2bb2cf67158460423c361a24df156b
Deleted: sha256:9d5afda6f6dcf8dd59aef5c02099f1d3b3b0c9ae4f2bb7a61627613e8cdfe562
#删除全部容器:
[root@iZmu0fa4hv0jwzZ ~]# docker rmi -f $(docker images -aq)
Untagged: mysql:latest
Untagged: mysql@sha256:d50098d7fcb25b1fcb24e2d3247cae3fc55815d64fec640dc395840f8fa80969
Deleted: sha256:c0cdc95609f1fc1daf2c7cae05ebd6adcf7b5c614b4f424949554a24012e3c09
Deleted: sha256:137bebc5ea278e61127e13cc7312fd83874cd19e392ee87252b532f0162fbd56
Deleted: sha256:7ed0de2ad4e43c97f58fa9e81fba73700167ef9f8a9970685059e0109749a56b
Deleted: sha256:9375660fbff871cd29c86b8643be60e376bfc96e99a3d7e8f93d74cd61500705
Deleted: sha256:d8a47065d005ac34d81017677330ce096eb5562eeb971e2db12b0e200fdb1cb6
Deleted: sha256:ca13c8ad9df5d824d5a259a927eaa6c04a60f022bc2abe8fc7866cf4b2b366f4
Deleted: sha256:7af1865d5c19316c3dc0829a2ee2b3a744ae756f7fec9c213d3afc5f1f6ed306
Deleted: sha256:f205c8f3c8aaa6376442b34c0c2062738461d37e0aa16ba021cd7e09c67213c2
Deleted: sha256:d690e8a8242cf13cbe98c5b2faffdd0fc7e6c4c13425b5da31de991aa1f89a76
Deleted: sha256:24efeee958e9f3d859fe15540e9296d5aaa6d3eb3b5f5494a2e8370608a4cfaa
Deleted: sha256:654f2ffede3bb536fd62d04c9c7b7826e890828bec92182634e38684959b2498
Deleted: sha256:de478a06eaa676052e665faa0b07d86a007f4b87cf82eb46a258742dc2d32260
Deleted: sha256:02c055ef67f5904019f43a41ea5f099996d8e7633749b6e606c400526b2c4b33
Untagged: hello-world:latest
Untagged: hello-world@sha256:9f6ad537c5132bcce57f7a0a20e317228d382c3cd61edae14650eec68b2b345c
Deleted: sha256:d1165f2212346b2bab48cb01c1e39ee8ad1be46b87873d9ca7a4e434980a7726

 


容器命令

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

docker pull centos

新建容器并启动

docker run [可选参数] image

#参数说明
--name='Name' 容器名字 tomcat01 tomcat02用来区分容器
-d 后台方式运行
-it 使用交互式方式运行,进入容器查看内容
-P 指定容器的端口(大写P)
  -P ip:主机端口:容器端口
  -P 主机端口:容器端口(常用)
  -P 容器端口
-p 随即指定端口(小写p)
#测试,启动并进入容器

[root@iZmu0fa4hv0jwzZ ~]# docker run -it centos /bin/bash
[root@1923e3dc5536 /]# ls
bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var

#从容器中退回主机
[root@1923e3dc5536 /]# exit exit [root@iZmu0fa4hv0jwzZ ~]#

 

列出所有运行的容器

#docker ps命令
-a #列出当前正在运行的容器+带出历史运行过的容器
-n=? #显示个数
-q #只显示容器的编号

[root@iZmu0fa4hv0jwzZ ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES [root@iZmu0fa4hv0jwzZ ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 1923e3dc5536 centos "/bin/bash" 6 minutes ago Exited (0) 2 minutes ago boring_banzai 0796a6c2955a d1165f221234 "/hello" 16 hours ago Exited (0) 16 hours ago jovial_tu

 

退出容器

exit              #退出时会关闭容器    
Ctrl + P + Q      #退出时不会关闭容器   

 

删除容器

docker rm 容器id    #删除指定容器(不能删除正在运行的容器,但是可以用 rm -f 强制删除)
docker rm -f $(docker ps -aq)    #删除所有容器
docker ps -a -q|xargs docker rm #删除所有容器

 

启动和停止容器的操作

docker start 容器id    #启动容器(start和run的区别是start是启动docker里面创建的容器,run是新建容器并运行)
docker restart 容器id  #重启容器
docker stop 容器id     #停止当前正在运行的容器
docker kill 容器id     #强制停止当前容器

 

常用其他命令

后台启动容器

#命令 docker run -d 镜像名
[root@iZmu0fa4hv0jwzZ ~]# docker run -d centos

#问题docker ps发现centos停止了
#常见的坑:docker容器使用后台运行,就必须要有一个前台进程,docker容器发现没有应用,就会自动停止
#例如nginx容器启动后发现自己没有提供服务,就会立刻停止,就是没有程序了

 

查看日志

docker logs [OPTIONS] CONTAINER

OPTIONS

Name, shorthand

Default

Description

--details

 

Show extra details provided to logs

--follow , -f

 

Follow log output

--since

 

Show logs since timestamp (e.g. 2013-01-02T13:23:37Z) or relative (e.g. 42m for 42 minutes)

--tail , -n

all

Number of lines to show from the end of the logs

--timestamps , -t

 

Show timestamps

--until

 

API 1.35+

Show logs before a timestamp (e.g. 2013-01-02T13:23:37Z) or relative (e.g. 42m for 42 minutes)

 
查看centos容器日志

[root@iZmu0fa4hv0jwzZ ~]# docker logs -f -t c74585cccccd
[root@c74585cccccd /]# exit
2021-06-20T07:43:53.882961182Z exit
2021-06-20T07:45:12.520341913Z [root@c74585cccccd /]# exit

 

查看容器中进程信息

docker top 容器id
[root@iZmu0fa4hv0jwzZ ~]# docker run -it  centos /bin/bash
[root@46e729e6fd26 /]# [root@iZmu0fa4hv0jwzZ ~]# 
[root@iZmu0fa4hv0jwzZ ~]# docker ps
CONTAINER ID   IMAGE     COMMAND       CREATED          STATUS          PORTS     NAMES
46e729e6fd26   centos    "/bin/bash"   13 seconds ago   Up 12 seconds             eager_pasteur
[root@iZmu0fa4hv0jwzZ ~]# docker top 46e729e6fd26
UID                 PID                 PPID                C                   STIME               TTY                 TIME                CMD
root                61518               61497               1                   16:16               ?                   00:00:00            /bin/bash

 

查看镜像元数据

docker inspect [OPTIONS] NAME|ID [NAME|ID...]

Options

Name, shorthand Default Description
--format , -f   Format the output using the given Go template
--size , -s   Display total file sizes if the type is container
--type   Return JSON for specified type
 
[root@iZmu0fa4hv0jwzZ ~]# docker inspect 46e729e6fd26

 

进入当前正在进行的容器

我们通常容器都是使用后台方式运行的,需要进入容器,修改一些配置

方式一

docker exec -it 容器id /bin/bash

 

[root@iZmu0fa4hv0jwzZ ~]# docker ps
CONTAINER ID   IMAGE     COMMAND       CREATED          STATUS          PORTS     NAMES
46e729e6fd26   centos    "/bin/bash"   10 minutes ago   Up 10 minutes             eager_pasteur
[root@iZmu0fa4hv0jwzZ ~]# docker exec -it 46e729e6fd26 /bin/bash
[root@46e729e6fd26 /]# 

 

方式二

docker attach 容器id

 

区别

docker exec       #进入容器后开启一个新的终端,可以在里面操作
docker attach     #进入容器正在执行的终端,不会启动新的进程

 

从容器内拷贝文件到主机上

docker cp 容器id:容器内路径 目的主机路径

 

把centos容器中的home目录下的test.java文件拷贝到主机的home目录下

[root@iZmu0fa4hv0jwzZ home]# docker cp 8346286a9354:/home/test.java /home
[root@iZmu0fa4hv0jwzZ home]# ls
admin  test.java

 

版权声明:本文为Ayanamidesu原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/Ayanamidesu/p/14906374.html