Docker命令详解

一、帮助启动命令

启动docker : systemctl start docker

停止dockersystemctl stop docker

重启dockersystemctl restart docker

查看docker状态systemctl status docker

开机启动systemctl enable docker

查看docker概要信息docker info

查看docker总体帮助文档docker --help

查看docker命令帮助文档docker 具体命令 --help

[root@localhost ~]# docker cp --help

Usage:  docker cp [OPTIONS] CONTAINER:SRC_PATH DEST_PATH|-
        docker cp [OPTIONS] SRC_PATH|- CONTAINER:DEST_PATH

Copy files/folders between a container and the local filesystem

Use '-' as the source to read a tar archive from stdin
and extract it to a directory destination in a container.
Use '-' as the destination to stream a tar archive of a
container source to stdout.

Options:
  -a, --archive       Archive mode (copy all uid/gid information)
  -L, --follow-link   Always follow symbol link in SRC_PATH

二、镜像命令

1.docker images

列出本地主机上的镜像docker images

OPTIONS:

  • -a :列出本地所有的镜像(含历史映像层)
  • -q :只显示镜像ID
[root@localhost ~]# docker images
REPOSITORY                                      TAG                     IMAGE ID       CREATED         SIZE
minio/minio                                     latest                  1370947c8f2f   2 weeks ago     363MB
lhstack/small-dns                               latest                  96cd34ad57b3   10 months ago   12.4MB
agent                                           1.0.0                   918981bf63a2   11 months ago   574MB
openjdk                                         8                       5bf086edab5e   12 months ago   526MB
mongo                                           latest                  96c85f49715a   12 months ago   690MB
redis                                           latest                  1ca2c2a1b554   12 months ago   117MB
mariadb                                         latest                  6e0162b44a5f   14 months ago   414MB
openresty/openresty                             1.19.9.1-4-alpine-fat   b9ec71f7ed51   18 months ago   340MB
docker.elastic.co/elasticsearch/elasticsearch   7.2.0                   0efa6a3de177   3 years ago     861MB
mobz/elasticsearch-head                         5-alpine                e2a76963bc18   6 years ago     78.9MB

[root@localhost ~]# docker images -a
REPOSITORY                                      TAG                     IMAGE ID       CREATED         SIZE
minio/minio                                     latest                  1370947c8f2f   2 weeks ago     363MB
lhstack/small-dns                               latest                  96cd34ad57b3   10 months ago   12.4MB
agent                                           1.0.0                   918981bf63a2   11 months ago   574MB
                                                            3827347179f4   11 months ago   574MB
                                                            f3502886a517   11 months ago   574MB
                                                            94d28d355ddc   11 months ago   574MB
                                                            3d9be7f6987b   11 months ago   526MB
                                                            141fbfacaf04   11 months ago   526MB
                                                            d98daa7f14e1   11 months ago   526MB
openjdk                                         8                       5bf086edab5e   12 months ago   526MB
mongo                                           latest                  96c85f49715a   12 months ago   690MB
redis                                           latest                  1ca2c2a1b554   12 months ago   117MB
mariadb                                         latest                  6e0162b44a5f   14 months ago   414MB
openresty/openresty                             1.19.9.1-4-alpine-fat   b9ec71f7ed51   18 months ago   340MB
docker.elastic.co/elasticsearch/elasticsearch   7.2.0                   0efa6a3de177   3 years ago     861MB
mobz/elasticsearch-head                         5-alpine                e2a76963bc18   6 years ago     78.9MB

[root@localhost ~]# docker images -q
1370947c8f2f
96cd34ad57b3
918981bf63a2
5bf086edab5e
96c85f49715a
1ca2c2a1b554
6e0162b44a5f
b9ec71f7ed51
0efa6a3de177
e2a76963bc18
  • REPOSITORY:镜像的仓库源
  • TAG:镜像的标签版本号
  • IMAGE ID:镜像ID
  • CREATED:镜像创建时间
  • SIZE:镜像大小

同一仓库源可以有多个TAG版本,代表这个仓库源的不同版本,使用REPOSITORY:TAG 来定义不同的镜像。如果不指定一个镜像的版本标签,例如只使用 ubuntu,docker 将默认使用ubuntu:latest 镜像

虚悬镜像dangling image:上图中仓库名、标签都是的镜像

2.docker search

