一、容器生命周期管理

1、run

2、start/stop/restart

docker start:启动一个或则多个已经被停止的容器
docker stop:停止一个运行的容器
docker restart:重启容器

3、kill

概述

杀掉一个运行中的容器

语法

docker kill [OPTIONS] CONTAINER [CONTAINER...]

[OPTIONS]

  • -s :向容器发送一个信号

examples

[root@localhost ~]# docker run --rm -it centos bash &
[1] 78366
[root@localhost ~]# docker ps -a 
CONTAINER ID   IMAGE      COMMAND                  CREATED         STATUS             PORTS                                                                                              NAMES
cad768f8f146   centos     "bash"                   9 seconds ago   Up 8 seconds                                                                                                          keen_khorana
fb11d2c47c17   rabbitmq   "docker-entrypoint.s…"   2 hours ago     Up About an hour   4369/tcp, 5671/tcp, 15691-15692/tcp, 25672/tcp, 0.0.0.0:5674->5672/tcp, 0.0.0.0:15674->15672/tcp   rabbit-node03
674dfe468431   rabbitmq   "docker-entrypoint.s…"   2 hours ago     Up About an hour   4369/tcp, 5671/tcp, 15691-15692/tcp, 25672/tcp, 0.0.0.0:5673->5672/tcp, 0.0.0.0:15673->15672/tcp   rabbit-node02
84a975d21f70   rabbitmq   "docker-entrypoint.s…"   2 hours ago     Up About an hour   4369/tcp, 0.0.0.0:5672->5672/tcp, 5671/tcp, 15691-15692/tcp, 25672/tcp, 0.0.0.0:15672->15672/tcp   rabbit-node01
[root@localhost ~]# docker kill -s KILL keen_khorana
keen_khorana
[root@localhost ~]# docker ps -a 
CONTAINER ID   IMAGE      COMMAND                  CREATED       STATUS             PORTS                                                                                              NAMES
fb11d2c47c17   rabbitmq   "docker-entrypoint.s…"   2 hours ago   Up About an hour   4369/tcp, 5671/tcp, 15691-15692/tcp, 25672/tcp, 0.0.0.0:5674->5672/tcp, 0.0.0.0:15674->15672/tcp   rabbit-node03
674dfe468431   rabbitmq   "docker-entrypoint.s…"   2 hours ago   Up About an hour   4369/tcp, 5671/tcp, 15691-15692/tcp, 25672/tcp, 0.0.0.0:5673->5672/tcp, 0.0.0.0:15673->15672/tcp   rabbit-node02
84a975d21f70   rabbitmq   "docker-entrypoint.s…"   2 hours ago   Up About an hour   4369/tcp, 0.0.0.0:5672->5672/tcp, 5671/tcp, 15691-15692/tcp, 25672/tcp, 0.0.0.0:15672->15672/tcp   rabbit-node01
[root@localhost ~]# 

4、rm

概述

删除一个或多个容器。

语法

docker rm [OPTIONS] CONTAINER [CONTAINER...]

[OPTIONS]

  • -f :通过 SIGKILL 信号强制删除一个运行中的容器。
  • -l :移除容器间的网络连接,而非容器本身。
  • -v :删除与容器关联的卷。

examples

[root@localhost ~]# docker run --name some-nginx -p 80:80 -v /some/content:/usr/share/nginx/html:ro -d nginx
83372e4a75d80ca7f68854fbd93792abdce2fa53cf838622ecafb9b48025f576
[root@localhost ~]# docker rm -f some-nginx
some-nginx
[root@localhost ~]# 
docker rm -l db 
[root@localhost ~]# docker rm -v some-nginx
some-nginx

删除所有已经停止的容器(正在运行的容器时无法删除掉的)

[root@localhost ~]# docker rm $(docker ps -a -q)
ec7bf5e2ffd3
19fd9adb0fe3
Error response from daemon: You cannot remove a running container fb11d2c47c17f20f7527370ba57b250c0d4452ca3aeffd418dc9f4d3951e38fe. Stop the container before attempting removal or force remove
Error response from daemon: You cannot remove a running container 674dfe46843113b660be824157193077e4cf8288069537fe2aeeaf6611e151de. Stop the container before attempting removal or force remove
Error response from daemon: You cannot remove a running container 84a975d21f70b5cd2460ff2582c4e55c1d3fd4a04b3dddc67fbecdbc85761967. Stop the container before attempting removal or force remove
[root@localhost ~]# docker ps -a 
CONTAINER ID   IMAGE      COMMAND                  CREATED       STATUS       PORTS                                                                                              NAMES
fb11d2c47c17   rabbitmq   "docker-entrypoint.s…"   2 hours ago   Up 2 hours   4369/tcp, 5671/tcp, 15691-15692/tcp, 25672/tcp, 0.0.0.0:5674->5672/tcp, 0.0.0.0:15674->15672/tcp   rabbit-node03
674dfe468431   rabbitmq   "docker-entrypoint.s…"   2 hours ago   Up 2 hours   4369/tcp, 5671/tcp, 15691-15692/tcp, 25672/tcp, 0.0.0.0:5673->5672/tcp, 0.0.0.0:15673->15672/tcp   rabbit-node02
84a975d21f70   rabbitmq   "docker-entrypoint.s…"   2 hours ago   Up 2 hours   4369/tcp, 0.0.0.0:5672->5672/tcp, 5671/tcp, 15691-15692/tcp, 25672/tcp, 0.0.0.0:15672->15672/tcp   rabbit-node01
[root@localhost ~]# 

