docker command learning

Last time we just listed the function of the command and its simple use (you can see: Summary of docker commands (1) ), then this article will detail the parameters of each command

search

docker search: This command is used to find a docker image in the repository

Grammar:

docker search parameter mirror name: tag (version)

Parameter description:

--filter, -f: Filter the output according to the relevant conditions
 --format: Use the Go template for beautiful print searches
 --limit: Specify maximum search results, default 25
 --no-trunc: Display a complete description of the image
 --stars, -s: Lists images where the number of collections is not less than a specified value
 --automated: Show only auto-built mirrors

Example:

Default Search

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:~$ 

Search for nginx images with more than 10 collections and display details

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:~$ 

Use conditions to find nginx images with more than 10 collections

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

Download mirror from docker repository to local

grammar

docker pull mirror name: tag (version)

Parameter Description

-a, --all-tags: Download all versions of the mirror
 -q, --quiet: do not display details
 --disable-content-trust: Do not check the downloaded image
 --platform: Specify the platform information to download the mirror server

Example

Download docker image for nginx default version

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

Push the specified image to the docker mirror server

grammar

docker push local mirror: tag (version)

Parameter Description

--disable-content-trust: No verification of mirrors

Example

docker push blxt:5000/blxt/nginx upload nginx to private repository

images

View all docker mirrors locally

grammar

docker images parameter mirror: tag (version)

Parameter Description

-a, --all: List all local mirrors
 --digests: Show mirror simple information description
 -f, --filter: Filter the output according to the relevant conditions
 --format: Use the Go template for beautiful print searches
 --no-trunc: Display complete information about the mirror
 -q, --quiet: Display only the mirror ID

Example

View all local mirrors

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:~$ 

View mirror details

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:~$ 

View Mirror ID

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

run

Create a container and run it

grammar

docker run parameter mirror: tag (version)

Parameter Description

-a, --attach: Specifies the standard input and output content type, optional STDIN/STDOUT/STDERR three items
 -d: The container will run in the background
 -t: Assign tty terminals to containers
 --sig-proxy=true: Proxy all received signals to a process (non-TTY mode only)
-i: Run the container in interactive mode
 --name:Set a name for the container
 --dns: Set up custom DNS servers for containers
 --dns-search: Specify DNS search domain name
 --network: Specify the network card for the container
 --network-alias: Set alias for container's network card
 --add-host: Add a record to the container's hosts file
 --mac-address: Set the MAC address of the container
 --ip: Set the IPV4 address of the container
 --ip6: Set container IPV6 address
 --link-local-ip: Set the link local IPv4/IPv6 address of the Ethernet device for one or more containers
 --rm: Automatically delete containers when they exit
 -m, --memory: Set the container memory size to a minimum of 4M
 --memory-swap: Sets the total memory size of the container, including the swap partition
 -c, --cpu-shares: set CPU
 --cpu s: Set the number of cpus
 --entrypoint: Default command executed when the container is running
 --expose: Set port mapping
 -P: Map all ports in the container to the host
 -p: Map a port of a container to the host
 --link: Add a link to another container
 -v, --volume: mount the host's files into a container
 --volumes-from: Mount all file systems of the host machine into containers
 -u, --user: Sets the user name or UID to be used and the optional group name or GID for the specified command
 -h: Specify container host name
 -e: Set environment variables
 --env-file: Import variables from the specified file
 There are many more parameters, not listed here, above are commonly used parameters

Example

Run the nginx container in the background, named 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:~$ 

