Docker 命令

  1. attach -- 登录到容器直接操作
  2. build -- 构建镜像
  3. commit -- 将容器打包成另一个镜像
  4. cp -- 在容器与本地系统间复制文件
  5. create -- 创建一个新的容器,不启动
  6. diff -- 查看容器中新增或修改的目录及文件
  7. events -- 查看 docker 服务端各种事件信息(包括容器的 启动, 停止, 删除, 更改等) -- 可以在调试的时候使用
  8. exec -- 用于在容器中执行命令
  9. export -- 将容器的文件系统导出为一个 tar 包文件
  10. import -- 将导出的文件系统创建成为一个镜像
  11. history -- 查看镜像操作的历史纪录
  12. images -- 查看当前系统中的镜像
  13. info -- 显示 docker 节点的信息
  14. inspect -- 查看 docker 镜像或容器的详细信息
  15. kill -- 停止一个或多个运行中的容器
  16. save -- 将一个或多个镜像 导出 为一个 tar 包文件
  17. load -- 从一个 tar 包文件或标准输入 导入一个镜像
  18. login -- 用于登录到 docker 的镜像仓库
  19. logout -- 退出已登录的 docker 镜像仓库
  20. logs -- 用于显示指定容器的日志
  21. pause unpause -- 暂停一个或多个容器, 取消暂停
  22. port -- 列出一个容器的端口映射
  23. ps -- 查看当前运行的容器
  24. pull push -- 镜像的拉去与推送
  25. rename -- 对容器重命名
  26. restart -- 重启一个或多个容器
  27. rm -- 删除一个或多个容器
  28. rmi -- 删除一个或多个镜像
  29. run -- 创建及启动容器
  30. search -- 通过命令行的方式从官方 hub 中查找镜像
  31. stop / start -- 停止 或 启动一个或多个容器
  32. stats -- 实时输出容器的资源使用情况
  33. tag -- 从一个指定的镜像创建另一个镜像
  34. top -- 显示指定容器的进程信息
  35. update -- 更新一个或多个容器的配置
  36. version -- 版本信息
  37. wait -- 捕捉容器的退出状态
  38. 管理命令 service -- 创建声明式的,可扩展的,有负载均衡的应用, 是面向用户的应用, 在 swarm 上运行
  39. 管理命令 config -- 用于创建docker 配置文件
  40. 管理命令 container -- 针对容器的基础命令
  41. 管理命令 image -- 针对镜像的基础命令
  42. 管理命令 node -- 管理 swarm 的节点
  43. 管理命令 plugin -- 管理docker的插件(安装卸载启用禁用)
  44. 管理命令 secret -- 管理secrets(敏感数据存储)

attach


   将本地终端的输入, 输出 以及 错误流 连接到容器上, (就是登录到容器中去操作)

// 使用 centos:7 镜像, 运行容器
docker run --name C7 -itd centos:7 /bin/sh
29a79dff571b58ce84a90e8ea28935a6a3c35ab707c7e48eae14b49d8ce19347

// 查看运行容器
docker container ls
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
29a79dff571b        centos:7            "/bin/sh"           2 minutes ago       Up 2 minutes                            C7

// 使用 attach 登录 C7 操作; 退出操作的话,容器会终止
docker attach C7
sh-4.2# ls
anaconda-post.log  bin    dev  etc  home    lib  lib64  media  mnt    opt  proc  root  run  sbin  srv  sys  tmp  usr    var
sh-4.2# ps
  PID TTY          TIME CMD
    1 pts/0    00:00:00 sh
    7 pts/0    00:00:00 ps

build


// docker build --help 查看帮助

// 首先创建目录, 使用 Dockerfile 创建一个镜像
mkdir /image_f
cd /image_f
vim Dockerfile          // Dockerfile简单使用
FROM centos:7

LABEL maintainer="Tian tzhr1225@163.com"

RUN mkdir -pv /data/tian && 
    echo "This is a test" > /data/tian/test

// 使用 docker build 通过 Dockerfile 文件构建新的镜像
docker build -t test:1 ./ 
Sending build context to Docker daemon  2.048kB
Step 1/3 : FROM centos:7
 ---> 9f38484d220f
Step 2/3 : LABEL maintainer="Tian tzhr1225@163.com"
 ---> Running in 0f0f45d5003c
Removing intermediate container 0f0f45d5003c
 ---> 242e247091b1
Step 3/3 : RUN mkdir -pv /data/tian &&     echo "This is a test" > /data/tian/test
 ---> Running in 9643e013f948
mkdir: created directory '/data'
mkdir: created directory '/data/tian'
Removing intermediate container 9643e013f948
 ---> af654e159686
Successfully built af654e159686
Successfully tagged test:1

// 查看构建的镜像
docker image ls
REPOSITORY                TAG                 IMAGE ID            CREATED             SIZE
test                      1                   af654e159686        31 seconds ago      202MB

// 运行镜像, 查看创建的目录等
docker run --name test --rm -it test:1
[root@ed21b42319e7 ~]# cat /data/tian/test 
This is a test

commit -- 将一个容器打包成另一个镜像


