• docker命令总结(二)


    上次只是给大家把命令的作用以及简单使用列出来了(大家可以查看:docker命令总结(一)),那这篇文章会详细介绍每条命令的参数

    search

    docker search:此命令是用来在仓库中查找docker镜像

    语法:

    docker search 参数 镜像名称:tag(版本)
    

    参数说明:

    --filter,-f:根据相关条件过滤输出
    --format:使用Go模板进行漂亮的打印搜索
    --limit:指定最大搜索结果,默认25个
    --no-trunc:显示完整的镜像描述
    --stars,-s:列出收藏数不小于指定值的镜像
    --automated:仅显示自动构建的镜像
    

    示例:

    默认搜索

    ubuntu@VM-101-242-ubuntu:~$ sudo docker search nginx
    NAME                              DESCRIPTION                                     STARS               OFFICIAL            AUTOMATED
    nginx                             Official build of Nginx.                        12438               [OK]                
    jwilder/nginx-proxy               Automated Nginx reverse proxy for docker con…   1713                                    [OK]
    richarvey/nginx-php-fpm           Container running Nginx + PHP-FPM capable of…   750                                     [OK]
    linuxserver/nginx                 An Nginx container, brought to you by LinuxS…   84                                      
    bitnami/nginx                     Bitnami nginx Docker Image                      75                                      [OK]
    tiangolo/nginx-rtmp               Docker image with Nginx using the nginx-rtmp…   60                                      [OK]
    nginxdemos/hello                  NGINX webserver that serves a simple page co…   35                                      [OK]
    jc21/nginx-proxy-manager          Docker container for managing Nginx proxy ho…   34                                      
    nginx/unit                        NGINX Unit is a dynamic web and application …   33                                      
    jlesage/nginx-proxy-manager       Docker container for Nginx Proxy Manager        31                                      [OK]
    nginx/nginx-ingress               NGINX Ingress Controller for Kubernetes         22                                      
    privatebin/nginx-fpm-alpine       PrivateBin running on an Nginx, php-fpm & Al…   19                                      [OK]
    schmunk42/nginx-redirect          A very simple container to redirect HTTP tra…   17                                      [OK]
    blacklabelops/nginx               Dockerized Nginx Reverse Proxy Server.          12                                      [OK]
    centos/nginx-18-centos7           Platform for running nginx 1.8 or building n…   12                                      
    nginxinc/nginx-unprivileged       Unprivileged NGINX Dockerfiles                  12                                      
    centos/nginx-112-centos7          Platform for running nginx 1.12 or building …   11                                      
    nginx/nginx-prometheus-exporter   NGINX Prometheus Exporter                       9                                       
    sophos/nginx-vts-exporter         Simple server that scrapes Nginx vts stats a…   6                                       [OK]
    mailu/nginx                       Mailu nginx frontend                            5                                       [OK]
    1science/nginx                    Nginx Docker images that include Consul Temp…   5                                       [OK]
    pebbletech/nginx-proxy            nginx-proxy sets up a container running ngin…   2                                       [OK]
    ansibleplaybookbundle/nginx-apb   An APB to deploy NGINX                          1                                       [OK]
    centos/nginx-110-centos7          Platform for running nginx 1.10 or building …   0                                       
    wodby/nginx                       Generic nginx                                   0                                       [OK]
    ubuntu@VM-101-242-ubuntu:~$ 
    

    搜索收藏数大于10的nginx镜像,并显示详细信息

    ubuntu@VM-101-242-ubuntu:~$ sudo docker search --stars=10 --no-trunc nginx
    Flag --stars has been deprecated, use --filter=stars=3 instead
    NAME                          DESCRIPTION                                                                                            STARS               OFFICIAL            AUTOMATED
    nginx                         Official build of Nginx.                                                                               12438               [OK]                
    jwilder/nginx-proxy           Automated Nginx reverse proxy for docker containers                                                    1713                                    [OK]
    richarvey/nginx-php-fpm       Container running Nginx + PHP-FPM capable of pulling application code from git                         750                                     [OK]
    linuxserver/nginx             An Nginx container, brought to you by LinuxServer.io.                                                  84                                      
    bitnami/nginx                 Bitnami nginx Docker Image                                                                             75                                      [OK]
    tiangolo/nginx-rtmp           Docker image with Nginx using the nginx-rtmp-module module for live multimedia (video) streaming.      60                                      [OK]
    nginxdemos/hello              NGINX webserver that serves a simple page containing its hostname, IP address and port ...             35                                      [OK]
    jc21/nginx-proxy-manager      Docker container for managing Nginx proxy hosts with a simple, powerful interface                      34                                      
    nginx/unit                    NGINX Unit is a dynamic web and application server designed to run applications in various languages   33                                      
    jlesage/nginx-proxy-manager   Docker container for Nginx Proxy Manager                                                               31                                      [OK]
    nginx/nginx-ingress           NGINX Ingress Controller for Kubernetes                                                                22                                      
    privatebin/nginx-fpm-alpine   PrivateBin running on an Nginx, php-fpm & Alpine Linux stack                                           19                                      [OK]
    schmunk42/nginx-redirect      A very simple container to redirect HTTP traffic to another server, based on nginx                     17                                      [OK]
    centos/nginx-18-centos7       Platform for running nginx 1.8 or building nginx-based application                                     12                                      
    nginxinc/nginx-unprivileged   Unprivileged NGINX Dockerfiles                                                                         12                                      
    blacklabelops/nginx           Dockerized Nginx Reverse Proxy Server.                                                                 12                                      [OK]
    centos/nginx-112-centos7      Platform for running nginx 1.12 or building nginx-based application                                    11                                      
    ubuntu@VM-101-242-ubuntu:~$ 
    

    使用条件查找收藏数大于10的nginx镜像

    ubuntu@VM-101-242-ubuntu:~$ sudo docker search --filter stars=3 nginx
    NAME                              DESCRIPTION                                     STARS               OFFICIAL            AUTOMATED
    nginx                             Official build of Nginx.                        12438               [OK]                
    jwilder/nginx-proxy               Automated Nginx reverse proxy for docker con…   1713                                    [OK]
    richarvey/nginx-php-fpm           Container running Nginx + PHP-FPM capable of…   750                                     [OK]
    linuxserver/nginx                 An Nginx container, brought to you by LinuxS…   84                                      
    bitnami/nginx                     Bitnami nginx Docker Image                      75                                      [OK]
    tiangolo/nginx-rtmp               Docker image with Nginx using the nginx-rtmp…   60                                      [OK]
    nginxdemos/hello                  NGINX webserver that serves a simple page co…   35                                      [OK]
    jc21/nginx-proxy-manager          Docker container for managing Nginx proxy ho…   34                                      
    nginx/unit                        NGINX Unit is a dynamic web and application …   33                                      
    jlesage/nginx-proxy-manager       Docker container for Nginx Proxy Manager        31                                      [OK]
    nginx/nginx-ingress               NGINX Ingress Controller for Kubernetes         22                                      
    privatebin/nginx-fpm-alpine       PrivateBin running on an Nginx, php-fpm & Al…   19                                      [OK]
    schmunk42/nginx-redirect          A very simple container to redirect HTTP tra…   17                                      [OK]
    centos/nginx-18-centos7           Platform for running nginx 1.8 or building n…   12                                      
    blacklabelops/nginx               Dockerized Nginx Reverse Proxy Server.          12                                      [OK]
    nginxinc/nginx-unprivileged       Unprivileged NGINX Dockerfiles                  12                                      
    centos/nginx-112-centos7          Platform for running nginx 1.12 or building …   11                                      
    nginx/nginx-prometheus-exporter   NGINX Prometheus Exporter                       9                                       
    sophos/nginx-vts-exporter         Simple server that scrapes Nginx vts stats a…   6                                       [OK]
    mailu/nginx                       Mailu nginx frontend                            5                                       [OK]
    1science/nginx                    Nginx Docker images that include Consul Temp…   5                                       [OK]
    ubuntu@VM-101-242-ubuntu:~$ 
    

    pull

    从docker仓库下载镜像到本地

    语法

    docker pull 镜像名称:tag(版本)
    

    参数说明

    -a, --all-tags:下载所有版本的镜像
    -q, --quiet:不显示详细信息
    --disable-content-trust:不对下载的镜像进行校验
    --platform:指定下载镜像服务器的平台信息
    

    示例

    下载nginx默认版本的docker镜像

    ubuntu@VM-35-226-ubuntu:~$ sudo docker pull nginx:latest
    latest: Pulling from library/nginx
    8ec398bc0356: Pull complete 
    465560073b6f: Pull complete 
    f473f9fd0a8c: Pull complete 
    Digest: sha256:b2d89d0a210398b4d1120b3e3a7672c16a4ba09c2c4a0395f18b9f7999b768f2
    Status: Downloaded newer image for nginx:latest
    docker.io/library/nginx:latest
    ubuntu@VM-35-226-ubuntu:~$ sudo docker images			//查看本地所有镜像
    REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
    nginx               latest              f7bb5701a33c        9 days ago          126MB
    ubuntu@VM-35-226-ubuntu:~$ 
    

    push

    推送指定镜像到docker镜像服务器

    语法

    docker push 本地镜像:tag(版本)
    

    参数说明

    --disable-content-trust:不对镜像进行校验
    

    示例

    docker push blxt:5000/blxt/nginx 上传nginx到私有仓库
    

    images

    查看本地所有docker镜像

    语法

    docker images 参数 镜像:tag(版本)
    

    参数说明

    -a, --all:列出本地所有镜像
    --digests:显示镜像简单信息说明
    -f, --filter:根据相关条件过滤输出
    --format:使用Go模板进行漂亮的打印搜索
    --no-trunc:显示镜像的完整信息
    -q, --quiet:只显示镜像ID
    

    示例

    查看本地所有镜像

    ubuntu@VM-35-226-ubuntu:~$ sudo docker images -a
    REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
    nginx               latest              f7bb5701a33c        9 days ago          126MB
    ubuntu@VM-35-226-ubuntu:~$ 
    

    查看镜像详细信息

    ubuntu@VM-35-226-ubuntu:~$ sudo docker images --no-trunc nginx:latest
    REPOSITORY          TAG                 IMAGE ID                                                                  CREATED             SIZE
    nginx               latest              sha256:f7bb5701a33c0e572ed06ca554edca1bee96cbbc1f76f3b01c985de7e19d0657   9 days ago          126MB
    ubuntu@VM-35-226-ubuntu:~$ 
    

    查看镜像ID

    ubuntu@VM-35-226-ubuntu:~$ sudo docker images -q nginx:latest
    f7bb5701a33c
    ubuntu@VM-35-226-ubuntu:~$ 
    

    run

    创建容器,并运行

    语法

    docker run 参数 镜像:tag(版本)
    

    参数说明

    -a, --attach:	指定标准输入输出内容类型,可选 STDIN/STDOUT/STDERR 三项
    -d:		容器将在后台运行
    -t:		给容器分配tty终端
    --sig-proxy=true:	将所有接收到的信号代理到进程(仅限非TTY模式)
    -i:		以交互模式运行容器
    --name:		给容器设置名称
    --dns:		为容器设置自定义dns服务器
    --dns-search:	指定DNS搜索域名
    --network:		为容器指定网卡
    --network-alias:		为容器的网卡设置别名
    --add-host:		在容器的hosts文件中添加记录
    --mac-address:		设置容器的MAC地址
    --ip:		设置容器的IPV4地址
    --ip6:		设置容器IPV6地址
    --link-local-ip:		设置一个或多个容器的以太网设备的链接本地IPv4/IPv6地址
    --rm:		容器退出时自动删除容器
    -m, --memory:		设置容器内存大小,最小4M
    --memory-swap:		设置容器的总内存大小,包含交换分区
    -c, --cpu-shares:		设置cpu
    --cpus:		设置cpu数量
    --entrypoint:	在容器运行时执行的默认命令
    --expose:		设置端口映射
    -P:			将容器中所有端口映射到宿主机
    -p:			将容器的某个端口映射到宿主机
    --link:		将链接添加到另外一个容器
    -v, --volume:		将宿主机的文件挂载到容器中
    --volumes-from:		将宿主机的所有文件系统挂载到容器
    -u,--user:		设置所使用的用户名或UID以及指定命令的可选组名或GID
    -h:		指定容器主机名称
    -e:		设置环境变量
    --env-file:		从指定文件中导入变量
    参数还有很多,这里就不一一列举了,上面是常用的参数
    

    示例

    后台运行nginx容器,并取名为nginx

    ubuntu@VM-35-226-ubuntu:~$ sudo docker run -d --name nginx nginx:latest
    06a913a442f9729e284b1a0628e5ce46561f81c6e8777f91d184f10de079cb55
    ubuntu@VM-35-226-ubuntu:~$ sudo docker ps -a			//查看所有容器
    CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
    06a913a442f9        nginx:latest        "nginx -g 'daemon of…"   18 seconds ago      Up 15 seconds       80/tcp              nginx
    ubuntu@VM-35-226-ubuntu:~$ 
    

    后台运行nginx容器,并映射容器80端口到宿主机的80端口

    ubuntu@VM-35-226-ubuntu:~$ sudo docker run -d --name nginx -p 80:80 nginx:latest
    c026ee7e0c9477c15ac42965ed19af92b6017e7bcce079fd450f8cee5f2d9de6
    ubuntu@VM-35-226-ubuntu:~$ sudo docker ps -a			//查看所有容器
    CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                NAMES
    c026ee7e0c94        nginx:latest        "nginx -g 'daemon of…"   8 seconds ago       Up 6 seconds        0.0.0.0:80->80/tcp   nginx
    ubuntu@VM-35-226-ubuntu:~$ sudo curl 127.0.0.1		//访问容器种nginx页面
    <!DOCTYPE html>
    <html>
    <head>
    <title>Welcome to nginx!</title>
    <style>
        body {
             35em;
            margin: 0 auto;
            font-family: Tahoma, Verdana, Arial, sans-serif;
        }
    </style>
    </head>
    <body>
    <h1>Welcome to nginx!</h1>
    <p>If you see this page, the nginx web server is successfully installed and
    working. Further configuration is required.</p>
    
    <p>For online documentation and support please refer to
    <a href="http://nginx.org/">nginx.org</a>.<br/>
    Commercial support is available at
    <a href="http://nginx.com/">nginx.com</a>.</p>
    
    <p><em>Thank you for using nginx.</em></p>
    </body>
    </html>
    ubuntu@VM-35-226-ubuntu:~$ 
    

    创建nginx容器,并进行交互式启动

    ubuntu@VM-35-226-ubuntu:~$ sudo docker run -it --name nginx nginx:latest /bin/bash
    root@4a518f4a2a01:/# 
    

    history

    查看镜像形成过程

    语法

    docker history 参数 镜像:tag(版本)
    

    参数

    --format:使用Go模板进行漂亮的打印搜索
    -H, --human:以可读格式打印大小和日期(默认为true)
    --no-trunc:显示形成详细信息
    -q, --quiet:只显示镜像ID
    

    示例

    默认使用方法

    ubuntu@VM-35-226-ubuntu:~$ sudo docker history nginx:latest
    IMAGE               CREATED             CREATED BY                                      SIZE                COMMENT
    f7bb5701a33c        9 days ago          /bin/sh -c #(nop)  CMD ["nginx" "-g" "daemon…   0B                  
    <missing>           9 days ago          /bin/sh -c #(nop)  STOPSIGNAL SIGTERM           0B                  
    <missing>           9 days ago          /bin/sh -c #(nop)  EXPOSE 80                    0B                  
    <missing>           9 days ago          /bin/sh -c ln -sf /dev/stdout /var/log/nginx…   22B                 
    <missing>           9 days ago          /bin/sh -c set -x     && addgroup --system -…   57.1MB              
    <missing>           9 days ago          /bin/sh -c #(nop)  ENV PKG_RELEASE=1~buster     0B                  
    <missing>           9 days ago          /bin/sh -c #(nop)  ENV NJS_VERSION=0.3.7        0B                  
    <missing>           9 days ago          /bin/sh -c #(nop)  ENV NGINX_VERSION=1.17.6     0B                  
    <missing>           9 days ago          /bin/sh -c #(nop)  LABEL maintainer=NGINX Do…   0B                  
    <missing>           10 days ago         /bin/sh -c #(nop)  CMD ["bash"]                 0B                  
    <missing>           10 days ago         /bin/sh -c #(nop) ADD file:04caaf303199c81ff…   69.2MB              
    ubuntu@VM-35-226-ubuntu:~$ 
    

    只打印镜像ID

    ubuntu@VM-35-226-ubuntu:~$ sudo docker history -q nginx:latest
    f7bb5701a33c
    <missing>
    <missing>
    <missing>
    <missing>
    <missing>
    <missing>
    <missing>
    <missing>
    <missing>
    <missing>
    ubuntu@VM-35-226-ubuntu:~$ 
    

    显示镜像形成的详细信息

    ubuntu@VM-35-226-ubuntu:~$ sudo docker history --no-trunc nginx
    IMAGE                                                                     CREATED             CREATED BY                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      SIZE                COMMENT
    sha256:f7bb5701a33c0e572ed06ca554edca1bee96cbbc1f76f3b01c985de7e19d0657   9 days ago          /bin/sh -c #(nop)  CMD ["nginx" "-g" "daemon off;"]                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             0B                  
    <missing>                                                                 9 days ago          /bin/sh -c #(nop)  STOPSIGNAL SIGTERM                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           0B                  
    <missing>                                                                 9 days ago          /bin/sh -c #(nop)  EXPOSE 80                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    0B                  
    <missing>                                                                 9 days ago          /bin/sh -c ln -sf /dev/stdout /var/log/nginx/access.log     && ln -sf /dev/stderr /var/log/nginx/error.log                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      22B                 
    <missing>                                                                 9 days ago          /bin/sh -c set -x     && addgroup --system --gid 101 nginx     && adduser --system --disabled-login --ingroup nginx --no-create-home --home /nonexistent --gecos "nginx user" --shell /bin/false --uid 101 nginx     && apt-get update     && apt-get install --no-install-recommends --no-install-suggests -y gnupg1 ca-certificates     &&     NGINX_GPGKEY=573BFD6B3D8FBC641079A6ABABF5BD827BD9BF62;     found='';     for server in         ha.pool.sks-keyservers.net         hkp://keyserver.ubuntu.com:80         hkp://p80.pool.sks-keyservers.net:80         pgp.mit.edu     ; do         echo "Fetching GPG key $NGINX_GPGKEY from $server";         apt-key adv --keyserver "$server" --keyserver-options timeout=10 --recv-keys "$NGINX_GPGKEY" && found=yes && break;     done;     test -z "$found" && echo >&2 "error: failed to fetch GPG key $NGINX_GPGKEY" && exit 1;     apt-get remove --purge --auto-remove -y gnupg1 && rm -rf /var/lib/apt/lists/*     && dpkgArch="$(dpkg --print-architecture)"     && nginxPackages="         nginx=${NGINX_VERSION}-${PKG_RELEASE}         nginx-module-xslt=${NGINX_VERSION}-${PKG_RELEASE}         nginx-module-geoip=${NGINX_VERSION}-${PKG_RELEASE}         nginx-module-image-filter=${NGINX_VERSION}-${PKG_RELEASE}         nginx-module-njs=${NGINX_VERSION}.${NJS_VERSION}-${PKG_RELEASE}     "     && case "$dpkgArch" in         amd64|i386)             echo "deb https://nginx.org/packages/mainline/debian/ buster nginx" >> /etc/apt/sources.list.d/nginx.list             && apt-get update             ;;         *)             echo "deb-src https://nginx.org/packages/mainline/debian/ buster nginx" >> /etc/apt/sources.list.d/nginx.list                         && tempDir="$(mktemp -d)"             && chmod 777 "$tempDir"                         && savedAptMark="$(apt-mark showmanual)"                         && apt-get update             && apt-get build-dep -y $nginxPackages             && (                 cd "$tempDir"                 && DEB_BUILD_OPTIONS="nocheck parallel=$(nproc)"                     apt-get source --compile $nginxPackages             )                         && apt-mark showmanual | xargs apt-mark auto > /dev/null             && { [ -z "$savedAptMark" ] || apt-mark manual $savedAptMark; }                         && ls -lAFh "$tempDir"             && ( cd "$tempDir" && dpkg-scanpackages . > Packages )             && grep '^Package: ' "$tempDir/Packages"             && echo "deb [ trusted=yes ] file://$tempDir ./" > /etc/apt/sources.list.d/temp.list             && apt-get -o Acquire::GzipIndexes=false update             ;;     esac         && apt-get install --no-install-recommends --no-install-suggests -y                         $nginxPackages                         gettext-base     && apt-get remove --purge --auto-remove -y ca-certificates && rm -rf /var/lib/apt/lists/* /etc/apt/sources.list.d/nginx.list         && if [ -n "$tempDir" ]; then         apt-get purge -y --auto-remove         && rm -rf "$tempDir" /etc/apt/sources.list.d/temp.list;     fi   57.1MB              
    <missing>                                                                 9 days ago          /bin/sh -c #(nop)  ENV PKG_RELEASE=1~buster                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     0B                  
    <missing>                                                                 9 days ago          /bin/sh -c #(nop)  ENV NJS_VERSION=0.3.7                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        0B                  
    <missing>                                                                 9 days ago          /bin/sh -c #(nop)  ENV NGINX_VERSION=1.17.6                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           ubuntu@VM-35-226-ubuntu:~$ sudo docker history --no-trunc nginx:latest
    IMAGE                                                                     CREATED             CREATED BY                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      SIZE                COMMENT
    sha256:f7bb5701a33c0e572ed06ca554edca1bee96cbbc1f76f3b01c985de7e19d0657   9 days ago          /bin/sh -c #(nop)  CMD ["nginx" "-g" "daemon off;"]                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             0B                  
    <missing>                                                                 9 days ago          /bin/sh -c #(nop)  STOPSIGNAL SIGTERM                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           0B                  
    <missing>                                                                 9 days ago          /bin/sh -c #(nop)  EXPOSE 80                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    0B                  
    <missing>                                                                 9 days ago          /bin/sh -c ln -sf /dev/stdout /var/log/nginx/access.log     && ln -sf /dev/stderr /var/log/nginx/error.log                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      22B                 
    <missing>                                                                 9 days ago          /bin/sh -c set -x     && addgroup --system --gid 101 nginx     && adduser --system --disabled-login --ingroup nginx --no-create-home --home /nonexistent --gecos "nginx user" --shell /bin/false --uid 101 nginx     && apt-get update     && apt-get install --no-install-recommends --no-install-suggests -y gnupg1 ca-certificates     &&     NGINX_GPGKEY=573BFD6B3D8FBC641079A6ABABF5BD827BD9BF62;     found='';     for server in         ha.pool.sks-keyservers.net         hkp://keyserver.ubuntu.com:80         hkp://p80.pool.sks-keyservers.net:80         pgp.mit.edu     ; do         echo "Fetching GPG key $NGINX_GPGKEY from $server";         apt-key adv --keyserver "$server" --keyserver-options timeout=10 --recv-keys "$NGINX_GPGKEY" && found=yes && break;     done;     test -z "$found" && echo >&2 "error: failed to fetch GPG key $NGINX_GPGKEY" && exit 1;     apt-get remove --purge --auto-remove -y gnupg1 && rm -rf /var/lib/apt/lists/*     && dpkgArch="$(dpkg --print-architecture)"     && nginxPackages="         nginx=${NGINX_VERSION}-${PKG_RELEASE}         nginx-module-xslt=${NGINX_VERSION}-${PKG_RELEASE}         nginx-module-geoip=${NGINX_VERSION}-${PKG_RELEASE}         nginx-module-image-filter=${NGINX_VERSION}-${PKG_RELEASE}         nginx-module-njs=${NGINX_VERSION}.${NJS_VERSION}-${PKG_RELEASE}     "     && case "$dpkgArch" in         amd64|i386)             echo "deb https://nginx.org/packages/mainline/debian/ buster nginx" >> /etc/apt/sources.list.d/nginx.list             && apt-get update             ;;         *)             echo "deb-src https://nginx.org/packages/mainline/debian/ buster nginx" >> /etc/apt/sources.list.d/nginx.list                         && tempDir="$(mktemp -d)"             && chmod 777 "$tempDir"                         && savedAptMark="$(apt-mark showmanual)"                         && apt-get update             && apt-get build-dep -y $nginxPackages             && (                 cd "$tempDir"                 && DEB_BUILD_OPTIONS="nocheck parallel=$(nproc)"                     apt-get source --compile $nginxPackages             )                         && apt-mark showmanual | xargs apt-mark auto > /dev/null             && { [ -z "$savedAptMark" ] || apt-mark manual $savedAptMark; }                         && ls -lAFh "$tempDir"             && ( cd "$tempDir" && dpkg-scanpackages . > Packages )             && grep '^Package: ' "$tempDir/Packages"             && echo "deb [ trusted=yes ] file://$tempDir ./" > /etc/apt/sources.list.d/temp.list             && apt-get -o Acquire::GzipIndexes=false update             ;;     esac         && apt-get install --no-install-recommends --no-install-suggests -y                         $nginxPackages                         gettext-base     && apt-get remove --purge --auto-remove -y ca-certificates && rm -rf /var/lib/apt/lists/* /etc/apt/sources.list.d/nginx.list         && if [ -n "$tempDir" ]; then         apt-get purge -y --auto-remove         && rm -rf "$tempDir" /etc/apt/sources.list.d/temp.list;     fi   57.1MB              
    <missing>                                                                 9 days ago          /bin/sh -c #(nop)  ENV PKG_RELEASE=1~buster                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     0B                  
    <missing>                                                                 9 days ago          /bin/sh -c #(nop)  ENV NJS_VERSION=0.3.7                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        0B                  
    <missing>                                                                 9 days ago          /bin/sh -c #(nop)  ENV NGINX_VERSION=1.17.6                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     0B                  
    <missing>                                                                 9 days ago          /bin/sh -c #(nop)  LABEL maintainer=NGINX Docker Maintainers <docker-maint@nginx.com>                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           0B                  
    <missing>                                                                 10 days ago         /bin/sh -c #(nop)  CMD ["bash"]                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 0B                  
    <missing>                                                                 10 days ago         /bin/sh -c #(nop) ADD file:04caaf303199c81ff1a94e2e39d5096f9d02b73294b82758e5bc6e23aff94272 in /                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                69.2MB              
    ubuntu@VM-35-226-ubuntu:~$ 
    

    start

    启动容器

    语法

    docker start 参数 容器ID/容器名称
    

    参数说明

    -a, --attach:附加STDOUT/STDERR和转发信号
    --checkpoint:从快照进行还原 
    --checkpoint-dir:使用自定义检查点存储目录
    --detach-keys:重写用于分离容器的键序列
    -i, --interactive:附加容器的STDIN
    

    示例

    启动使用create创建的nginx容器

    ubuntu@VM-35-226-ubuntu:~$ sudo docker ps -a			//查看所有容器
    CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
    8b9053318855        nginx:latest        "nginx -g 'daemon of…"   9 seconds ago       Created                                 admiring_rhodes
    ubuntu@VM-35-226-ubuntu:~$ sudo docker start 8b9053318855 	
    8b9053318855
    ubuntu@VM-35-226-ubuntu:~$ sudo docker ps 		//查看已运行容器
    CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
    8b9053318855        nginx:latest        "nginx -g 'daemon of…"   40 seconds ago      Up 7 seconds        80/tcp              admiring_rhodes
    ubuntu@VM-35-226-ubuntu:~$ 
    

    stop

    关闭容器

    语法

    docker stop 参数 容器ID/容器名称
    

    参数说明

    --time , -t:在多少时间后关闭容器
    

    示例

    默认使用方法

    ubuntu@VM-121-163-ubuntu:~$ sudo docker run -d --name nginx nginx:latest		//创建并后台启动nginx容器
    4c1e4ab3cfdcb7c5c55052238525e79bb82c0b5cf70b18420520ec2884669272
    ubuntu@VM-121-163-ubuntu:~$ sudo docker ps -a		//查看所有容器
    CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
    4c1e4ab3cfdc        nginx:latest        "nginx -g 'daemon of…"   7 seconds ago       Up 5 seconds        80/tcp              nginx
    ubuntu@VM-121-163-ubuntu:~$ sudo docker stop nginx
    nginx
    ubuntu@VM-121-163-ubuntu:~$ sudo docker ps -a		//查看所有容器
    CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                     PORTS               NAMES
    4c1e4ab3cfdc        nginx:latest        "nginx -g 'daemon of…"   26 seconds ago      Exited (0) 5 seconds ago                       nginx
    ubuntu@VM-121-163-ubuntu:~$ 
    

    build

    通过dockerfile制作镜像

    语法

    docker build 参数 路径
    

    参数说明

    --build-arg:设置创建时的变量
    --cgroup-parent:容器的可选父cgroup
    --compress:使用gzip压缩构建上下文
    --cpu-shares :设置 cpu 使用权重
    --cpu-period :限制 CPU CFS周期
    --cpu-quota :限制 CPU CFS配额
    --cpuset-cpus :指定使用的CPU id
    --cpuset-mems :指定使用的内存 id
    --disable-content-trus:跳过镜像验证
    --file , -f:指定dockerfile的名称,默认为dockerfile
    --force-rm:始终移除中间容器
    --iidfile:将镜像ID写入文件
    --isolation:使用容器隔离
    --label:为镜像设置元数据
    --memory , -m:设置内存限制
    --memory-swap:设置总内存,包含交换内存
    --network:设置镜像网络信息
    --no-cache:构建镜像不使用缓存
    --output , -o:设置镜像生成位置
    --pull:始终尝试提取图像的较新版本
    --quiet , -q:	禁止生成输出并成功打印图像ID
    --rm:成功构建后删除中间容器
    --tag, -t: 镜像的名字及标签
    --ulimit :Ulimit配置
    --shm-size :设置/dev/shm的大小,默认值是64M
    

    示例

    使用当前目录下的dockerfile制作nginx:V1镜像

    docker build -t nginx:V1 .
    

    创建nginx:V2镜像,使用非当前目录下的dockerfile

    docker build -t nginx:V2 -f /opt/dockerfile
    

    添加其他主机到容器文件中

    docker build --add-host=docker:1.1.1.1 .
    

    使用参数–squash创建名为test:1的镜像

    docker build --squash -t test:1  .
    

    attach

    当前shell连接运行容器

    语法

    docker attach 参数 容器ID/容器名称
    

    参数

    --detach-keys:	覆盖分离容器的键序列
    --no-stdin:不要附上STDIN
    --sig-proxy:代理所有接收到的信号到进程
    

    示例

    默认使用方法

    ubuntu@VM-121-163-ubuntu:~$ sudo docker run -d --name ubuntu ubuntu /usr/bin/top -b		//创建并后台启动ubuntu容器
    98c3f0964ad3a38e360867941a5510cd77e164a835be5b9b69aa47d3d5ceed17
    ubuntu@VM-121-163-ubuntu:~$ sudo docker attach ubuntu
    top - 08:24:58 up  1:12,  0 users,  load average: 0.04, 0.06, 0.01
    Tasks:   1 total,   1 running,   0 sleeping,   0 stopped,   0 zombie
    %Cpu(s):  2.3 us,  1.7 sy,  0.0 ni, 96.0 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
    KiB Mem :   885080 total,   149760 free,   231524 used,   503796 buff/cache
    KiB Swap:        0 total,        0 free,        0 used.   499100 avail Mem 
    
      PID USER      PR  NI    VIRT    RES    SHR S %CPU %MEM     TIME+ COMMAND
        1 root      20   0   36480   3100   2748 R  0.0  0.4   0:00.19 top
    
    top - 08:25:01 up  1:12,  0 users,  load average: 0.04, 0.06, 0.01
    Tasks:   1 total,   1 running,   0 sleeping,   0 stopped,   0 zombie
    %Cpu(s):  1.0 us,  0.7 sy,  0.0 ni, 98.0 id,  0.3 wa,  0.0 hi,  0.0 si,  0.0 st
    KiB Mem :   885080 total,   151252 free,   230048 used,   503780 buff/cache
    KiB Swap:        0 total,        0 free,        0 used.   500588 avail Mem 
    
      PID USER      PR  NI    VIRT    RES    SHR S %CPU %MEM     TIME+ COMMAND
        1 root      20   0   36480   3100   2748 R  0.0  0.4   0:00.19 top
    
    
    top - 08:25:04 up  1:13,  0 users,  load average: 0.04, 0.06, 0.01
    Tasks:   1 total,   1 running,   0 sleeping,   0 stopped,   0 zombie
    %Cpu(s):  1.3 us,  1.0 sy,  0.0 ni, 97.7 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
    KiB Mem :   885080 total,   151264 free,   230016 used,   503800 buff/cache
    KiB Swap:        0 total,        0 free,        0 used.   500616 avail Mem 
    
      PID USER      PR  NI    VIRT    RES    SHR S %CPU %MEM     TIME+ COMMAND
        1 root      20   0   36480   3100   2748 R  0.0  0.4   0:00.19 top
    

    您可以看到该bash 过程返回的退出代码也由docker attach命令返回给其调用者

    ubuntu@VM-121-163-ubuntu:~$ sudo docker run --name ubuntu -d -it ubuntu		//创建并后台运行ubuntu容器
    9ac7802feb2b77bb905bc9f4c5ca9782e6800fc995b1f2b39da78239d86a7405
    ubuntu@VM-121-163-ubuntu:~$ sudo docker attach ubuntu
    root@9ac7802feb2b:/# 
    root@9ac7802feb2b:/# 
    

    commit

    保存当前容器为镜像/快照

    语法

    docker commit 容器名称/容器ID 镜像名:tag(版本)
    

    示例

    ubuntu@VM-121-163-ubuntu:~$ sudo docker images		//查看镜像
    REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
    nginx               latest              f7bb5701a33c        9 days ago          126MB
    ubuntu              latest              549b9b86cb8d        2 weeks ago         64.2MB
    ubuntu@VM-121-163-ubuntu:~$ sudo docker run -itd --name nginx nginx:latest		//创建容器
    9566c54f3883b0a3b2f92dc94eb8520772d5d05826d7025be23212768f4d7967
    ubuntu@VM-121-163-ubuntu:~$ sudo docker ps -a		//查看容器状态
    CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
    9566c54f3883        nginx:latest        "nginx -g 'daemon of…"   11 seconds ago      Up 9 seconds        80/tcp              nginx
    ubuntu@VM-121-163-ubuntu:~$ sudo docker commit nginx nginx:V1		//生成快照
    sha256:e091997549ff02cf6309d6146b7626c6efef6dd9ce598c632f6bf0f4d090978c
    ubuntu@VM-121-163-ubuntu:~$ sudo docker images		//查看镜像
    REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
    nginx               V1                  e091997549ff        22 seconds ago      126MB
    nginx               latest              f7bb5701a33c        9 days ago          126MB
    ubuntu              latest              549b9b86cb8d        2 weeks ago         64.2MB
    ubuntu@VM-121-163-ubuntu:~$ 
    

    cp

    容器与宿主机互相复制文件

    语法

    docker cp 容器ID/容器名:文件路径 宿主机路径		复制容器中文件到宿主机
    docker cp 宿主机路径 容器ID/容器名:文件存储路径		复制宿主机文件到容器
    

    参数说明

     -a, --archive:复制文件同时复制文件用户信息
     -L, --follow-link:保持源目标中的链接
    

    示例

    复制宿主机文件到容器的根目录中

    root@test:/jia/test# docker run -d --name nginx nginx:latest	//创建并运行nginx容器
    33b6339003ef41fd62b909fd79e9056d53d944b3172f0f02fc717fd64a2648e3
    root@test:/jia/test# docker exec nginx ls /			//列出容器根目录
    bin
    boot
    dev
    etc
    home
    lib
    lib64
    media
    mnt
    opt
    proc
    root
    run
    sbin
    srv
    sys
    tmp
    usr
    var
    root@test:/jia/test# docker cp test nginx:/		//复制文件到容器
    root@test:/jia/test# docker exec nginx ls /		//列出容器根目录
    bin
    boot
    dev
    etc
    home
    lib
    lib64
    media
    mnt
    opt
    proc
    root
    run
    sbin
    srv
    sys
    test
    tmp
    usr
    var
    

    从容器中复制文件到宿主目录

    root@test:/jia/test# ls 		//列出目录
    root@test:/jia/test# docker cp nginx:/test .		//复制容器中文件到当前目录
    root@test:/jia/test# ls			//列出目录
    test
    root@test:/jia/test# 
    

    create

    创建容器但不启动容器

    语法

    docker create 参数 镜像:tag(版本)
    

    参数说明

    --add-host:			在容器的hosts文件中添加记录	
    -a, --attach:		指定标准输入输出内容类型,可选 STDIN/STDOUT/STDERR 三项
    --cap-add:		添加linux功能
    --cap-drop:		删除linux功能
    --cidfile:		将容器ID写入文件
    -c, --cpu-shares:		设置cpu
    --device:		将主机设备添加到容器
    --disable-content-trus:		不对镜像进行验证
    --dns:		为容器设置自定义dns服务器‘
    --dns-opt:		设置DNS选项
    --dns-search:		指定DNS搜索域名
    --domainname:		设置容器域名
    --env , -e:		设置环境变量
    --env-file:		读取变量文件进行设置容器变量
    --expose:		设置端口映射
    --hostname , -h:		设置容器主机名
    --init:		在容器内运行一个初始化程序,以转发信号并获取进程
    --ip:		设置容器的IPV4地址
    --ip6:		设置容器IPV6地址
    --isolation:		使用隔离技术
    --link:		将链接添加到宿主机,也就是创建软连接
    --link-local-ip:		设置容器本地地址
    --mac-address:		设置容器MAC地址
    --memory , -m:		设置内存
    --memory-swap:		设置总内存大小,包含交换内存
    --mount:		挂载文件系统
    --name:		设置容器名称
    --net:		将容器链接网络
    --net-alias:		为容器网络起别名
    --network:		容器链接到网络
    --network-alias:		为容器网络起别名
    --publish , -p:		将容器端口映射到宿主机
    --publish-all , -P:		将容器所有端口映射到宿主机
    --read-only:		将容器的根文件系统挂载为只读
    --restart:		容器退出时重新启动策略以应用
    --rm:		退出时自动删除容器
    --stop-signal:		停止容器的信号
    --tty , -t:		分配终端
    --user , -u:		设置运行容器的用户
    --workdir , -w:		设置容器的工作目录
    参数还有很多,这里就不一一列举了,上面是常用的参数
    

    示例

    创建nginx容器并启动

    root@test:/jia/test# docker create -t -i  nginx:latest /bin/bash		//创建容器
    6f2cea61b80c117fe877589073b5f259a57623bd511a89330c72c825c0cbe4fd		
    root@test:/jia/test# docker ps -a		//查看容器
    CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
    6f2cea61b80c        nginx:latest        "/bin/bash"         12 seconds ago      Created                                 funny_gould
    root@test:/jia/test# docker start -a -i 6f2cea61b80c		//启动容器
    root@6f2cea61b80c:/# ls		//列出容器当前目录
    bin  boot  dev  etc  home  lib  lib64  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var
    root@6f2cea61b80c:/# 
    

    创建名为nginx的容器并进行启动

    root@test:/jia/test# docker create --name nginx -t -i nginx:latest /bin/bash		//创建容器
    5487d362e25bfcd39c70754972eb964121ba8408f3f97c89cac930208882e745
    root@test:/jia/test# docker ps -a		//查看容器
    CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
    5487d362e25b        nginx:latest        "/bin/bash"         5 seconds ago       Created                                 nginx
    root@test:/jia/test# docker start -a -i nginx		//启动容器并进行链接
    root@5487d362e25b:/# ls
    bin  boot  dev  etc  home  lib  lib64  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var
    root@5487d362e25b:/# 
    

    创建nginx容器并映射端口

    root@test:/jia/test# docker create --name nginx -p 80:80-it nginx:latest /bin/bash	//创建容器
    4cc06988f4284f01281a338b914aff247a16969e33177088673a281d848f99d4
    root@test:/jia/test# docker ps -a		//查看容器
    CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                     PORTS               NAMES
    ad7eb9da0f48        nginx:latest        "/bin/bash"         2 minutes ago       Exited (0) 2 seconds ago                       nginx
    root@test:/jia/test# docker start nginx		//启动容器
    nginx
    root@test:/jia/test# docker ps -a		//查看容器状态
    CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS                  NAMES
    ad7eb9da0f48        nginx:latest        "/bin/bash"         2 minutes ago       Up 5 seconds        0.0.0.0:8080->80/tcp   nginx
    root@test:/jia/test# curl 127.0.0.1
    <!DOCTYPE html>
    <html>
    <head>
    <title>Welcome to nginx!</title>
    <style>
        body {
             35em;
            margin: 0 auto;
            font-family: Tahoma, Verdana, Arial, sans-serif;
        }
    </style>
    </head>
    <body>
    <h1>Welcome to nginx!</h1>
    <p>If you see this page, the nginx web server is successfully installed and
    working. Further configuration is required.</p>
    
    <p>For online documentation and support please refer to
    <a href="http://nginx.org/">nginx.org</a>.<br/>
    Commercial support is available at
    <a href="http://nginx.com/">nginx.com</a>.</p>
    
    <p><em>Thank you for using nginx.</em></p>
    </body>
    </html>
    root@test:/jia/test#
    

    diff

    查看容器改动

    语法

    docker diff 容器
    

    符号说明

    A	添加了文件或目录
    D	文件或目录已删除
    C	文件或目录已更改
    

    示例

    默认使用方法

    root@test:/jia/test# docker diff nginx
    C /root
    A /root/.bash_history
    root@test:/jia/test# 
    

    复制test文件到nginx容器中查看你改动情况

    root@test:/jia/test# docker cp test nginx:/		//复制test文件到容器的根目录
    root@test:/jia/test# docker diff nginx
    A /test
    C /root
    A /root/.bash_history
    root@test:/jia/test# 
    

    exec

    在容器中执行命令

    语法

    docker exec 参数 容器ID/容器名称 命令
    

    参数说明

    --detach , -d:后台运行容器
    --detach-keys:覆盖分离容器的键序列
    --env , -e:设置环境变量
    --interactive , -i:	即使未连接STDIN也保持打开状态
    --privileged:赋予命令扩展权限
    --tty , -t:	分配伪TTY
    --user , -u:指定运行用户
    --workdir , -w:设置工作目录
    

    示例

    使用容器打印“hello word”

    root@test:/jia/test# docker run --name centos -itd centos:latest		//创建并启动容器
    1b75dac7104122e021fb8cc7989c8992eb8502f34713e76ee87e6d6f7796e83c
    root@test:/jia/test# docker ps -a		//查看容器
    CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
    1b75dac71041        centos:latest       "/bin/bash"         4 seconds ago       Up 3 seconds                            centos
    root@test:/jia/test# docker exec centos echo "hello word"		//使用 echo 打印“hello word”
    hello word
    root@test:/jia/test# 
    

    给容器建立交互式shell,并查看容器IP地址

    root@test:/jia/test# docker ps -a		//查看容器
    CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
    1b75dac71041        centos:latest       "/bin/bash"         2 minutes ago       Up 2 minutes                            centos
    root@test:/jia/test# docker exec -it centos /bin/bash		//建立bash交互式shell
    [root@1b75dac71041 /]# ip addr		//查看IP地址
    1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
        link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
        inet 127.0.0.1/8 scope host lo
           valid_lft forever preferred_lft forever
    20: eth0@if21: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default 
        link/ether 02:42:ac:11:00:02 brd ff:ff:ff:ff:ff:ff link-netnsid 0
        inet 172.17.0.2/16 brd 172.17.255.255 scope global eth0
           valid_lft forever preferred_lft forever
    [root@1b75dac71041 /]# 
    

    指定工作目录,并打印路径

    root@test:/jia/test# docker ps -a		//查看容器
    CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
    1b75dac71041        centos:latest       "/bin/bash"         9 minutes ago       Up 9 minutes                            centos
    root@test:/jia/test# docker exec -w /var/www/html centos pwd		//设置工作目录,并进行打印目录
    /var/www/html
    root@test:/jia/test#
    

    login

    登入docker镜像源服务器

    语法

    docker login 参数 服务器地址
    

    参数说明

    -p, --password:指定登录服务器用户的密码
    -u, --username:指定登录服务器用户的账户
    

    示例

    默认登录dockerhub

    root@test:/jia/test# docker login
    Login with your Docker ID to push and pull images from Docker Hub. If you don't have a Docker ID, head over to https://hub.docker.com to create one.
    Username: blxt
    Password: 
    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
    root@test:/jia/test#
    

    指定用户名和密码进行登录

    root@test:/jia/test# docker login -u blxt -p 123456
    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
    root@test:/jia/test# 
    

    登录阿里镜像仓库

    root@jia:/etc# docker login -u '弑天倾城' -p '123456' registry.cn-shenzhen.aliyuncs.com
    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
    root@jia:/etc# 
    

    logout

    退出登录镜像源服务器

    语法

    docker logout 镜像仓库地址
    

    示例

    退出dockerhub用户登录

    root@jia:/etc# docker login -u blxt -p 'JKY@jjj.123'		//登录dockerhub服务器
    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
    root@jia:/etc# docker logout		//退出登录
    Removing login credentials for https://index.docker.io/v1/
    root@jia:/etc# 
    

    退出阿里镜像仓库

    root@jia:/etc# docker login -u '弑天倾城' -p '123456' registry.cn-shenzhen.aliyuncs.com		//登录阿里镜像仓库
    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
    root@jia:/etc# docker logout		//退出失败没有登录dockerhub仓库
    Not logged in to https://index.docker.io/v1/
    root@jia:/etc# docker logout registry.cn-shenzhen.aliyuncs.com
    Removing login credentials for registry.cn-shenzhen.aliyuncs.com		//退出登录阿里镜像仓库
    root@jia:/etc# 
    

    logs

    输出当前容器的日志信息

    语法

    docker logs 参数 容器ID/容器名
    

    参数说明

    --details:查看日志详细信息
    --follow , -f:实时动态的查看日志
    --since:显示生成日志的时间
    --tail:显示日志行数
    --timestamps , -t:显示时间戳
    --until:显示某一时间段的日志信息
    

    示例

    默认查看容器日志

    root@jia:/etc# docker ps -a
    CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
    85c8be92df44        nginx:latest        "nginx -g 'daemon of…"   7 minutes ago       Up 7 minutes        80/tcp              nginx
    root@jia:/etc# docker logs nginx
    172.17.0.1 - - [08/Jan/2020:08:19:02 +0000] "GET / HTTP/1.1" 200 612 "-" "curl/7.58.0" "-"
    172.17.0.1 - - [08/Jan/2020:08:19:04 +0000] "GET / HTTP/1.1" 200 612 "-" "curl/7.58.0" "-"
    172.17.0.1 - - [08/Jan/2020:08:19:07 +0000] "GET / HTTP/1.1" 200 612 "-" "curl/7.58.0" "-"
    root@jia:/etc# 
    

    实时显示nginx容器的日志

    root@jia:/etc# docker run --name nginx -itd nginx:latest		//创建并启动nginx容器
    85c8be92df44960e0973ae2b48907661679d921d0523cb93526351f07850f290
    root@jia:/etc# docker ps -a		//查看容器
    CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
    85c8be92df44        nginx:latest        "nginx -g 'daemon of…"   4 seconds ago       Up 3 seconds        80/tcp              nginx
    root@jia:~# docker inspect nginx | IPAddress	//查看容器的ip地址
    IPAddress: command not found
    root@jia:~# docker inspect nginx | grep IPAddress
                "SecondaryIPAddresses": null,
                "IPAddress": "172.17.0.2",
                        "IPAddress": "172.17.0.2",
    root@jia:~# curl 127.0.0.1		//访问nginx服务
    <!DOCTYPE html>
    <html>
    <head>
    <title>Welcome to nginx!</title>
    <style>
        body {
             35em;
            margin: 0 auto;
            font-family: Tahoma, Verdana, Arial, sans-serif;
        }
    </style>
    </head>
    <body>
    <h1>Welcome to nginx!</h1>
    <p>If you see this page, the nginx web server is successfully installed and
    working. Further configuration is required.</p>
    
    <p>For online documentation and support please refer to
    <a href="http://nginx.org/">nginx.org</a>.<br/>
    Commercial support is available at
    <a href="http://nginx.com/">nginx.com</a>.</p>
    
    <p><em>Thank you for using nginx.</em></p>
    </body>
    </html>
    root@jia:/etc# docker logs -f nginx		//输出日志信息
    172.17.0.1 - - [08/Jan/2020:08:19:02 +0000] "GET / HTTP/1.1" 200 612 "-" "curl/7.58.0" "-"
    172.17.0.1 - - [08/Jan/2020:08:19:04 +0000] "GET / HTTP/1.1" 200 612 "-" "curl/7.58.0" "-"
    172.17.0.1 - - [08/Jan/2020:08:19:07 +0000] "GET / HTTP/1.1" 200 612 "-" "curl/7.58.0" "-"
    

    port

    查看容器的端口映射情况

    语法

    docker port 容器ID/容器名称
    

    示例

    列出nginx容器所有端口映射情况

    root@jia:/etc# docker run --name nginx -itd -P nginx:latest 		//创建并启动容器,映射容器所有端口
    b4e3a51697c2e35b4a17acee93630ac77c6e9fe94df99c70750b60c4f87b535a
    root@jia:/etc# docker ps -a			//查看容器
    CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                   NAMES
    b4e3a51697c2        nginx:latest        "nginx -g 'daemon of…"   4 seconds ago       Up 3 seconds        0.0.0.0:32768->80/tcp   nginx
    root@jia:/etc# docker port nginx		//显示容器端口映射情况
    80/tcp -> 0.0.0.0:32768
    root@jia:/etc# 
    

    ps

    列出容器列表

    语法

    docker ps 参数 
    

    参数说明

    --all , -a:查看所有容器
    --filter , -f:根据相关条件进行过滤
    --format:	使用Go模板打印漂亮的容器
    --last , -n:显示第几个创建的容器
    --latest , -l:显示最新创建的容器
    --no-trunc:显示详细信息
    --quiet , -q:只显示容器ID
    --size , -s:显示容器的总大小
    

    示例

    查看所有容器

    root@jia:~# docker ps -a
    CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                   NAMES
    79b53ebbc86e        centos:latest       "/bin/bash"              5 seconds ago       Created                                     fervent_payne
    b4e3a51697c2        nginx:latest        "nginx -g 'daemon of…"   13 minutes ago      Up 13 minutes       0.0.0.0:32768->80/tcp   nginx
    root@jia:~# 
    

    查看正在运行的容器

    root@jia:~# docker ps
    CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                   NAMES
    b4e3a51697c2        nginx:latest        "nginx -g 'daemon of…"   13 minutes ago      Up 13 minutes       0.0.0.0:32768->80/tcp   nginx
    root@jia:~# 
    

    restart

    重新启动容器

    语法

    docker restart 容器名称/容器ID
    

    参数

    -t:设置重启执行时间
    

    示例

    root@jia:~# docker ps -a
    CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                   NAMES
    b4e3a51697c2        nginx:latest        "nginx -g 'daemon of…"   16 minutes ago      Up 16 minutes       0.0.0.0:32768->80/tcp   nginx
    root@jia:~# docker restart nginx
    nginx
    root@jia:~# 
    

    rm

    删除容器

    语法

    docker rm 参数 容器ID/容器名称
    

    参数

    --force , -f:根据条件进行删除容器,强制删除正在运行的容器
    --link , -l:移除容器间的网络连接,而非容器本身
    -v, --volumes:删除与容器关联的文件系统
    

    示例

    删除nginx容器

    root@jia:~# docker ps -a		//查看容器
    CONTAINER ID        IMAGE               COMMAND                  CREATED              STATUS              PORTS                   NAMES
    c3800e1bda43        centos              "/bin/bash"              About a minute ago   Up About a minute                           centos
    b4e3a51697c2        nginx:latest        "nginx -g 'daemon of…"   22 minutes ago       Up 5 minutes        0.0.0.0:32769->80/tcp   nginx
    root@jia:~# docker rm nginx		//删除容器,但nginx容器正在运行所以无法使用此参数删除
    Error response from daemon: You cannot remove a running container b4e3a51697c2e35b4a17acee93630ac77c6e9fe94df99c70750b60c4f87b535a. Stop the container before attempting removal or force remove
    root@jia:~# docker rm -f nginx		//强制删除nginx容器
    nginx
    root@jia:~# docker ps -a		//查看容器
    CONTAINER ID        IMAGE               COMMAND             CREATED              STATUS              PORTS               NAMES
    c3800e1bda43        centos              "/bin/bash"         About a minute ago   Up About a minute                       centos
    root@jia:~# 
    

    删除所有容器

    root@jia:~# docker ps -a		//查看容器
    CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
    4ee9b7488e32        nginx               "nginx -g 'daemon of…"   26 seconds ago      Up 25 seconds       80/tcp              nginx
    c3800e1bda43        centos              "/bin/bash"              4 minutes ago       Up 4 minutes                            centos
    root@jia:~# docker rm $(docker ps -a -q)		//删除容器失败,因为容器正在运行
    Error response from daemon: You cannot remove a running container 4ee9b7488e323c80d9acaa01a214d00bc3072e2430df5ce7a0560acea0c9587c. Stop the container before attempting removal or force remove
    Error response from daemon: You cannot remove a running container c3800e1bda43416add00ac18fb9c2d15e7d106e302ceef090e04d7fa6133368d. Stop the container before attempting removal or force remove
    root@jia:~# docker rm -f  $(docker ps -a -q)		//删除所有容器
    4ee9b7488e32
    c3800e1bda43
    root@jia:~# docker ps -a
    CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
    root@jia:~# 
    

    rmi

    删除本地镜像

    语法

    docker rmi 参数 镜像名称/镜像ID
    

    参数

    --force , -f:强制删除
    --no-prune:不移除该镜像的过程镜像,默认移除
    

    示例

    默认删除镜像

    root@jia:~# docker images		//查看本地镜像
    REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
    nginx               latest              f7bb5701a33c        10 days ago         126MB
    ubuntu              latest              549b9b86cb8d        2 weeks ago         64.2MB
    centos              latest              0f3e07c0138f        3 months ago        220MB
    root@jia:~# docker rmi  549b9b86cb8d 		//删除ubuntu的镜像
    Untagged: ubuntu:latest
    Untagged: ubuntu@sha256:250cc6f3f3ffc5cdaa9d8f4946ac79821aafb4d3afc93928f0de9336eba21aa4
    Deleted: sha256:549b9b86cb8d75a2b668c21c50ee092716d070f129fd1493f95ab7e43767eab8
    Deleted: sha256:7c52cdc1e32d67e3d5d9f83c95ebe18a58857e68bb6985b0381ebdcec73ff303
    Deleted: sha256:a3c2e83788e20188bb7d720f36ebeef2f111c7b939f1b19aa1b4756791beece0
    Deleted: sha256:61199b56f34827cbab596c63fd6e0ac0c448faa7e026e330994818190852d479
    Deleted: sha256:2dc9f76fb25b31e0ae9d36adce713364c682ba0d2fa70756486e5cedfaf40012
    root@jia:~# docker images		//查看本地镜像
    REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
    nginx               latest              f7bb5701a33c        10 days ago         126MB
    centos              latest              0f3e07c0138f        3 months ago        220MB
    root@jia:~# 
    

    强制删除正在运行的镜像

    root@jia:~# docker run -itd --name nginx nginx		//启动创建容器
    36b203a3fb5cf8fe6a1e13e298998c8510f7d24e49da36ada8d7181d89b16089
    root@jia:~# docker ps -a		//查看容器
    CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
    36b203a3fb5c        nginx               "nginx -g 'daemon of…"   3 seconds ago       Up 2 seconds        80/tcp              nginx
    root@jia:~# docker images		//查看本地镜像
    REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
    nginx               latest              f7bb5701a33c        10 days ago         126MB
    centos              latest              0f3e07c0138f        3 months ago        220MB
    root@jia:~# docker rmi nginx:latest		//默认删除镜像,删除失败,报错镜像正在使用
    Error response from daemon: conflict: unable to remove repository reference "nginx:latest" (must force) - container 36b203a3fb5c is using its referenced image f7bb5701a33c
    root@jia:~# docker rmi -f nginx:latest		//强制删除镜像
    Untagged: nginx:latest
    Untagged: nginx@sha256:b2d89d0a210398b4d1120b3e3a7672c16a4ba09c2c4a0395f18b9f7999b768f2
    root@jia:~# docker images		//查看镜像
    REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
    <none>              <none>              f7bb5701a33c        10 days ago         126MB
    centos              latest              0f3e07c0138f        3 months ago        220MB
    root@jia:~# 
    

    注意:当强制删除镜像后,会直接删除,但是会生成新的镜像,容器不会被删除

    save

    保存镜像为tar包

    语法

    docker save 参数 镜像名称 文件目录及名称.tar
    

    参数说明

    --output , -o:保存到某文件
    

    示例

    将nginx镜像保存为文件

    root@jia:/jia/jia# docker images
    REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
    nginx               latest              f7bb5701a33c        11 days ago         126MB
    centos              latest              0f3e07c0138f        3 months ago        220MB
    root@jia:/jia/jia# docker save nginx:latest > nginx.tar
    root@jia:/jia/jia# ls
    nginx.tar
    root@jia:/jia/jia# 
    

    将多个镜像保存到一个文件

    root@jia:/jia/jia# docker save  -o test.tar nginx:latest centos:latest
    root@jia:/jia/jia# ls
    nginx.tar  test.tar
    root@jia:/jia/jia# 
    

    使用gzip+tar进行打包压缩

    root@jia:/jia/jia# docker save nginx:latest | gzip > nginx.tar.gz
    root@jia:/jia/jia# ls -lh		//查看目录中文件长格式显示
    total 513M
    -rw-r--r-- 1 root root 125M Jan  9 16:57 nginx.tar
    -rw-r--r-- 1 root root  47M Jan  9 17:03 nginx.tar.gz
    -rw------- 1 root root 342M Jan  9 17:00 test.tar
    root@jia:/jia/jia# 
    

    top

    查看容器中进程信息

    语法

    docker top 参数 容器ID/容器名称
    

    示例
    查看nginx容器中所有进程信息

    root@jia:/jia/jia# docker run --name nginx -itd nginx:latest		//创建启动nginx容器
    69e353d8564025e07ee201098cafb9870d95b92edf990db2455aa6261c0bede6
    root@jia:/jia/jia# docker ps -a			//列出所有容器
    CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
    69e353d85640        nginx:latest        "nginx -g 'daemon of…"   5 seconds ago       Up 4 seconds        80/tcp              nginx
    root@jia:/jia/jia# docker top nginx
    UID                 PID                 PPID                C                   STIME               TTY                 TIME                CMD
    root                28665               28637               1                   17:08               pts/0               00:00:00            nginx: master process nginx -g daemon off;
    systemd+            28724               28665               0                   17:08               pts/0               00:00:00            nginx: worker process
    root@jia:/jia/jia# 
    

    version

    查看docker软件版本

    语法

    docker version 参数
    

    参数说明

    -f, --format:使用给定的Go模板格式化输出
    --kubeconfig:Kubernetes配置文件
    

    示例
    查看docker版本详细信息

    root@jia:/jia/jia# docker version
    Client: Docker Engine - Community
     Version:           19.03.5
     API version:       1.40
     Go version:        go1.12.12
     Git commit:        633a0ea838
     Built:             Wed Nov 13 07:29:52 2019
     OS/Arch:           linux/amd64
     Experimental:      false
    
    Server: Docker Engine - Community
     Engine:
      Version:          19.03.5
      API version:      1.40 (minimum version 1.12)
      Go version:       go1.12.12
      Git commit:       633a0ea838
      Built:            Wed Nov 13 07:28:22 2019
      OS/Arch:          linux/amd64
      Experimental:     false
     containerd:
      Version:          1.2.10
      GitCommit:        b34a5c8af56e510852c35414db4c1f4fa6172339
     runc:
      Version:          1.0.0-rc8+dev
      GitCommit:        3e425f80a8c931f88e6d94a8c831b9d5aa481657
     docker-init:
      Version:          0.18.0
      GitCommit:        fec3683
    root@jia:/jia/jia# 
    

    查看docker版本

    root@jia:/jia/jia# docker version --format '{{.Server.Version}}'
    19.03.5
    root@jia:/jia/jia#
    

    events

    查看docker服务器实时事件

    语法

    docker events 参数
    

    参数说明

    -f,--filter:根据条件过滤事件
    --since :从指定的时间戳后显示所有事件
    --until :流水时间显示到指定的时间为止
    --format:使用给定的Go模板格式化输出
    

    示例
    容器启动停止,使用events显示事件

    root@jia:/jia/jia# docker ps -a		//查看容器列表
    CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
    69e353d85640        nginx:latest        "nginx -g 'daemon of…"   13 minutes ago      Up 13 minutes       80/tcp              nginx
    root@jia:/jia/jia# docker stop nginx		//停止容器
    nginx
    root@jia:/jia/jia# docker start nginx		//启动容器
    nginx
    root@jia:/jia/jia# 
    
    root@jia:~# docker events 		//查看实时事件,注意此命令不会自动终止需要crtl+c终止
    2020-01-09T17:22:26.880177305+08:00 container kill 69e353d8564025e07ee201098cafb9870d95b92edf990db2455aa6261c0bede6 (image=nginx:latest, maintainer=NGINX Docker Maintainers <docker-maint@nginx.com>, name=nginx, signal=15)
    2020-01-09T17:22:27.005016080+08:00 container die 69e353d8564025e07ee201098cafb9870d95b92edf990db2455aa6261c0bede6 (exitCode=0, image=nginx:latest, maintainer=NGINX Docker Maintainers <docker-maint@nginx.com>, name=nginx)
    2020-01-09T17:22:27.103273324+08:00 network disconnect f2ccf449e47668433fb96511ef577c7515be7f481a7a9cb386c9d46650cbabbf (container=69e353d8564025e07ee201098cafb9870d95b92edf990db2455aa6261c0bede6, name=bridge, type=bridge)
    2020-01-09T17:22:27.145650268+08:00 container stop 69e353d8564025e07ee201098cafb9870d95b92edf990db2455aa6261c0bede6 (image=nginx:latest, maintainer=NGINX Docker Maintainers <docker-maint@nginx.com>, name=nginx)
    2020-01-09T17:22:33.680477968+08:00 network connect f2ccf449e47668433fb96511ef577c7515be7f481a7a9cb386c9d46650cbabbf (container=69e353d8564025e07ee201098cafb9870d95b92edf990db2455aa6261c0bede6, name=bridge, type=bridge)
    2020-01-09T17:22:34.093551544+08:00 container start 69e353d8564025e07ee201098cafb9870d95b92edf990db2455aa6261c0bede6 (image=nginx:latest, maintainer=NGINX Docker Maintainers <docker-maint@nginx.com>, name=nginx)
    

    import

    从tar文件导入docker为镜像

    语法

    docker import 参数 tar文件目录名称 导入的镜像名称:tag(版本)
    

    参数说明

    --change , -c:	将Dockerfile指令应用于创建的映像
    --message , -m:	设置导入图像的提交消息
    

    示例
    将nginx.tar的镜像包导入docker并命名nginx版本V1

    root@jia:/jia/jia# docker images		//查看本地所有镜像
    REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
    nginx               latest              f7bb5701a33c        11 days ago         126MB
    centos              latest              0f3e07c0138f        3 months ago        220MB
    root@jia:/jia/jia# ls
    nginx.tar  nginx.tar.gz  test.tar
    root@jia:/jia/jia# docker import nginx.tar nginx:V1
    sha256:07b2b1766ed11df9e7a12f98a4c5127c1aa0087e6b7d4eb0bc21490f168c1b0d
    root@jia:/jia/jia# docker images
    REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
    nginx               V1                  07b2b1766ed1        3 seconds ago       130MB
    nginx               latest              f7bb5701a33c        11 days ago         126MB
    centos              latest              0f3e07c0138f        3 months ago        220MB
    root@jia:/jia/jia# 
    

    导入使用tar打包gzip压缩的镜像

    root@jia:/jia/jia# docker import nginx.tar.gz nginx:V2
    sha256:ca7505dbea914f7babb1ed3667d1a133461c344d92ea978585b64fe7dabf5b36
    root@jia:/jia/jia# docker images
    REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
    nginx               V2                  ca7505dbea91        8 seconds ago       130MB
    nginx               V1                  07b2b1766ed1        2 minutes ago       130MB
    nginx               latest              f7bb5701a33c        11 days ago         126MB
    centos              latest              0f3e07c0138f        3 months ago        220MB
    root@jia:/jia/jia# 
    

    export

    从docker导出容器为tar文件

    语法

    docker export 参数 容器名称/容器ID
    

    参数说明

    --output , -o:写入文件
    

    示例
    导出centos容器并命名为centos.tar

    root@jia:/jia/jia# docker run --name centos -itd centos:latest		//创建并启动centos容器
    06b5abbb068dc493056aae6f1526b81028f931fd160fe470a937e15ac0110bdf
    root@jia:/jia/jia# docker ps 
    CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
    06b5abbb068d        centos:latest       "/bin/bash"              4 seconds ago       Up 3 seconds                            centos
    69e353d85640        nginx:latest        "nginx -g 'daemon of…"   32 minutes ago      Up 18 minutes       80/tcp              nginx
    root@jia:/jia/jia# docker export centos > centos.tar
    root@jia:/jia/jia# ls
    centos.tar  nginx.tar  nginx.tar.gz  test.tar
    root@jia:/jia/jia# 
    

    导出centos容器并命名为centos.tar.gz,使用gzip压缩

    root@jia:/jia/jia# docker export centos > centos.tar
    root@jia:/jia/jia# ls
    centos.tar  nginx.tar  nginx.tar.gz  test.tar
    root@jia:/jia/jia# docker export centos | gzip > centos.tar.gz
    root@jia:/jia/jia# ls -lh
    total 796M
    -rw-r--r-- 1 root root 217M Jan  9 17:41 centos.tar
    -rw-r--r-- 1 root root  67M Jan  9 17:43 centos.tar.gz
    -rw-r--r-- 1 root root 125M Jan  9 16:57 nginx.tar
    -rw-r--r-- 1 root root  47M Jan  9 17:03 nginx.tar.gz
    -rw------- 1 root root 342M Jan  9 17:00 test.tar
    root@jia:/jia/jia# 
    

    导出nginx容器,并命名nginx1.tar

    root@jia:/jia/jia# docker export -o nginx1.tar nginx
    root@jia:/jia/jia# ls
    centos.tar  centos.tar.gz  nginx1.tar  nginx.tar  nginx.tar.gz  test.tar
    root@jia:/jia/jia# 
    

    info

    查看docker系统信息

    语法

    docekr info 参数
    

    参数说明

    --format , -f:使用给定的Go模板格式化输出
    

    示例

    root@jia:/jia/jia# docker info
    	Client:
    	 Debug Mode: false
    	
    	Server:
    	 Containers: 2
    	  Running: 2
    	  Paused: 0
    	  Stopped: 0
    	 Images: 4
    	 Server Version: 19.03.5
    	 Storage Driver: overlay2
    	  Backing Filesystem: extfs
    	  Supports d_type: true
    	  Native Overlay Diff: true
    	 Logging Driver: json-file
    	 Cgroup Driver: cgroupfs
    	 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: runc
    	 Default Runtime: runc
    	 Init Binary: docker-init
    	 containerd version: b34a5c8af56e510852c35414db4c1f4fa6172339
    	 runc version: 3e425f80a8c931f88e6d94a8c831b9d5aa481657
    	 init version: fec3683
    	 Security Options:
    	  apparmor
    	  seccomp
    	   Profile: default
    	 Kernel Version: 4.15.0-65-generic
    	 Operating System: Ubuntu 18.04.3 LTS
    	 OSType: linux
    	 Architecture: x86_64
    	 CPUs: 2
    	 Total Memory: 3.852GiB
    	 Name: jia
    	 ID: 74XS:4TCO:7OQX:BKF2:O2WW:JYEF:6Y6Q:RT3X:YVRO:Y2LP:JZIA:AHSB
    	 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
    	 Live Restore Enabled: false
    	
    	WARNING: No swap limit support
    	root@jia:/jia/jia# 
    

    inspect

    查看容器详细信息

    语法

    docker inspect 参数 容器ID/容器名称
    

    参数说明

    --format , -f:使用给定的Go模板格式化输出
    --size , -s:显示文件总大小
    --type :为指定类型返回JSON
    

    示例

    获取容器IP地址

    第一种方式:
    
    root@jia:/jia/jia# docker ps -a		//查看容器
    CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
    06b5abbb068d        centos:latest       "/bin/bash"              17 hours ago        Up 17 hours                             centos
    69e353d85640        nginx:latest        "nginx -g 'daemon of…"   17 hours ago        Up 17 hours         80/tcp              nginx
    root@jia:/jia/jia# docker inspect centos | grep IPAddress
            "SecondaryIPAddresses": null,
            "IPAddress": "172.17.0.3",
                    "IPAddress": "172.17.0.3",
    root@jia:/jia/jia# 
    
    第二种方式:(只显示IP地址)
    
    root@jia:/jia/jia#  docker inspect --format='{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' centos
    172.17.0.3
    root@jia:/jia/jia# 
    

    获取容器MAC地址

    root@jia:/jia/jia# docker inspect centos | grep MacAddress
            "MacAddress": "02:42:ac:11:00:03",
                    "MacAddress": "02:42:ac:11:00:03",
    root@jia:/jia/jia# 
    

    获取容器日志目录

    root@jia:/jia/jia# docker inspect centos | grep log
        "LogPath": "/var/lib/docker/containers/06b5abbb068dc493056aae6f1526b81028f931fd160fe470a937e15ac0110bdf/06b5abbb068dc493056aae6f1526b81028f931fd160fe470a937e15ac0110bdf-json.log",
    root@jia:/jia/jia# 
    

    kill

    杀死正在运行的容器
    

    语法

    docker kill 参数 容器ID/容器名称
    

    参数说明

    --signal , -s:向容器发送信号
    

    示例

    杀死正在运行的centos容器

    root@jia:/jia/jia# docker ps		//查看容器
    CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
    06b5abbb068d        centos:latest       "/bin/bash"              17 hours ago        Up 17 hours                             centos
    69e353d85640        nginx:latest        "nginx -g 'daemon of…"   18 hours ago        Up 17 hours         80/tcp              nginx
    root@jia:/jia/jia# docker kill centos
    centos
    root@jia:/jia/jia# docker ps -a		//查看所有容器
    CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                       PORTS               NAMES
    06b5abbb068d        centos:latest       "/bin/bash"              17 hours ago        Exited (137) 6 seconds ago                       centos
    69e353d85640        nginx:latest        "nginx -g 'daemon of…"   18 hours ago        Up 17 hours                  80/tcp              nginx
    root@jia:/jia/jia# 
    

    杀死正在运行的nginx容器,发送kill信号

    root@jia:/jia/jia# docker kill --signal=kill  nginx
    nginx
    root@jia:/jia/jia# docker ps -a
    CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                       PORTS               NAMES
    06b5abbb068d        centos:latest       "/bin/bash"              17 hours ago        Exited (137) 3 minutes ago                       centos
    69e353d85640        nginx:latest        "nginx -g 'daemon of…"   18 hours ago        Exited (137) 1 second ago                        nginx
    root@jia:/jia/jia# 
    

    load

    从tar文件中加载为docker镜像

    语法

    docker load 参数 tar文件
    

    参数说明

    -i, --input:从tar存档文件中读取,而不是从STDIN中读取
    --quiet , -q:显示简要信息
    

    示例

    导入nginx镜像

    root@jia:/jia/jia# docker images		//查看所有镜像
    REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
    centos              latest              0f3e07c0138f        3 months ago        220MB
    root@jia:/jia/jia# docker load < nginx.tar.gz 
    556c5fb0d91b: Loading layer [==================================================>]  72.48MB/72.48MB
    49434cc20e95: Loading layer [==================================================>]  57.67MB/57.67MB
    75248c0d5438: Loading layer [==================================================>]  3.584kB/3.584kB
    Loaded image: nginx:latest
    root@jia:/jia/jia# docker images		//查看所有镜像
    REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
    nginx               latest              f7bb5701a33c        12 days ago         126MB
    centos              latest              0f3e07c0138f        3 months ago        220MB
    root@jia:/jia/jia# 
    

    pause

    暂停容器

    语法

    docker pause 容器ID/容器名称
    

    示例

    暂停centos容器

    root@jia:/jia/jia# docker run -itd --name centos centos:latest		//创建并运行
    48e49042b927f427aed68437e12e5acd14b82e3a33a71b6f236bcef5c3edd41d
    root@jia:/jia/jia# docker ps		//查看容器
    CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
    48e49042b927        centos:latest       "/bin/bash"         2 seconds ago       Up 2 seconds                            centos
    root@jia:/jia/jia# docker pause centos	
    centos
    root@jia:/jia/jia# docker ps -a		//查看所有容器
    CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                   PORTS               NAMES
    48e49042b927        centos:latest       "/bin/bash"         14 seconds ago      Up 13 seconds (Paused)                       centos
    root@jia:/jia/jia# 
    
    容器暂停后会在状态处加(Paused) 
    

    unpause

    取消容器暂停状态

    语法

    docker unpause 容器ID/容器名称
    

    示例

    取消centos容器的暂停操作

    root@jia:/jia/jia# docker ps -a		//查看所有容器
    CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                   PORTS               NAMES
    48e49042b927        centos:latest       "/bin/bash"         14 seconds ago      Up 13 seconds (Paused)                       centos
    root@jia:/jia/jia# docker unpause centos
    centos
    root@jia:/jia/jia# docker ps -a		//查看所有容器
    CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
    48e49042b927        centos:latest       "/bin/bash"         5 minutes ago       Up 5 minutes                            centos
    root@jia:/jia/jia# 
    

    tag

    给镜像进行打标签

    语法

    docker tag 源镜像ID/源镜像名称:tag(版本) 目标镜像名称:tag(版本)
    

    示例

    给centos镜像重新打标签

    root@jia:/jia/jia# docker tag  centos:latest centos:V1
    root@jia:/jia/jia# docker images
    REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
    nginx               latest              f7bb5701a33c        12 days ago         126MB
    centos              V1                  0f3e07c0138f        3 months ago        220MB
    centos              latest              0f3e07c0138f        3 months ago        220MB
    root@jia:/jia/jia# 
    

    wait

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

    语法

    docekr wait 容器ID/容器名称
    

    示例

    root@jia:/jia/jia# docker run -itd --name nginx nginx		//创建启动容器
    57af5446f0eaff56a54c3a5559ac0073511de9b9f48c93ff04bbcd0c62b3a809
    root@jia:/jia/jia# docker ps		//查看容器
    CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
    57af5446f0ea        nginx               "nginx -g 'daemon of…"   4 seconds ago       Up 3 seconds        80/tcp              nginx
    root@jia:/jia/jia# docker inspect nginx | grep IPAddress		//查看容器IP地址
            "SecondaryIPAddresses": null,
            "IPAddress": "172.17.0.2",
                    "IPAddress": "172.17.0.2",
    root@jia:/jia/jia# curl 172.17.0.2		//访问容器的80端口
    <!DOCTYPE html>
    <html>
    <head>
    <title>Welcome to nginx!</title>
    <style>
        body {
             35em;
            margin: 0 auto;
            font-family: Tahoma, Verdana, Arial, sans-serif;
        }
    </style>
    </head>
    <body>
    <h1>Welcome to nginx!</h1>
    <p>If you see this page, the nginx web server is successfully installed and
    working. Further configuration is required.</p>
    
    <p>For online documentation and support please refer to
    <a href="http://nginx.org/">nginx.org</a>.<br/>
    Commercial support is available at
    <a href="http://nginx.com/">nginx.com</a>.</p>
    
    <p><em>Thank you for using nginx.</em></p>
    </body>
    </html>
    root@jia:/jia/jia# docker wait nginx		//启动阻塞
    
    root@jia:~# curl 172.18.0.2		//发现无法访问容器nginx页面
    
  • 相关阅读:
    Hibernate连接mysql数据库的配置
    opendaynight(karaf) 和 mininet测试openflow
    电信新势力,TIP/CORD能颠覆电信设备商吗?
    minnet sample
    ONIE
    升级Mininet自带的OpenvSwitch & 编译OpenvSwitch
    mininet test
    dpctl 命令实践
    白盒交换机
    Wedge 100-32X 100GbE Data Center Switch
  • 原文地址:https://www.cnblogs.com/blxt/p/14501173.html
Copyright © 2020-2023  润新知