5、pause/unpause

docker pause :暂停容器中所有的进程。
docker unpause :恢复容器中所有的进程。

docker pause CONTAINER [CONTAINER...]
docker unpause CONTAINER [CONTAINER...]
[root@localhost ~]# docker pause some-nginx
some-nginx
[root@localhost ~]# docker unpause some-nginx 
some-nginx
[root@localhost ~]#

6、create

概述

创建一个新的容器但不启动它

语法

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

[OPTIONS]

null

examples

[root@localhost ~]# docker create --name nginx2 nginx
ffe093a05cba28714eed412e51994676388694f2e2cd9cac86770c118d06a4a1
[root@localhost ~]# 

7、exec

概述

在运行的容器中执行命令

语法

docker exec [OPTIONS] CONTAINER COMMAND [ARG...]

[OPTIONS]

  • -d :分离模式: 在后台运行
  • -i :即使没有附加也保持STDIN 打开
  • -t :分配一个伪终端

examples

在容器 nginx2中以交互模式执行容器内pwd命令:

[root@localhost ~]# docker exec -it nginx2 pwd
/
[root@localhost ~]# 

在容器 mynginx 中开启一个交互模式的终端

[root@localhost ~]# docker exec -it nginx2 /bin/bash
root@ffe093a05cba:/# 

二、容器操作

1、ps

2、inspect

概述

获取容器/镜像的元数据。

语法

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

[OPTIONS]

  • -f :指定返回值的模板文件。
  • -s :显示总的文件大小。
  • –type :为指定类型返回JSON。

examples

获取镜像nginx2的元信息。