Run nginx container in the background and map container port 80 to host port 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
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    body {
        width: 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:~$ 

Create nginx container and start it interactively

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

history

View the mirroring process

grammar

docker history parameter mirror: tag (version)

parameter

--format: Use the Go template for beautiful print searches
 -H, --human: Print size and date in readable format (default is true)
--no-trunc: Display Formation Details
 -q, --quiet: Display only the mirror ID

Example

Default usage

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:~$ 

Print only mirror 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:~$ 

Show details of mirror formation

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

Start Container

grammar

docker start parameter container ID/container name

Parameter Description

-a, --attach: additional STDOUT/STDERR and forward signal
 --checkpoint: Restore from snapshot 
--checkpoint-dir: Store directories using custom checkpoints
 --detach-keys: Rewrite the key sequence used to separate containers
 -i, --interactive: STDIN with additional containers

Example

Start nginx container created with create

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

Close Container

grammar

docker stop parameter container ID/container name

Parameter Description

--time, -t: How long after closing the container

Example

Default usage

ubuntu@VM-121-163-ubuntu:~$ sudo docker run -d --name nginx nginx:latest
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

Mirroring with dockerfile

grammar

docker build parameter path

Parameter Description

--build-arg: Set variables at creation time
 --cgroup-parent: The optional parent CGroup of the container
 --compress: Build context using gzip compression
 --cpu-shares: Set CPU usage weight
 --cpu-period: Limit CPU CFS cycle
 --cpu-quota: Limit CPU CFS quota
 --cpuset-cpus: Specifies the CPU id to use
 --cpuset-mems: Specifies the memory id to use
 --disable-content-trus: Skip mirror validation
 --file, -f: Specifies the name of the dockerfile, defaulting to dockerfile
 --force-rm: Always remove intermediate containers
 --iidfile: Write the mirror ID to the file
 --isolation: Use container isolation
 --label: Setting metadata for mirroring
 --memory, -m: Set memory limits
 --memory-swap: Set total memory, including swap memory
 --network: Set mirror network information
 --no-cache: Build mirrors without caching
 --output, -o: Set the mirror generation location
 --pull: Always try to extract newer versions of images
 --quiet, -q: Prohibit output generation and print image ID successfully
 --rm: Delete intermediate container after successful build
 --tag, -t:The name and label of the mirror
 --ulimit: Ulimit configuration
 --shm-size: Set/dev/shm size, default is 64M

Example

Using dockerfile in current directory to make nginx:V1 image

docker build -t nginx: V1 .

Create nginx:V2 mirror using dockerfile s that are not in the current directory

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

Add another host to the container file

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

Create a mirror named test:1 with the parameter--square

docker build --squash -t test:1  .

attach

Current shell connection run container

grammar

docker attach parameter container ID/container name

parameter

--detach-keys: The key sequence that covers the detach container
 --no-stdin: Don't attach STDIN
 --sig-proxy: Proxy all received signals to the process

Example

Default usage

ubuntu@VM-121-163-ubuntu:~$ sudo docker run -d --name ubuntu ubuntu /usr/bin/top -b
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

You can see that the exit code returned by the bash procedure is also returned to its caller by the docker attach command

ubuntu@VM-121-163-ubuntu:~$ sudo docker run --name ubuntu -d -it ubuntu
9ac7802feb2b77bb905bc9f4c5ca9782e6800fc995b1f2b39da78239d86a7405
ubuntu@VM-121-163-ubuntu:~$ sudo docker attach ubuntu
root@9ac7802feb2b:/# 
root@9ac7802feb2b:/# 

commit

Save the current container as a mirror/snapshot

grammar

docker commit container name/container ID mirror name: tag (version)

Example

ubuntu@VM-121-163-ubuntu:~$ sudo docker images      //View Mirror
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      //Create Container
9566c54f3883b0a3b2f92dc94eb8520772d5d05826d7025be23212768f4d7967
ubuntu@VM-121-163-ubuntu:~$ sudo docker ps -a       //View container status
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       //take a snapshot
sha256:e091997549ff02cf6309d6146b7626c6efef6dd9ce598c632f6bf0f4d090978c
ubuntu@VM-121-163-ubuntu:~$ sudo docker images      //View Mirror
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

Containers and hosts copy files to each other

grammar

docker cp container ID/container name: file path host path Copy files from container to host
 docker cp host path container ID/container name: file storage path Copy host file to container

Parameter Description

 -a, --archive: copy files while copying file user information
 -L, --follow-link: Keep links in source and target

Example

Copy the host file to the root directory of the container

root@test:/jia/test# Docker run-d --name nginx nginx:latest //Create and run nginx container
33b6339003ef41fd62b909fd79e9056d53d944b3172f0f02fc717fd64a2648e3
root@test:/jia/test# docker exec nginx ls / //list container root directory
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:///Copy file to container
root@test:/jia/test# docker exec nginx ls / //list container root directory
bin
boot
dev
etc
home
lib
lib64
media
mnt
opt
proc
root
run
sbin
srv
sys
test
tmp
usr
var

Copy files from container to Host Directory

root@test:/jia/test# ls //List Directories
root@test:/jia/test# docker cp nginx:/test. //Copy files from container to current directory
root@test:/jia/test# ls //List Directories
test
root@test:/jia/test# 

create

Create containers but do not start them

grammar

docker create parameter mirror: tag (version)

Parameter Description

--add-host: Add a record to the container's hosts file    
-a, --attach: Specifies the standard input and output content type, optional STDIN/STDOUT/STDERR three items
 --cap-add: Add linux functionality
 --cap-drop: Remove linux functionality
 --cidfile: Write the container ID to the file
 -c, --cpu-shares: set CPU
 --device: Add host device to container
 --disable-content-trus: Do not validate mirrors
 --dns: Set up custom DNS servers for containers'
--dns-opt: Set DNS options
 --dns-search: Specify DNS search domain name
 --domainname: Set container domain name
 --env, -e: Setting environment variables
 --env-file: Read variable file to set container variables
 --expose: Set port mapping
 --hostname, -h: Set the container host name
 --init: Run an initializer inside the container to forward signals and get processes
 --ip: Set the IPV4 address of the container
 --ip6: Set container IPV6 address
 --isolation: Using isolation technology
 --link: Add a link to the host, that is, create a soft connection
 --link-local-ip: Set container local address
 --mac-address: Set the container MAC address
 --memory, -m: Set up memory
 --memory-swap: Set the total memory size, including swap memory
 --mount: Mount the file system
 --name: Set the container name
 --net: Link containers to the network
 --net-alias: alias the container network
 --network: Container links to network
 --network-alias: alias the container network
 --publish, -p: Map container ports to hosts
 --publish-all, -P: Map all ports of the container to the host
 --read-only: Mount the container's root file system as read-only
 --restart: Restart policy when container exits to apply
 --rm: Automatically delete containers on exit
 --stop-signal: the signal to stop the container
 --tty, -t: allocation terminal
 --user, -u: Set the user who runs the container
 --workdir, -w: Set the working directory of the container
 There are many more parameters, not listed here, above are commonly used parameters

Example

Create nginx container and start

root@test:/jia/test# Docker create-t-i nginx:latest/bin/bash //create container
6f2cea61b80c117fe877589073b5f259a57623bd511a89330c72c825c0cbe4fd        
root@test:/jia/test# Docker PS-A //view container
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 //startup container
root@6f2cea61b80c:/# ls //List the current directory of the container
bin  boot  dev  etc  home  lib  lib64  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var
root@6f2cea61b80c:/# 

Create a container named nginx and start it

root@test:/jia/test# Docker create --name nginx-t-i nginx:latest/bin/bash//create container
5487d362e25bfcd39c70754972eb964121ba8408f3f97c89cac930208882e745
root@test:/jia/test# Docker PS-A //view container
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 //start container and link
root@5487d362e25b:/# ls
bin  boot  dev  etc  home  lib  lib64  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var
root@5487d362e25b:/# 

Create nginx container and map ports

root@test:/jia/test# Docker create --name nginx-p 80:80-it nginx:latest/bin/bash //create container
4cc06988f4284f01281a338b914aff247a16969e33177088673a281d848f99d4
root@test:/jia/test# Docker PS-A //view container
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 //start container
nginx
root@test:/jia/test# Docker PS-A //View container status
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 {
        width: 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

View container changes

grammar

docker diff container

Symbol Description

A Add a file or directory
 D file or directory deleted
 C file or directory changed

Example

Default usage

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

Copy the test file into the nginx container to see your changes

root@test:/jia/test# docker cp test nginx:///copy test file to the root directory of the container
root@test:/jia/test# docker diff nginx
A /test
C /root
A /root/.bash_history
root@test:/jia/test# 

exec

Execute commands in containers

grammar

docker exec parameter container ID/container name command

Parameter Description

--detach, -d: Background running container
 --detach-keys: The key sequence that covers the detach container
 --env, -e: Setting environment variables
 --interactive, -i: Keep open even if STDIN is not connected
 --privileged: Give commands extended permissions
 --tty, -t: assign pseudo TTY
 --user, -u: Specify the running user
 --workdir, -w: Set working directory

Example

Print "hello word" using containers

root@test:/jia/test# Docker run --name centos-itd centos:latest //Create and start container
1b75dac7104122e021fb8cc7989c8992eb8502f34713e76ee87e6d6f7796e83c
root@test:/jia/test# Docker PS-A //view container
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" //use echo to print "hello word"
hello word
root@test:/jia/test# 

Create an interactive shell for the container and view the container IP address

root@test:/jia/test# Docker PS-A //view container
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 //create bash interactive shell
[root@1b75dac71041 /]# ip addr //view IP address
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 /]# 

Specify a working directory and print the path

root@test:/jia/test# Docker PS-A //view container
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/w w w/html CentOS PWD //set working directory and print directory
/var/www/html
root@test:/jia/test#

login

Log on to docker mirror source server

grammar

docker login parameter server address

Parameter Description

-p, --password: Specifies the password of the logged-in server user
 -u, --username: Specify the account of the user logged in to the server

Example

Default login 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#

Specify user name and password for login

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# 

Login to Ali Mirror Warehouse

root@jia:/etc# Docker login-u'Dumping City'-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

Exit Logon Mirror Source Server

grammar

docker logout mirror warehouse address

Example

Exit dockerhub user login

root@jia:/etc# Docker login-u blxt-p'JKY@jj.123'//Log on to dockerhub server
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 //logout
Removing login credentials for https://index.docker.io/v1/
root@jia:/etc# 

Exit Ali Mirror Warehouse

root@jia:/etc# Docker login-u'Dumping City'-p'123456'registry.cn-shenzhen.aliyuncs.com//Login to Ali Mirror Warehouse
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 //exit failure not logged in to dockerhub repository
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        //Log out of Ali Mirror Warehouse
root@jia:/etc# 

logs

Output log information for the current container

grammar

docker logs parameter container ID/container name

Parameter Description

--details: View log details
 --follow, -f: View logs dynamically and in real time
 --since: Displays the time at which the log was generated
 --tail: Displays the number of log lines
 --timestamps, -t: Show timestamps
 --until: Display log information for a period of time

Example

View container log by default

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# 

Display logs of nginx containers in real time

root@jia:/etc# Docker run --name nginx-itd nginx:latest //Create and start nginx container
85c8be92df44960e0973ae2b48907661679d921d0523cb93526351f07850f290
root@jia:/etc# Docker PS-A //view container
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 //View the ip address of the container
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 //access nginx service
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    body {
        width: 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 //output log information
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

View port mappings for containers

grammar

docker port container ID/container name

Example

List all port mappings for nginx container

root@jia:/etc# Docker run --name nginx-itd-P nginx:latest //Create and start the container, mapping all ports of the container
b4e3a51697c2e35b4a17acee93630ac77c6e9fe94df99c70750b60c4f87b535a
root@jia:/etc# Docker PS-A //view container
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 //Display container port mapping
80/tcp -> 0.0.0.0:32768
root@jia:/etc# 

ps

List Containers

grammar

docker ps parameter 

Parameter Description

--all, -a: View all containers
 --filter, -f: Filter according to relevant conditions
 --format: Use the Go template to print beautiful containers
 --last, -n: Displays the number of containers created
 --latest, -l: Show newly created containers
 --no-trunc: Show details
 --quiet, -q: Display only container ID
 --size, -s: Displays the total size of the container

Example

View all containers

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:~# 

View running containers

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

Restart Container

grammar

docker restart container name/container ID

parameter

-t:Set restart execution time

Example

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

Delete Container

grammar

docker rm parameter container ID/container name

parameter

--force, -f: Delete containers according to conditions, forcing the removal of running containers
 --link, -l: Remove network connections between containers, not containers themselves
 -v, --volumes: Delete the file system associated with the container

Example

Delete nginx container

root@jia:~# Docker PS-A //view container
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 //delete container, but nginx container is running so cannot be deleted with this parameter
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 //Force deletion of nginx container
nginx
root@jia:~# Docker PS-A //view container
CONTAINER ID        IMAGE               COMMAND             CREATED              STATUS              PORTS               NAMES
c3800e1bda43        centos              "/bin/bash"         About a minute ago   Up About a minute                       centos
root@jia:~# 

Delete all containers

root@jia:~# Docker PS-A //view container
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) //Delete container failed because container is running
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)//Delete all containers
4ee9b7488e32
c3800e1bda43
root@jia:~# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
root@jia:~# 