适用与在一个镜像容器中操作(安装或卸载服务等), 将操作后的容器再构建成一个镜像, 以便有需求直接使用

// 运行 test 容器中, 默认没有 vim; 使用 yum 安装 vim
[root@localhost ~]# docker run --name test -it test:1
[root@3c43c355e9d7 /]# yum -y install vim

// 复制上面的 ID; 退出 test 容器, 使用 docker commit 创建新的镜像
[root@3c43c355e9d7 /]# exit
exit
[root@localhost ~]# docker commit 3c43c355e9d7 test:2.0

// 运行 test:2.0 , 直接使用存在 vim 命令
[root@localhost ~]# docker run --name test2 --rm -it test:2.0
[root@42303df50598 /]# vim
... ...                                                                                                        
~                                     VIM - Vi IMproved
... ...

cp


// 查看到没有运行的容器 test, 启动test
[root@localhost ~]# docker container ls -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                     PORTS               NAMES
3c43c355e9d7        test:1              "/bin/bash"         8 minutes ago       Exited (0) 6 minutes ago                       test
[root@localhost ~]# docker start test
test

// 将 容器中的 /data/tian 中的 test 文件 复制到本地
[root@localhost ~]# docker cp test:/data/tian/test ./
[root@localhost ~]# cat test
This is a test

// 将本地 Dockerfile 文件上传到容器中
[root@localhost ~]# docker cp /image_f/Dockerfile test:/data/tian/
[root@localhost ~]# docker exec test cat /data/tian/Dockerfile
FROM centos:7

LABEL maintainer="Tian tzhr1225@163.com"

RUN mkdir -pv /data/tian && 
    echo "This is a test" > /data/tian/test

create


// 创建一个容器, 但是不启动它, 用法 docker create --help 查看, 参数用法同 docker run 差不多

// 创建 centos:6 
[root@localhost ~]# docker create centos:6

// 查看容器创建, centos 6 有, 但是没有运行;
[root@localhost ~]# docker container ls -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
6243c7c92ac1        centos:6            "/bin/bash"         2 minutes ago       Created                                 gracious_lehmann

diff


// 使用 centos:6 镜像启动 c6 容器, 并使用 yum 安装 vim;
[root@localhost ~]# docker run --name c6 --rm -it centos:6
[root@d849adb301f4 /]# yum -y install vim

// 另起一个终端 使用 docker diff 查看 c6 容器; C 表示创建, A 表示追加,修改;
[root@localhost ~]# docker diff c6
... ...
A /var/lib/yum/yumdb/v/a8ff3e5026c514ae782a731398908b8322561b1c-vim-enhanced-7.4.629-5.el6_8.1-x86_64/from_repo_timestamp
C /var/lib/rpm
... ...

events


// 直接使用 docker events 就可以动态查看记录容器的操作
[root@localhost ~]# docker events

// 另起一个终端, exit 停止一个容器, 并删除上面 创建 的容器,
[root@d849adb301f4 /]# exit
[root@localhost ~]#
[root@localhost ~]# docker container ls -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                      PORTS               NAMES
6243c7c92ac1        centos:6            "/bin/bash"         29 hours ago        Exited (0) 10 minutes ago                       gracious_lehmann
[root@localhost ~]# docker container rm 624
624

// 查看 docker events
[root@localhost ~]# docker events 
2019-06-18T04:46:37.623915823-04:00 container die d849adb301f45670a08ab9f90e20e36df4a9d8159bef3fd02af977d8dcc560c3 (exitCode=0, image=centos:6, name=c6, org.label-schema.build-date=20181006, org.label-schema.license=GPLv2, org.label-schema.name=CentOS Base Image, org.label-schema.schema-version=1.0, org.label-schema.vendor=CentOS)
2019-06-18T04:46:37.712754544-04:00 network disconnect dd54f129406eb1e595af66fc0abe6a6b1cba8d0c69bff35a4a1eba549e3fb820 (container=d849adb301f45670a08ab9f90e20e36df4a9d8159bef3fd02af977d8dcc560c3, name=bridge, type=bridge)
2019-06-18T04:47:09.282202462-04:00 container destroy 6243c7c92ac17aaa0dd026be829323dfb8521e8349d1e727184f4e91b5259cd8 (image=centos:6, name=gracious_lehmann, org.label-schema.build-date=20181006, org.label-schema.license=GPLv2, org.label-schema.name=CentOS Base Image, org.label-schema.schema-version=1.0, org.label-schema.vendor=CentOS)

exec


// 使用 centos:7 镜像起一个容器
[root@localhost ~]# docker run --name C7 --rm -itd centos:7
a3e536afc59255910c818ffdc4d81b6afa9cdde1b1a49803a3997e694b685d13

// 使用 exec 执行命令
[root@localhost ~]# docker exec C7 hostname
a3e536afc592

// 使用 exec 交互式执行 bash; 与 attach 的区别是这个执行完退出后容器不终止;
[root@localhost ~]# docker exec -it C7 /bin/bash
[root@a3e536afc592 /]# hostname
a3e536afc592
[root@a3e536afc592 /]# w
 08:58:58 up 51 days, 8 min,  0 users,  load average: 0.00, 0.01, 0.05