[root@localhost ~]# docker inspect nginx2
[
    {
        "Id": "ffe093a05cba28714eed412e51994676388694f2e2cd9cac86770c118d06a4a1",
        "Created": "2021-04-07T20:01:44.838946485Z",
        "Path": "/docker-entrypoint.sh",
        "Args": [
            "nginx",
            "-g",
            "daemon off;"
        ],
        "State": {
            "Status": "running",
            "Running": true,
            "Paused": false,
            "Restarting": false,
            "OOMKilled": false,
            "Dead": false,
            "Pid": 93887,
            "ExitCode": 0,
            "Error": "",
            "StartedAt": "2021-04-07T20:04:44.212088395Z",
            "FinishedAt": "0001-01-01T00:00:00Z"
        },
        "Image": "sha256:519e12e2a84a9eb18094635ae1edfd97b26f95dbc66e317eefb657a1cb08c8dc",
        "ResolvConfPath": "/var/lib/docker/containers/ffe093a05cba28714eed412e51994676388694f2e2cd9cac86770c118d06a4a1/resolv.conf",
        "HostnamePath": "/var/lib/docker/containers/ffe093a05cba28714eed412e51994676388694f2e2cd9cac86770c118d06a4a1/hostname",
        "HostsPath": "/var/lib/docker/containers/ffe093a05cba28714eed412e51994676388694f2e2cd9cac86770c118d06a4a1/hosts",
        "LogPath": "/var/lib/docker/containers/ffe093a05cba28714eed412e51994676388694f2e2cd9cac86770c118d06a4a1/ffe093a05cba28714eed412e51994676388694f2e2cd9cac86770c118d06a4a1-json.log",
        "Name": "/nginx2",
        "RestartCount": 0,
        "Driver": "overlay2",
        .........略

3、top

概述

查看容器中运行的进程信息,支持 ps 命令参数。

语法

docker top [OPTIONS] CONTAINER [ps OPTIONS]

[OPTIONS]

null

examples

查看容器rabbit-node01的进程信息。

[root@localhost ~]# docker top rabbit-node01
UID                 PID                 PPID                C                   STIME               TTY                 TIME                CMD
polkitd             66934               66912               0                   13:59               ?                   00:00:00            /bin/sh /opt/rabbitmq/sbin/rabbitmq-server
polkitd             66983               66934               0                   13:59               ?                   00:01:20            /usr/local/lib/erlang/erts-11.2/bin/beam.smp -W w -MBas ageffcbf -MHas ageffcbf -MBlmbcs 512 -MHlmbcs 512 -MMmcs 30 -P 1048576 -t 5000000 -stbt db -zdbbl 128000 -sbwt none -sbwtdcpu none -sbwtdio none -B i -- -root /usr/local/lib/erlang -progname erl -- -home /var/lib/rabbitmq -- -pa -noshell -noinput -s rabbit boot -boot start_sasl -lager crash_log false -lager handlers []
polkitd             66990               66983               0                   13:59               ?                   00:00:00            erl_child_setup 1048576
polkitd             67030               66934               0                   13:59               ?                   00:00:00            /usr/local/lib/erlang/erts-11.2/bin/epmd -daemon
polkitd             67053               66990               0                   13:59               ?                   00:00:00            inet_gethost 4
polkitd             67054               67053               0                   13:59               ?                   00:00:00            inet_gethost 4
[root@localhost ~]# 

4、attach

概述

连接到正在运行中的容器
官方文档释义

语法

docker attach [OPTIONS] CONTAINER

[OPTIONS]

null

examples

[root@localhost ~]# docker pull ubuntu
Using default tag: latest
latest: Pulling from library/ubuntu
a70d879fa598: Pull complete 
c4394a92d1f8: Pull complete 
10e6159c56c0: Pull complete 
Digest: sha256:3c9c713e0979e9bd6061ed52ac1e9e1f246c9495aa063619d9d695fb8039aa1f
Status: Downloaded newer image for ubuntu:latest
docker.io/library/ubuntu:latest
[root@localhost ~]# docker run -d --name topdemo ubuntu /usr/bin/top -b
81a6b69b63291809e8dcb69d2e290dc4b5aea36ce21cacc60bad26fb48be8b33
[root@localhost ~]# docker attach topdemo


top - 20:28:50 up 14:57,  0 users,  load average: 6.51, 6.21, 5.89
Tasks:   1 total,   1 running,   0 sleeping,   0 stopped,   0 zombie
%Cpu(s): 36.9 us, 59.4 sy,  0.0 ni,  3.8 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
MiB Mem :   2124.3 total,    169.2 free,    806.9 used,   1148.2 buff/cache
MiB Swap:   2176.0 total,   2174.2 free,      1.8 used.   1124.2 avail Mem 

   PID USER      PR  NI    VIRT    RES    SHR S  %CPU  %MEM     TIME+ COMMAND
     1 root      20   0    5960   1700   1280 R   0.0   0.1   0:00.04 top

top - 20:28:53 up 14:58,  0 users,  load average: 6.47, 6.21, 5.89
Tasks:   1 total,   1 running,   0 sleeping,   0 stopped,   0 zombie
%Cpu(s): 33.5 us, 61.7 sy,  0.0 ni,  4.6 id,  0.0 wa,  0.0 hi,  0.2 si,  0.0 st
MiB Mem :   2124.3 total,    169.1 free,    806.9 used,   1148.2 buff/cache
MiB Swap:   2176.0 total,   2174.2 free,      1.8 used.   1124.2 avail Mem 

   PID USER      PR  NI    VIRT    RES    SHR S  %CPU  %MEM     TIME+ COMMAND
     1 root      20   0    5960   1700   1280 R   0.0   0.1   0:00.04 top

top - 20:28:56 up 14:58,  0 users,  load average: 6.47, 6.21, 5.89
Tasks:   1 total,   1 running,   0 sleeping,   0 stopped,   0 zombie
%Cpu(s): 35.0 us, 61.9 sy,  0.0 ni,  2.9 id,  0.0 wa,  0.0 hi,  0.2 si,  0.0 st
MiB Mem :   2124.3 total,    169.2 free,    806.9 used,   1148.2 buff/cache
MiB Swap:   2176.0 total,   2174.2 free,      1.8 used.   1124.2 avail Mem 

   PID USER      PR  NI    VIRT    RES    SHR S  %CPU  %MEM     TIME+ COMMAND
     1 root      20   0    5960   1700   1280 R   0.0   0.1   0:00.04 top^C
[root@localhost ~]# 

5、events

概述

从服务器获取实时事件
官方文档释义

语法

docker events [OPTIONS]

[OPTIONS]

  • -f :根据条件过滤事件;
  • --since :从指定的时间戳后显示所有事件;
  • --until :流水时间显示到指定的时间为止;

examples

shell 1:

[root@localhost ~]# docker events 

shell 2:

[root@localhost ~]# docker pull alpine
Using default tag: latest
latest: Pulling from library/alpine
ca3cd42a7c95: Pull complete 
Digest: sha256:ec14c7992a97fc11425907e908340c6c3d6ff602f5f13d899e6b7027c9b4133a
Status: Downloaded newer image for alpine:latest
docker.io/library/alpine:latest
[root@localhost ~]# docker create --name test alpine:latest top
847fff8008327cb8f9d83667e028f253929476faa57ef68727d608df516b8f6e
[root@localhost ~]# docker start test
test
[root@localhost ~]# docker stop test
test
[root@localhost ~]# 

shell 1:

[root@localhost ~]# docker events 


2021-04-07T16:42:27.074522525-04:00 image pull alpine:latest (name=alpine)
2021-04-07T16:42:43.448418877-04:00 container create 847fff8008327cb8f9d83667e028f253929476faa57ef68727d608df516b8f6e (image=alpine:latest, name=test)
2021-04-07T16:42:55.540600638-04:00 network connect c45896dfb787b26d62a5a2d7a87040196a0c11e00c19ae02f95f96a3408d1c0f (container=847fff8008327cb8f9d83667e028f253929476faa57ef68727d608df516b8f6e, name=bridge, type=bridge)
2021-04-07T16:42:56.464944078-04:00 container start 847fff8008327cb8f9d83667e028f253929476faa57ef68727d608df516b8f6e (image=alpine:latest, name=test)
2021-04-07T16:43:01.627079846-04:00 container kill 847fff8008327cb8f9d83667e028f253929476faa57ef68727d608df516b8f6e (image=alpine:latest, name=test, signal=15)
2021-04-07T16:43:01.685146953-04:00 container die 847fff8008327cb8f9d83667e028f253929476faa57ef68727d608df516b8f6e (exitCode=143, image=alpine:latest, name=test)
2021-04-07T16:43:01.765151589-04:00 network disconnect c45896dfb787b26d62a5a2d7a87040196a0c11e00c19ae02f95f96a3408d1c0f (container=847fff8008327cb8f9d83667e028f253929476faa57ef68727d608df516b8f6e, name=bridge, type=bridge)
2021-04-07T16:43:01.782004465-04:00 container stop 847fff8008327cb8f9d83667e028f253929476faa57ef68727d608df516b8f6e (image=alpine:latest, name=test)
^C
[root@localhost ~]# 

6、logs

概述

获取容器的日志

语法

docker logs [OPTIONS] CONTAINER

[OPTIONS]

  • -f : 跟踪日志输出
  • --since :显示某个开始时间的所有日志
  • -t : 显示时间戳
  • –tail :仅列出最新N条容器日志

examples

跟踪rabbit-node01的最新5条日志

[root@localhost ~]# docker logs -f --tail 5 rabbit-node01
2021-04-07 17:59:28.257 [info] <0.590.0> Resetting node maintenance status
2021-04-07 17:59:38.519 [info] <0.520.0> node 'rabbit@rabbit-node02' up
2021-04-07 17:59:40.398 [info] <0.520.0> rabbit on node 'rabbit@rabbit-node02' up
2021-04-07 17:59:52.382 [info] <0.520.0> node 'rabbit@rabbit-node03' up
2021-04-07 17:59:54.873 [info] <0.520.0> rabbit on node 'rabbit@rabbit-node03' up

7、wait

概述

阻塞运行直到容器停止,然后打印出它的退出代码。

语法

docker wait [OPTIONS] CONTAINER [CONTAINER...]

[OPTIONS]

null

examples

[root@localhost ~]# docker wait nginx2
0
[root@localhost ~]# 

8、export

概述

将文件系统作为一个tar归档文件导出到STDOUT。

语法

docker export [OPTIONS] CONTAINER

[OPTIONS]

  • -o :将输入内容写到文件。

example

将容器nginx2的容器按日期保存为tar文件。

[root@localhost ~]# docker export -o nginx2-`date +%Y%m%d`.tar nginx2
[root@localhost ~]# ls
anaconda-ks.cfg  nginx2-20210407.tar
[root@localhost ~]# 

9、port

概述

列出指定的容器的端口映射,或者查找将PRIVATE_PORT NAT到面向公众的端口。

语法

docker port [OPTIONS] CONTAINER [PRIVATE_PORT[/PROTO]]

[OPTIONS]

null

examples

[root@localhost ~]# docker port rabbit-node01
5672/tcp -> 0.0.0.0:5672
15672/tcp -> 0.0.0.0:15672
[root@localhost ~]# 

三、容器rootfs

1、commit

概述

从容器创建一个新的镜像。

语法

docker commit [OPTIONS] CONTAINER [REPOSITORY[:TAG]]

[OPTIONS]

  • -a :提交的镜像作者;
  • -c :使用Dockerfile指令来创建镜像;
  • -m :提交时的说明文字;
  • -p :在commit时,将容器暂停。

examples

将容器fb11d2c47c17 保存为新的镜像,并添加提交人信息和说明信息。

[root@localhost ~]# docker commit -a "energetic" -m "rabbit-commit" fb11d2c47c17 rabbit-node01:v1 
sha256:66676217280e35bf520d745875fa209c0bf8c4568886ff7cd0c6db852646729a
[root@localhost ~]# docker images rabbit-node01:v1
REPOSITORY      TAG       IMAGE ID       CREATED          SIZE
rabbit-node01   v1        66676217280e   21 seconds ago   156MB
[root@localhost ~]# 

2、cp

概述

用于容器与主机之间的数据拷贝

语法

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

[OPTIONS]

  • -L :保持源目标中的链接

examples

将主机家目录下的test文件拷贝到容器fb11d2c47c17的根目录下

[root@localhost ~]# docker cp test fb11d2c47c17:/
[root@localhost ~]# docker exec -it fb11d2c47c17 bash 
root@rabbit-node03:/# ls
bin  boot  dev  etc  home  lib  lib64  media  mnt  opt  plugins  proc  root  run  sbin  srv  sys  test  tmp  usr  var
root@rabbit-node03:/# 

将主机/www/directory目录拷贝到容器fb11d2c47c17中,目录重命名为www。

[root@localhost www]# docker cp /www/directory/ fb11d2c47c17:/www
[root@localhost www]# docker exec -it fb11d2c47c17 bash
root@rabbit-node03:/# ls
bin  boot  dev  etc  home  lib  lib64  media  mnt  opt  plugins  proc  root  run  sbin  srv  sys  test  tmp  usr  var  www
root@rabbit-node03:/# cd www/
root@rabbit-node03:/www# ls
directory  index.html
root@rabbit-node03:/www# 

将容器fb11d2c47c17的/www/index.html目录拷贝到主机的/opt目录中。

[root@localhost www]# docker cp fb11d2c47c17:/www/index.html /opt/
[root@localhost www]# ls /opt/
containerd  index.html
[root@localhost www]# 

3、diff

概述

检查容器里文件结构的更改。

语法

docker diff [OPTIONS] CONTAINER

[OPTIONS]

null

examples

查看容器rabbit-node01的文件结构更改

[root@localhost www]# docker diff rabbit-node01
C /etc
C /etc/rabbitmq
A /etc/rabbitmq/rabbitmq.conf
C /etc/rabbitmq/enabled_plugins
C /var
C /var/log
C /var/log/rabbitmq
A /var/log/rabbitmq/log
A /var/log/rabbitmq/log/crash.log
[root@localhost www]# 

四、镜像仓库

1、login

概述

docker login : 登陆到一个Docker镜像仓库,如果未指定镜像仓库地址,默认为官方仓库 Docker Hub

docker logout : 登出一个Docker镜像仓库,如果未指定镜像仓库地址,默认为官方仓库 Docker Hub

语法

[OPTIONS]

docker login [OPTIONS] [SERVER]
docker logout [OPTIONS] [SERVER]

examples

登陆到Docker Hub

docker login -u 用户名 -p 密码

登出Docker Hub

docker logout

2、pull

概述

从镜像仓库中拉取或者更新指定镜像

语法

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

[OPTIONS]

  • -a :拉取所有 tagged 镜像
  • --disable-content-trust :忽略镜像的校验,默认开启

examples

docker pull java

3、push

概述

将本地的镜像上传到镜像仓库,要先登陆到镜像仓库

语法

docker push [OPTIONS] NAME[:TAG]

[OPTIONS]

  • disable-content-trust :忽略镜像的校验,默认开启

examples

上传本地镜像myapache:v1到镜像仓库中。

docker push myapache:v1

4、search

概述

从Docker Hub查找镜像

语法

docker search [OPTIONS] TERM

[OPTIONS]

  • --automated :只列出 automated build类型的镜像;
  • --no-trunc :显示完整的镜像描述;
  • -f <过滤条件>:列出收藏数不小于指定值的镜像。

examples

从 Docker Hub 查找所有镜像名包含 java,并且收藏数大于 20的镜像

[root@localhost ~]# docker search -f stars=20 java
NAME                              DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
node                              Node.js is a JavaScript-based platform for s…   9917      [OK]       
tomcat                            Apache Tomcat is an open source implementati…   2991      [OK]       
openjdk                           OpenJDK is an open-source implementation of …   2685      [OK]       
java                              Java is a concurrent, class-based, and objec…   1976      [OK]       
ghost                             Ghost is a free and open source blogging pla…   1345      [OK]       
couchdb                           CouchDB is a database that uses JSON for doc…   398       [OK]       
jetty                             Jetty provides a Web server and javax.servle…   357       [OK]       
groovy                            Apache Groovy is a multi-faceted language fo…   105       [OK]       
lwieske/java-8                    Oracle Java 8 Container - Full + Slim - Base…   49                   [OK]
fabric8/java-jboss-openjdk8-jdk   Fabric8 Java Base Image (JBoss, OpenJDK 8)      29                   [OK]
[root@localhost ~]# 

参数说明:

  • NAME: 镜像仓库源的名称
  • DESCRIPTION: 镜像的描述
  • OFFICIAL: 是否 docker 官方发布
  • stars: 类似 Github 里面的 star,表示点赞、喜欢的意思
  • AUTOMATED: 自动构建。

五、本地镜像管理

1、images

概述

列出本地镜像

语法

docker images [OPTIONS] [REPOSITORY[:TAG]]

[OPTIONS]

  • -a :列出本地所有的镜像(含中间映像层,默认情况下,过滤掉中间映像层);
  • --digests :显示镜像的摘要信息;
  • -f :显示满足条件的镜像;
  • --format :指定返回值的模板文件;
  • --no-trunc :显示完整的镜像信息;
  • -q :只显示镜像ID。

examples

查看本地镜像列表

[root@localhost ~]# docker images 
REPOSITORY      TAG       IMAGE ID       CREATED                  SIZE
nginx           latest    519e12e2a84a   Less than a second ago   133MB
rabbit-node01   v1        66676217280e   About an hour ago        156MB
ubuntu          latest    26b77e58432b   4 days ago               72.9MB
alpine          latest    49f356fa4513   7 days ago               5.61MB
rabbitmq        latest    f83a0c22b15a   8 days ago               156MB
centos          latest    300e315adb2f   4 months ago             209MB
[root@localhost ~]# 

列出本地镜像中REPOSITORY为ubuntu的镜像列表

[root@localhost ~]# docker images  ubuntu
REPOSITORY   TAG       IMAGE ID       CREATED      SIZE
ubuntu       latest    26b77e58432b   4 days ago   72.9MB
[root@localhost ~]# 

2、rmi

概述

删除本地一个或多少镜像。

语法

docker rmi [OPTIONS] IMAGE [IMAGE...]

[OPTIONS]

  • -f :强制删除;

  • --no-prune :不移除该镜像的过程镜像,默认移除;

examples

删除本地镜像

[root@localhost ~]# docker rmi ubuntu
Untagged: ubuntu:latest
Untagged: ubuntu@sha256:3c9c713e0979e9bd6061ed52ac1e9e1f246c9495aa063619d9d695fb8039aa1f
Deleted: sha256:26b77e58432b01665d7e876248c9056fa58bf4a7ab82576a024f5cf3dac146d6
Deleted: sha256:9de65d1e8b2782409b2420bf9347003a43e91bb65c1e4c8fbd7d098d6234f359
Deleted: sha256:e0f8e3acb2bf7fe9384463ae7009179d299b211e7cf17c2bf9d8e5e248cfe5b0
Deleted: sha256:0e64bafdc7ee828d0f3995bebfa388ced52a625ad2969eeb569f4a83db56d505
[root@localhost ~]# 

3、tag

概述

标记本地镜像,将其归入某一仓库

语法

docker tag [OPTIONS] IMAGE[:TAG] [REGISTRYHOST/][USERNAME/]NAME[:TAG]

[OPTIONS]

null

examples

将镜像ubuntu标记为ubuntu:v1镜像

[root@localhost ~]# docker tag ubuntu ubuntu:v1
[root@localhost ~]# docker images 
REPOSITORY      TAG       IMAGE ID       CREATED                  SIZE
nginx           latest    519e12e2a84a   Less than a second ago   133MB
rabbit-node01   v1        66676217280e   About an hour ago        156MB
ubuntu          latest    26b77e58432b   4 days ago               72.9MB
ubuntu          v1        26b77e58432b   4 days ago               72.9MB
alpine          latest    49f356fa4513   7 days ago               5.61MB
rabbitmq        latest    f83a0c22b15a   8 days ago               156MB
centos          latest    300e315adb2f   4 months ago             209MB
[root@localhost ~]# 

4、build

概述

命令用于使用 Dockerfile 创建镜像

语法

docker build [OPTIONS] PATH | URL | -

[OPTIONS]

  • --build-arg=[] :设置镜像创建时的变量;
  • --cpu-shares :设置 cpu 使用权重;
  • --cpu-period :限制 CPU CFS周期;
  • --cpu-quota :限制 CPU CFS配额;
  • --cpuset-cpus :指定使用的CPU id;
  • --cpuset-mems :指定使用的内存 id;
  • --disable-content-trust :忽略校验,默认开启;
  • -f :指定要使用的Dockerfile路径;
  • --force-rm :设置镜像过程中删除中间容器;
  • --isolation :使用容器隔离技术;
  • --label=[] :设置镜像使用的元数据;
  • -m :设置内存最大值;
  • --memory-swap :设置Swap的最大值为内存+swap,"-1"表示不限swap;
  • --no-cache :创建镜像的过程不使用缓存;
  • --pull :尝试去更新镜像的新版本;
  • --quiet, -q :安静模式,成功后只输出镜像 ID;
  • --rm :设置镜像成功后删除中间容器;
  • --shm-size :设置/dev/shm的大小,默认值是64M;
  • --ulimit :Ulimit配置。
  • --squash :将 Dockerfile 中所有的操作压缩为一层。
  • --tag, -t: 镜像的名字及标签,通常 name:tag 或者 name 格式;可以在一次构建中为一个镜像设置多个标签。
  • --network: 默认 default。在构建期间设置RUN指令的网络模式

examples

使用当前目录的 Dockerfile 创建镜像,标签为nginx:1

[root@localhost ~]# mkdir Dockefile
[root@localhost ~]# 
[root@localhost ~]# cd Dockefile/
[root@localhost Dockefile]# 
[root@localhost Dockefile]# vi dockerfile 
FROM nginx
RUN echo 'this is a local image'  > /usr/share/nginx/html/index.html
[root@localhost Dockefile]# docker build -t nginx:1 .
Sending build context to Docker daemon  2.048kB
Step 1/2 : FROM nginx
 ---> 519e12e2a84a
Step 2/2 : RUN echo 'this is a local image'  > /usr/share/nginx/html/index.html
 ---> Running in eacb4006658d
Removing intermediate container eacb4006658d
 ---> e4a4a18962c3
Successfully built e4a4a18962c3
Successfully tagged nginx:1
[root@localhost Dockefile]# 

也可以通过 -f Dockerfile 文件的位置:

[root@localhost ~]# docker build  -f /opt/dockerfile  .
Sending build context to Docker daemon  135.6MB
Step 1/2 : FROM nginx
 ---> 519e12e2a84a
Step 2/2 : RUN echo 'this is a local image'  > /usr/share/nginx/html/index.html
 ---> Using cache
 ---> e4a4a18962c3
Successfully built e4a4a18962c3

5、history

概述

查看指定镜像的创建历史

语法

docker history [OPTIONS] IMAGE

[OPTIONS]

  • -H :以可读的格式打印镜像大小和日期,默认为true;
  • --no-trunc :显示完整的提交记录;
  • -q :仅列出提交记录ID。

examples

查看本地镜像nginx:1的创建历史

[root@localhost ~]# docker history nginx:1
IMAGE          CREATED                  CREATED BY                                      SIZE      COMMENT
e4a4a18962c3   25 minutes ago           /bin/sh -c echo 'this is a local image'  > /…   22B       
519e12e2a84a   Less than a second ago   /bin/sh -c #(nop)  CMD ["nginx" "-g" "daemon…   0B        
<missing>      Less than a second ago   /bin/sh -c #(nop)  STOPSIGNAL SIGQUIT           0B        
<missing>      Less than a second ago   /bin/sh -c #(nop)  EXPOSE 80                    0B        
<missing>      Less than a second ago   /bin/sh -c #(nop)  ENTRYPOINT ["/docker-entr…   0B        
<missing>      Less than a second ago   /bin/sh -c #(nop) COPY file:09a214a3e07c919a…   4.61kB    
<missing>      Less than a second ago   /bin/sh -c #(nop) COPY file:0fd5fca330dcd6a7…   1.04kB    
<missing>      Less than a second ago   /bin/sh -c #(nop) COPY file:0b866ff3fc1ef5b0…   1.96kB    
<missing>      Less than a second ago   /bin/sh -c #(nop) COPY file:65504f71f5855ca0…   1.2kB     
<missing>      Less than a second ago   /bin/sh -c set -x     && addgroup --system -…   63.8MB    
<missing>      Less than a second ago   /bin/sh -c #(nop)  ENV PKG_RELEASE=1~buster     0B        
<missing>      Less than a second ago   /bin/sh -c #(nop)  ENV NJS_VERSION=0.5.3        0B        
<missing>      Less than a second ago   /bin/sh -c #(nop)  ENV NGINX_VERSION=1.19.9     0B        
<missing>      Less than a second ago   /bin/sh -c #(nop)  LABEL maintainer=NGINX Do…   0B        
<missing>      Less than a second ago   /bin/sh -c #(nop)  CMD ["bash"]                 0B        
<missing>      Less than a second ago   /bin/sh -c #(nop) ADD file:c855b3c65f5ba94d5…   69.3MB    
[root@localhost ~]# 

6、save

概述

将指定镜像保存成 tar 归档文件

语法

docker save [OPTIONS] IMAGE [IMAGE...]

[OPTIONS]

  • -o :输出到的文件。

examples

将镜像 nginx:1 生成 nginx_1.tar 文档

[root@localhost ~]# docker save -o nginx_1.tar nginx:1
[root@localhost ~]# ll
total 266596
-rw-------. 1 root root      1220 Apr  7 08:48 anaconda-ks.cfg
drwxr-xr-x. 2 root root         6 Apr  7 20:16 Dockefile
-rw-r--r--. 1 root root         0 Apr  7 18:13 index.html
-rw-------. 1 root root 137383936 Apr  7 20:27 nginx_1.tar
-rw-------. 1 root root 135603712 Apr  7 17:07 nginx2-20210407.tar
-rw-r--r--. 1 root root         0 Apr  7 18:07 test
[root@localhost ~]# 

7、load

概述

导入使用 docker save 命令导出的镜像

语法

docker load [OPTIONS]

[OPTIONS]

  • --input , -i : 指定导入的文件,代替 STDIN。
  • --quiet , -q : 精简输出信息。

examples

导入镜像

[root@localhost ~]# docker load --input nginx_1.tar 
997e631f6fc3: Loading layer  4.096kB/4.096kB
Loaded image: nginx:1
[root@localhost ~]# 

8、import

概述

从归档文件中创建镜像。

语法

docker import [OPTIONS] file|URL|- [REPOSITORY[:TAG]]

[OPTIONS]

  • -c :应用docker 指令创建镜像;
  • -m :提交时的说明文字;

examples

从镜像归档文件nginx_1.tar创建镜像,命名为nginx22

[root@localhost ~]# docker import nginx_1.tar  nginx:22
sha256:bd8bbdbc0ffa3fa96ec1ab1ddee704e0a52a3cd734e4842bbca164cc11538e51
[root@localhost ~]# docker image ls
REPOSITORY      TAG       IMAGE ID       CREATED                  SIZE
<none>          <none>    519e12e2a84a   Less than a second ago   133MB
nginx           22        bd8bbdbc0ffa   11 seconds ago           137MB
nginx           1         e4a4a18962c3   2 hours ago              133MB
rabbit-node01   v1        66676217280e   4 hours ago              156MB
ubuntu          latest    26b77e58432b   5 days ago               72.9MB
ubuntu          v1        26b77e58432b   5 days ago               72.9MB
alpine          latest    49f356fa4513   7 days ago               5.61MB
rabbitmq        latest    f83a0c22b15a   8 days ago               156MB
centos          latest    300e315adb2f   4 months ago             209MB
[root@localhost ~]# 

六、info|version

1、info

概述

显示 Docker 系统信息,包括镜像和容器数

语法

docker info [OPTIONS]

[OPTIONS]

null

examples

[root@localhost ~]# docker info 
Client:
 Context:    default
 Debug Mode: false
 Plugins:
  app: Docker App (Docker Inc., v0.9.1-beta3)
  buildx: Build with BuildKit (Docker Inc., v0.5.1-docker)

Server:
 Containers: 6
  Running: 3
  Paused: 0
  Stopped: 3
 Images: 8
 Server Version: 20.10.5
 Storage Driver: overlay2
  Backing Filesystem: xfs
  Supports d_type: true
  Native Overlay Diff: true
 Logging Driver: json-file
 Cgroup Driver: cgroupfs
 Cgroup Version: 1
 Plugins:
  Volume: local
  Network: bridge host ipvlan macvlan null overlay
  Log: awslogs fluentd gcplogs gelf journald json-file local logentries splunk syslog
 Swarm: inactive
 Runtimes: io.containerd.runc.v2 io.containerd.runtime.v1.linux runc
 Default Runtime: runc
 Init Binary: docker-init
 containerd version: 05f951a3781f4f2c1911b05e61c160e9c30eaa8e
 runc version: 12644e614e25b05da6fd08a38ffa0cfe1903fdec
 init version: de40ad0
 Security Options:
  seccomp
   Profile: default
 Kernel Version: 3.10.0-693.el7.x86_64
 Operating System: CentOS Linux 7 (Core)
 OSType: linux
 Architecture: x86_64
 CPUs: 2
 Total Memory: 2.075GiB
 Name: localhost.localdomain
 ID: KDWR:PTHT:DIIY:LPSG:4VPB:EMEW:4F74:NIZX:XZJO:AYYN:QVLE:7J23
 Docker Root Dir: /var/lib/docker
 Debug Mode: false
 Registry: https://index.docker.io/v1/
 Labels:
 Experimental: false
 Insecure Registries:
  127.0.0.0/8
 Registry Mirrors:
  https://sn4wsulj.mirror.aliyuncs.com/
 Live Restore Enabled: false

WARNING: bridge-nf-call-iptables is disabled
WARNING: bridge-nf-call-ip6tables is disabled
[root@localhost ~]# 

2、version

概述

显示 Docker 版本信息。

语法

docker version [OPTIONS]

[OPTIONS]

  • -f :指定返回值的模板文件。

examples

[root@localhost ~]# docker version
Client: Docker Engine - Community
 Version:           20.10.5
 API version:       1.41
 Go version:        go1.13.15
 Git commit:        55c4c88
 Built:             Tue Mar  2 20:33:55 2021
 OS/Arch:           linux/amd64
 Context:           default
 Experimental:      true

Server: Docker Engine - Community
 Engine:
  Version:          20.10.5
  API version:      1.41 (minimum version 1.12)
  Go version:       go1.13.15
  Git commit:       363e9a8
  Built:            Tue Mar  2 20:32:17 2021
  OS/Arch:          linux/amd64
  Experimental:     false
 containerd:
  Version:          1.4.4
  GitCommit:        05f951a3781f4f2c1911b05e61c160e9c30eaa8e
 runc:
  Version:          1.0.0-rc93
  GitCommit:        12644e614e25b05da6fd08a38ffa0cfe1903fdec
 docker-init:
  Version:          0.19.0
  GitCommit:        de40ad0
[root@localhost ~]# 
Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