rmi

Remove Local Mirrors

grammar

docker rmi parameter mirror name/mirror ID

parameter

--force, -f: Force deletion
 --no-prune: Do not remove the process image of the image, remove it by default

Example

Delete mirror by default

root@jia:~# docker images //view local image
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 //Remove the mirror of 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 //view local image
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
nginx               latest              f7bb5701a33c        10 days ago         126MB
centos              latest              0f3e07c0138f        3 months ago        220MB
root@jia:~# 

Force deletion of running images

root@jia:~# Docker run-itd --name nginx nginx //start creating container
36b203a3fb5cf8fe6a1e13e298998c8510f7d24e49da36ada8d7181d89b16089
root@jia:~# Docker PS-A //view container
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 //view local image
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 //Default delete image, delete failed, error image in use
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 //Force deletion of mirror
Untagged: nginx:latest
Untagged: nginx@sha256:b2d89d0a210398b4d1120b3e3a7672c16a4ba09c2c4a0395f18b9f7999b768f2
root@jia:~# docker images //view mirror
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
<none>              <none>              f7bb5701a33c        10 days ago         126MB
centos              latest              0f3e07c0138f        3 months ago        220MB
root@jia:~# 

Note: When a mirror is forcibly deleted, it will be deleted directly, but a new image will be generated and the container will not be deleted