docker search 镜像名字:从docker hub(https://hub.docker.com)上查找对应的镜像。

[root@localhost ~]# docker search hello-world
NAME                                       DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
hello-world                                Hello World! (an example of minimal Dockeriz…   2039      [OK]       
kitematic/hello-world-nginx                A light-weight nginx container that demonstr…   152                  
tutum/hello-world                          Image to test docker deployments. Has Apache…   90                   [OK]
dockercloud/hello-world                    Hello World!                                    20                   [OK]
crccheck/hello-world                       Hello World web server in under 2.5 MB          17                   [OK]
vad1mo/hello-world-rest                    A simple REST Service that echoes back all t…   7                    [OK]
rancher/hello-world                                                                        4                    
ansibleplaybookbundle/hello-world-db-apb   An APB which deploys a sample Hello World! a…   2                    [OK]
ppc64le/hello-world                        Hello World! (an example of minimal Dockeriz…   2                    
thomaspoignant/hello-world-rest-json       This project is a REST hello-world API to bu…   2                    
ansibleplaybookbundle/hello-world-apb      An APB which deploys a sample Hello World! a…   1                    [OK]
businessgeeks00/hello-world-nodejs                                                         0                    
okteto/hello-world                                                                         0                    
strimzi/hello-world-consumer                                                               0                    
strimzi/hello-world-producer                                                               0                    
golift/hello-world                         Hello World Go-App built by Go Lift Applicat…   0                    
koudaiii/hello-world                                                                       0                    
freddiedevops/hello-world-spring-boot                                                      0                    
strimzi/hello-world-streams                                                                0                    
garystafford/hello-world                   Simple hello-world Spring Boot service for t…   0                    [OK]
tacc/hello-world                                                                           0                    
tsepotesting123/hello-world                                                                0                    
kevindockercompany/hello-world                                                             0                    
dandando/hello-world-dotnet                                                                0                    
armswdev/c-hello-world                     Simple hello-world C program on Alpine Linux…   0                    
  • NAME:镜像名字
  • DESCRIPTION:镜像说明
  • STARS:点赞数
  • OFFICIAL:是否是官方
  • AUTOMATED:是否是自动构建

docker search [OPTIONS] 镜像名字:条件查找镜像

  • –limit:只列出n个镜像,默认25个
[root@localhost ~]# docker search --limit 3 hello-world
NAME                  DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
hello-world           Hello World! (an example of minimal Dockeriz…   2039      [OK]       
rancher/hello-world                                                   4                    
okteto/hello-world                                                    0                    
3.docker pull
  • docker pull 镜像名字:拉取最新版本(相当于docker pull 镜像名字 latest
  • docker pull 镜像名字 [:tag]:拉取指定版本
[root@localhost ~]# docker pull hello-world
Using default tag: latest
latest: Pulling from library/hello-world
719385e32844: Pull complete 
Digest: sha256:fc6cf906cbfa013e80938cdf0bb199fbdbb86d6e3e013783e5a766f50f5dbce0
Status: Downloaded newer image for hello-world:latest
docker.io/library/hello-world:latest

[root@localhost ~]# docker pull redis:6.2.4
6.2.4: Pulling from library/redis
33847f680f63: Pull complete 
26a746039521: Pull complete 
18d87da94363: Pull complete 
78e9d65cb9ae: Pull complete 
985fcd1202ac: Pull complete 
ffbec49e5b6a: Pull complete 
Digest: sha256:6bc98f513258e0c17bd150a7a26f38a8ce3e7d584f0c451cf31df70d461a200a
Status: Downloaded newer image for redis:6.2.4
docker.io/library/redis:6.2.4
4.docker system df

查看镜像/容器/数据卷所占空间

[root@localhost ~]# docker system df
TYPE            TOTAL     ACTIVE    SIZE      RECLAIMABLE
Images          12        9         3.556GB   631.5MB (17%)
Containers      10        3         318.4kB   318.4kB (99%)
Local Volumes   19        6         1.205GB   119.9MB (9%)
Build Cache     0         0         0B        0B
5.docker rmi

docker rmi 镜像名字/ID

  • 删除单个:docker rmi -f 镜像ID
  • 删除多个:docker rmi -f 镜像名1:TAG 镜像名2:TAG
  • 删除全部:docker rmi -f $(docker images -qa)
[root@localhost ~]# docker images
REPOSITORY                                      TAG                     IMAGE ID       CREATED         SIZE
minio/minio                                     latest                  1370947c8f2f   2 weeks ago     363MB
hello-world                                     latest                  9c7a54a9a43c   4 weeks ago     13.3kB
lhstack/small-dns                               latest                  96cd34ad57b3   10 months ago   12.4MB
agent                                           1.0.0                   918981bf63a2   11 months ago   574MB
openjdk                                         8                       5bf086edab5e   12 months ago   526MB
mongo                                           latest                  96c85f49715a   12 months ago   690MB
redis                                           latest                  1ca2c2a1b554   12 months ago   117MB
mariadb                                         latest                  6e0162b44a5f   14 months ago   414MB
openresty/openresty                             1.19.9.1-4-alpine-fat   b9ec71f7ed51   18 months ago   340MB
redis                                           6.2.4                   9dae5b22eb39   22 months ago   105MB
docker.elastic.co/elasticsearch/elasticsearch   7.2.0                   0efa6a3de177   3 years ago     861MB
mobz/elasticsearch-head                         5-alpine                e2a76963bc18   6 years ago     78.9MB
[root@localhost ~]# docker rmi 9dae5b22eb39
Untagged: redis:6.2.4
Untagged: redis@sha256:6bc98f513258e0c17bd150a7a26f38a8ce3e7d584f0c451cf31df70d461a200a
Deleted: sha256:9dae5b22eb39cce1b58967d6425a2617b51338a2fdab3a05a66d2fa12f11f094
Deleted: sha256:38887028d023f832c8af04be98c6794e22f6ccf77f523d364fe37888970f56db
Deleted: sha256:8d159ba2313ce920e80fbc1cc5f5c3c5e99d36f601531720f2ccbb966b79376c
Deleted: sha256:64cee1d06a6acc67a7125d52976f840b0aa9e52719d1dbf631420f32482d4ccd
Deleted: sha256:188c498579cef37b65a93d6448c6b129fa07d5740fc213a18843ff22d80cd10d
Deleted: sha256:2117165cd53c98f13ec7af36c9d8acd239fc541c847efaccb49885decf615d68
Deleted: sha256:814bff7343242acfd20a2c841e041dd57c50f0cf844d4abd2329f78b992197f4

三、容器命令

1.新建+启动容器

docker run [OPTIONS] IMAGE [COMMAND] [ARG...]

  • –name=“容器新名字”       为容器指定一个名称;
  • -d: 后台运行容器并返回容器ID,也即启动守护式容器(后台运行);
  • -i:以交互模式运行容器,通常与 -t 同时使用;
  • -t:为容器重新分配一个伪输入终端,通常与 -i 同时使用;也即启动交互式容器(前台有伪终端,等待交互);
  • -P: 随机端口映射
  • -p: 指定端口映射
-p hostPort:containerPort:端口映射 -p 8080:80
-p ip:hostPort:containerPort:配置监听地址 -p 10.0.0.0:8080:80
-p ip::containerPort:随机分配端口 -p 10.0.0.0::80
-p hostPort:containerPort:udp:指定协议 -p 8080:80:tcp
-p 80:80 -p 443:443:指定多个
hongcaixia@hongcaixiadeMacBook-Pro ~ % docker run -it centos bash
[root@83c857543d80 /]# ps -ef
UID        PID  PPID  C STIME TTY          TIME CMD
root         1     0  0 13:19 pts/0    00:00:00 bash
root        16     1  0 13:20 pts/0    00:00:00 ps -ef
[root@83c857543d80 /]# ls -lf
home  media  etc  sbin	mnt  .	bin  tmp  lib  dev  opt  root  run  proc  sys  usr  srv  ..  lib64  var  lost+found  .dockerenv
[root@83c857543d80 /]# exit
exit
hongcaixia@hongcaixiadeMacBook-Pro ~ % docker run --name=mycentos centos
hongcaixia@hongcaixiadeMacBook-Pro ~ % docker ps -a
CONTAINER ID   IMAGE                                                 COMMAND                  CREATED          STATUS                      PORTS                                                 NAMES
4e548998672e   centos                                                "/bin/bash"              5 seconds ago    Exited (0) 3 seconds ago                                                          mycentos
83c857543d80   centos                                                "bash"                   10 minutes ago   Exited (0) 7 minutes ago                                                          naughty_lehmann
7355d7e19de5   centos                                                "bash"                   10 minutes ago   Exited (0) 10 minutes ago                                                         heuristic_edison
2.查看当前所有正在运行的容器

docker ps [OPTIONS]

  • -a :列出当前所有正在运行的容器+历史上运行过的
  • -l :显示最近创建的容器。
  • -n:显示最近n个创建的容器。
  • -q :静默模式,只显示容器编号。
hongcaixia@hongcaixiadeMacBook-Pro ~ % docker ps -a
CONTAINER ID   IMAGE                                                 COMMAND                  CREATED          STATUS                      PORTS                                                 NAMES
4e548998672e   centos                                                "/bin/bash"              5 seconds ago    Exited (0) 3 seconds ago                                                          mycentos
83c857543d80   centos                                                "bash"                   10 minutes ago   Exited (0) 7 minutes ago                                                          naughty_lehmann
7355d7e19de5   centos                                                "bash"                   10 minutes ago   Exited (0) 10 minutes ago                                                         heuristic_edison
e7e2b18fadf0   apacherocketmq/rocketmq-dashboard:latest              "sh -c 'java $JAVA_O…"   7 weeks ago      Exited (255) 4 days ago     0.0.0.0:8080->8080/tcp, :::8080->8080/tcp             rocketmq-dashboard
e13b2badcf88   xuxueli/xxl-job-admin:2.3.1                           "sh -c 'java -jar $J…"   2 months ago     Up 13 hours                 0.0.0.0:8787->8081/tcp, :::8787->8081/tcp             xxl-job
da73c24a7fda   nginx                                                 "/docker-entrypoint.…"   2 months ago     Exited (0) 2 months ago                                                           nginx
06d84894f6ca   centos:latest                                         "/bin/bash"              2 months ago     Up 12 hours                                                                       elated_matsumoto
5c2845b99941   docker.elastic.co/elasticsearch/elasticsearch:7.1.0   "/usr/local/bin/dock…"   4 months ago     Exited (255) 2 months ago   0.0.0.0:9200->9200/tcp, :::9200->9200/tcp, 9300/tcp   es7_01
a14873b0bdec   docker.elastic.co/kibana/kibana:7.1.0                 "/usr/local/bin/kiba…"   4 months ago     Exited (255) 2 months ago   0.0.0.0:5601->5601/tcp, :::5601->5601/tcp             kibana7
c089a67feb25   docker.elastic.co/elasticsearch/elasticsearch:7.1.0   "/usr/local/bin/dock…"   4 months ago     Exited (137) 4 months ago                                                         es7_02
5047a993af2a   lmenezes/cerebro:0.8.3                                "/opt/cerebro/bin/ce…"   4 months ago     Exited (255) 2 months ago   0.0.0.0:9000->9000/tcp, :::9000->9000/tcp             cerebro
0f12d73328e5   hello-world                                           "/hello"                 4 months ago     Exited (0) 4 months ago                                                           tender_jackson
4716629faa58   bitnami/kafka                                         "/opt/bitnami/script…"   4 months ago     Exited (255) 4 months ago   9092/tcp                                              sad_booth
21a958642038   zookeeper                                             "/docker-entrypoint.…"   4 months ago     Exited (255) 4 months ago   2181/tcp, 2888/tcp, 3888/tcp, 8080/tcp                elegant_antonelli

hongcaixia@hongcaixiadeMacBook-Pro ~ % docker ps -q
e13b2badcf88
06d84894f6ca

hongcaixia@hongcaixiadeMacBook-Pro ~ % docker ps -n 1
CONTAINER ID   IMAGE     COMMAND       CREATED         STATUS                     PORTS     NAMES
4e548998672e   centos    "/bin/bash"   4 minutes ago   Exited (0) 4 minutes ago             mycentos
hongcaixia@hongcaixiadeMacBook-Pro ~ % 
3.退出容器
  • exit:容器停止
  • ctrl+p+q:容器不停止
4.启动已停止的容器

docker start 容器ID/容器名

hongcaixia@hongcaixiadeMacBook-Pro ~ % docker ps -n2
CONTAINER ID   IMAGE     COMMAND       CREATED          STATUS                      PORTS     NAMES
4e548998672e   centos    "/bin/bash"   11 minutes ago   Exited (0) 11 minutes ago             mycentos
83c857543d80   centos    "bash"        22 minutes ago   Exited (0) 19 minutes ago             naughty_lehmann
hongcaixia@hongcaixiadeMacBook-Pro ~ % docker start 4e548998672e
4e548998672e
hongcaixia@hongcaixiadeMacBook-Pro ~ % 
5.重启容器

docker restart 容器ID/容器名

6.停止容器

docker stop 容器ID/容器名

7.强制停止容器

docker kill 容器id/容器名

8.删除已经停止的容器

docker rm 容器id

一次性删除多个容器实例:

  • docker rm -f $(docker ps -a -q)
  • docker ps -a -q | xargs docker rm
hongcaixia@hongcaixiadeMacBook-Pro ~ % docker ps -n2            
CONTAINER ID   IMAGE     COMMAND       CREATED          STATUS                      PORTS     NAMES
4e548998672e   centos    "/bin/bash"   13 minutes ago   Exited (0) 2 seconds ago              mycentos
83c857543d80   centos    "bash"        23 minutes ago   Exited (0) 20 minutes ago             naughty_lehmann
hongcaixia@hongcaixiadeMacBook-Pro ~ % docker rm 4e548998672e
4e548998672e

hongcaixia@hongcaixiadeMacBook-Pro ~ % docker ps
CONTAINER ID   IMAGE                         COMMAND                  CREATED        STATUS        PORTS                                       NAMES
e13b2badcf88   xuxueli/xxl-job-admin:2.3.1   "sh -c 'java -jar $J…"   2 months ago   Up 13 hours   0.0.0.0:8787->8081/tcp, :::8787->8081/tcp   xxl-job
06d84894f6ca   centos:latest                 "/bin/bash"              2 months ago   Up 13 hours                                               elated_matsumoto
hongcaixia@hongcaixiadeMacBook-Pro ~ % docker rm e13b2badcf88
Error response from daemon: You cannot remove a running container e13b2badcf884fc97d7ccfe85a066c7b61ae8642715ce08a64f9c00f64c7e955. Stop the container before attempting removal or force remove
hongcaixia@hongcaixiadeMacBook-Pro ~ % docker rm -f e13b2badcf88
e13b2badcf88
9.后台运行容器

run命令执行过程:
1.Docker在本地寻找镜像
2.本机存在镜像,以该镜像为模板生产容器实例运行
3.本机不存在镜像,去docker hub上查找镜像
4.docker hub找不到,返回错误,查不到该镜像
5.docker hub找到,下载该镜像到本地,以该镜像为模板生产容器实例运行

Docker容器后台运行,就必须有一个前台进程。容器运行的命令如果不是那些一直挂起的命令(比如运行top,tail),就是会自动退出的。

hongcaixia@hongcaixiadeMacBook-Pro ~ % docker run --name=mycentos centos
hongcaixia@hongcaixiadeMacBook-Pro ~ % docker ps
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES

docker 的服务是在后台运行的, 可以过 -d 指定容器的后台运行模式。
docker run -d 容器名

hongcaixia@hongcaixiadeMacBook-Pro ~ % docker run -d redis
417d29580fad28a8c64b16d52b86c50249f90645a9df68d86db3cee0645b6467
hongcaixia@hongcaixiadeMacBook-Pro ~ % docker ps
CONTAINER ID   IMAGE     COMMAND                  CREATED         STATUS         PORTS      NAMES
417d29580fad   redis     "docker-entrypoint.s…"   3 seconds ago   Up 2 seconds   6379/tcp   hopeful_pascal
hongcaixia@hongcaixiadeMacBook-Pro ~ % 
10.进入正在运行的容器并以命令行交互

重新进入已经退出了交互界面的正在运行的容器的交互界面:

  • docker exec -it 容器ID /bin/bash:在容器中打开新的终端,可以启动新的进程;用exit退出,不会导致容器的停止。(推荐)
  • docker attach 容器ID:直接进入容器启动命令的终端,不会启动新的进程;用exit退出,会导致容器的停止。
11.查看容器日志

docker logs 容器ID

12.查看容器内运行的进程

docker top 容器ID

root@hmd-jenkins:~# docker top a3d02a8c6f13
UID                 PID                 PPID                C                   STIME               TTY                 TIME                CMD
root                751240              751218              0                   13:33               ?                   00:00:01            tini java -javaagent:./jacocoagent.jar=includes=com.*,output=tcpserver,port=17297,address=0.0.0.0,append=true -jar api.jar --spring.profiles.active=dev
root                751269              751240              1                   13:33               ?                   00:06:26            java -javaagent:./jacocoagent.jar=includes=com.*,output=tcpserver,port=17297,address=0.0.0.0,append=true -jar api.jar --spring.profiles.active=dev
root@hmd-jenkins:~#
13.查看容器内部细节

docker inspect 容器ID

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

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

15.导入和导出容器
  • export: 导出容器的内容留作为一个tar归档文件,对应import命令

  • import: 从tar包中的内容创建一个新的文件系统再导入为镜像,对应export

  • 把镜像导出成文件:docker export 容器ID > 文件名.tar

  • 把文件导入到容器中:cat 文件名.tar | docker import - 镜像用户/镜像名:镜像版本号

四、总结

Docker命令详解_第1张图片

你可能感兴趣的:(docker,jenkins)