USER     TTY      FROM             LOGIN@   IDLE   JCPU   PCPU WHAT

// 使用exit 退出容器, 再查看容器还在运行
[root@a3e536afc592 /]# exit
exit
[root@localhost ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
a3e536afc592        centos:7            "/bin/bash"         4 minutes ago       Up 4 minutes                            C7

export


// 两种方式导出为 tar 文件
[root@localhost ~]# docker export C7 -o centos7_1.tar
[root@localhost ~]# docker export C7 > centos7_2.tar

// 两种方式权限不同, -o 600 ; > 644;
[root@localhost ~]# ll centos7_*
-rw------- 1 root root 209489920 Jun 18 05:05 centos7_1.tar
-rw-r--r-- 1 root root 209489920 Jun 18 05:05 centos7_2.tar

import


// 常用于开发/测试对容器中进行修改后, 使用 export 生成 tar 包, 再恢复的使用使用 import
[root@localhost ~]# docker import centos7_1.tar centos:7-t
sha256:626dda4747388dc14eac5920afd23b3d2109a3c81a718e68ee96509bd1fe3e34

// 查看 镜像
[root@localhost ~]# docker images
REPOSITORY                TAG                 IMAGE ID            CREATED              SIZE
centos                    7-t                 626dda474738        About a minute ago   202MB
... ...

// 运行一个容器
[root@localhost ~]# docker run --name C_7-n -it --rm centos:7-t /bin/bash

history


// 查看 centos:6 的历史记录, 等同于 docker image history
[root@localhost ~]# docker history centos:6
IMAGE               CREATED             CREATED BY                                      SIZE                COMMENT
d0957ffdf8a2        3 months ago        /bin/sh -c #(nop)  CMD ["/bin/bash"]            0B                  
<missing>           3 months ago        /bin/sh -c #(nop)  LABEL org.label-schema.sc…   0B                  
<missing>           3 months ago        /bin/sh -c #(nop) ADD file:0065316a41144e95b…   194MB               
<missing>           8 months ago        /bin/sh -c #(nop)  MAINTAINER https://github…   0B

images


// 查看系统中的镜像, 相当于 docker image ls
[root@localhost ~]# docker images
REPOSITORY                TAG                 IMAGE ID            CREATED             SIZE
busybox                   latest              64f5d945efcc        5 weeks ago         1.2MB
tian/httpd                v.1.0               4c1ef9008f84        7 weeks ago         1.2MB
192.168.9.30:5000/httpd   v.2.0               4f14d7a10dc3        7 weeks ago         1.2MB
tian/httpd                v.2.0               4f14d7a10dc3        7 weeks ago         1.2MB
centos                    6                   d0957ffdf8a2        3 months ago        194MB
centos                    7                   9f38484d220f        3 months ago        202MB
hello-world               latest              fce289e99eb9        5 months ago        1.84kB

info


// 查看 docker 系统中的所有容器信息
[root@localhost ~]# docker info
Containers: 1
 Running: 1
 Paused: 0
 Stopped: 0
Images: 7
Server Version: 18.09.6
Storage Driver: overlay2
 Backing Filesystem: xfs
 Supports d_type: true
 Native Overlay Diff: true
Logging Driver: json-file
Cgroup Driver: cgroupfs
Plugins:
 Volume: local
 Network: bridge host macvlan null overlay
 Log: awslogs fluentd gcplogs gelf journald json-file local logentries splunk syslog
Swarm: inactive
Runtimes: runc
Default Runtime: runc
Init Binary: docker-init
containerd version: bb71b10fd8f58240ca47fbb579b9d1028eea7c84
runc version: 2b18fe1d885ee5083ef9f0838fee39b62d653e30
init version: fec3683
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: 1.796GiB
Name: localhost.localdomain
ID: BJU6:VTUC:HA23:LARR:VULF:H2LR:4UNY:KGRP:2OL3:G5GS:77KA:ZGCZ
Docker Root Dir: /var/lib/docker
Debug Mode (client): false
Debug Mode (server): false
Registry: https://index.docker.io/v1/
Labels:
Experimental: false
Insecure Registries:
 192.168.9.30:5000
 127.0.0.0/8
Registry Mirrors:
 https://registry.docker-cn.com/
Live Restore Enabled: false
Product License: Community Engine

inspect


// 查看镜像的信息  *** 调试中使用的会比较多 ***
[root@localhost ~]# docker inspect hello-world:latest
[
    {
        "Id": "sha256:fce289e99eb9bca977dae136fbe2a82b6b7d4c372474c9235adc1741675f587e",
        "RepoTags": [
            "hello-world:latest"
        ],
        "RepoDigests": [
            "hello-world@sha256:41a65640635299bab090f783209c1e3a3f11934cf7756b09cb2f1e02147c6ed8"
        ],
        "Parent": "",
        "Comment": "",
        "Created": "2019-01-01T01:29:27.650294696Z",
        "Container": "8e2caa5a514bb6d8b4f2a2553e9067498d261a0fd83a96aeaaf303943dff6ff9",
        "ContainerConfig": {
            "Hostname": "8e2caa5a514b",
            "Domainname": "",
... ...

// 查看容器的详细信息
[root@localhost ~]# docker inspect C7
[
    {
        "Id": "a3e536afc59255910c818ffdc4d81b6afa9cdde1b1a49803a3997e694b685d13",
        "Created": "2019-06-18T08:56:49.557911196Z",
        "Path": "/bin/bash",
        "Args": [],
        "State": {
            "Status": "running",
            "Running": true,
            "Paused": false,
            "Restarting": false,
            "OOMKilled": false,
            "Dead": false,
            "Pid": 20501,
            "ExitCode": 0,
            "Error": "",
            "StartedAt": "2019-06-18T08:56:50.052990757Z",
            "FinishedAt": "0001-01-01T00:00:00Z"
        },
... ...

// 查看指定对应的值
[root@localhost ~]# docker inspect hello-world:latest -f '{{.ContainerConfig.Hostname}}'
8e2caa5a514b

kill


[root@localhost ~]# docker container ls
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
a3e536afc592        centos:7            "/bin/bash"         21 hours ago        Up 21 hours                             C7
[root@localhost ~]# docker kill C7
C7
[root@localhost ~]# docker container ls
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES

save


// 两种方式, 生成的 tar 包 权限不同
[root@localhost ~]# docker image ls
REPOSITORY                TAG                 IMAGE ID            CREATED             SIZE
centos                    7-t                 626dda474738        3 hours ago         202MB
busybox                   latest              64f5d945efcc        5 weeks ago         1.2MB
tian/httpd                v.1.0               4c1ef9008f84        7 weeks ago         1.2MB
192.168.9.30:5000/httpd   v.2.0               4f14d7a10dc3        7 weeks ago         1.2MB
tian/httpd                v.2.0               4f14d7a10dc3        7 weeks ago         1.2MB
centos                    6                   d0957ffdf8a2        3 months ago        194MB
centos                    7                   9f38484d220f        3 months ago        202MB
hello-world               latest              fce289e99eb9        5 months ago        1.84kB
[root@localhost ~]# docker save busybox:latest -o busybox.tar
[root@localhost ~]# docker save hello-world:latest > hello.tar
[root@localhost ~]# ll busybox.tar hello.tar 
-rw------- 1 root root 1424896 Jun 19 02:47 busybox.tar
-rw-r--r-- 1 root root   12800 Jun 19 02:48 hello.tar

load


// 需要使用 -i 的参数指定 tar 文件, 否则将从标准输入中寻找
[root@localhost ~]# docker load -i busybox.tar 
Loaded image: busybox:latest

login


// -u 用户名 -p 密码, 后面不跟网址默认是 docker hub 的镜像仓库; 要是别的需要跟上指定的网址
[root@localhost ~]# docker login -u tzhr -p xxxx 
WARNING! Using --password via the CLI is insecure. Use --password-stdin.
WARNING! Your password will be stored unencrypted in /root/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store

Login Succeeded

logout


// 若登录的是 docker hub 镜像仓库, 直接使用 docker logout 
[root@localhost ~]# docker logout 
Removing login credentials for https://index.docker.io/v1/

// 若是登录的是 私有的 镜像仓库, 在后面跟上网址

logs


// 查看容器的操作日志
[root@localhost ~]# docker logs C7

// 可以指定只显示最后十行
[root@localhost ~]# docker logs --tail 10 C7

// 动态实时输出
[root@localhost ~]# docker logs -f C7

pause unpause


// 暂停 C7 容器  应用场景: 有的容器启动快了.. 需要暂停, 等候一下其他的环境/容器的启动加载;
[root@localhost ~]# docker pause C7
C7
[root@localhost ~]# docker container ls -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                  PORTS               NAMES
3f442c7c1e7e        centos:7            "/bin/bash"         9 minutes ago       Up 9 minutes (Paused)                       C7

// 取消暂停
[root@localhost ~]# docker unpause C7
C7
[root@localhost ~]# docker container ls -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
3f442c7c1e7e        centos:7            "/bin/bash"         10 minutes ago      Up 10 minutes

port


// 运行 C6 容器, 映射 80 端口;
[root@localhost ~]# docker run --name C6 --rm -itd -p 80 centos:6
4ba4536302a7664eefecdce7660bb9aa2affad79fae7fc01782d5e8394a08ce8
[root@localhost ~]# docker port C6
80/tcp -> 0.0.0.0:32769

ps


// 默认查看运行的容器
[root@localhost ~]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
3f442c7c1e7e        centos:7            "/bin/bash"         21 minutes ago      Up 21 minutes                           C7

// 使用 ls -a 查看所有的 容器
[root@localhost ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                     PORTS               NAMES
d7e6fc22cfa4        centos:6            "/bin/bash"         5 seconds ago       Exited (0) 3 seconds ago                       C6
3f442c7c1e7e        centos:7            "/bin/bash"         21 minutes ago      Up 21 minutes                                  C7

// 使用 docker ps -l 查看最近的一个容器, 无论运行与不运行
[root@localhost ~]# docker ps -l
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                     PORTS               NAMES
d7e6fc22cfa4        centos:6            "/bin/bash"         5 minutes ago       Exited (0) 5 minutes ago                       C6

pull push


// 下拉镜像, 后面直接指定镜像的名称 ; 最后使用 -a 参数表示拉取所有的 tag;
[root@localhost ~]# docker pull hello-world
Using default tag: latest
latest: Pulling from library/hello-world
1b930d010525: Pull complete 
Digest: sha256:41a65640635299bab090f783209c1e3a3f11934cf7756b09cb2f1e02147c6ed8
Status: Downloaded newer image for hello-world:latest

// push 推送的话, 需要先登录, 然后push 
docker push 地址/镜像:tag

rename


[root@localhost ~]# docker container ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
3f442c7c1e7e        centos:7            "/bin/bash"         4 days ago          Up 4 days                               C7
// 将 C7 更名为 Centos_7
[root@localhost ~]# docker rename C7 Centos_7 
[root@localhost ~]# docker ps 
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
3f442c7c1e7e        centos:7            "/bin/bash"         4 days ago          Up 4 days                               Centos_7

restart


[root@localhost ~]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
717aac1e0b79        centos:6            "/bin/bash"         3 seconds ago       Up 2 seconds                            Centos_6
3f442c7c1e7e        centos:7            "/bin/bash"         4 days ago          Up 4 days                               Centos_7

// 重启 Centos_6 和 Centos_7 容器; 可以使用 -t 参数指定多长时间, 默认 10 秒;
[root@localhost ~]# docker restart Centos_6 Centos_7
Centos_6
Centos_7

rm


// 查看所有的容器
[root@localhost ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                  PORTS               NAMES
717aac1e0b79        centos:6            "/bin/bash"         10 minutes ago      Up 10 minutes                               Centos_6
d7e6fc22cfa4        centos:6            "/bin/bash"         4 days ago          Exited (0) 4 days ago                       C6
3f442c7c1e7e        centos:7            "/bin/bash"         4 days ago          Up 6 minutes                                Centos_7

// 删除没有运行的容器, 可以使用容器名, 也可以使用 ID , 这里使用 ID 的前两位(简写,匹配)
[root@localhost ~]# docker rm d7
d7

// 删除运行的容器报错
[root@localhost ~]# docker rm 71
Error response from daemon: You cannot remove a running container 717aac1e0b7967120db07aa15d91a8be4c8efe127eb5206e4b667c9278a82f42. Stop the container before attempting removal or force remove

// 使用 -f 参数删除运行的容器
[root@localhost ~]# docker rm -f 71
71
[root@localhost ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
3f442c7c1e7e        centos:7            "/bin/bash"         4 days ago          Up 7 minutes                            Centos_7

rmi


// 查看镜像, 容器使用的镜像是删不掉的
[root@localhost ~]# docker images
REPOSITORY                TAG                 IMAGE ID            CREATED             SIZE
centos                    7-t                 626dda474738        4 days ago          202MB
busybox                   latest              64f5d945efcc        6 weeks ago         1.2MB
tian/httpd                v.1.0               4c1ef9008f84        7 weeks ago         1.2MB
192.168.9.30:5000/httpd   v.2.0               4f14d7a10dc3        8 weeks ago         1.2MB
tian/httpd                v.2.0               4f14d7a10dc3        8 weeks ago         1.2MB
centos                    6                   d0957ffdf8a2        3 months ago        194MB
centos                    7                   9f38484d220f        3 months ago        202MB
hello-world               latest              fce289e99eb9        5 months ago        1.84kB

// 可以跟 镜像:标签 也可以跟 ID; 使用 -f 可以强制删除一个镜像;
[root@localhost ~]# docker rmi centos:7-t 4c
Untagged: centos:7-t
Deleted: sha256:626dda4747388dc14eac5920afd23b3d2109a3c81a718e68ee96509bd1fe3e34
Deleted: sha256:0b685c6e322d224524d9c7baf4dcb12991020faee6530cf6b1898ddb70c70b80
Untagged: tian/httpd:v.1.0
Deleted: sha256:4c1ef9008f84aae233d73222d38e71c3156dfce8a4515cb9e2c951d9706bcabc
Deleted: sha256:b4f291dd94aa6ef25df0272b1c7bb5da99e675e4ef19fa94fe719d46491ed6e3

run


  参数有很多, 可以指定 主机名 内存 CPU 等; 使用 docker run --help 查看使用帮助;;

// --name 定义容器名, --rm 表示停止容器就删除, -it 交互式, -d 后台运行;
[root@localhost ~]# docker run --name C6 --rm -itd centos:6 
dde7d991f1990409e9247ef797267289d06190f138ee83e96a0298afb2d3281f
[root@localhost ~]# docker ps 
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
dde7d991f199        centos:6            "/bin/bash"         4 seconds ago       Up 3 seconds                            C6
[root@localhost ~]# docker run --name centos_6 --rm -it centos:6 /bin/bash
[root@8f2d2fab3d16 /]# 

search


// 查找 redis 镜像
[root@localhost ~]# docker search redis
NAME                             DESCRIPTION                                     STARS               OFFICIAL            AUTOMATED
redis                            Redis is an open source key-value store that…   7034                [OK]                
bitnami/redis                    Bitnami Redis Docker Image                      114                                     [OK]
sameersbn/redis                                                                  75
... ...

// 过滤出 stars 大于 100 的 ; 过滤使用 -f;
[root@localhost ~]# docker search redis -f stars=100
NAME                DESCRIPTION                                     STARS               OFFICIAL            AUTOMATED
redis               Redis is an open source key-value store that…   7034                [OK]                
bitnami/redis       Bitnami Redis Docker Image                      114                                     [OK]

// 过滤出官方的
[root@localhost ~]# docker search redis -f is-official=true
NAME                DESCRIPTION                                     STARS               OFFICIAL            AUTOMATED
redis               Redis is an open source key-value store that…   7034                [OK]

// 多个因素过滤 --filter
[root@localhost ~]# docker search redis --filter is-official=true --filter stars=100
NAME                DESCRIPTION                                     STARS               OFFICIAL            AUTOMATED
redis               Redis is an open source key-value store that…   7034                [OK]

stop / start


// 运行一个容器
[root@localhost ~]# docker run --name C6 -itd centos:6
ce5db9dc2f4407f40042fcb71ef1fab6ecc5de8b2f603918a4c7ae098dfd3a7c
[root@localhost ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                          PORTS               NAMES
ce5db9dc2f44        centos:6            "/bin/bash"         5 seconds ago       Up 4 seconds                                        C6
d518ba8150ef        centos:7            "/bin/bash"         4 minutes ago       Exited (0) About a minute ago                       friendly_davinci

// 停止容器
[root@localhost ~]# docker stop C6
C6

// 查看状态
[root@localhost ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                          PORTS               NAMES
ce5db9dc2f44        centos:6            "/bin/bash"         36 seconds ago      Exited (137) 13 seconds ago                         C6
d518ba8150ef        centos:7            "/bin/bash"         5 minutes ago       Exited (0) About a minute ago                       friendly_davinci

// 启动容器
[root@localhost ~]# docker start C6
C6
[root@localhost ~]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
ce5db9dc2f44        centos:6            "/bin/bash"         47 seconds ago      Up 3 seconds                            C6

stats


// 输出 容器 的资源使用情况,  --no-stream 只输出一次,  -a 参数 输出所有容器的状态;;
[root@localhost ~]# docker stats C6

CONTAINER ID        NAME                CPU %               MEM USAGE / LIMIT   MEM %               NET I/O             BLOCK I/O           PIDS
ce5db9dc2f44        C6                  0.00%               348KiB / 1.796GiB   0.02%               648B / 0B           0B / 0B             1

CONTAINER ID        NAME                CPU %               MEM USAGE / LIMIT   MEM %               NET I/O             BLOCK I/O           PIDS
ce5db9dc2f44        C6                  0.00%               348KiB / 1.796GiB   0.02%               648B / 0B           0B / 0B             1
... ...

tag


// 通过一个指定的镜像生成另一个镜像, 不指定标签默认 latest
[root@localhost ~]# docker images
REPOSITORY                TAG                 IMAGE ID            CREATED             SIZE
busybox                   latest              64f5d945efcc        6 weeks ago         1.2MB
192.168.9.30:5000/httpd   v.2.0               4f14d7a10dc3        8 weeks ago         1.2MB
tian/httpd                v.2.0               4f14d7a10dc3        8 weeks ago         1.2MB
centos                    6                   d0957ffdf8a2        3 months ago        194MB
centos                    7                   9f38484d220f        3 months ago        202MB
hello-world               latest              fce289e99eb9        5 months ago        1.84kB
[root@localhost ~]# docker tag busybox busybox:new

// 生成新的镜像与原镜像的 ID 号相同;
[root@localhost ~]# docker images
REPOSITORY                TAG                 IMAGE ID            CREATED             SIZE
busybox                   latest              64f5d945efcc        6 weeks ago         1.2MB
busybox                   new                 64f5d945efcc        6 weeks ago         1.2MB
192.168.9.30:5000/httpd   v.2.0               4f14d7a10dc3        8 weeks ago         1.2MB
tian/httpd                v.2.0               4f14d7a10dc3        8 weeks ago         1.2MB
centos                    6                   d0957ffdf8a2        3 months ago        194MB
centos                    7                   9f38484d220f        3 months ago        202MB
hello-world               latest              fce289e99eb9        5 months ago        1.84kB

top


[root@localhost ~]# docker top C6
UID                 PID                 PPID                C                   STIME               TTY                 TIME                CMD
root                26247               26230               0                   21:52               pts/0               00:00:00            /bin/bash

update


// 启动的使用对容器进行了一些资源的限制, 运行过程中发现资源不够用, 可以使用指定的参数更新容器的资源
[root@localhost ~]# docker update --help

Usage:    docker update [OPTIONS] CONTAINER [CONTAINER...]

Update configuration of one or more containers

Options:
      --blkio-weight uint16        Block IO (relative weight), between 10 and 1000, or
                                   0 to disable (default 0)
      --cpu-period int             Limit CPU CFS (Completely Fair Scheduler) period
      --cpu-quota int              Limit CPU CFS (Completely Fair Scheduler) quota
      --cpu-rt-period int          Limit the CPU real-time period in microseconds
      --cpu-rt-runtime int         Limit the CPU real-time runtime in microseconds
  -c, --cpu-shares int             CPU shares (relative weight)
      --cpus decimal               Number of CPUs
      --cpuset-cpus string         CPUs in which to allow execution (0-3, 0,1)
      --cpuset-mems string         MEMs in which to allow execution (0-3, 0,1)
      --kernel-memory bytes        Kernel memory limit
  -m, --memory bytes               Memory limit
      --memory-reservation bytes   Memory soft limit
      --memory-swap bytes          Swap limit equal to memory plus swap: '-1' to enable
                                   unlimited swap
      --restart string             Restart policy to apply when a container exits


version


[root@localhost ~]# docker version
Client:
 Version:           18.09.6
 API version:       1.39
 Go version:        go1.10.8
 Git commit:        481bc77156
 Built:             Sat May  4 02:34:58 2019
 OS/Arch:           linux/amd64
 Experimental:      false

Server: Docker Engine - Community
 Engine:
  Version:          18.09.6
  API version:      1.39 (minimum version 1.12)
  Go version:       go1.10.8
  Git commit:       481bc77
  Built:            Sat May  4 02:02:43 2019
  OS/Arch:          linux/amd64
  Experimental:     false

wait


[root@localhost ~]# docker run --name C7 -it -d centos:7
72035c965f748c800b432120bb9a87bdb03efe19160a6ccae469bc3cc97e2777
[root@localhost ~]# docker stop C7
C7
[root@localhost ~]# docker wait C7
137

service


// docker service 的用法
[root@localhost ~]# docker service --help

Usage:    docker service COMMAND

Manage services

Commands:
  create      Create a new service
  inspect     Display detailed information on one or more services
  logs        Fetch the logs of a service or task
  ls          List services
  ps          List the tasks of one or more services
  rm          Remove one or more services
  rollback    Revert changes to a service's configuration
  scale       Scale one or multiple replicated services      // 扩展节点数量
  update      Update a service

// 先查看没有就service
[root@localhost ~]# docker service ls
ID                  NAME                MODE                REPLICAS            IMAGE               PORTS
// 创建一个service
[root@localhost ~]# docker service create --name redis redis:alpine
o6paknwk9wcyqd6uvxaimd448
overall progress: 1 out of 1 tasks 
1/1: running   
verify: Service converged 
[root@localhost ~]# docker service ls
ID                  NAME                MODE                REPLICAS            IMAGE               PORTS
o6paknwk9wcy        redis               replicated          1/1                 redis:alpine

// 扩展 redis , 将replicas 节点扩展为 3 个;
[root@localhost ~]# docker service scale redis=3
redis scaled to 3
overall progress: 3 out of 3 tasks 
1/3: running   
2/3: running   
3/3: running   
verify: Service converged

// 查看
[root@localhost ~]# docker service ls
ID                  NAME                MODE                REPLICAS            IMAGE               PORTS
o6paknwk9wcy        redis               replicated          3/3                 redis:alpine        
[root@localhost ~]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
e0070f5403b3        redis:alpine        "docker-entrypoint.s…"   58 seconds ago      Up 57 seconds       6379/tcp            redis.3.5p2550af9hvoft2qxs4qoe3g7
a62dfe79ee9a        redis:alpine        "docker-entrypoint.s…"   58 seconds ago      Up 57 seconds       6379/tcp            redis.2.lil9796511b7dsl8luupgc7uj
dea48d1e4a5b        redis:alpine        "docker-entrypoint.s…"   2 minutes ago       Up 2 minutes        6379/tcp            redis.1.obwahecglli7g6i1wn4hjhh14
ce5db9dc2f44        centos:6            "/bin/bash"              2 hours ago         Up 2 hours                              C6

config


// 创建文件, 用于容器中的一些配置文件, 公共的配置文件的使用;
[root@localhost ~]# echo "This is configure test"|docker config create my-config -
1n32fabs69c73jixr6nsqg4wm

// 查看创建的文件
[root@localhost ~]# docker config ls
ID                          NAME                CREATED             UPDATED
1n32fabs69c73jixr6nsqg4wm   my-config           8 seconds ago       8 seconds ago

// 启动容器, 挂载使用上面的配置文件
[root@localhost ~]# docker service create --name redis --config my-config redis:alpine
zrhd42cysh0o3i557riaq00tl
overall progress: 1 out of 1 tasks 
1/1: running   
verify: Service converged

// 查看
[root@localhost ~]# docker service ps  redis
ID                  NAME                IMAGE               NODE                    DESIRED STATE       CURRENT STATE           ERROR               PORTS
7hm9sbsckh6x        redis.1             redis:alpine        localhost.localdomain   Running             Running 3 minutes ago

// 进入容器, 查看上面的文件
[root@localhost ~]# docker ps |grep redis
ea63f28bfc9d        redis:alpine        "docker-entrypoint.s…"   5 minutes ago       Up 5 minutes        6379/tcp            redis.1.7hm9sbsckh6x6kotnxh3eqdyd
[root@localhost ~]# docker exec ea6 ls -l /
total 8
drwxr-xr-x    1 root     root          4096 May 16 00:52 bin
drwxr-xr-x    2 redis    redis            6 May 16 00:52 data
drwxr-xr-x    5 root     root           340 Jun 24 02:30 dev
drwxr-xr-x    1 root     root            66 Jun 24 02:30 etc
drwxr-xr-x    1 root     root            19 May 11 01:43 home
drwxr-xr-x    1 root     root            17 May 16 00:52 lib
drwxr-xr-x    5 root     root            44 May  9 20:49 media
drwxr-xr-x    2 root     root             6 May  9 20:49 mnt
-r--r--r--    1 root     root            23 Jun 24 02:30 my-config
drwxr-xr-x    2 root     root             6 May  9 20:49 opt
dr-xr-xr-x  137 root     root             0 Jun 24 02:30 proc
drwx------    2 root     root             6 May  9 20:49 root
drwxr-xr-x    2 root     root             6 May  9 20:49 run
drwxr-xr-x    1 root     root            21 May 11 01:43 sbin
drwxr-xr-x    2 root     root             6 May  9 20:49 srv
dr-xr-xr-x   13 root     root             0 Jun 24 02:30 sys
drwxrwxrwt    1 root     root             6 May 16 00:52 tmp
drwxr-xr-x    1 root     root            19 May 16 00:52 usr
drwxr-xr-x    1 root     root            19 May  9 20:49 var

// 看到有 my-config 文件, 查看文件内容
[root@localhost ~]# docker exec ea6 cat /my-config
This is configure test

// 删除容器, 及删除文件
[root@localhost ~]# docker service ls
ID                  NAME                MODE                REPLICAS            IMAGE               PORTS
zrhd42cysh0o        redis               replicated          1/1                 redis:alpine        
[root@localhost ~]# docker service rm zrh
zrh
[root@localhost ~]# docker config ls
ID                          NAME                CREATED             UPDATED
1n32fabs69c73jixr6nsqg4wm   my-config           17 minutes ago      17 minutes ago
[root@localhost ~]# docker config rm 1n32
1n32

container


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

Usage:    docker container COMMAND

Manage containers

Commands:
  attach      Attach local standard input, output, and error streams to a running container
  commit      Create a new image from a container's changes
  cp          Copy files/folders between a container and the local filesystem
  create      Create a new container
  diff        Inspect changes to files or directories on a container's filesystem
  exec        Run a command in a running container
  export      Export a container's filesystem as a tar archive
  inspect     Display detailed information on one or more containers
  kill        Kill one or more running containers
  logs        Fetch the logs of a container
  ls          List containers
  pause       Pause all processes within one or more containers
  port        List port mappings or a specific mapping for the container
  prune       Remove all stopped containers
  rename      Rename a container
  restart     Restart one or more containers
  rm          Remove one or more containers
  run         Run a command in a new container
  start       Start one or more stopped containers
  stats       Display a live stream of container(s) resource usage statistics
  stop        Stop one or more running containers
  top         Display the running processes of a container
  unpause     Unpause all processes within one or more containers
  update      Update configuration of one or more containers
  wait        Block until one or more containers stop, then print their exit codes

image


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

Usage:    docker image COMMAND

Manage images

Commands:
  build       Build an image from a Dockerfile
  history     Show the history of an image
  import      Import the contents from a tarball to create a filesystem image
  inspect     Display detailed information on one or more images
  load        Load an image from a tar archive or STDIN
  ls          List images
  prune       Remove unused images         // 删除未使用的镜像
  pull        Pull an image or a repository from a registry
  push        Push an image or a repository to a registry
  rm          Remove one or more images
  save        Save one or more images to a tar archive (streamed to STDOUT by default)
  tag         Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE

node


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

Usage:    docker node COMMAND

Manage Swarm nodes

Commands:
  demote      Demote one or more nodes from manager in the swarm
  inspect     Display detailed information on one or more nodes
  ls          List nodes in the swarm
  promote     Promote one or more nodes to manager in the swarm
  ps          List tasks running on one or more nodes, defaults to current node
  rm          Remove one or more nodes from the swarm
  update      Update a node

plugin


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

Usage:    docker plugin COMMAND

Manage plugins

Commands:
  create      Create a plugin from a rootfs and configuration. Plugin data directory must contain config.json and rootfs directory.
  disable     Disable a plugin
  enable      Enable a plugin
  inspect     Display detailed information on one or more plugins
  install     Install a plugin
  ls          List plugins
  push        Push a plugin to a registry
  rm          Remove one or more plugins
  set         Change settings for a plugin
  upgrade     Upgrade an existing plugin

secret


// 用于给容器提供一些共享的数据; 如 证书 或者 配置等
[root@localhost ~]# docker secret --help

Usage:    docker secret COMMAND

Manage Docker secrets

Commands:
  create      Create a secret from a file or STDIN as content
  inspect     Display detailed information on one or more secrets
  ls          List secrets
  rm          Remove one or more secrets
原文地址:https://www.cnblogs.com/haorong/p/11010656.html