save

Save mirror as tar package

grammar

Doker save parameter mirror name file directory and name.tar

Parameter Description

--output, -o: Save to a file

Example

Save nginx image as file

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# 

Save multiple mirrors to a file

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# 

Packaging compression using gzip+tar

root@jia:/jia/jia# docker save nginx:latest | gzip > nginx.tar.gz
root@jia:/jia/jia# Ls-lh //View long file format display in directory
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

View process information in container

grammar

docker top parameter container ID/container name

Example
View all process information in nginx container

root@jia:/jia/jia# docker run --name nginx -itd nginx:latest //Create startup nginx container
69e353d8564025e07ee201098cafb9870d95b92edf990db2455aa6261c0bede6
root@jia:/jia/jia# Docker PS-A //List all containers
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

View docker software version

grammar

docker version parameter

Parameter Description

-f, --format: Format the output using the given Go template
 --kubeconfig:Kubernetes configuration file

Example
View docker version details

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# 

View docker version

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

events

View docker server real-time events

grammar

docker events parameter

Parameter Description

-f, --filter: filter events by condition
 --since: Displays all events after the specified timestamp
 --until: The running time is displayed until the specified time
 --format: Formats the output using the given Go template

Example
Container Start Stop, events Shown

root@jia:/jia/jia# Docker PS-A //view list of containers
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 //stop container
nginx
root@jia:/jia/jia# docker start nginx //start container
nginx
root@jia:/jia/jia# 

