您好,登錄后才能下訂單哦!
上次只是給大家把命令的作用以及簡單使用列出來了(大家可以查看:docker命令總結(一)),那這篇文章會詳細介紹每條命令的參數 ## search docker search:此命令是用來在倉庫中查找docker鏡像 語法: docker search 參數 鏡像名稱:tag(版本) 參數說明: --filter,-f:根據相關條件過濾輸出 --format:使用Go模板進行漂亮的打印搜索 --limit:指定最大搜索結果,默認25個 --no-trunc:顯示完整的鏡像描述 --stars,-s:列出收藏數不小于指定值的鏡像 --automated:僅顯示自動構建的鏡像 示例: 默認搜索 ubuntu@VM-101-242-ubuntu:~$ sudo docker search nginx NAME DESCRIPTION STARS OFFICIAL AUTOMATED nginx Official build of Nginx. 12438 jwilder/nginx-proxy Automated Nginx reverse proxy for docker con… 1713 richarvey/nginx-php-fpm Container running Nginx + PHP-FPM capable of… 750 linuxserver/nginx An Nginx container, brought to you by LinuxS… 84 bitnami/nginx Bitnami nginx Docker Image 75 tiangolo/nginx-rtmp Docker image with Nginx using the nginx-rtmp… 60 nginxdemos/hello NGINX webserver that serves a simple page co… 35 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 nginx/nginx-ingress NGINX Ingress Controller for Kubernetes 22 privatebin/nginx-fpm-alpine PrivateBin running on an Nginx, php-fpm & Al… 19 schmunk42/nginx-redirect A very simple container to redirect HTTP tra… 17 blacklabelops/nginx Dockerized Nginx Reverse Proxy Server. 12 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 mailu/nginx Mailu nginx frontend 5 1science/nginx Nginx Docker images that include Consul Temp… 5 pebbletech/nginx-proxy nginx-proxy sets up a container running ngin… 2 ansibleplaybookbundle/nginx-apb An APB to deploy NGINX 1 centos/nginx-110-centos7 Platform for running nginx 1.10 or building … 0 wodby/nginx Generic nginx 0 ubuntu@VM-101-242-ubuntu:~$ 搜索收藏數大于10的nginx鏡像,并顯示詳細信息 ubuntu@VM-101-242-ubuntu:~$ sudo docker search --stars=10 --no-trunc nginx Flag --stars has been deprecated, use --filter=stars=3 instead NAME DESCRIPTION STARS OFFICIAL AUTOMATED nginx Official build of Nginx. 12438 jwilder/nginx-proxy Automated Nginx reverse proxy for docker containers 1713 richarvey/nginx-php-fpm Container running Nginx + PHP-FPM capable of pulling application code from git 750 linuxserver/nginx An Nginx container, brought to you by LinuxServer.io. 84 bitnami/nginx Bitnami nginx Docker Image 75 tiangolo/nginx-rtmp Docker image with Nginx using the nginx-rtmp-module module for live multimedia (video) streaming. 60 nginxdemos/hello NGINX webserver that serves a simple page containing its hostname, IP address and port ... 35 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 nginx/nginx-ingress NGINX Ingress Controller for Kubernetes 22 privatebin/nginx-fpm-alpine PrivateBin running on an Nginx, php-fpm & Alpine Linux stack 19 schmunk42/nginx-redirect A very simple container to redirect HTTP traffic to another server, based on nginx 17 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 centos/nginx-112-centos7 Platform for running nginx 1.12 or building nginx-based application 11 ubuntu@VM-101-242-ubuntu:~$ 使用條件查找收藏數大于10的nginx鏡像 ubuntu@VM-101-242-ubuntu:~$ sudo docker search --filter stars=3 nginx NAME DESCRIPTION STARS OFFICIAL AUTOMATED nginx Official build of Nginx. 12438 jwilder/nginx-proxy Automated Nginx reverse proxy for docker con… 1713 richarvey/nginx-php-fpm Container running Nginx + PHP-FPM capable of… 750 linuxserver/nginx An Nginx container, brought to you by LinuxS… 84 bitnami/nginx Bitnami nginx Docker Image 75 tiangolo/nginx-rtmp Docker image with Nginx using the nginx-rtmp… 60 nginxdemos/hello NGINX webserver that serves a simple page co… 35 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 nginx/nginx-ingress NGINX Ingress Controller for Kubernetes 22 privatebin/nginx-fpm-alpine PrivateBin running on an Nginx, php-fpm & Al… 19 schmunk42/nginx-redirect A very simple container to redirect HTTP tra… 17 centos/nginx-18-centos7 Platform for running nginx 1.8 or building n… 12 blacklabelops/nginx Dockerized Nginx Reverse Proxy Server. 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 mailu/nginx Mailu nginx frontend 5 1science/nginx Nginx Docker images that include Consul Temp… 5 ubuntu@VM-101-242-ubuntu:~$ ## pull 從docker倉庫下載鏡像到本地 語法 docker pull 鏡像名稱:tag(版本) 參數說明 -a, --all-tags:下載所有版本的鏡像 -q, --quiet:不顯示詳細信息 --disable-content-trust:不對下載的鏡像進行校驗 --platform:指定下載鏡像服務器的平臺信息 示例 下載nginx默認版本的docker鏡像 ubuntu@VM-35-226-ubuntu:~$ sudo docker pull nginx:latest latest: Pulling from library/nginx 8ec398bc0356: Pull complete 465560073b6f: Pull complete f473f9fd0a8c: Pull complete Digest: sha256:b2d89d0a210398b4d1120b3e3a7672c16a4ba09c2c4a0395f18b9f7999b768f2 Status: Downloaded newer image for nginx:latest docker.io/library/nginx:latest ubuntu@VM-35-226-ubuntu:~$ sudo docker images REPOSITORY TAG IMAGE ID CREATED SIZE nginx latest f7bb5701a33c 9 days ago 126MB ubuntu@VM-35-226-ubuntu:~$ ## push 推送指定鏡像到docker鏡像服務器 語法 docker push 本地鏡像:tag(版本) 參數說明 --disable-content-trust:不對鏡像進行校驗 示例 docker push blxt:5000/blxt/nginx 上傳nginx到私有倉庫 ## images 查看本地所有docker鏡像 語法 docker images 參數 鏡像:tag(版本) 參數說明 -a, --all:列出本地所有鏡像 --digests:顯示鏡像簡單信息說明 -f, --filter:根據相關條件過濾輸出 --format:使用Go模板進行漂亮的打印搜索 --no-trunc:顯示鏡像的完整信息 -q, --quiet:只顯示鏡像ID 示例 查看本地所有鏡像 ubuntu@VM-35-226-ubuntu:~$ sudo docker images -a REPOSITORY TAG IMAGE ID CREATED SIZE nginx latest f7bb5701a33c 9 days ago 126MB ubuntu@VM-35-226-ubuntu:~$ 查看鏡像詳細信息 ubuntu@VM-35-226-ubuntu:~$ sudo docker images --no-trunc nginx:latest REPOSITORY TAG IMAGE ID CREATED SIZE nginx latest sha256:f7bb5701a33c0e572ed06ca554edca1bee96cbbc1f76f3b01c985de7e19d0657 9 days ago 126MB ubuntu@VM-35-226-ubuntu:~$ 查看鏡像ID ubuntu@VM-35-226-ubuntu:~$ sudo docker images -q nginx:latest f7bb5701a33c ubuntu@VM-35-226-ubuntu:~$ ## run 創建容器,并運行 語法 docker run 參數 鏡像:tag(版本) 參數說明 -a, --attach: 指定標準輸入輸出內容類型,可選 STDIN/STDOUT/STDERR 三項 -d: 容器將在后臺運行 -t: 給容器分配tty終端 --sig-proxy=true: 將所有接收到的信號代理到進程(僅限非TTY模式) -i: 以交互模式運行容器 --name: 給容器設置名稱 --dns: 為容器設置自定義dns服務器 --dns-search: 指定DNS搜索域名 --network: 為容器指定網卡 --network-alias: 為容器的網卡設置別名 --add-host: 在容器的hosts文件中添加記錄 --mac-address: 設置容器的MAC地址 --ip: 設置容器的IPV4地址 --ip6: 設置容器IPV6地址 --link-local-ip: 設置一個或多個容器的以太網設備的鏈接本地IPv4/IPv6地址 --rm: 容器退出時自動刪除容器 -m, --memory: 設置容器內存大小,最小4M --memory-swap: 設置容器的總內存大小,包含交換分區 -c, --cpu-shares: 設置cpu --cpus: 設置cpu數量 --entrypoint: 在容器運行時執行的默認命令 --expose: 設置端口映射 -P: 將容器中所有端口映射到宿主機 -p: 將容器的某個端口映射到宿主機 --link: 將鏈接添加到另外一個容器 -v, --volume: 將宿主機的文件掛載到容器中 --volumes-from: 將宿主機的所有文件系統掛載到容器 -u,--user: 設置所使用的用戶名或UID以及指定命令的可選組名或GID -h: 指定容器主機名稱 -e: 設置環境變量 --env-file: 從指定文件中導入變量 參數還有很多,這里就不一一列舉了,上面是常用的參數 示例 后臺運行nginx容器,并取名為nginx ubuntu@VM-35-226-ubuntu:~$ sudo docker run -d --name nginx nginx:latest 06a913a442f9729e284b1a0628e5ce46561f81c6e8777f91d184f10de079cb55 ubuntu@VM-35-226-ubuntu:~$ sudo docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 06a913a442f9 nginx:latest "nginx -g 'daemon of…" 18 seconds ago Up 15 seconds 80/tcp nginx ubuntu@VM-35-226-ubuntu:~$ 后臺運行nginx容器,并映射容器80端口到宿主機的80端口 ubuntu@VM-35-226-ubuntu:~$ sudo docker run -d --name nginx -p 80:80 nginx:latest c026ee7e0c9477c15ac42965ed19af92b6017e7bcce079fd450f8cee5f2d9de6 ubuntu@VM-35-226-ubuntu:~$ sudo docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES c026ee7e0c94 nginx:latest "nginx -g 'daemon of…" 8 seconds ago Up 6 seconds 0.0.0.0:80->80/tcp nginx ubuntu@VM-35-226-ubuntu:~$ sudo curl 127.0.0.1 <!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> <h2>Welcome to nginx!</h2> <p>If you see this page, the nginx web server is successfully installed and working. Further configuration is required.</p> <p>For online documentation and support please refer to <a href="http://nginx.org/">nginx.org</a>.<br/> Commercial support is available at <a href="http://nginx.com/">nginx.com</a>.</p> <p><em>Thank you for using nginx.</em></p> </body> </html> ubuntu@VM-35-226-ubuntu:~$ 創建nginx容器,并進行交互式啟動 ubuntu@VM-35-226-ubuntu:~$ sudo docker run -it --name nginx nginx:latest /bin/bash root@4a518f4a2a01:/# ## history 查看鏡像形成過程 語法 docker history 參數 鏡像:tag(版本) 參數 --format:使用Go模板進行漂亮的打印搜索 -H, --human:以可讀格式打印大小和日期(默認為true) --no-trunc:顯示形成詳細信息 -q, --quiet:只顯示鏡像ID 示例 默認使用方法 ubuntu@VM-35-226-ubuntu:~$ sudo docker history nginx:latest IMAGE CREATED CREATED BY SIZE COMMENT f7bb5701a33c 9 days ago /bin/sh -c #(nop) CMD ["nginx" "-g" "daemon… 0B <missing> 9 days ago /bin/sh -c #(nop) STOPSIGNAL SIGTERM 0B <missing> 9 days ago /bin/sh -c #(nop) EXPOSE 80 0B <missing> 9 days ago /bin/sh -c ln -sf /dev/stdout /var/log/nginx… 22B <missing> 9 days ago /bin/sh -c set -x && addgroup --system -… 57.1MB <missing> 9 days ago /bin/sh -c #(nop) ENV PKG_RELEASE=1~buster 0B <missing> 9 days ago /bin/sh -c #(nop) ENV NJS_VERSION=0.3.7 0B <missing> 9 days ago /bin/sh -c #(nop) ENV NGINX_VERSION=1.17.6 0B <missing> 9 days ago /bin/sh -c #(nop) LABEL maintainer=NGINX Do… 0B <missing> 10 days ago /bin/sh -c #(nop) CMD ["bash"] 0B <missing> 10 days ago /bin/sh -c #(nop) ADD file:04caaf303199c81ff… 69.2MB ubuntu@VM-35-226-ubuntu:~$ 只打印鏡像ID ubuntu@VM-35-226-ubuntu:~$ sudo docker history -q nginx:latest f7bb5701a33c <missing> <missing> <missing> <missing> <missing> <missing> <missing> <missing> <missing> <missing> ubuntu@VM-35-226-ubuntu:~$ 顯示鏡像形成的詳細信息 ubuntu@VM-35-226-ubuntu:~$ sudo docker history --no-trunc nginx IMAGE CREATED CREATED BY SIZE COMMENT sha256:f7bb5701a33c0e572ed06ca554edca1bee96cbbc1f76f3b01c985de7e19d0657 9 days ago /bin/sh -c #(nop) CMD ["nginx" "-g" "daemon off;"] 0B <missing> 9 days ago /bin/sh -c #(nop) STOPSIGNAL SIGTERM 0B <missing> 9 days ago /bin/sh -c #(nop) EXPOSE 80 0B <missing> 9 days ago /bin/sh -c ln -sf /dev/stdout /var/log/nginx/access.log && ln -sf /dev/stderr /var/log/nginx/error.log 22B <missing> 9 days ago /bin/sh -c set -x && addgroup --system --gid 101 nginx && adduser --system --disabled-login --ingroup nginx --no-create-home --home /nonexistent --gecos "nginx user" --shell /bin/false --uid 101 nginx && apt-get update && apt-get install --no-install-recommends --no-install-suggests -y gnupg1 ca-certificates && NGINX_GPGKEY=573BFD6B3D8FBC641079A6ABABF5BD827BD9BF62; found=''; for server in ha.pool.sks-keyservers.net hkp://keyserver.ubuntu.com:80 hkp://p80.pool.sks-keyservers.net:80 pgp.mit.edu ; do echo "Fetching GPG key $NGINX_GPGKEY from $server"; apt-key adv --keyserver "$server" --keyserver-options timeout=10 --recv-keys "$NGINX_GPGKEY" && found=yes && break; done; test -z "$found" && echo >&2 "error: failed to fetch GPG key $NGINX_GPGKEY" && exit 1; apt-get remove --purge --auto-remove -y gnupg1 && rm -rf /var/lib/apt/lists/ && dpkgArch="$(dpkg --print-architecture)" && nginxPackages=" nginx=${NGINX_VERSION}-${PKG_RELEASE} nginx-module-xslt=${NGINX_VERSION}-${PKG_RELEASE} nginx-module-geoip=${NGINX_VERSION}-${PKG_RELEASE} nginx-module-image-filter=${NGINX_VERSION}-${PKG_RELEASE} nginx-module-njs=${NGINX_VERSION}.${NJS_VERSION}-${PKG_RELEASE} " && case "$dpkgArch" in amd64|i386) echo "deb https://nginx.org/packages/mainline/debian/ buster nginx" >> /etc/apt/sources.list.d/nginx.list && apt-get update ;; ) echo "deb-src https://nginx.org/packages/mainline/debian/ buster nginx" >> /etc/apt/sources.list.d/nginx.list && tempDir="$(mktemp -d)" && chmod 777 "$tempDir" && savedAptMark="$(apt-mark showmanual)" && apt-get update && apt-get build-dep -y $nginxPackages && ( cd "$tempDir" && DEB_BUILD_OPTIONS="nocheck parallel=$(nproc)" apt-get source --compile $nginxPackages ) && apt-mark showmanual | xargs apt-mark auto > /dev/null && { [ -z "$savedAptMark" ] || apt-mark manual $savedAptMark; } && ls -lAFh "$tempDir" && ( cd "$tempDir" && dpkg-scanpackages . > Packages ) && grep '^Package: ' "$tempDir/Packages" && echo "deb [ trusted=yes ] file://$tempDir ./" > /etc/apt/sources.list.d/temp.list && apt-get -o Acquire::GzipIndexes=false update ;; esac && apt-get install --no-install-recommends --no-install-suggests -y $nginxPackages gettext-base && apt-get remove --purge --auto-remove -y ca-certificates && rm -rf /var/lib/apt/lists/ /etc/apt/sources.list.d/nginx.list && if [ -n "$tempDir" ]; then apt-get purge -y --auto-remove && rm -rf "$tempDir" /etc/apt/sources.list.d/temp.list; fi 57.1MB <missing> 9 days ago /bin/sh -c #(nop) ENV PKG_RELEASE=1~buster 0B <missing> 9 days ago /bin/sh -c #(nop) ENV NJS_VERSION=0.3.7 0B <missing> 9 days ago /bin/sh -c #(nop) ENV NGINX_VERSION=1.17.6 ubuntu@VM-35-226-ubuntu:~$ sudo docker history --no-trunc nginx:latest IMAGE CREATED CREATED BY SIZE COMMENT sha256:f7bb5701a33c0e572ed06ca554edca1bee96cbbc1f76f3b01c985de7e19d0657 9 days ago /bin/sh -c #(nop) CMD ["nginx" "-g" "daemon off;"] 0B <missing> 9 days ago /bin/sh -c #(nop) STOPSIGNAL SIGTERM 0B <missing> 9 days ago /bin/sh -c #(nop) EXPOSE 80 0B <missing> 9 days ago /bin/sh -c ln -sf /dev/stdout /var/log/nginx/access.log && ln -sf /dev/stderr /var/log/nginx/error.log 22B <missing> 9 days ago /bin/sh -c set -x && addgroup --system --gid 101 nginx && adduser --system --disabled-login --ingroup nginx --no-create-home --home /nonexistent --gecos "nginx user" --shell /bin/false --uid 101 nginx && apt-get update && apt-get install --no-install-recommends --no-install-suggests -y gnupg1 ca-certificates && NGINX_GPGKEY=573BFD6B3D8FBC641079A6ABABF5BD827BD9BF62; found=''; for server in ha.pool.sks-keyservers.net hkp://keyserver.ubuntu.com:80 hkp://p80.pool.sks-keyservers.net:80 pgp.mit.edu ; do echo "Fetching GPG key $NGINX_GPGKEY from $server"; apt-key adv --keyserver "$server" --keyserver-options timeout=10 --recv-keys "$NGINX_GPGKEY" && found=yes && break; done; test -z "$found" && echo >&2 "error: failed to fetch GPG key $NGINX_GPGKEY" && exit 1; apt-get remove --purge --auto-remove -y gnupg1 && rm -rf /var/lib/apt/lists/ && dpkgArch="$(dpkg --print-architecture)" && nginxPackages=" nginx=${NGINX_VERSION}-${PKG_RELEASE} nginx-module-xslt=${NGINX_VERSION}-${PKG_RELEASE} nginx-module-geoip=${NGINX_VERSION}-${PKG_RELEASE} nginx-module-image-filter=${NGINX_VERSION}-${PKG_RELEASE} nginx-module-njs=${NGINX_VERSION}.${NJS_VERSION}-${PKG_RELEASE} " && case "$dpkgArch" in amd64|i386) echo "deb https://nginx.org/packages/mainline/debian/ buster nginx" >> /etc/apt/sources.list.d/nginx.list && apt-get update ;; ) echo "deb-src https://nginx.org/packages/mainline/debian/ buster nginx" >> /etc/apt/sources.list.d/nginx.list && tempDir="$(mktemp -d)" && chmod 777 "$tempDir" && savedAptMark="$(apt-mark showmanual)" && apt-get update && apt-get build-dep -y $nginxPackages && ( cd "$tempDir" && DEB_BUILD_OPTIONS="nocheck parallel=$(nproc)" apt-get source --compile $nginxPackages ) && apt-mark showmanual | xargs apt-mark auto > /dev/null && { [ -z "$savedAptMark" ] || apt-mark manual $savedAptMark; } && ls -lAFh "$tempDir" && ( cd "$tempDir" && dpkg-scanpackages . > Packages ) && grep '^Package: ' "$tempDir/Packages" && echo "deb [ trusted=yes ] file://$tempDir ./" > /etc/apt/sources.list.d/temp.list && apt-get -o Acquire::GzipIndexes=false update ;; esac && apt-get install --no-install-recommends --no-install-suggests -y $nginxPackages gettext-base && apt-get remove --purge --auto-remove -y ca-certificates && rm -rf /var/lib/apt/lists/ /etc/apt/sources.list.d/nginx.list && if [ -n "$tempDir" ]; then apt-get purge -y --auto-remove && rm -rf "$tempDir" /etc/apt/sources.list.d/temp.list; fi 57.1MB <missing> 9 days ago /bin/sh -c #(nop) ENV PKG_RELEASE=1~buster 0B <missing> 9 days ago /bin/sh -c #(nop) ENV NJS_VERSION=0.3.7 0B <missing> 9 days ago /bin/sh -c #(nop) ENV NGINX_VERSION=1.17.6 0B <missing> 9 days ago /bin/sh -c #(nop) LABEL maintainer=NGINX Docker Maintainers <docker-maint@nginx.com> 0B <missing> 10 days ago /bin/sh -c #(nop) CMD ["bash"] 0B <missing> 10 days ago /bin/sh -c #(nop) ADD file:04caaf303199c81ff1a94e2e39d5096f9d02b73294b82758e5bc6e23aff94272 in / 69.2MB ubuntu@VM-35-226-ubuntu:~$ ## start 啟動容器 語法 docker start 參數 容器ID/容器名稱 參數說明 -a, --attach:附加STDOUT/STDERR和轉發信號 --checkpoint:從快照進行還原 --checkpoint-dir:使用自定義檢查點存儲目錄 --detach-keys:重寫用于分離容器的鍵序列 -i, --interactive:附加容器的STDIN 示例 啟動使用create創建的nginx容器 ubuntu@VM-35-226-ubuntu:~$ sudo docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 8b9053318855 nginx:latest "nginx -g 'daemon of…" 9 seconds ago Created admiring_rhodes ubuntu@VM-35-226-ubuntu:~$ sudo docker start 8b9053318855 8b9053318855 ubuntu@VM-35-226-ubuntu:~$ sudo docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 8b9053318855 nginx:latest "nginx -g 'daemon of…" 40 seconds ago Up 7 seconds 80/tcp admiring_rhodes ubuntu@VM-35-226-ubuntu:~$ ## stop 關閉容器 語法 docker stop 參數 容器ID/容器名稱 參數說明 --time , -t:在多少時間后關閉容器 示例 默認使用方法 ubuntu@VM-121-163-ubuntu:~$ sudo docker run -d --name nginx nginx:latest 4c1e4ab3cfdcb7c5c55052238525e79bb82c0b5cf70b18420520ec2884669272 ubuntu@VM-121-163-ubuntu:~$ sudo docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 4c1e4ab3cfdc nginx:latest "nginx -g 'daemon of…" 7 seconds ago Up 5 seconds 80/tcp nginx ubuntu@VM-121-163-ubuntu:~$ sudo docker stop nginx nginx ubuntu@VM-121-163-ubuntu:~$ sudo docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 4c1e4ab3cfdc nginx:latest "nginx -g 'daemon of…" 26 seconds ago Exited (0) 5 seconds ago nginx ubuntu@VM-121-163-ubuntu:~$ ## build 通過dockerfile制作鏡像 語法 docker build 參數 路徑 參數說明 --build-arg:設置創建時的變量 --cgroup-parent:容器的可選父cgroup --compress:使用gzip壓縮構建上下文 --cpu-shares :設置 cpu 使用權重 --cpu-period :限制 CPU CFS周期 --cpu-quota :限制 CPU CFS配額 --cpuset-cpus :指定使用的CPU id --cpuset-mems :指定使用的內存 id --disable-content-trus:跳過鏡像驗證 --file , -f:指定dockerfile的名稱,默認為dockerfile --force-rm:始終移除中間容器 --iidfile:將鏡像ID寫入文件 --isolation:使用容器隔離 --label:為鏡像設置元數據 --memory , -m:設置內存限制 --memory-swap:設置總內存,包含交換內存 --network:設置鏡像網絡信息 --no-cache:構建鏡像不使用緩存 --output , -o:設置鏡像生成位置 --pull:始終嘗試提取圖像的較新版本 --quiet , -q: 禁止生成輸出并成功打印圖像ID --rm:成功構建后刪除中間容器 --tag, -t: 鏡像的名字及標簽 --ulimit :Ulimit配置 --shm-size :設置/dev/shm的大小,默認值是64M 示例 使用當前目錄下的dockerfile制作nginx:V1鏡像 docker build -t nginx:V1 . 創建nginx:V2鏡像,使用非當前目錄下的dockerfile docker build -t nginx:V2 -f /opt/dockerfile 添加其他主機到容器文件中 docker build --add-host=docker:1.1.1.1 . 使用參數--squash創建名為test:1的鏡像 docker build --squash -t test:1 . ## attach 當前shell連接運行容器 語法 docker attach 參數 容器ID/容器名稱 參數 --detach-keys: 覆蓋分離容器的鍵序列 --no-stdin:不要附上STDIN --sig-proxy:代理所有接收到的信號到進程 示例 默認使用方法 ubuntu@VM-121-163-ubuntu:~$ sudo docker run -d --name ubuntu ubuntu /usr/bin/top -b 98c3f0964ad3a38e360867941a5510cd77e164a835be5b9b69aa47d3d5ceed17 ubuntu@VM-121-163-ubuntu:~$ sudo docker attach ubuntu top - 08:24:58 up 1:12, 0 users, load average: 0.04, 0.06, 0.01 Tasks: 1 total, 1 running, 0 sleeping, 0 stopped, 0 zombie %Cpu(s): 2.3 us, 1.7 sy, 0.0 ni, 96.0 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st KiB Mem : 885080 total, 149760 free, 231524 used, 503796 buff/cache KiB Swap: 0 total, 0 free, 0 used. 499100 avail Mem PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND 1 root 20 0 36480 3100 2748 R 0.0 0.4 0:00.19 top top - 08:25:01 up 1:12, 0 users, load average: 0.04, 0.06, 0.01 Tasks: 1 total, 1 running, 0 sleeping, 0 stopped, 0 zombie %Cpu(s): 1.0 us, 0.7 sy, 0.0 ni, 98.0 id, 0.3 wa, 0.0 hi, 0.0 si, 0.0 st KiB Mem : 885080 total, 151252 free, 230048 used, 503780 buff/cache KiB Swap: 0 total, 0 free, 0 used. 500588 avail Mem PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND 1 root 20 0 36480 3100 2748 R 0.0 0.4 0:00.19 top top - 08:25:04 up 1:13, 0 users, load average: 0.04, 0.06, 0.01 Tasks: 1 total, 1 running, 0 sleeping, 0 stopped, 0 zombie %Cpu(s): 1.3 us, 1.0 sy, 0.0 ni, 97.7 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st KiB Mem : 885080 total, 151264 free, 230016 used, 503800 buff/cache KiB Swap: 0 total, 0 free, 0 used. 500616 avail Mem PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND 1 root 20 0 36480 3100 2748 R 0.0 0.4 0:00.19 top 您可以看到該bash 過程返回的退出代碼也由docker attach命令返回給其調用者 ubuntu@VM-121-163-ubuntu:~$ sudo docker run --name ubuntu -d -it ubuntu 9ac7802feb2b77bb905bc9f4c5ca9782e6800fc995b1f2b39da78239d86a7405 ubuntu@VM-121-163-ubuntu:~$ sudo docker attach ubuntu root@9ac7802feb2b:/# root@9ac7802feb2b:/# ## commit 保存當前容器為鏡像/快照 語法 docker commit 容器名稱/容器ID 鏡像名:tag(版本) 示例 ubuntu@VM-121-163-ubuntu:~$ sudo docker images //查看鏡像 REPOSITORY TAG IMAGE ID CREATED SIZE nginx latest f7bb5701a33c 9 days ago 126MB ubuntu latest 549b9b86cb8d 2 weeks ago 64.2MB ubuntu@VM-121-163-ubuntu:~$ sudo docker run -itd --name nginx nginx:latest //創建容器 9566c54f3883b0a3b2f92dc94eb8520772d5d05826d7025be23212768f4d7967 ubuntu@VM-121-163-ubuntu:~$ sudo docker ps -a //查看容器狀態 CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 9566c54f3883 nginx:latest "nginx -g 'daemon of…" 11 seconds ago Up 9 seconds 80/tcp nginx ubuntu@VM-121-163-ubuntu:~$ sudo docker commit nginx nginx:V1 //生成快照 sha256:e091997549ff02cf6309d6146b7626c6efef6dd9ce598c632f6bf0f4d090978c ubuntu@VM-121-163-ubuntu:~$ sudo docker images //查看鏡像 REPOSITORY TAG IMAGE ID CREATED SIZE nginx V1 e091997549ff 22 seconds ago 126MB nginx latest f7bb5701a33c 9 days ago 126MB ubuntu latest 549b9b86cb8d 2 weeks ago 64.2MB ubuntu@VM-121-163-ubuntu:~$ ## cp 容器與宿主機互相復制文件 語法 docker cp 容器ID/容器名:文件路徑 宿主機路徑 復制容器中文件到宿主機 docker cp 宿主機路徑 容器ID/容器名:文件存儲路徑 復制宿主機文件到容器 參數說明 -a, --archive:復制文件同時復制文件用戶信息 -L, --follow-link:保持源目標中的鏈接 示例 復制宿主機文件到容器的根目錄中 root@test:/jia/test# docker run -d --name nginx nginx:latest //創建并運行nginx容器 33b6339003ef41fd62b909fd79e9056d53d944b3172f0f02fc717fd64a2648e3 root@test:/jia/test# docker exec nginx ls / //列出容器根目錄 bin boot dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var root@test:/jia/test# docker cp test nginx:/ //復制文件到容器 root@test:/jia/test# docker exec nginx ls / //列出容器根目錄 bin boot dev etc home lib lib64 media mnt opt proc root run sbin srv sys test tmp usr var 從容器中復制文件到宿主目錄 root@test:/jia/test# ls //列出目錄 root@test:/jia/test# docker cp nginx:/test . //復制容器中文件到當前目錄 root@test:/jia/test# ls //列出目錄 test root@test:/jia/test# ## create 創建容器但不啟動容器 語法 docker create 參數 鏡像:tag(版本) 參數說明 --add-host: 在容器的hosts文件中添加記錄 -a, --attach: 指定標準輸入輸出內容類型,可選 STDIN/STDOUT/STDERR 三項 --cap-add: 添加linux功能 --cap-drop: 刪除linux功能 --cidfile: 將容器ID寫入文件 -c, --cpu-shares: 設置cpu --device: 將主機設備添加到容器 --disable-content-trus: 不對鏡像進行驗證 --dns: 為容器設置自定義dns服務器‘ --dns-opt: 設置DNS選項 --dns-search: 指定DNS搜索域名 --domainname: 設置容器域名 --env , -e: 設置環境變量 --env-file: 讀取變量文件進行設置容器變量 --expose: 設置端口映射 --hostname , -h: 設置容器主機名 --init: 在容器內運行一個初始化程序,以轉發信號并獲取進程 --ip: 設置容器的IPV4地址 --ip6: 設置容器IPV6地址 --isolation: 使用隔離技術 --link: 將鏈接添加到宿主機,也就是創建軟連接 --link-local-ip: 設置容器本地地址 --mac-address: 設置容器MAC地址 --memory , -m: 設置內存 --memory-swap: 設置總內存大小,包含交換內存 --mount: 掛載文件系統 --name: 設置容器名稱 --net: 將容器鏈接網絡 --net-alias: 為容器網絡起別名 --network: 容器鏈接到網絡 --network-alias: 為容器網絡起別名 --publish , -p: 將容器端口映射到宿主機 --publish-all , -P: 將容器所有端口映射到宿主機 --read-only: 將容器的根文件系統掛載為只讀 --restart: 容器退出時重新啟動策略以應用 --rm: 退出時自動刪除容器 --stop-signal: 停止容器的信號 --tty , -t: 分配終端 --user , -u: 設置運行容器的用戶 --workdir , -w: 設置容器的工作目錄 參數還有很多,這里就不一一列舉了,上面是常用的參數 示例 創建nginx容器并啟動 root@test:/jia/test# docker create -t -i nginx:latest /bin/bash //創建容器 6f2cea61b80c117fe877589073b5f259a57623bd511a89330c72c825c0cbe4fd root@test:/jia/test# docker ps -a //查看容器 CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 6f2cea61b80c nginx:latest "/bin/bash" 12 seconds ago Created funny_gould root@test:/jia/test# docker start -a -i 6f2cea61b80c //啟動容器 root@6f2cea61b80c:/# ls //列出容器當前目錄 bin boot dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var root@6f2cea61b80c:/# 創建名為nginx的容器并進行啟動 root@test:/jia/test# docker create --name nginx -t -i nginx:latest /bin/bash //創建容器 5487d362e25bfcd39c70754972eb964121ba8408f3f97c89cac930208882e745 root@test:/jia/test# docker ps -a //查看容器 CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 5487d362e25b nginx:latest "/bin/bash" 5 seconds ago Created nginx root@test:/jia/test# docker start -a -i nginx //啟動容器并進行鏈接 root@5487d362e25b:/# ls bin boot dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var root@5487d362e25b:/# 創建nginx容器并映射端口 root@test:/jia/test# docker create --name nginx -p 80:80-it nginx:latest /bin/bash //創建容器 4cc06988f4284f01281a338b914aff247a16969e33177088673a281d848f99d4 root@test:/jia/test# docker ps -a //查看容器 CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES ad7eb9da0f48 nginx:latest "/bin/bash" 2 minutes ago Exited (0) 2 seconds ago nginx root@test:/jia/test# docker start nginx //啟動容器 nginx root@test:/jia/test# docker ps -a //查看容器狀態 CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES ad7eb9da0f48 nginx:latest "/bin/bash" 2 minutes ago Up 5 seconds 0.0.0.0:8080->80/tcp nginx root@test:/jia/test# curl 127.0.0.1 <!DOCTYPE html> <html> <head> <title>Welcome to nginx!</title> <style> body { width: 35em; margin: 0 auto; font-family: Tahoma, Verdana, Arial, sans-serif; } </style> </head> <body> <h2>Welcome to nginx!</h2> <p>If you see this page, the nginx web server is successfully installed and working. Further configuration is required.</p> <p>For online documentation and support please refer to <a href="http://nginx.org/">nginx.org</a>.<br/> Commercial support is available at <a href="http://nginx.com/">nginx.com</a>.</p> <p><em>Thank you for using nginx.</em></p> </body> </html> root@test:/jia/test# ## diff 查看容器改動 語法 docker diff 容器 符號說明 A 添加了文件或目錄 D 文件或目錄已刪除 C 文件或目錄已更改 示例 默認使用方法 root@test:/jia/test# docker diff nginx C /root A /root/.bash_history root@test:/jia/test# 復制test文件到nginx容器中查看你改動情況 root@test:/jia/test# docker cp test nginx:/ //復制test文件到容器的根目錄 root@test:/jia/test# docker diff nginx A /test C /root A /root/.bash_history root@test:/jia/test# ## exec 在容器中執行命令 語法 docker exec 參數 容器ID/容器名稱 命令 參數說明 --detach , -d:后臺運行容器 --detach-keys:覆蓋分離容器的鍵序列 --env , -e:設置環境變量 --interactive , -i: 即使未連接STDIN也保持打開狀態 --privileged:賦予命令擴展權限 --tty , -t: 分配偽TTY --user , -u:指定運行用戶 --workdir , -w:設置工作目錄 示例 使用容器打印“hello word” root@test:/jia/test# docker run --name centos -itd centos:latest //創建并啟動容器 1b75dac7104122e021fb8cc7989c8992eb8502f34713e76ee87e6d6f7796e83c root@test:/jia/test# docker ps -a //查看容器 CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 1b75dac71041 centos:latest "/bin/bash" 4 seconds ago Up 3 seconds centos root@test:/jia/test# docker exec centos echo "hello word" //使用 echo 打印“hello word” hello word root@test:/jia/test# 給容器建立交互式shell,并查看容器IP地址 root@test:/jia/test# docker ps -a //查看容器 CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 1b75dac71041 centos:latest "/bin/bash" 2 minutes ago Up 2 minutes centos root@test:/jia/test# docker exec -it centos /bin/bash //建立bash交互式shell [root@1b75dac71041 /]# ip addr //查看IP地址 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo valid_lft forever preferred_lft forever 20: eth0@if21: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default link/ether 02:42:ac:11:00:02 brd ff:ff:ff:ff:ff:ff link-netnsid 0 inet 172.17.0.2/16 brd 172.17.255.255 scope global eth0 valid_lft forever preferred_lft forever [root@1b75dac71041 /]# 指定工作目錄,并打印路徑 root@test:/jia/test# docker ps -a //查看容器 CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 1b75dac71041 centos:latest "/bin/bash" 9 minutes ago Up 9 minutes centos root@test:/jia/test# docker exec -w /var/www/html centos pwd //設置工作目錄,并進行打印目錄 /var/www/html root@test:/jia/test# ## login 登入docker鏡像源服務器 語法 docker login 參數 服務器地址 參數說明 -p, --password:指定登錄服務器用戶的密碼 -u, --username:指定登錄服務器用戶的賬戶 示例 默認登錄dockerhub root@test:/jia/test# docker login Login with your Docker ID to push and pull images from Docker Hub. If you don't have a Docker ID, head over to https://hub.docker.com to create one. Username: blxt Password: WARNING! Your password will be stored unencrypted in /root/.docker/config.json. Configure a credential helper to remove this warning. See https://docs.docker.com/engine/reference/commandline/login/#credentials-store Login Succeeded root@test:/jia/test# 指定用戶名和密碼進行登錄 root@test:/jia/test# docker login -u blxt -p 123456 WARNING! Using --password via the CLI is insecure. Use --password-stdin. WARNING! Your password will be stored unencrypted in /root/.docker/config.json. Configure a credential helper to remove this warning. See https://docs.docker.com/engine/reference/commandline/login/#credentials-store Login Succeeded root@test:/jia/test# 登錄阿里鏡像倉庫 root@jia:/etc# docker login -u '弒天傾城' -p '123456' registry.cn-shenzhen.aliyuncs.com WARNING! Using --password via the CLI is insecure. Use --password-stdin. WARNING! Your password will be stored unencrypted in /root/.docker/config.json. Configure a credential helper to remove this warning. See https://docs.docker.com/engine/reference/commandline/login/#credentials-store Login Succeeded root@jia:/etc# ## logout 退出登錄鏡像源服務器 語法 docker logout 鏡像倉庫地址 示例 退出dockerhub用戶登錄 root@jia:/etc# docker login -u blxt -p 'JKY@jjj.123' //登錄dockerhub服務器 WARNING! Using --password via the CLI is insecure. Use --password-stdin. WARNING! Your password will be stored unencrypted in /root/.docker/config.json. Configure a credential helper to remove this warning. See https://docs.docker.com/engine/reference/commandline/login/#credentials-store Login Succeeded root@jia:/etc# docker logout //退出登錄 Removing login credentials for https://index.docker.io/v1/ root@jia:/etc# 退出阿里鏡像倉庫 root@jia:/etc# docker login -u '弒天傾城' -p '123456' registry.cn-shenzhen.aliyuncs.com //登錄阿里鏡像倉庫 WARNING! Using --password via the CLI is insecure. Use --password-stdin. WARNING! Your password will be stored unencrypted in /root/.docker/config.json. Configure a credential helper to remove this warning. See https://docs.docker.com/engine/reference/commandline/login/#credentials-store Login Succeeded root@jia:/etc# docker logout //退出失敗沒有登錄dockerhub倉庫 Not logged in to https://index.docker.io/v1/ root@jia:/etc# docker logout registry.cn-shenzhen.aliyuncs.com Removing login credentials for registry.cn-shenzhen.aliyuncs.com //退出登錄阿里鏡像倉庫 root@jia:/etc# ## logs 輸出當前容器的日志信息 語法 docker logs 參數 容器ID/容器名 參數說明 --details:查看日志詳細信息 --follow , -f:實時動態的查看日志 --since:顯示生成日志的時間 --tail:顯示日志行數 --timestamps , -t:顯示時間戳 --until:顯示某一時間段的日志信息 示例 默認查看容器日志 root@jia:/etc# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 85c8be92df44 nginx:latest "nginx -g 'daemon of…" 7 minutes ago Up 7 minutes 80/tcp nginx root@jia:/etc# docker logs nginx 172.17.0.1 - - [08/Jan/2020:08:19:02 +0000] "GET / HTTP/1.1" 200 612 "-" "curl/7.58.0" "-" 172.17.0.1 - - [08/Jan/2020:08:19:04 +0000] "GET / HTTP/1.1" 200 612 "-" "curl/7.58.0" "-" 172.17.0.1 - - [08/Jan/2020:08:19:07 +0000] "GET / HTTP/1.1" 200 612 "-" "curl/7.58.0" "-" root@jia:/etc# 實時顯示nginx容器的日志 root@jia:/etc# docker run --name nginx -itd nginx:latest //創建并啟動nginx容器 85c8be92df44960e0973ae2b48907661679d921d0523cb93526351f07850f290 root@jia:/etc# docker ps -a //查看容器 CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 85c8be92df44 nginx:latest "nginx -g 'daemon of…" 4 seconds ago Up 3 seconds 80/tcp nginx root@jia:~# docker inspect nginx | IPAddress //查看容器的ip地址 IPAddress: command not found root@jia:~# docker inspect nginx | grep IPAddress "SecondaryIPAddresses": null, "IPAddress": "172.17.0.2", "IPAddress": "172.17.0.2", root@jia:~# curl 127.0.0.1 //訪問nginx服務 <!DOCTYPE html> <html> <head> <title>Welcome to nginx!</title> <style> body { width: 35em; margin: 0 auto; font-family: Tahoma, Verdana, Arial, sans-serif; } </style> </head> <body> <h2>Welcome to nginx!</h2> <p>If you see this page, the nginx web server is successfully installed and working. Further configuration is required.</p> <p>For online documentation and support please refer to <a href="http://nginx.org/">nginx.org</a>.<br/> Commercial support is available at <a href="http://nginx.com/">nginx.com</a>.</p> <p><em>Thank you for using nginx.</em></p> </body> </html> root@jia:/etc# docker logs -f nginx //輸出日志信息 172.17.0.1 - - [08/Jan/2020:08:19:02 +0000] "GET / HTTP/1.1" 200 612 "-" "curl/7.58.0" "-" 172.17.0.1 - - [08/Jan/2020:08:19:04 +0000] "GET / HTTP/1.1" 200 612 "-" "curl/7.58.0" "-" 172.17.0.1 - - [08/Jan/2020:08:19:07 +0000] "GET / HTTP/1.1" 200 612 "-" "curl/7.58.0" "-" ## port 查看容器的端口映射情況 語法 docker port 容器ID/容器名稱 示例 列出nginx容器所有端口映射情況 root@jia:/etc# docker run --name nginx -itd -P nginx:latest //創建并啟動容器,映射容器所有端口 b4e3a51697c2e35b4a17acee93630ac77c6e9fe94df99c70750b60c4f87b535a root@jia:/etc# docker ps -a //查看容器 CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES b4e3a51697c2 nginx:latest "nginx -g 'daemon of…" 4 seconds ago Up 3 seconds 0.0.0.0:32768->80/tcp nginx root@jia:/etc# docker port nginx //顯示容器端口映射情況 80/tcp -> 0.0.0.0:32768 root@jia:/etc# ## ps 列出容器列表 語法 docker ps 參數 參數說明 --all , -a:查看所有容器 --filter , -f:根據相關條件進行過濾 --format: 使用Go模板打印漂亮的容器 --last , -n:顯示第幾個創建的容器 --latest , -l:顯示最新創建的容器 --no-trunc:顯示詳細信息 --quiet , -q:只顯示容器ID --size , -s:顯示容器的總大小 示例 查看所有容器 root@jia:~# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 79b53ebbc86e centos:latest "/bin/bash" 5 seconds ago Created fervent_payne b4e3a51697c2 nginx:latest "nginx -g 'daemon of…" 13 minutes ago Up 13 minutes 0.0.0.0:32768->80/tcp nginx root@jia:~# 查看正在運行的容器 root@jia:~# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES b4e3a51697c2 nginx:latest "nginx -g 'daemon of…" 13 minutes ago Up 13 minutes 0.0.0.0:32768->80/tcp nginx root@jia:~# ## restart 重新啟動容器 語法 docker restart 容器名稱/容器ID 參數 -t:設置重啟執行時間 示例 root@jia:~# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES b4e3a51697c2 nginx:latest "nginx -g 'daemon of…" 16 minutes ago Up 16 minutes 0.0.0.0:32768->80/tcp nginx root@jia:~# docker restart nginx nginx root@jia:~# ## rm 刪除容器 語法 docker rm 參數 容器ID/容器名稱 參數 --force , -f:根據條件進行刪除容器,強制刪除正在運行的容器 --link , -l:移除容器間的網絡連接,而非容器本身 -v, --volumes:刪除與容器關聯的文件系統 示例 刪除nginx容器 root@jia:~# docker ps -a //查看容器 CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES c3800e1bda43 centos "/bin/bash" About a minute ago Up About a minute centos b4e3a51697c2 nginx:latest "nginx -g 'daemon of…" 22 minutes ago Up 5 minutes 0.0.0.0:32769->80/tcp nginx root@jia:~# docker rm nginx //刪除容器,但nginx容器正在運行所以無法使用此參數刪除 Error response from daemon: You cannot remove a running container b4e3a51697c2e35b4a17acee93630ac77c6e9fe94df99c70750b60c4f87b535a. Stop the container before attempting removal or force remove root@jia:~# docker rm -f nginx /制刪除nginx容器 nginx root@jia:~# docker ps -a //查看容器 CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES c3800e1bda43 centos "/bin/bash" About a minute ago Up About a minute centos root@jia:~# 刪除所有容器 root@jia:~# docker ps -a //查看容器 CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 4ee9b7488e32 nginx "nginx -g 'daemon of…" 26 seconds ago Up 25 seconds 80/tcp nginx c3800e1bda43 centos "/bin/bash" 4 minutes ago Up 4 minutes centos root@jia:~# docker rm $(docker ps -a -q) //刪除容器失敗,因為容器正在運行 Error response from daemon: You cannot remove a running container 4ee9b7488e323c80d9acaa01a214d00bc3072e2430df5ce7a0560acea0c9587c. Stop the container before attempting removal or force remove Error response from daemon: You cannot remove a running container c3800e1bda43416add00ac18fb9c2d15e7d106e302ceef090e04d7fa6133368d. Stop the container before attempting removal or force remove root@jia:~# docker rm -f $(docker ps -a -q) //刪除所有容器 4ee9b7488e32 c3800e1bda43 root@jia:~# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES root@jia:~# ## rmi 刪除本地鏡像 語法 docker rmi 參數 鏡像名稱/鏡像ID 參數 --force , -f:強制刪除 --no-prune:不移除該鏡像的過程鏡像,默認移除 示例 默認刪除鏡像 root@jia:~# docker images //查看本地鏡像 REPOSITORY TAG IMAGE ID CREATED SIZE nginx latest f7bb5701a33c 10 days ago 126MB ubuntu latest 549b9b86cb8d 2 weeks ago 64.2MB centos latest 0f3e07c0138f 3 months ago 220MB root@jia:~# docker rmi 549b9b86cb8d //刪除ubuntu的鏡像 Untagged: ubuntu:latest Untagged: ubuntu@sha256:250cc6f3f3ffc5cdaa9d8f4946ac79821aafb4d3afc93928f0de9336eba21aa4 Deleted: sha256:549b9b86cb8d75a2b668c21c50ee092716d070f129fd1493f95ab7e43767eab8 Deleted: sha256:7c52cdc1e32d67e3d5d9f83c95ebe18a58857e68bb6985b0381ebdcec73ff303 Deleted: sha256:a3c2e83788e20188bb7d720f36ebeef2f111c7b939f1b19aa1b4756791beece0 Deleted: sha256:61199b56f34827cbab596c63fd6e0ac0c448faa7e026e330994818190852d479 Deleted: sha256:2dc9f76fb25b31e0ae9d36adce713364c682ba0d2fa70756486e5cedfaf40012 root@jia:~# docker images //查看本地鏡像 REPOSITORY TAG IMAGE ID CREATED SIZE nginx latest f7bb5701a33c 10 days ago 126MB centos latest 0f3e07c0138f 3 months ago 220MB root@jia:~# 強制刪除正在運行的鏡像 root@jia:~# docker run -itd --name nginx nginx //啟動創建容器 36b203a3fb5cf8fe6a1e13e298998c8510f7d24e49da36ada8d7181d89b16089 root@jia:~# docker ps -a //查看容器 CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 36b203a3fb5c nginx "nginx -g 'daemon of…" 3 seconds ago Up 2 seconds 80/tcp nginx root@jia:~# docker images //查看本地鏡像 REPOSITORY TAG IMAGE ID CREATED SIZE nginx latest f7bb5701a33c 10 days ago 126MB centos latest 0f3e07c0138f 3 months ago 220MB root@jia:~# docker rmi nginx:latest //默認刪除鏡像,刪除失敗,報錯鏡像正在使用 Error response from daemon: conflict: unable to remove repository reference "nginx:latest" (must force) - container 36b203a3fb5c is using its referenced image f7bb5701a33c root@jia:~# docker rmi -f nginx:latest /制刪除鏡像 Untagged: nginx:latest Untagged: nginx@sha256:b2d89d0a210398b4d1120b3e3a7672c16a4ba09c2c4a0395f18b9f7999b768f2 root@jia:~# docker images //查看鏡像 REPOSITORY TAG IMAGE ID CREATED SIZE <none> <none> f7bb5701a33c 10 days ago 126MB centos latest 0f3e07c0138f 3 months ago 220MB root@jia:~# 注意:當強制刪除鏡像后,會直接刪除,但是會生成新的鏡像,容器不會被刪除 ## save 保存鏡像為tar包 語法 docker save 參數 鏡像名稱 文件目錄及名稱.tar 參數說明 --output , -o:保存到某文件 示例 將nginx鏡像保存為文件 root@jia:/jia/jia# docker images REPOSITORY TAG IMAGE ID CREATED SIZE nginx latest f7bb5701a33c 11 days ago 126MB centos latest 0f3e07c0138f 3 months ago 220MB root@jia:/jia/jia# docker save nginx:latest > nginx.tar root@jia:/jia/jia# ls nginx.tar root@jia:/jia/jia# 將多個鏡像保存到一個文件 root@jia:/jia/jia# docker save -o test.tar nginx:latest centos:latest root@jia:/jia/jia# ls nginx.tar test.tar root@jia:/jia/jia# 使用gzip+tar進行打包壓縮 root@jia:/jia/jia# docker save nginx:latest | gzip > nginx.tar.gz root@jia:/jia/jia# ls -lh //查看目錄中文件長格式顯示 total 513M -rw-r--r-- 1 root root 125M Jan 9 16:57 nginx.tar -rw-r--r-- 1 root root 47M Jan 9 17:03 nginx.tar.gz -rw------- 1 root root 342M Jan 9 17:00 test.tar root@jia:/jia/jia# ## top 查看容器中進程信息 語法 docker top 參數 容器ID/容器名稱 示例 查看nginx容器中所有進程信息 root@jia:/jia/jia# docker run --name nginx -itd nginx:latest //創建啟動nginx容器 69e353d8564025e07ee201098cafb9870d95b92edf990db2455aa6261c0bede6 root@jia:/jia/jia# docker ps -a //列出所有容器 CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 69e353d85640 nginx:latest "nginx -g 'daemon of…" 5 seconds ago Up 4 seconds 80/tcp nginx root@jia:/jia/jia# docker top nginx UID PID PPID C STIME TTY TIME CMD root 28665 28637 1 17:08 pts/0 00:00:00 nginx: master process nginx -g daemon off; systemd+ 28724 28665 0 17:08 pts/0 00:00:00 nginx: worker process root@jia:/jia/jia# ## version 查看docker軟件版本 語法 docker version 參數 參數說明 -f, --format:使用給定的Go模板格式化輸出 --kubeconfig:Kubernetes配置文件 示例 查看docker版本詳細信息 root@jia:/jia/jia# docker version Client: Docker Engine - Community Version: 19.03.5 API version: 1.40 Go version: go1.12.12 Git commit: 633a0ea838 Built: Wed Nov 13 07:29:52 2019 OS/Arch: linux/amd64 Experimental: false Server: Docker Engine - Community Engine: Version: 19.03.5 API version: 1.40 (minimum version 1.12) Go version: go1.12.12 Git commit: 633a0ea838 Built: Wed Nov 13 07:28:22 2019 OS/Arch: linux/amd64 Experimental: false containerd: Version: 1.2.10 GitCommit: b34a5c8af56e510852c35414db4c1f4fa6172339 runc: Version: 1.0.0-rc8+dev GitCommit: 3e425f80a8c931f88e6d94a8c831b9d5aa481657 docker-init: Version: 0.18.0 GitCommit: fec3683 root@jia:/jia/jia# 查看docker版本 root@jia:/jia/jia# docker version --format '{{.Server.Version}}' 19.03.5 root@jia:/jia/jia# ## events 查看docker服務器實時事件 語法 docker events 參數 參數說明 -f,--filter:根據條件過濾事件 --since :從指定的時間戳后顯示所有事件 --until :流水時間顯示到指定的時間為止 --format:使用給定的Go模板格式化輸出 示例 容器啟動停止,使用events顯示事件 root@jia:/jia/jia# docker ps -a //查看容器列表 CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 69e353d85640 nginx:latest "nginx -g 'daemon of…" 13 minutes ago Up 13 minutes 80/tcp nginx root@jia:/jia/jia# docker stop nginx //停止容器 nginx root@jia:/jia/jia# docker start nginx //啟動容器 nginx root@jia:/jia/jia# root@jia:~# docker events //查看實時事件,注意此命令不會自動終止需要crtl+c終止 2020-01-09T17:22:26.880177305+08:00 container kill 69e353d8564025e07ee201098cafb9870d95b92edf990db2455aa6261c0bede6 (image=nginx:latest, maintainer=NGINX Docker Maintainers <docker-maint@nginx.com>, name=nginx, signal=15) 2020-01-09T17:22:27.005016080+08:00 container die 69e353d8564025e07ee201098cafb9870d95b92edf990db2455aa6261c0bede6 (exitCode=0, image=nginx:latest, maintainer=NGINX Docker Maintainers <docker-maint@nginx.com>, name=nginx) 2020-01-09T17:22:27.103273324+08:00 network disconnect f2ccf449e47668433fb96511ef577c7515be7f481a7a9cb386c9d46650cbabbf (container=69e353d8564025e07ee201098cafb9870d95b92edf990db2455aa6261c0bede6, name=bridge, type=bridge) 2020-01-09T17:22:27.145650268+08:00 container stop 69e353d8564025e07ee201098cafb9870d95b92edf990db2455aa6261c0bede6 (image=nginx:latest, maintainer=NGINX Docker Maintainers <docker-maint@nginx.com>, name=nginx) 2020-01-09T17:22:33.680477968+08:00 network connect f2ccf449e47668433fb96511ef577c7515be7f481a7a9cb386c9d46650cbabbf (container=69e353d8564025e07ee201098cafb9870d95b92edf990db2455aa6261c0bede6, name=bridge, type=bridge) 2020-01-09T17:22:34.093551544+08:00 container start 69e353d8564025e07ee201098cafb9870d95b92edf990db2455aa6261c0bede6 (image=nginx:latest, maintainer=NGINX Docker Maintainers <docker-maint@nginx.com>, name=nginx) ## import 從tar文件導入docker為鏡像 語法 docker import 參數 tar文件目錄名稱 導入的鏡像名稱:tag(版本) 參數說明 --change , -c: 將Dockerfile指令應用于創建的映像 --message , -m: 設置導入圖像的提交消息 示例 將nginx.tar的鏡像包導入docker并命名nginx版本V1 root@jia:/jia/jia# docker images //查看本地所有鏡像 REPOSITORY TAG IMAGE ID CREATED SIZE nginx latest f7bb5701a33c 11 days ago 126MB centos latest 0f3e07c0138f 3 months ago 220MB root@jia:/jia/jia# ls nginx.tar nginx.tar.gz test.tar root@jia:/jia/jia# docker import nginx.tar nginx:V1 sha256:07b2b1766ed11df9e7a12f98a4c5127c1aa0087e6b7d4eb0bc21490f168c1b0d root@jia:/jia/jia# docker images REPOSITORY TAG IMAGE ID CREATED SIZE nginx V1 07b2b1766ed1 3 seconds ago 130MB nginx latest f7bb5701a33c 11 days ago 126MB centos latest 0f3e07c0138f 3 months ago 220MB root@jia:/jia/jia# 導入使用tar打包gzip壓縮的鏡像 root@jia:/jia/jia# docker import nginx.tar.gz nginx:V2 sha256:ca7505dbea914f7babb1ed3667d1a133461c344d92ea978585b64fe7dabf5b36 root@jia:/jia/jia# docker images REPOSITORY TAG IMAGE ID CREATED SIZE nginx V2 ca7505dbea91 8 seconds ago 130MB nginx V1 07b2b1766ed1 2 minutes ago 130MB nginx latest f7bb5701a33c 11 days ago 126MB centos latest 0f3e07c0138f 3 months ago 220MB root@jia:/jia/jia# ## export 從docker導出容器為tar文件 語法 docker export 參數 容器名稱/容器ID 參數說明 --output , -o:寫入文件 示例 導出centos容器并命名為centos.tar root@jia:/jia/jia# docker run --name centos -itd centos:latest //創建并啟動centos容器 06b5abbb068dc493056aae6f1526b81028f931fd160fe470a937e15ac0110bdf root@jia:/jia/jia# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 06b5abbb068d centos:latest "/bin/bash" 4 seconds ago Up 3 seconds centos 69e353d85640 nginx:latest "nginx -g 'daemon of…" 32 minutes ago Up 18 minutes 80/tcp nginx root@jia:/jia/jia# docker export centos > centos.tar root@jia:/jia/jia# ls centos.tar nginx.tar nginx.tar.gz test.tar root@jia:/jia/jia# 導出centos容器并命名為centos.tar.gz,使用gzip壓縮 root@jia:/jia/jia# docker export centos > centos.tar root@jia:/jia/jia# ls centos.tar nginx.tar nginx.tar.gz test.tar root@jia:/jia/jia# docker export centos | gzip > centos.tar.gz root@jia:/jia/jia# ls -lh total 796M -rw-r--r-- 1 root root 217M Jan 9 17:41 centos.tar -rw-r--r-- 1 root root 67M Jan 9 17:43 centos.tar.gz -rw-r--r-- 1 root root 125M Jan 9 16:57 nginx.tar -rw-r--r-- 1 root root 47M Jan 9 17:03 nginx.tar.gz -rw------- 1 root root 342M Jan 9 17:00 test.tar root@jia:/jia/jia# 導出nginx容器,并命名nginx1.tar root@jia:/jia/jia# docker export -o nginx1.tar nginx root@jia:/jia/jia# ls centos.tar centos.tar.gz nginx1.tar nginx.tar nginx.tar.gz test.tar root@jia:/jia/jia# ## info 查看docker系統信息 語法 docekr info 參數 參數說明 --format , -f:使用給定的Go模板格式化輸出 示例 root@jia:/jia/jia# docker info Client: Debug Mode: false Server: Containers: 2 Running: 2 Paused: 0 Stopped: 0 Images: 4 Server Version: 19.03.5 Storage Driver: overlay2 Backing Filesystem: extfs Supports d_type: true Native Overlay Diff: true Logging Driver: json-file Cgroup Driver: cgroupfs Plugins: Volume: local Network: bridge host ipvlan macvlan null overlay Log: awslogs fluentd gcplogs gelf journald json-file local logentries splunk syslog Swarm: inactive Runtimes: runc Default Runtime: runc Init Binary: docker-init containerd version: b34a5c8af56e510852c35414db4c1f4fa6172339 runc version: 3e425f80a8c931f88e6d94a8c831b9d5aa481657 init version: fec3683 Security Options: apparmor seccomp Profile: default Kernel Version: 4.15.0-65-generic Operating System: Ubuntu 18.04.3 LTS OSType: linux Architecture: x86_64 CPUs: 2 Total Memory: 3.852GiB Name: jia ID: 74XS:4TCO:7OQX:BKF2:O2WW:JYEF:6Y6Q:RT3X:YVRO:Y2LP:JZIA:AHSB Docker Root Dir: /var/lib/docker Debug Mode: false Registry: https://index.docker.io/v1/ Labels: Experimental: false Insecure Registries: 127.0.0.0/8 Live Restore Enabled: false WARNING: No swap limit support root@jia:/jia/jia# ## inspect 查看容器詳細信息 語法 docker inspect 參數 容器ID/容器名稱 參數說明 --format , -f:使用給定的Go模板格式化輸出 --size , -s:顯示文件總大小 --type :為指定類型返回JSON 示例 獲取容器IP地址 第一種方式: root@jia:/jia/jia# docker ps -a //查看容器 CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 06b5abbb068d centos:latest "/bin/bash" 17 hours ago Up 17 hours centos 69e353d85640 nginx:latest "nginx -g 'daemon of…" 17 hours ago Up 17 hours 80/tcp nginx root@jia:/jia/jia# docker inspect centos | grep IPAddress "SecondaryIPAddresses": null, "IPAddress": "172.17.0.3", "IPAddress": "172.17.0.3", root@jia:/jia/jia# 第二種方式:(只顯示IP地址) root@jia:/jia/jia# docker inspect --format='{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' centos 172.17.0.3 root@jia:/jia/jia# 獲取容器MAC地址 root@jia:/jia/jia# docker inspect centos | grep MacAddress "MacAddress": "02:42:ac:11:00:03", "MacAddress": "02:42:ac:11:00:03", root@jia:/jia/jia# 獲取容器日志目錄 root@jia:/jia/jia# docker inspect centos | grep log "LogPath": "/var/lib/docker/containers/06b5abbb068dc493056aae6f1526b81028f931fd160fe470a937e15ac0110bdf/06b5abbb068dc493056aae6f1526b81028f931fd160fe470a937e15ac0110bdf-json.log", root@jia:/jia/jia# ## kill 殺死正在運行的容器 語法 docker kill 參數 容器ID/容器名稱 參數說明 --signal , -s:向容器發送信號 示例 殺死正在運行的centos容器 root@jia:/jia/jia# docker ps //查看容器 CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 06b5abbb068d centos:latest "/bin/bash" 17 hours ago Up 17 hours centos 69e353d85640 nginx:latest "nginx -g 'daemon of…" 18 hours ago Up 17 hours 80/tcp nginx root@jia:/jia/jia# docker kill centos centos root@jia:/jia/jia# docker ps -a //查看所有容器 CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 06b5abbb068d centos:latest "/bin/bash" 17 hours ago Exited (137) 6 seconds ago centos 69e353d85640 nginx:latest "nginx -g 'daemon of…" 18 hours ago Up 17 hours 80/tcp nginx root@jia:/jia/jia# 殺死正在運行的nginx容器,發送kill信號 root@jia:/jia/jia# docker kill --signal=kill nginx nginx root@jia:/jia/jia# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 06b5abbb068d centos:latest "/bin/bash" 17 hours ago Exited (137) 3 minutes ago centos 69e353d85640 nginx:latest "nginx -g 'daemon of…" 18 hours ago Exited (137) 1 second ago nginx root@jia:/jia/jia# ## load 從tar文件中加載為docker鏡像 語法 docker load 參數 tar文件 參數說明 -i, --input:從tar存檔文件中讀取,而不是從STDIN中讀取 --quiet , -q:顯示簡要信息 示例 導入nginx鏡像 root@jia:/jia/jia# docker images //查看所有鏡像 REPOSITORY TAG IMAGE ID CREATED SIZE centos latest 0f3e07c0138f 3 months ago 220MB root@jia:/jia/jia# docker load < nginx.tar.gz 556c5fb0d91b: Loading layer [==================================================>] 72.48MB/72.48MB 49434cc20e95: Loading layer [==================================================>] 57.67MB/57.67MB 75248c0d5438: Loading layer [==================================================>] 3.584kB/3.584kB Loaded image: nginx:latest root@jia:/jia/jia# docker images //查看所有鏡像 REPOSITORY TAG IMAGE ID CREATED SIZE nginx latest f7bb5701a33c 12 days ago 126MB centos latest 0f3e07c0138f 3 months ago 220MB root@jia:/jia/jia# ## pause 暫停容器 語法 docker pause 容器ID/容器名稱 示例 暫停centos容器 root@jia:/jia/jia# docker run -itd --name centos centos:latest //創建并運行 48e49042b927f427aed68437e12e5acd14b82e3a33a71b6f236bcef5c3edd41d root@jia:/jia/jia# docker ps //查看容器 CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 48e49042b927 centos:latest "/bin/bash" 2 seconds ago Up 2 seconds centos root@jia:/jia/jia# docker pause centos centos root@jia:/jia/jia# docker ps -a //查看所有容器 CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 48e49042b927 centos:latest "/bin/bash" 14 seconds ago Up 13 seconds (Paused) centos root@jia:/jia/jia# 容器暫停后會在狀態處加(Paused) ## unpause 取消容器暫停狀態 語法 docker unpause 容器ID/容器名稱 示例 取消centos容器的暫停操作 root@jia:/jia/jia# docker ps -a //查看所有容器 CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 48e49042b927 centos:latest "/bin/bash" 14 seconds ago Up 13 seconds (Paused) centos root@jia:/jia/jia# docker unpause centos centos root@jia:/jia/jia# docker ps -a //查看所有容器 CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 48e49042b927 centos:latest "/bin/bash" 5 minutes ago Up 5 minutes centos root@jia:/jia/jia# ## tag 給鏡像進行打標簽 語法 docker tag 源鏡像ID/源鏡像名稱:tag(版本) 目標鏡像名稱:tag(版本) 示例 給centos鏡像重新打標簽 root@jia:/jia/jia# docker tag centos:latest centos:V1 root@jia:/jia/jia# docker images REPOSITORY TAG IMAGE ID CREATED SIZE nginx latest f7bb5701a33c 12 days ago 126MB centos V1 0f3e07c0138f 3 months ago 220MB centos latest 0f3e07c0138f 3 months ago 220MB root@jia:/jia/jia# ## wait 阻塞運行直到容器停止,然后打印出它的退出代碼 語法 docekr wait 容器ID/容器名稱 示例 root@jia:/jia/jia# docker run -itd --name nginx nginx //創建啟動容器 57af5446f0eaff56a54c3a5559ac0073511de9b9f48c93ff04bbcd0c62b3a809 root@jia:/jia/jia# docker ps //查看容器 CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 57af5446f0ea nginx "nginx -g 'daemon of…" 4 seconds ago Up 3 seconds 80/tcp nginx root@jia:/jia/jia# docker inspect nginx | grep IPAddress //查看容器IP地址 "SecondaryIPAddresses": null, "IPAddress": "172.17.0.2", "IPAddress": "172.17.0.2", root@jia:/jia/jia# curl 172.17.0.2 //訪問容器的80端口 <!DOCTYPE html> <html> <head> <title>Welcome to nginx!</title> <style> body { width: 35em; margin: 0 auto; font-family: Tahoma, Verdana, Arial, sans-serif; } </style> </head> <body> <h2>Welcome to nginx!</h2> <p>If you see this page, the nginx web server is successfully installed and working. Further configuration is required.</p> <p>For online documentation and support please refer to <a href="http://nginx.org/">nginx.org</a>.<br/> Commercial support is available at <a href="http://nginx.com/">nginx.com</a>.</p> <p><em>Thank you for using nginx.</em></p> </body> </html> root@jia:/jia/jia# docker wait nginx //啟動阻塞 root@jia:~# curl 172.18.0.2 //發現無法訪問容器nginx頁面上次只是給大家把命令的作用以及簡單使用列出來了(大家可以查看:docker命令總結(一)),那這篇文章會詳細介紹每條命令的參數 ## search docker search:此命令是用來在倉庫中查找docker鏡像 語法: docker search 參數 鏡像名稱:tag(版本) 參數說明: --filter,-f:根據相關條件過濾輸出 --format:使用Go模板進行漂亮的打印搜索 --limit:指定最大搜索結果,默認25個 --no-trunc:顯示完整的鏡像描述 --stars,-s:列出收藏數不小于指定值的鏡像 --automated:僅顯示自動構建的鏡像 示例: 默認搜索 ubuntu@VM-101-242-ubuntu:~$ sudo docker search nginx NAME DESCRIPTION STARS OFFICIAL AUTOMATED nginx Official build of Nginx. 12438 jwilder/nginx-proxy Automated Nginx reverse proxy for docker con… 1713 richarvey/nginx-php-fpm Container running Nginx + PHP-FPM capable of… 750 linuxserver/nginx An Nginx container, brought to you by LinuxS… 84 bitnami/nginx Bitnami nginx Docker Image 75 tiangolo/nginx-rtmp Docker image with Nginx using the nginx-rtmp… 60 nginxdemos/hello NGINX webserver that serves a simple page co… 35 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 nginx/nginx-ingress NGINX Ingress Controller for Kubernetes 22 privatebin/nginx-fpm-alpine PrivateBin running on an Nginx, php-fpm & Al… 19 schmunk42/nginx-redirect A very simple container to redirect HTTP tra… 17 blacklabelops/nginx Dockerized Nginx Reverse Proxy Server. 12 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 mailu/nginx Mailu nginx frontend 5 1science/nginx Nginx Docker images that include Consul Temp… 5 pebbletech/nginx-proxy nginx-proxy sets up a container running ngin… 2 ansibleplaybookbundle/nginx-apb An APB to deploy NGINX 1 centos/nginx-110-centos7 Platform for running nginx 1.10 or building … 0 wodby/nginx Generic nginx 0 ubuntu@VM-101-242-ubuntu:~$ 搜索收藏數大于10的nginx鏡像,并顯示詳細信息 ubuntu@VM-101-242-ubuntu:~$ sudo docker search --stars=10 --no-trunc nginx Flag --stars has been deprecated, use --filter=stars=3 instead NAME DESCRIPTION STARS OFFICIAL AUTOMATED nginx Official build of Nginx. 12438 jwilder/nginx-proxy Automated Nginx reverse proxy for docker containers 1713 richarvey/nginx-php-fpm Container running Nginx + PHP-FPM capable of pulling application code from git 750 linuxserver/nginx An Nginx container, brought to you by LinuxServer.io. 84 bitnami/nginx Bitnami nginx Docker Image 75 tiangolo/nginx-rtmp Docker image with Nginx using the nginx-rtmp-module module for live multimedia (video) streaming. 60 nginxdemos/hello NGINX webserver that serves a simple page containing its hostname, IP address and port ... 35 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 nginx/nginx-ingress NGINX Ingress Controller for Kubernetes 22 privatebin/nginx-fpm-alpine PrivateBin running on an Nginx, php-fpm & Alpine Linux stack 19 schmunk42/nginx-redirect A very simple container to redirect HTTP traffic to another server, based on nginx 17 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 centos/nginx-112-centos7 Platform for running nginx 1.12 or building nginx-based application 11 ubuntu@VM-101-242-ubuntu:~$ 使用條件查找收藏數大于10的nginx鏡像 ubuntu@VM-101-242-ubuntu:~$ sudo docker search --filter stars=3 nginx NAME DESCRIPTION STARS OFFICIAL AUTOMATED nginx Official build of Nginx. 12438 jwilder/nginx-proxy Automated Nginx reverse proxy for docker con… 1713 richarvey/nginx-php-fpm Container running Nginx + PHP-FPM capable of… 750 linuxserver/nginx An Nginx container, brought to you by LinuxS… 84 bitnami/nginx Bitnami nginx Docker Image 75 tiangolo/nginx-rtmp Docker image with Nginx using the nginx-rtmp… 60 nginxdemos/hello NGINX webserver that serves a simple page co… 35 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 nginx/nginx-ingress NGINX Ingress Controller for Kubernetes 22 privatebin/nginx-fpm-alpine PrivateBin running on an Nginx, php-fpm & Al… 19 schmunk42/nginx-redirect A very simple container to redirect HTTP tra… 17 centos/nginx-18-centos7 Platform for running nginx 1.8 or building n… 12 blacklabelops/nginx Dockerized Nginx Reverse Proxy Server. 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 mailu/nginx Mailu nginx frontend 5 1science/nginx Nginx Docker images that include Consul Temp… 5 ubuntu@VM-101-242-ubuntu:~$ ## pull 從docker倉庫下載鏡像到本地 語法 docker pull 鏡像名稱:tag(版本) 參數說明 -a, --all-tags:下載所有版本的鏡像 -q, --quiet:不顯示詳細信息 --disable-content-trust:不對下載的鏡像進行校驗 --platform:指定下載鏡像服務器的平臺信息 示例 下載nginx默認版本的docker鏡像 ubuntu@VM-35-226-ubuntu:~$ sudo docker pull nginx:latest latest: Pulling from library/nginx 8ec398bc0356: Pull complete 465560073b6f: Pull complete f473f9fd0a8c: Pull complete Digest: sha256:b2d89d0a210398b4d1120b3e3a7672c16a4ba09c2c4a0395f18b9f7999b768f2 Status: Downloaded newer image for nginx:latest docker.io/library/nginx:latest ubuntu@VM-35-226-ubuntu:~$ sudo docker images REPOSITORY TAG IMAGE ID CREATED SIZE nginx latest f7bb5701a33c 9 days ago 126MB ubuntu@VM-35-226-ubuntu:~$ ## push 推送指定鏡像到docker鏡像服務器 語法 docker push 本地鏡像:tag(版本) 參數說明 --disable-content-trust:不對鏡像進行校驗 示例 docker push blxt:5000/blxt/nginx 上傳nginx到私有倉庫 ## images 查看本地所有docker鏡像 語法 docker images 參數 鏡像:tag(版本) 參數說明 -a, --all:列出本地所有鏡像 --digests:顯示鏡像簡單信息說明 -f, --filter:根據相關條件過濾輸出 --format:使用Go模板進行漂亮的打印搜索 --no-trunc:顯示鏡像的完整信息 -q, --quiet:只顯示鏡像ID 示例 查看本地所有鏡像 ubuntu@VM-35-226-ubuntu:~$ sudo docker images -a REPOSITORY TAG IMAGE ID CREATED SIZE nginx latest f7bb5701a33c 9 days ago 126MB ubuntu@VM-35-226-ubuntu:~$ 查看鏡像詳細信息 ubuntu@VM-35-226-ubuntu:~$ sudo docker images --no-trunc nginx:latest REPOSITORY TAG IMAGE ID CREATED SIZE nginx latest sha256:f7bb5701a33c0e572ed06ca554edca1bee96cbbc1f76f3b01c985de7e19d0657 9 days ago 126MB ubuntu@VM-35-226-ubuntu:~$ 查看鏡像ID ubuntu@VM-35-226-ubuntu:~$ sudo docker images -q nginx:latest f7bb5701a33c ubuntu@VM-35-226-ubuntu:~$ ## run 創建容器,并運行 語法 docker run 參數 鏡像:tag(版本) 參數說明 -a, --attach: 指定標準輸入輸出內容類型,可選 STDIN/STDOUT/STDERR 三項 -d: 容器將在后臺運行 -t: 給容器分配tty終端 --sig-proxy=true: 將所有接收到的信號代理到進程(僅限非TTY模式) -i: 以交互模式運行容器 --name: 給容器設置名稱 --dns: 為容器設置自定義dns服務器 --dns-search: 指定DNS搜索域名 --network: 為容器指定網卡 --network-alias: 為容器的網卡設置別名 --add-host: 在容器的hosts文件中添加記錄 --mac-address: 設置容器的MAC地址 --ip: 設置容器的IPV4地址 --ip6: 設置容器IPV6地址 --link-local-ip: 設置一個或多個容器的以太網設備的鏈接本地IPv4/IPv6地址 --rm: 容器退出時自動刪除容器 -m, --memory: 設置容器內存大小,最小4M --memory-swap: 設置容器的總內存大小,包含交換分區 -c, --cpu-shares: 設置cpu --cpus: 設置cpu數量 --entrypoint: 在容器運行時執行的默認命令 --expose: 設置端口映射 -P: 將容器中所有端口映射到宿主機 -p: 將容器的某個端口映射到宿主機 --link: 將鏈接添加到另外一個容器 -v, --volume: 將宿主機的文件掛載到容器中 --volumes-from: 將宿主機的所有文件系統掛載到容器 -u,--user: 設置所使用的用戶名或UID以及指定命令的可選組名或GID -h: 指定容器主機名稱 -e: 設置環境變量 --env-file: 從指定文件中導入變量 參數還有很多,這里就不一一列舉了,上面是常用的參數 示例 后臺運行nginx容器,并取名為nginx ubuntu@VM-35-226-ubuntu:~$ sudo docker run -d --name nginx nginx:latest 06a913a442f9729e284b1a0628e5ce46561f81c6e8777f91d184f10de079cb55 ubuntu@VM-35-226-ubuntu:~$ sudo docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 06a913a442f9 nginx:latest "nginx -g 'daemon of…" 18 seconds ago Up 15 seconds 80/tcp nginx ubuntu@VM-35-226-ubuntu:~$ 后臺運行nginx容器,并映射容器80端口到宿主機的80端口 ubuntu@VM-35-226-ubuntu:~$ sudo docker run -d --name nginx -p 80:80 nginx:latest c026ee7e0c9477c15ac42965ed19af92b6017e7bcce079fd450f8cee5f2d9de6 ubuntu@VM-35-226-ubuntu:~$ sudo docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES c026ee7e0c94 nginx:latest "nginx -g 'daemon of…" 8 seconds ago Up 6 seconds 0.0.0.0:80->80/tcp nginx ubuntu@VM-35-226-ubuntu:~$ sudo curl 127.0.0.1 <!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> <h2>Welcome to nginx!</h2> <p>If you see this page, the nginx web server is successfully installed and working. Further configuration is required.</p> <p>For online documentation and support please refer to <a href="http://nginx.org/">nginx.org</a>.<br/> Commercial support is available at <a href="http://nginx.com/">nginx.com</a>.</p> <p><em>Thank you for using nginx.</em></p> </body> </html> ubuntu@VM-35-226-ubuntu:~$ 創建nginx容器,并進行交互式啟動 ubuntu@VM-35-226-ubuntu:~$ sudo docker run -it --name nginx nginx:latest /bin/bash root@4a518f4a2a01:/# ## history 查看鏡像形成過程 語法 docker history 參數 鏡像:tag(版本) 參數 --format:使用Go模板進行漂亮的打印搜索 -H, --human:以可讀格式打印大小和日期(默認為true) --no-trunc:顯示形成詳細信息 -q, --quiet:只顯示鏡像ID 示例 默認使用方法 ubuntu@VM-35-226-ubuntu:~$ sudo docker history nginx:latest IMAGE CREATED CREATED BY SIZE COMMENT f7bb5701a33c 9 days ago /bin/sh -c #(nop) CMD ["nginx" "-g" "daemon… 0B <missing> 9 days ago /bin/sh -c #(nop) STOPSIGNAL SIGTERM 0B <missing> 9 days ago /bin/sh -c #(nop) EXPOSE 80 0B <missing> 9 days ago /bin/sh -c ln -sf /dev/stdout /var/log/nginx… 22B <missing> 9 days ago /bin/sh -c set -x && addgroup --system -… 57.1MB <missing> 9 days ago /bin/sh -c #(nop) ENV PKG_RELEASE=1~buster 0B <missing> 9 days ago /bin/sh -c #(nop) ENV NJS_VERSION=0.3.7 0B <missing> 9 days ago /bin/sh -c #(nop) ENV NGINX_VERSION=1.17.6 0B <missing> 9 days ago /bin/sh -c #(nop) LABEL maintainer=NGINX Do… 0B <missing> 10 days ago /bin/sh -c #(nop) CMD ["bash"] 0B <missing> 10 days ago /bin/sh -c #(nop) ADD file:04caaf303199c81ff… 69.2MB ubuntu@VM-35-226-ubuntu:~$ 只打印鏡像ID ubuntu@VM-35-226-ubuntu:~$ sudo docker history -q nginx:latest f7bb5701a33c <missing> <missing> <missing> <missing> <missing> <missing> <missing> <missing> <missing> <missing> ubuntu@VM-35-226-ubuntu:~$ 顯示鏡像形成的詳細信息 ubuntu@VM-35-226-ubuntu:~$ sudo docker history --no-trunc nginx IMAGE CREATED CREATED BY SIZE COMMENT sha256:f7bb5701a33c0e572ed06ca554edca1bee96cbbc1f76f3b01c985de7e19d0657 9 days ago /bin/sh -c #(nop) CMD ["nginx" "-g" "daemon off;"] 0B <missing> 9 days ago /bin/sh -c #(nop) STOPSIGNAL SIGTERM 0B <missing> 9 days ago /bin/sh -c #(nop) EXPOSE 80 0B <missing> 9 days ago /bin/sh -c ln -sf /dev/stdout /var/log/nginx/access.log && ln -sf /dev/stderr /var/log/nginx/error.log 22B <missing> 9 days ago /bin/sh -c set -x && addgroup --system --gid 101 nginx && adduser --system --disabled-login --ingroup nginx --no-create-home --home /nonexistent --gecos "nginx user" --shell /bin/false --uid 101 nginx && apt-get update && apt-get install --no-install-recommends --no-install-suggests -y gnupg1 ca-certificates && NGINX_GPGKEY=573BFD6B3D8FBC641079A6ABABF5BD827BD9BF62; found=''; for server in ha.pool.sks-keyservers.net hkp://keyserver.ubuntu.com:80 hkp://p80.pool.sks-keyservers.net:80 pgp.mit.edu ; do echo "Fetching GPG key $NGINX_GPGKEY from $server"; apt-key adv --keyserver "$server" --keyserver-options timeout=10 --recv-keys "$NGINX_GPGKEY" && found=yes && break; done; test -z "$found" && echo >&2 "error: failed to fetch GPG key $NGINX_GPGKEY" && exit 1; apt-get remove --purge --auto-remove -y gnupg1 && rm -rf /var/lib/apt/lists/ && dpkgArch="$(dpkg --print-architecture)" && nginxPackages=" nginx=${NGINX_VERSION}-${PKG_RELEASE} nginx-module-xslt=${NGINX_VERSION}-${PKG_RELEASE} nginx-module-geoip=${NGINX_VERSION}-${PKG_RELEASE} nginx-module-image-filter=${NGINX_VERSION}-${PKG_RELEASE} nginx-module-njs=${NGINX_VERSION}.${NJS_VERSION}-${PKG_RELEASE} " && case "$dpkgArch" in amd64|i386) echo "deb https://nginx.org/packages/mainline/debian/ buster nginx" >> /etc/apt/sources.list.d/nginx.list && apt-get update ;; ) echo "deb-src https://nginx.org/packages/mainline/debian/ buster nginx" >> /etc/apt/sources.list.d/nginx.list && tempDir="$(mktemp -d)" && chmod 777 "$tempDir" && savedAptMark="$(apt-mark showmanual)" && apt-get update && apt-get build-dep -y $nginxPackages && ( cd "$tempDir" && DEB_BUILD_OPTIONS="nocheck parallel=$(nproc)" apt-get source --compile $nginxPackages ) && apt-mark showmanual | xargs apt-mark auto > /dev/null && { [ -z "$savedAptMark" ] || apt-mark manual $savedAptMark; } && ls -lAFh "$tempDir" && ( cd "$tempDir" && dpkg-scanpackages . > Packages ) && grep '^Package: ' "$tempDir/Packages" && echo "deb [ trusted=yes ] file://$tempDir ./" > /etc/apt/sources.list.d/temp.list && apt-get -o Acquire::GzipIndexes=false update ;; esac && apt-get install --no-install-recommends --no-install-suggests -y $nginxPackages gettext-base && apt-get remove --purge --auto-remove -y ca-certificates && rm -rf /var/lib/apt/lists/ /etc/apt/sources.list.d/nginx.list && if [ -n "$tempDir" ]; then apt-get purge -y --auto-remove && rm -rf "$tempDir" /etc/apt/sources.list.d/temp.list; fi 57.1MB <missing> 9 days ago /bin/sh -c #(nop) ENV PKG_RELEASE=1~buster 0B <missing> 9 days ago /bin/sh -c #(nop) ENV NJS_VERSION=0.3.7 0B <missing> 9 days ago /bin/sh -c #(nop) ENV NGINX_VERSION=1.17.6 ubuntu@VM-35-226-ubuntu:~$ sudo docker history --no-trunc nginx:latest IMAGE CREATED CREATED BY SIZE COMMENT sha256:f7bb5701a33c0e572ed06ca554edca1bee96cbbc1f76f3b01c985de7e19d0657 9 days ago /bin/sh -c #(nop) CMD ["nginx" "-g" "daemon off;"] 0B <missing> 9 days ago /bin/sh -c #(nop) STOPSIGNAL SIGTERM 0B <missing> 9 days ago /bin/sh -c #(nop) EXPOSE 80 0B <missing> 9 days ago /bin/sh -c ln -sf /dev/stdout /var/log/nginx/access.log && ln -sf /dev/stderr /var/log/nginx/error.log 22B <missing> 9 days ago /bin/sh -c set -x && addgroup --system --gid 101 nginx && adduser --system --disabled-login --ingroup nginx --no-create-home --home /nonexistent --gecos "nginx user" --shell /bin/false --uid 101 nginx && apt-get update && apt-get install --no-install-recommends --no-install-suggests -y gnupg1 ca-certificates && NGINX_GPGKEY=573BFD6B3D8FBC641079A6ABABF5BD827BD9BF62; found=''; for server in ha.pool.sks-keyservers.net hkp://keyserver.ubuntu.com:80 hkp://p80.pool.sks-keyservers.net:80 pgp.mit.edu ; do echo "Fetching GPG key $NGINX_GPGKEY from $server"; apt-key adv --keyserver "$server" --keyserver-options timeout=10 --recv-keys "$NGINX_GPGKEY" && found=yes && break; done; test -z "$found" && echo >&2 "error: failed to fetch GPG key $NGINX_GPGKEY" && exit 1; apt-get remove --purge --auto-remove -y gnupg1 && rm -rf /var/lib/apt/lists/ && dpkgArch="$(dpkg --print-architecture)" && nginxPackages=" nginx=${NGINX_VERSION}-${PKG_RELEASE} nginx-module-xslt=${NGINX_VERSION}-${PKG_RELEASE} nginx-module-geoip=${NGINX_VERSION}-${PKG_RELEASE} nginx-module-image-filter=${NGINX_VERSION}-${PKG_RELEASE} nginx-module-njs=${NGINX_VERSION}.${NJS_VERSION}-${PKG_RELEASE} " && case "$dpkgArch" in amd64|i386) echo "deb https://nginx.org/packages/mainline/debian/ buster nginx" >> /etc/apt/sources.list.d/nginx.list && apt-get update ;; ) echo "deb-src https://nginx.org/packages/mainline/debian/ buster nginx" >> /etc/apt/sources.list.d/nginx.list && tempDir="$(mktemp -d)" && chmod 777 "$tempDir" && savedAptMark="$(apt-mark showmanual)" && apt-get update && apt-get build-dep -y $nginxPackages && ( cd "$tempDir" && DEB_BUILD_OPTIONS="nocheck parallel=$(nproc)" apt-get source --compile $nginxPackages ) && apt-mark showmanual | xargs apt-mark auto > /dev/null && { [ -z "$savedAptMark" ] || apt-mark manual $savedAptMark; } && ls -lAFh "$tempDir" && ( cd "$tempDir" && dpkg-scanpackages . > Packages ) && grep '^Package: ' "$tempDir/Packages" && echo "deb [ trusted=yes ] file://$tempDir ./" > /etc/apt/sources.list.d/temp.list && apt-get -o Acquire::GzipIndexes=false update ;; esac && apt-get install --no-install-recommends --no-install-suggests -y $nginxPackages gettext-base && apt-get remove --purge --auto-remove -y ca-certificates && rm -rf /var/lib/apt/lists/ /etc/apt/sources.list.d/nginx.list && if [ -n "$tempDir" ]; then apt-get purge -y --auto-remove && rm -rf "$tempDir" /etc/apt/sources.list.d/temp.list; fi 57.1MB <missing> 9 days ago /bin/sh -c #(nop) ENV PKG_RELEASE=1~buster 0B <missing> 9 days ago /bin/sh -c #(nop) ENV NJS_VERSION=0.3.7 0B <missing> 9 days ago /bin/sh -c #(nop) ENV NGINX_VERSION=1.17.6 0B <missing> 9 days ago /bin/sh -c #(nop) LABEL maintainer=NGINX Docker Maintainers <docker-maint@nginx.com> 0B <missing> 10 days ago /bin/sh -c #(nop) CMD ["bash"] 0B <missing> 10 days ago /bin/sh -c #(nop) ADD file:04caaf303199c81ff1a94e2e39d5096f9d02b73294b82758e5bc6e23aff94272 in / 69.2MB ubuntu@VM-35-226-ubuntu:~$ ## start 啟動容器 語法 docker start 參數 容器ID/容器名稱 參數說明 -a, --attach:附加STDOUT/STDERR和轉發信號 --checkpoint:從快照進行還原 --checkpoint-dir:使用自定義檢查點存儲目錄 --detach-keys:重寫用于分離容器的鍵序列 -i, --interactive:附加容器的STDIN 示例 啟動使用create創建的nginx容器 ubuntu@VM-35-226-ubuntu:~$ sudo docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 8b9053318855 nginx:latest "nginx -g 'daemon of…" 9 seconds ago Created admiring_rhodes ubuntu@VM-35-226-ubuntu:~$ sudo docker start 8b9053318855 8b9053318855 ubuntu@VM-35-226-ubuntu:~$ sudo docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 8b9053318855 nginx:latest "nginx -g 'daemon of…" 40 seconds ago Up 7 seconds 80/tcp admiring_rhodes ubuntu@VM-35-226-ubuntu:~$ ## stop 關閉容器 語法 docker stop 參數 容器ID/容器名稱 參數說明 --time , -t:在多少時間后關閉容器 示例 默認使用方法 ubuntu@VM-121-163-ubuntu:~$ sudo docker run -d --name nginx nginx:latest 4c1e4ab3cfdcb7c5c55052238525e79bb82c0b5cf70b18420520ec2884669272 ubuntu@VM-121-163-ubuntu:~$ sudo docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 4c1e4ab3cfdc nginx:latest "nginx -g 'daemon of…" 7 seconds ago Up 5 seconds 80/tcp nginx ubuntu@VM-121-163-ubuntu:~$ sudo docker stop nginx nginx ubuntu@VM-121-163-ubuntu:~$ sudo docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 4c1e4ab3cfdc nginx:latest "nginx -g 'daemon of…" 26 seconds ago Exited (0) 5 seconds ago nginx ubuntu@VM-121-163-ubuntu:~$ ## build 通過dockerfile制作鏡像 語法 docker build 參數 路徑 參數說明 --build-arg:設置創建時的變量 --cgroup-parent:容器的可選父cgroup --compress:使用gzip壓縮構建上下文 --cpu-shares :設置 cpu 使用權重 --cpu-period :限制 CPU CFS周期 --cpu-quota :限制 CPU CFS配額 --cpuset-cpus :指定使用的CPU id --cpuset-mems :指定使用的內存 id --disable-content-trus:跳過鏡像驗證 --file , -f:指定dockerfile的名稱,默認為dockerfile --force-rm:始終移除中間容器 --iidfile:將鏡像ID寫入文件 --isolation:使用容器隔離 --label:為鏡像設置元數據 --memory , -m:設置內存限制 --memory-swap:設置總內存,包含交換內存 --network:設置鏡像網絡信息 --no-cache:構建鏡像不使用緩存 --output , -o:設置鏡像生成位置 --pull:始終嘗試提取圖像的較新版本 --quiet , -q: 禁止生成輸出并成功打印圖像ID --rm:成功構建后刪除中間容器 --tag, -t: 鏡像的名字及標簽 --ulimit :Ulimit配置 --shm-size :設置/dev/shm的大小,默認值是64M 示例 使用當前目錄下的dockerfile制作nginx:V1鏡像 docker build -t nginx:V1 . 創建nginx:V2鏡像,使用非當前目錄下的dockerfile docker build -t nginx:V2 -f /opt/dockerfile 添加其他主機到容器文件中 docker build --add-host=docker:1.1.1.1 . 使用參數--squash創建名為test:1的鏡像 docker build --squash -t test:1 . ## attach 當前shell連接運行容器 語法 docker attach 參數 容器ID/容器名稱 參數 --detach-keys: 覆蓋分離容器的鍵序列 --no-stdin:不要附上STDIN --sig-proxy:代理所有接收到的信號到進程 示例 默認使用方法 ubuntu@VM-121-163-ubuntu:~$ sudo docker run -d --name ubuntu ubuntu /usr/bin/top -b 98c3f0964ad3a38e360867941a5510cd77e164a835be5b9b69aa47d3d5ceed17 ubuntu@VM-121-163-ubuntu:~$ sudo docker attach ubuntu top - 08:24:58 up 1:12, 0 users, load average: 0.04, 0.06, 0.01 Tasks: 1 total, 1 running, 0 sleeping, 0 stopped, 0 zombie %Cpu(s): 2.3 us, 1.7 sy, 0.0 ni, 96.0 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st KiB Mem : 885080 total, 149760 free, 231524 used, 503796 buff/cache KiB Swap: 0 total, 0 free, 0 used. 499100 avail Mem PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND 1 root 20 0 36480 3100 2748 R 0.0 0.4 0:00.19 top top - 08:25:01 up 1:12, 0 users, load average: 0.04, 0.06, 0.01 Tasks: 1 total, 1 running, 0 sleeping, 0 stopped, 0 zombie %Cpu(s): 1.0 us, 0.7 sy, 0.0 ni, 98.0 id, 0.3 wa, 0.0 hi, 0.0 si, 0.0 st KiB Mem : 885080 total, 151252 free, 230048 used, 503780 buff/cache KiB Swap: 0 total, 0 free, 0 used. 500588 avail Mem PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND 1 root 20 0 36480 3100 2748 R 0.0 0.4 0:00.19 top top - 08:25:04 up 1:13, 0 users, load average: 0.04, 0.06, 0.01 Tasks: 1 total, 1 running, 0 sleeping, 0 stopped, 0 zombie %Cpu(s): 1.3 us, 1.0 sy, 0.0 ni, 97.7 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st KiB Mem : 885080 total, 151264 free, 230016 used, 503800 buff/cache KiB Swap: 0 total, 0 free, 0 used. 500616 avail Mem PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND 1 root 20 0 36480 3100 2748 R 0.0 0.4 0:00.19 top 您可以看到該bash 過程返回的退出代碼也由docker attach命令返回給其調用者 ubuntu@VM-121-163-ubuntu:~$ sudo docker run --name ubuntu -d -it ubuntu 9ac7802feb2b77bb905bc9f4c5ca9782e6800fc995b1f2b39da78239d86a7405 ubuntu@VM-121-163-ubuntu:~$ sudo docker attach ubuntu root@9ac7802feb2b:/# root@9ac7802feb2b:/# ## commit 保存當前容器為鏡像/快照 語法 docker commit 容器名稱/容器ID 鏡像名:tag(版本) 示例 ubuntu@VM-121-163-ubuntu:~$ sudo docker images //查看鏡像 REPOSITORY TAG IMAGE ID CREATED SIZE nginx latest f7bb5701a33c 9 days ago 126MB ubuntu latest 549b9b86cb8d 2 weeks ago 64.2MB ubuntu@VM-121-163-ubuntu:~$ sudo docker run -itd --name nginx nginx:latest //創建容器 9566c54f3883b0a3b2f92dc94eb8520772d5d05826d7025be23212768f4d7967 ubuntu@VM-121-163-ubuntu:~$ sudo docker ps -a //查看容器狀態 CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 9566c54f3883 nginx:latest "nginx -g 'daemon of…" 11 seconds ago Up 9 seconds 80/tcp nginx ubuntu@VM-121-163-ubuntu:~$ sudo docker commit nginx nginx:V1 //生成快照 sha256:e091997549ff02cf6309d6146b7626c6efef6dd9ce598c632f6bf0f4d090978c ubuntu@VM-121-163-ubuntu:~$ sudo docker images //查看鏡像 REPOSITORY TAG IMAGE ID CREATED SIZE nginx V1 e091997549ff 22 seconds ago 126MB nginx latest f7bb5701a33c 9 days ago 126MB ubuntu latest 549b9b86cb8d 2 weeks ago 64.2MB ubuntu@VM-121-163-ubuntu:~$ ## cp 容器與宿主機互相復制文件 語法 docker cp 容器ID/容器名:文件路徑 宿主機路徑 復制容器中文件到宿主機 docker cp 宿主機路徑 容器ID/容器名:文件存儲路徑 復制宿主機文件到容器 參數說明 -a, --archive:復制文件同時復制文件用戶信息 -L, --follow-link:保持源目標中的鏈接 示例 復制宿主機文件到容器的根目錄中 root@test:/jia/test# docker run -d --name nginx nginx:latest //創建并運行nginx容器 33b6339003ef41fd62b909fd79e9056d53d944b3172f0f02fc717fd64a2648e3 root@test:/jia/test# docker exec nginx ls / //列出容器根目錄 bin boot dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var root@test:/jia/test# docker cp test nginx:/ //復制文件到容器 root@test:/jia/test# docker exec nginx ls / //列出容器根目錄 bin boot dev etc home lib lib64 media mnt opt proc root run sbin srv sys test tmp usr var 從容器中復制文件到宿主目錄 root@test:/jia/test# ls //列出目錄 root@test:/jia/test# docker cp nginx:/test . //復制容器中文件到當前目錄 root@test:/jia/test# ls //列出目錄 test root@test:/jia/test# ## create 創建容器但不啟動容器 語法 docker create 參數 鏡像:tag(版本) 參數說明 --add-host: 在容器的hosts文件中添加記錄 -a, --attach: 指定標準輸入輸出內容類型,可選 STDIN/STDOUT/STDERR 三項 --cap-add: 添加linux功能 --cap-drop: 刪除linux功能 --cidfile: 將容器ID寫入文件 -c, --cpu-shares: 設置cpu --device: 將主機設備添加到容器 --disable-content-trus: 不對鏡像進行驗證 --dns: 為容器設置自定義dns服務器‘ --dns-opt: 設置DNS選項 --dns-search: 指定DNS搜索域名 --domainname: 設置容器域名 --env , -e: 設置環境變量 --env-file: 讀取變量文件進行設置容器變量 --expose: 設置端口映射 --hostname , -h: 設置容器主機名 --init: 在容器內運行一個初始化程序,以轉發信號并獲取進程 --ip: 設置容器的IPV4地址 --ip6: 設置容器IPV6地址 --isolation: 使用隔離技術 --link: 將鏈接添加到宿主機,也就是創建軟連接 --link-local-ip: 設置容器本地地址 --mac-address: 設置容器MAC地址 --memory , -m: 設置內存 --memory-swap: 設置總內存大小,包含交換內存 --mount: 掛載文件系統 --name: 設置容器名稱 --net: 將容器鏈接網絡 --net-alias: 為容器網絡起別名 --network: 容器鏈接到網絡 --network-alias: 為容器網絡起別名 --publish , -p: 將容器端口映射到宿主機 --publish-all , -P: 將容器所有端口映射到宿主機 --read-only: 將容器的根文件系統掛載為只讀 --restart: 容器退出時重新啟動策略以應用 --rm: 退出時自動刪除容器 --stop-signal: 停止容器的信號 --tty , -t: 分配終端 --user , -u: 設置運行容器的用戶 --workdir , -w: 設置容器的工作目錄 參數還有很多,這里就不一一列舉了,上面是常用的參數 示例 創建nginx容器并啟動 root@test:/jia/test# docker create -t -i nginx:latest /bin/bash //創建容器 6f2cea61b80c117fe877589073b5f259a57623bd511a89330c72c825c0cbe4fd root@test:/jia/test# docker ps -a //查看容器 CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 6f2cea61b80c nginx:latest "/bin/bash" 12 seconds ago Created funny_gould root@test:/jia/test# docker start -a -i 6f2cea61b80c //啟動容器 root@6f2cea61b80c:/# ls //列出容器當前目錄 bin boot dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var root@6f2cea61b80c:/# 創建名為nginx的容器并進行啟動 root@test:/jia/test# docker create --name nginx -t -i nginx:latest /bin/bash //創建容器 5487d362e25bfcd39c70754972eb964121ba8408f3f97c89cac930208882e745 root@test:/jia/test# docker ps -a //查看容器 CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 5487d362e25b nginx:latest "/bin/bash" 5 seconds ago Created nginx root@test:/jia/test# docker start -a -i nginx //啟動容器并進行鏈接 root@5487d362e25b:/# ls bin boot dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var root@5487d362e25b:/# 創建nginx容器并映射端口 root@test:/jia/test# docker create --name nginx -p 80:80-it nginx:latest /bin/bash //創建容器 4cc06988f4284f01281a338b914aff247a16969e33177088673a281d848f99d4 root@test:/jia/test# docker ps -a //查看容器 CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES ad7eb9da0f48 nginx:latest "/bin/bash" 2 minutes ago Exited (0) 2 seconds ago nginx root@test:/jia/test# docker start nginx //啟動容器 nginx root@test:/jia/test# docker ps -a //查看容器狀態 CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES ad7eb9da0f48 nginx:latest "/bin/bash" 2 minutes ago Up 5 seconds 0.0.0.0:8080->80/tcp nginx root@test:/jia/test# curl 127.0.0.1 <!DOCTYPE html> <html> <head> <title>Welcome to nginx!</title> <style> body { width: 35em; margin: 0 auto; font-family: Tahoma, Verdana, Arial, sans-serif; } </style> </head> <body> <h2>Welcome to nginx!</h2> <p>If you see this page, the nginx web server is successfully installed and working. Further configuration is required.</p> <p>For online documentation and support please refer to <a href="http://nginx.org/">nginx.org</a>.<br/> Commercial support is available at <a href="http://nginx.com/">nginx.com</a>.</p> <p><em>Thank you for using nginx.</em></p> </body> </html> root@test:/jia/test# ## diff 查看容器改動 語法 docker diff 容器 符號說明 A 添加了文件或目錄 D 文件或目錄已刪除 C 文件或目錄已更改 示例 默認使用方法 root@test:/jia/test# docker diff nginx C /root A /root/.bash_history root@test:/jia/test# 復制test文件到nginx容器中查看你改動情況 root@test:/jia/test# docker cp test nginx:/ //復制test文件到容器的根目錄 root@test:/jia/test# docker diff nginx A /test C /root A /root/.bash_history root@test:/jia/test# ## exec 在容器中執行命令 語法 docker exec 參數 容器ID/容器名稱 命令 參數說明 --detach , -d:后臺運行容器 --detach-keys:覆蓋分離容器的鍵序列 --env , -e:設置環境變量 --interactive , -i: 即使未連接STDIN也保持打開狀態 --privileged:賦予命令擴展權限 --tty , -t: 分配偽TTY --user , -u:指定運行用戶 --workdir , -w:設置工作目錄 示例 使用容器打印“hello word” root@test:/jia/test# docker run --name centos -itd centos:latest //創建并啟動容器 1b75dac7104122e021fb8cc7989c8992eb8502f34713e76ee87e6d6f7796e83c root@test:/jia/test# docker ps -a //查看容器 CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 1b75dac71041 centos:latest "/bin/bash" 4 seconds ago Up 3 seconds centos root@test:/jia/test# docker exec centos echo "hello word" //使用 echo 打印“hello word” hello word root@test:/jia/test# 給容器建立交互式shell,并查看容器IP地址 root@test:/jia/test# docker ps -a //查看容器 CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 1b75dac71041 centos:latest "/bin/bash" 2 minutes ago Up 2 minutes centos root@test:/jia/test# docker exec -it centos /bin/bash //建立bash交互式shell [root@1b75dac71041 /]# ip addr //查看IP地址 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo valid_lft forever preferred_lft forever 20: eth0@if21: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default link/ether 02:42:ac:11:00:02 brd ff:ff:ff:ff:ff:ff link-netnsid 0 inet 172.17.0.2/16 brd 172.17.255.255 scope global eth0 valid_lft forever preferred_lft forever [root@1b75dac71041 /]# 指定工作目錄,并打印路徑 root@test:/jia/test# docker ps -a //查看容器 CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 1b75dac71041 centos:latest "/bin/bash" 9 minutes ago Up 9 minutes centos root@test:/jia/test# docker exec -w /var/www/html centos pwd //設置工作目錄,并進行打印目錄 /var/www/html root@test:/jia/test# ## login 登入docker鏡像源服務器 語法 docker login 參數 服務器地址 參數說明 -p, --password:指定登錄服務器用戶的密碼 -u, --username:指定登錄服務器用戶的賬戶 示例 默認登錄dockerhub root@test:/jia/test# docker login Login with your Docker ID to push and pull images from Docker Hub. If you don't have a Docker ID, head over to https://hub.docker.com to create one. Username: blxt Password: WARNING! Your password will be stored unencrypted in /root/.docker/config.json. Configure a credential helper to remove this warning. See https://docs.docker.com/engine/reference/commandline/login/#credentials-store Login Succeeded root@test:/jia/test# 指定用戶名和密碼進行登錄 root@test:/jia/test# docker login -u blxt -p 123456 WARNING! Using --password via the CLI is insecure. Use --password-stdin. WARNING! Your password will be stored unencrypted in /root/.docker/config.json. Configure a credential helper to remove this warning. See https://docs.docker.com/engine/reference/commandline/login/#credentials-store Login Succeeded root@test:/jia/test# 登錄阿里鏡像倉庫 root@jia:/etc# docker login -u '弒天傾城' -p '123456' registry.cn-shenzhen.aliyuncs.com WARNING! Using --password via the CLI is insecure. Use --password-stdin. WARNING! Your password will be stored unencrypted in /root/.docker/config.json. Configure a credential helper to remove this warning. See https://docs.docker.com/engine/reference/commandline/login/#credentials-store Login Succeeded root@jia:/etc# ## logout 退出登錄鏡像源服務器 語法 docker logout 鏡像倉庫地址 示例 退出dockerhub用戶登錄 root@jia:/etc# docker login -u blxt -p 'JKY@jjj.123' //登錄dockerhub服務器 WARNING! Using --password via the CLI is insecure. Use --password-stdin. WARNING! Your password will be stored unencrypted in /root/.docker/config.json. Configure a credential helper to remove this warning. See https://docs.docker.com/engine/reference/commandline/login/#credentials-store Login Succeeded root@jia:/etc# docker logout //退出登錄 Removing login credentials for https://index.docker.io/v1/ root@jia:/etc# 退出阿里鏡像倉庫 root@jia:/etc# docker login -u '弒天傾城' -p '123456' registry.cn-shenzhen.aliyuncs.com //登錄阿里鏡像倉庫 WARNING! Using --password via the CLI is insecure. Use --password-stdin. WARNING! Your password will be stored unencrypted in /root/.docker/config.json. Configure a credential helper to remove this warning. See https://docs.docker.com/engine/reference/commandline/login/#credentials-store Login Succeeded root@jia:/etc# docker logout //退出失敗沒有登錄dockerhub倉庫 Not logged in to https://index.docker.io/v1/ root@jia:/etc# docker logout registry.cn-shenzhen.aliyuncs.com Removing login credentials for registry.cn-shenzhen.aliyuncs.com //退出登錄阿里鏡像倉庫 root@jia:/etc# ## logs 輸出當前容器的日志信息 語法 docker logs 參數 容器ID/容器名 參數說明 --details:查看日志詳細信息 --follow , -f:實時動態的查看日志 --since:顯示生成日志的時間 --tail:顯示日志行數 --timestamps , -t:顯示時間戳 --until:顯示某一時間段的日志信息 示例 默認查看容器日志 root@jia:/etc# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 85c8be92df44 nginx:latest "nginx -g 'daemon of…" 7 minutes ago Up 7 minutes 80/tcp nginx root@jia:/etc# docker logs nginx 172.17.0.1 - - [08/Jan/2020:08:19:02 +0000] "GET / HTTP/1.1" 200 612 "-" "curl/7.58.0" "-" 172.17.0.1 - - [08/Jan/2020:08:19:04 +0000] "GET / HTTP/1.1" 200 612 "-" "curl/7.58.0" "-" 172.17.0.1 - - [08/Jan/2020:08:19:07 +0000] "GET / HTTP/1.1" 200 612 "-" "curl/7.58.0" "-" root@jia:/etc# 實時顯示nginx容器的日志 root@jia:/etc# docker run --name nginx -itd nginx:latest //創建并啟動nginx容器 85c8be92df44960e0973ae2b48907661679d921d0523cb93526351f07850f290 root@jia:/etc# docker ps -a //查看容器 CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 85c8be92df44 nginx:latest "nginx -g 'daemon of…" 4 seconds ago Up 3 seconds 80/tcp nginx root@jia:~# docker inspect nginx | IPAddress //查看容器的ip地址 IPAddress: command not found root@jia:~# docker inspect nginx | grep IPAddress "SecondaryIPAddresses": null, "IPAddress": "172.17.0.2", "IPAddress": "172.17.0.2", root@jia:~# curl 127.0.0.1 //訪問nginx服務 <!DOCTYPE html> <html> <head> <title>Welcome to nginx!</title> <style> body { width: 35em; margin: 0 auto; font-family: Tahoma, Verdana, Arial, sans-serif; } </style> </head> <body> <h2>Welcome to nginx!</h2> <p>If you see this page, the nginx web server is successfully installed and working. Further configuration is required.</p> <p>For online documentation and support please refer to <a href="http://nginx.org/">nginx.org</a>.<br/> Commercial support is available at <a href="http://nginx.com/">nginx.com</a>.</p> <p><em>Thank you for using nginx.</em></p> </body> </html> root@jia:/etc# docker logs -f nginx //輸出日志信息 172.17.0.1 - - [08/Jan/2020:08:19:02 +0000] "GET / HTTP/1.1" 200 612 "-" "curl/7.58.0" "-" 172.17.0.1 - - [08/Jan/2020:08:19:04 +0000] "GET / HTTP/1.1" 200 612 "-" "curl/7.58.0" "-" 172.17.0.1 - - [08/Jan/2020:08:19:07 +0000] "GET / HTTP/1.1" 200 612 "-" "curl/7.58.0" "-" ## port 查看容器的端口映射情況 語法 docker port 容器ID/容器名稱 示例 列出nginx容器所有端口映射情況 root@jia:/etc# docker run --name nginx -itd -P nginx:latest //創建并啟動容器,映射容器所有端口 b4e3a51697c2e35b4a17acee93630ac77c6e9fe94df99c70750b60c4f87b535a root@jia:/etc# docker ps -a //查看容器 CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES b4e3a51697c2 nginx:latest "nginx -g 'daemon of…" 4 seconds ago Up 3 seconds 0.0.0.0:32768->80/tcp nginx root@jia:/etc# docker port nginx //顯示容器端口映射情況 80/tcp -> 0.0.0.0:32768 root@jia:/etc# ## ps 列出容器列表 語法 docker ps 參數 參數說明 --all , -a:查看所有容器 --filter , -f:根據相關條件進行過濾 --format: 使用Go模板打印漂亮的容器 --last , -n:顯示第幾個創建的容器 --latest , -l:顯示最新創建的容器 --no-trunc:顯示詳細信息 --quiet , -q:只顯示容器ID --size , -s:顯示容器的總大小 示例 查看所有容器 root@jia:~# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 79b53ebbc86e centos:latest "/bin/bash" 5 seconds ago Created fervent_payne b4e3a51697c2 nginx:latest "nginx -g 'daemon of…" 13 minutes ago Up 13 minutes 0.0.0.0:32768->80/tcp nginx root@jia:~# 查看正在運行的容器 root@jia:~# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES b4e3a51697c2 nginx:latest "nginx -g 'daemon of…" 13 minutes ago Up 13 minutes 0.0.0.0:32768->80/tcp nginx root@jia:~# ## restart 重新啟動容器 語法 docker restart 容器名稱/容器ID 參數 -t:設置重啟執行時間 示例 root@jia:~# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES b4e3a51697c2 nginx:latest "nginx -g 'daemon of…" 16 minutes ago Up 16 minutes 0.0.0.0:32768->80/tcp nginx root@jia:~# docker restart nginx nginx root@jia:~# ## rm 刪除容器 語法 docker rm 參數 容器ID/容器名稱 參數 --force , -f:根據條件進行刪除容器,強制刪除正在運行的容器 --link , -l:移除容器間的網絡連接,而非容器本身 -v, --volumes:刪除與容器關聯的文件系統 示例 刪除nginx容器 root@jia:~# docker ps -a //查看容器 CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES c3800e1bda43 centos "/bin/bash" About a minute ago Up About a minute centos b4e3a51697c2 nginx:latest "nginx -g 'daemon of…" 22 minutes ago Up 5 minutes 0.0.0.0:32769->80/tcp nginx root@jia:~# docker rm nginx //刪除容器,但nginx容器正在運行所以無法使用此參數刪除 Error response from daemon: You cannot remove a running container b4e3a51697c2e35b4a17acee93630ac77c6e9fe94df99c70750b60c4f87b535a. Stop the container before attempting removal or force remove root@jia:~# docker rm -f nginx /制刪除nginx容器 nginx root@jia:~# docker ps -a //查看容器 CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES c3800e1bda43 centos "/bin/bash" About a minute ago Up About a minute centos root@jia:~# 刪除所有容器 root@jia:~# docker ps -a //查看容器 CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 4ee9b7488e32 nginx "nginx -g 'daemon of…" 26 seconds ago Up 25 seconds 80/tcp nginx c3800e1bda43 centos "/bin/bash" 4 minutes ago Up 4 minutes centos root@jia:~# docker rm $(docker ps -a -q) //刪除容器失敗,因為容器正在運行 Error response from daemon: You cannot remove a running container 4ee9b7488e323c80d9acaa01a214d00bc3072e2430df5ce7a0560acea0c9587c. Stop the container before attempting removal or force remove Error response from daemon: You cannot remove a running container c3800e1bda43416add00ac18fb9c2d15e7d106e302ceef090e04d7fa6133368d. Stop the container before attempting removal or force remove root@jia:~# docker rm -f $(docker ps -a -q) //刪除所有容器 4ee9b7488e32 c3800e1bda43 root@jia:~# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES root@jia:~# ## rmi 刪除本地鏡像 語法 docker rmi 參數 鏡像名稱/鏡像ID 參數 --force , -f:強制刪除 --no-prune:不移除該鏡像的過程鏡像,默認移除 示例 默認刪除鏡像 root@jia:~# docker images //查看本地鏡像 REPOSITORY TAG IMAGE ID CREATED SIZE nginx latest f7bb5701a33c 10 days ago 126MB ubuntu latest 549b9b86cb8d 2 weeks ago 64.2MB centos latest 0f3e07c0138f 3 months ago 220MB root@jia:~# docker rmi 549b9b86cb8d //刪除ubuntu的鏡像 Untagged: ubuntu:latest Untagged: ubuntu@sha256:250cc6f3f3ffc5cdaa9d8f4946ac79821aafb4d3afc93928f0de9336eba21aa4 Deleted: sha256:549b9b86cb8d75a2b668c21c50ee092716d070f129fd1493f95ab7e43767eab8 Deleted: sha256:7c52cdc1e32d67e3d5d9f83c95ebe18a58857e68bb6985b0381ebdcec73ff303 Deleted: sha256:a3c2e83788e20188bb7d720f36ebeef2f111c7b939f1b19aa1b4756791beece0 Deleted: sha256:61199b56f34827cbab596c63fd6e0ac0c448faa7e026e330994818190852d479 Deleted: sha256:2dc9f76fb25b31e0ae9d36adce713364c682ba0d2fa70756486e5cedfaf40012 root@jia:~# docker images //查看本地鏡像 REPOSITORY TAG IMAGE ID CREATED SIZE nginx latest f7bb5701a33c 10 days ago 126MB centos latest 0f3e07c0138f 3 months ago 220MB root@jia:~# 強制刪除正在運行的鏡像 root@jia:~# docker run -itd --name nginx nginx //啟動創建容器 36b203a3fb5cf8fe6a1e13e298998c8510f7d24e49da36ada8d7181d89b16089 root@jia:~# docker ps -a //查看容器 CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 36b203a3fb5c nginx "nginx -g 'daemon of…" 3 seconds ago Up 2 seconds 80/tcp nginx root@jia:~# docker images //查看本地鏡像 REPOSITORY TAG IMAGE ID CREATED SIZE nginx latest f7bb5701a33c 10 days ago 126MB centos latest 0f3e07c0138f 3 months ago 220MB root@jia:~# docker rmi nginx:latest //默認刪除鏡像,刪除失敗,報錯鏡像正在使用 Error response from daemon: conflict: unable to remove repository reference "nginx:latest" (must force) - container 36b203a3fb5c is using its referenced image f7bb5701a33c root@jia:~# docker rmi -f nginx:latest /制刪除鏡像 Untagged: nginx:latest Untagged: nginx@sha256:b2d89d0a210398b4d1120b3e3a7672c16a4ba09c2c4a0395f18b9f7999b768f2 root@jia:~# docker images //查看鏡像 REPOSITORY TAG IMAGE ID CREATED SIZE <none> <none> f7bb5701a33c 10 days ago 126MB centos latest 0f3e07c0138f 3 months ago 220MB root@jia:~# 注意:當強制刪除鏡像后,會直接刪除,但是會生成新的鏡像,容器不會被刪除 ## save 保存鏡像為tar包 語法 docker save 參數 鏡像名稱 文件目錄及名稱.tar 參數說明 --output , -o:保存到某文件 示例 將nginx鏡像保存為文件 root@jia:/jia/jia# docker images REPOSITORY TAG IMAGE ID CREATED SIZE nginx latest f7bb5701a33c 11 days ago 126MB centos latest 0f3e07c0138f 3 months ago 220MB root@jia:/jia/jia# docker save nginx:latest > nginx.tar root@jia:/jia/jia# ls nginx.tar root@jia:/jia/jia# 將多個鏡像保存到一個文件 root@jia:/jia/jia# docker save -o test.tar nginx:latest centos:latest root@jia:/jia/jia# ls nginx.tar test.tar root@jia:/jia/jia# 使用gzip+tar進行打包壓縮 root@jia:/jia/jia# docker save nginx:latest | gzip > nginx.tar.gz root@jia:/jia/jia# ls -lh //查看目錄中文件長格式顯示 total 513M -rw-r--r-- 1 root root 125M Jan 9 16:57 nginx.tar -rw-r--r-- 1 root root 47M Jan 9 17:03 nginx.tar.gz -rw------- 1 root root 342M Jan 9 17:00 test.tar root@jia:/jia/jia# ## top 查看容器中進程信息 語法 docker top 參數 容器ID/容器名稱 示例 查看nginx容器中所有進程信息 root@jia:/jia/jia# docker run --name nginx -itd nginx:latest //創建啟動nginx容器 69e353d8564025e07ee201098cafb9870d95b92edf990db2455aa6261c0bede6 root@jia:/jia/jia# docker ps -a //列出所有容器 CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 69e353d85640 nginx:latest "nginx -g 'daemon of…" 5 seconds ago Up 4 seconds 80/tcp nginx root@jia:/jia/jia# docker top nginx UID PID PPID C STIME TTY TIME CMD root 28665 28637 1 17:08 pts/0 00:00:00 nginx: master process nginx -g daemon off; systemd+ 28724 28665 0 17:08 pts/0 00:00:00 nginx: worker process root@jia:/jia/jia# ## version 查看docker軟件版本 語法 docker version 參數 參數說明 -f, --format:使用給定的Go模板格式化輸出 --kubeconfig:Kubernetes配置文件 示例 查看docker版本詳細信息 root@jia:/jia/jia# docker version Client: Docker Engine - Community Version: 19.03.5 API version: 1.40 Go version: go1.12.12 Git commit: 633a0ea838 Built: Wed Nov 13 07:29:52 2019 OS/Arch: linux/amd64 Experimental: false Server: Docker Engine - Community Engine: Version: 19.03.5 API version: 1.40 (minimum version 1.12) Go version: go1.12.12 Git commit: 633a0ea838 Built: Wed Nov 13 07:28:22 2019 OS/Arch: linux/amd64 Experimental: false containerd: Version: 1.2.10 GitCommit: b34a5c8af56e510852c35414db4c1f4fa6172339 runc: Version: 1.0.0-rc8+dev GitCommit: 3e425f80a8c931f88e6d94a8c831b9d5aa481657 docker-init: Version: 0.18.0 GitCommit: fec3683 root@jia:/jia/jia# 查看docker版本 root@jia:/jia/jia# docker version --format '{{.Server.Version}}' 19.03.5 root@jia:/jia/jia# ## events 查看docker服務器實時事件 語法 docker events 參數 參數說明 -f,--filter:根據條件過濾事件 --since :從指定的時間戳后顯示所有事件 --until :流水時間顯示到指定的時間為止 --format:使用給定的Go模板格式化輸出 示例 容器啟動停止,使用events顯示事件 root@jia:/jia/jia# docker ps -a //查看容器列表 CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 69e353d85640 nginx:latest "nginx -g 'daemon of…" 13 minutes ago Up 13 minutes 80/tcp nginx root@jia:/jia/jia# docker stop nginx //停止容器 nginx root@jia:/jia/jia# docker start nginx //啟動容器 nginx root@jia:/jia/jia# root@jia:~# docker events //查看實時事件,注意此命令不會自動終止需要crtl+c終止 2020-01-09T17:22:26.880177305+08:00 container kill 69e353d8564025e07ee201098cafb9870d95b92edf990db2455aa6261c0bede6 (image=nginx:latest, maintainer=NGINX Docker Maintainers <docker-maint@nginx.com>, name=nginx, signal=15) 2020-01-09T17:22:27.005016080+08:00 container die 69e353d8564025e07ee201098cafb9870d95b92edf990db2455aa6261c0bede6 (exitCode=0, image=nginx:latest, maintainer=NGINX Docker Maintainers <docker-maint@nginx.com>, name=nginx) 2020-01-09T17:22:27.103273324+08:00 network disconnect f2ccf449e47668433fb96511ef577c7515be7f481a7a9cb386c9d46650cbabbf (container=69e353d8564025e07ee201098cafb9870d95b92edf990db2455aa6261c0bede6, name=bridge, type=bridge) 2020-01-09T17:22:27.145650268+08:00 container stop 69e353d8564025e07ee201098cafb9870d95b92edf990db2455aa6261c0bede6 (image=nginx:latest, maintainer=NGINX Docker Maintainers <docker-maint@nginx.com>, name=nginx) 2020-01-09T17:22:33.680477968+08:00 network connect f2ccf449e47668433fb96511ef577c7515be7f481a7a9cb386c9d46650cbabbf (container=69e353d8564025e07ee201098cafb9870d95b92edf990db2455aa6261c0bede6, name=bridge, type=bridge) 2020-01-09T17:22:34.093551544+08:00 container start 69e353d8564025e07ee201098cafb9870d95b92edf990db2455aa6261c0bede6 (image=nginx:latest, maintainer=NGINX Docker Maintainers <docker-maint@nginx.com>, name=nginx) ## import 從tar文件導入docker為鏡像 語法 docker import 參數 tar文件目錄名稱 導入的鏡像名稱:tag(版本) 參數說明 --change , -c: 將Dockerfile指令應用于創建的映像 --message , -m: 設置導入圖像的提交消息 示例 將nginx.tar的鏡像包導入docker并命名nginx版本V1 root@jia:/jia/jia# docker images //查看本地所有鏡像 REPOSITORY TAG IMAGE ID CREATED SIZE nginx latest f7bb5701a33c 11 days ago 126MB centos latest 0f3e07c0138f 3 months ago 220MB root@jia:/jia/jia# ls nginx.tar nginx.tar.gz test.tar root@jia:/jia/jia# docker import nginx.tar nginx:V1 sha256:07b2b1766ed11df9e7a12f98a4c5127c1aa0087e6b7d4eb0bc21490f168c1b0d root@jia:/jia/jia# docker images REPOSITORY TAG IMAGE ID CREATED SIZE nginx V1 07b2b1766ed1 3 seconds ago 130MB nginx latest f7bb5701a33c 11 days ago 126MB centos latest 0f3e07c0138f 3 months ago 220MB root@jia:/jia/jia# 導入使用tar打包gzip壓縮的鏡像 root@jia:/jia/jia# docker import nginx.tar.gz nginx:V2 sha256:ca7505dbea914f7babb1ed3667d1a133461c344d92ea978585b64fe7dabf5b36 root@jia:/jia/jia# docker images REPOSITORY TAG IMAGE ID CREATED SIZE nginx V2 ca7505dbea91 8 seconds ago 130MB nginx V1 07b2b1766ed1 2 minutes ago 130MB nginx latest f7bb5701a33c 11 days ago 126MB centos latest 0f3e07c0138f 3 months ago 220MB root@jia:/jia/jia# ## export 從docker導出容器為tar文件 語法 docker export 參數 容器名稱/容器ID 參數說明 --output , -o:寫入文件 示例 導出centos容器并命名為centos.tar root@jia:/jia/jia# docker run --name centos -itd centos:latest //創建并啟動centos容器 06b5abbb068dc493056aae6f1526b81028f931fd160fe470a937e15ac0110bdf root@jia:/jia/jia# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 06b5abbb068d centos:latest "/bin/bash" 4 seconds ago Up 3 seconds centos 69e353d85640 nginx:latest "nginx -g 'daemon of…" 32 minutes ago Up 18 minutes 80/tcp nginx root@jia:/jia/jia# docker export centos > centos.tar root@jia:/jia/jia# ls centos.tar nginx.tar nginx.tar.gz test.tar root@jia:/jia/jia# 導出centos容器并命名為centos.tar.gz,使用gzip壓縮 root@jia:/jia/jia# docker export centos > centos.tar root@jia:/jia/jia# ls centos.tar nginx.tar nginx.tar.gz test.tar root@jia:/jia/jia# docker export centos | gzip > centos.tar.gz root@jia:/jia/jia# ls -lh total 796M -rw-r--r-- 1 root root 217M Jan 9 17:41 centos.tar -rw-r--r-- 1 root root 67M Jan 9 17:43 centos.tar.gz -rw-r--r-- 1 root root 125M Jan 9 16:57 nginx.tar -rw-r--r-- 1 root root 47M Jan 9 17:03 nginx.tar.gz -rw------- 1 root root 342M Jan 9 17:00 test.tar root@jia:/jia/jia# 導出nginx容器,并命名nginx1.tar root@jia:/jia/jia# docker export -o nginx1.tar nginx root@jia:/jia/jia# ls centos.tar centos.tar.gz nginx1.tar nginx.tar nginx.tar.gz test.tar root@jia:/jia/jia# ## info 查看docker系統信息 語法 docekr info 參數 參數說明 --format , -f:使用給定的Go模板格式化輸出 示例 root@jia:/jia/jia# docker info Client: Debug Mode: false Server: Containers: 2 Running: 2 Paused: 0 Stopped: 0 Images: 4 Server Version: 19.03.5 Storage Driver: overlay2 Backing Filesystem: extfs Supports d_type: true Native Overlay Diff: true Logging Driver: json-file Cgroup Driver: cgroupfs Plugins: Volume: local Network: bridge host ipvlan macvlan null overlay Log: awslogs fluentd gcplogs gelf journald json-file local logentries splunk syslog Swarm: inactive Runtimes: runc Default Runtime: runc Init Binary: docker-init containerd version: b34a5c8af56e510852c35414db4c1f4fa6172339 runc version: 3e425f80a8c931f88e6d94a8c831b9d5aa481657 init version: fec3683 Security Options: apparmor seccomp Profile: default Kernel Version: 4.15.0-65-generic Operating System: Ubuntu 18.04.3 LTS OSType: linux Architecture: x86_64 CPUs: 2 Total Memory: 3.852GiB Name: jia ID: 74XS:4TCO:7OQX:BKF2:O2WW:JYEF:6Y6Q:RT3X:YVRO:Y2LP:JZIA:AHSB Docker Root Dir: /var/lib/docker Debug Mode: false Registry: https://index.docker.io/v1/ Labels: Experimental: false Insecure Registries: 127.0.0.0/8 Live Restore Enabled: false WARNING: No swap limit support root@jia:/jia/jia# ## inspect 查看容器詳細信息 語法 docker inspect 參數 容器ID/容器名稱 參數說明 --format , -f:使用給定的Go模板格式化輸出 --size , -s:顯示文件總大小 --type :為指定類型返回JSON 示例 獲取容器IP地址 第一種方式: root@jia:/jia/jia# docker ps -a //查看容器 CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 06b5abbb068d centos:latest "/bin/bash" 17 hours ago Up 17 hours centos 69e353d85640 nginx:latest "nginx -g 'daemon of…" 17 hours ago Up 17 hours 80/tcp nginx root@jia:/jia/jia# docker inspect centos | grep IPAddress "SecondaryIPAddresses": null, "IPAddress": "172.17.0.3", "IPAddress": "172.17.0.3", root@jia:/jia/jia# 第二種方式:(只顯示IP地址) root@jia:/jia/jia# docker inspect --format='{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' centos 172.17.0.3 root@jia:/jia/jia# 獲取容器MAC地址 root@jia:/jia/jia# docker inspect centos | grep MacAddress "MacAddress": "02:42:ac:11:00:03", "MacAddress": "02:42:ac:11:00:03", root@jia:/jia/jia# 獲取容器日志目錄 root@jia:/jia/jia# docker inspect centos | grep log "LogPath": "/var/lib/docker/containers/06b5abbb068dc493056aae6f1526b81028f931fd160fe470a937e15ac0110bdf/06b5abbb068dc493056aae6f1526b81028f931fd160fe470a937e15ac0110bdf-json.log", root@jia:/jia/jia# ## kill 殺死正在運行的容器 語法 docker kill 參數 容器ID/容器名稱 參數說明 --signal , -s:向容器發送信號 示例 殺死正在運行的centos容器 root@jia:/jia/jia# docker ps //查看容器 CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 06b5abbb068d centos:latest "/bin/bash" 17 hours ago Up 17 hours centos 69e353d85640 nginx:latest "nginx -g 'daemon of…" 18 hours ago Up 17 hours 80/tcp nginx root@jia:/jia/jia# docker kill centos centos root@jia:/jia/jia# docker ps -a //查看所有容器 CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 06b5abbb068d centos:latest "/bin/bash" 17 hours ago Exited (137) 6 seconds ago centos 69e353d85640 nginx:latest "nginx -g 'daemon of…" 18 hours ago Up 17 hours 80/tcp nginx root@jia:/jia/jia# 殺死正在運行的nginx容器,發送kill信號 root@jia:/jia/jia# docker kill --signal=kill nginx nginx root@jia:/jia/jia# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 06b5abbb068d centos:latest "/bin/bash" 17 hours ago Exited (137) 3 minutes ago centos 69e353d85640 nginx:latest "nginx -g 'daemon of…" 18 hours ago Exited (137) 1 second ago nginx root@jia:/jia/jia# ## load 從tar文件中加載為docker鏡像 語法 docker load 參數 tar文件 參數說明 -i, --input:從tar存檔文件中讀取,而不是從STDIN中讀取 --quiet , -q:顯示簡要信息 示例 導入nginx鏡像 root@jia:/jia/jia# docker images //查看所有鏡像 REPOSITORY TAG IMAGE ID CREATED SIZE centos latest 0f3e07c0138f 3 months ago 220MB root@jia:/jia/jia# docker load < nginx.tar.gz 556c5fb0d91b: Loading layer [==================================================>] 72.48MB/72.48MB 49434cc20e95: Loading layer [==================================================>] 57.67MB/57.67MB 75248c0d5438: Loading layer [==================================================>] 3.584kB/3.584kB Loaded image: nginx:latest root@jia:/jia/jia# docker images //查看所有鏡像 REPOSITORY TAG IMAGE ID CREATED SIZE nginx latest f7bb5701a33c 12 days ago 126MB centos latest 0f3e07c0138f 3 months ago 220MB root@jia:/jia/jia# ## pause 暫停容器 語法 docker pause 容器ID/容器名稱 示例 暫停centos容器 root@jia:/jia/jia# docker run -itd --name centos centos:latest //創建并運行 48e49042b927f427aed68437e12e5acd14b82e3a33a71b6f236bcef5c3edd41d root@jia:/jia/jia# docker ps //查看容器 CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 48e49042b927 centos:latest "/bin/bash" 2 seconds ago Up 2 seconds centos root@jia:/jia/jia# docker pause centos centos root@jia:/jia/jia# docker ps -a //查看所有容器 CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 48e49042b927 centos:latest "/bin/bash" 14 seconds ago Up 13 seconds (Paused) centos root@jia:/jia/jia# 容器暫停后會在狀態處加(Paused) ## unpause 取消容器暫停狀態 語法 docker unpause 容器ID/容器名稱 示例 取消centos容器的暫停操作 root@jia:/jia/jia# docker ps -a //查看所有容器 CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 48e49042b927 centos:latest "/bin/bash" 14 seconds ago Up 13 seconds (Paused) centos root@jia:/jia/jia# docker unpause centos centos root@jia:/jia/jia# docker ps -a //查看所有容器 CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 48e49042b927 centos:latest "/bin/bash" 5 minutes ago Up 5 minutes centos root@jia:/jia/jia# ## tag 給鏡像進行打標簽 語法 docker tag 源鏡像ID/源鏡像名稱:tag(版本) 目標鏡像名稱:tag(版本) 示例 給centos鏡像重新打標簽 root@jia:/jia/jia# docker tag centos:latest centos:V1 root@jia:/jia/jia# docker images REPOSITORY TAG IMAGE ID CREATED SIZE nginx latest f7bb5701a33c 12 days ago 126MB centos V1 0f3e07c0138f 3 months ago 220MB centos latest 0f3e07c0138f 3 months ago 220MB root@jia:/jia/jia# ## wait 阻塞運行直到容器停止,然后打印出它的退出代碼 語法 docekr wait 容器ID/容器名稱 示例 root@jia:/jia/jia# docker run -itd --name nginx nginx //創建啟動容器 57af5446f0eaff56a54c3a5559ac0073511de9b9f48c93ff04bbcd0c62b3a809 root@jia:/jia/jia# docker ps //查看容器 CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 57af5446f0ea nginx "nginx -g 'daemon of…" 4 seconds ago Up 3 seconds 80/tcp nginx root@jia:/jia/jia# docker inspect nginx | grep IPAddress //查看容器IP地址 "SecondaryIPAddresses": null, "IPAddress": "172.17.0.2", "IPAddress": "172.17.0.2", root@jia:/jia/jia# curl 172.17.0.2 //訪問容器的80端口 <!DOCTYPE html> <html> <head> <title>Welcome to nginx!</title> <style> body { width: 35em; margin: 0 auto; font-family: Tahoma, Verdana, Arial, sans-serif; } </style> </head> <body> <h2>Welcome to nginx!</h2> <p>If you see this page, the nginx web server is successfully installed and working. Further configuration is required.</p> <p>For online documentation and support please refer to <a href="http://nginx.org/">nginx.org</a>.<br/> Commercial support is available at <a href="http://nginx.com/">nginx.com</a>.</p> <p><em>Thank you for using nginx.</em></p> </body> </html> root@jia:/jia/jia# docker wait nginx //啟動阻塞 root@jia:~# curl 172.18.0.2 //發現無法訪問容器nginx頁面
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。