root@jia:~# docker events //View real-time events, note that this command does not automatically terminate requiring crtl+c termination
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

Import docker from tar file as mirror

grammar

docker import parameter tar file directory name import mirror name: tag (version)

Parameter Description

--change, -c: Apply the Dockerfile directive to the created image
 --message, -m: Set the submission message for the imported image

Example
Import the mirror package of nginx.tar into docker and name nginx version V1

root@jia:/jia/jia# docker images //View all local mirrors
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# 

Import a gzip compressed image packaged with tar

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

Export container from docker to tar file

grammar

docker export parameter container name/container ID

Parameter Description

--output, -o: Write to file

Example
Export the CentOS container and name it centos.tar

root@jia:/jia/jia# docker run --name centos -itd centos:latest //Create and start CentOS container
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# 

Export the CentOS container and name it centos.tar.gz, using gzip compression

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# 

Export the nginx container and name it 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

View docker system information

grammar

docekr info parameter

Parameter Description

--format, -f: Format the output using the given Go template

Example

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

View Container Details

grammar

docker inspect parameter container ID/container name

Parameter Description

--format, -f: Format the output using the given Go template
 --size, -s: Displays the total file size
 --type: Returns JSON for the specified type

Example

Get Container IP Address

First way:

root@jia:/jia/jia# Docker PS-A //view container
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# 

//Second way: (show only IP addresses)

root@jia:/jia/jia#  docker inspect --format='{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' centos
172.17.0.3
root@jia:/jia/jia# 

Get Container MAC Address

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# 

Get Container Log Directory

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

kill

Kill the running container

grammar

docker kill parameter container ID/container name

Parameter Description

--signal, -s: Send signal to container

Example

Kill the running centos container

root@jia:/jia/jia# docker ps //view container
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 //View all containers
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# 

Kill the running nginx container and send a kill signal

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

Load as docker image from tar file

grammar

docker load parameter tar file

Parameter Description

-i, --input: read from the tar archive file, not from STDIN
 --quiet, -q: Show brief information

Example

Import nginx mirror

root@jia:/jia/jia# docker images //View all mirrors
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 //View all mirrors
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

Pause Container

grammar

docker pause container ID/container name

Example

Pause centos container

root@jia:/jia/jia# Docker run-itd --name CentOS centos:latest //create and run
48e49042b927f427aed68437e12e5acd14b82e3a33a71b6f236bcef5c3edd41d
root@jia:/jia/jia# docker ps //view container
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 //View all containers
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 when the container is paused 

unpause

Cancel container pause state

grammar

docker unpause container ID/container name

Example

Cancel pause of centos container

root@jia:/jia/jia# Docker PS-A //View all containers
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 //View all containers
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
48e49042b927        centos:latest       "/bin/bash"         5 minutes ago       Up 5 minutes                            centos
root@jia:/jia/jia# 

t a g

Label the mirror

grammar

docker tag source image ID/source image name: tag (version) target image name: tag (version)

Example

Relabel centos image

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

Block until the container stops and print out its exit code

grammar

docekr wait container ID/container name

Example

root@jia:/jia/jia# Docker run-itd --name nginx nginx //create startup container
57af5446f0eaff56a54c3a5559ac0073511de9b9f48c93ff04bbcd0c62b3a809
root@jia:/jia/jia# docker ps //view container
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 //View container IP address
        "SecondaryIPAddresses": null,
        "IPAddress": "172.17.0.2",
                "IPAddress": "172.17.0.2",
root@jia:/jia/jia# curl 172.17.0.2 //80 port to access container
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    body {
        width: 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 //startup blocking

root@jia:~# curl 172.18.0.2 //Discovery failed to access container nginx page

Tags: Nginx Docker Ubuntu CentOS

Posted on Mon, 13 Jan 2020 08:22:48 -0800 by smilesmita