溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

docker命令總結(jié)(二)

發(fā)布時(shí)間:2020-02-26 18:38:01 來源:網(wǎng)絡(luò) 閱讀:867 作者:滄境悠瞳 欄目:云計(jì)算

上次只是給大家把命令的作用以及簡單使用列出來了(大家可以查看:docker命令總結(jié)(一)),那這篇文章會(huì)詳細(xì)介紹每條命令的參數(shù) ## search docker search:此命令是用來在倉庫中查找docker鏡像 語法: docker search 參數(shù) 鏡像名稱:tag(版本) 參數(shù)說明: --filter,-f:根據(jù)相關(guān)條件過濾輸出 --format:使用Go模板進(jìn)行漂亮的打印搜索 --limit:指定最大搜索結(jié)果,默認(rèn)25個(gè) --no-trunc:顯示完整的鏡像描述 --stars,-s:列出收藏?cái)?shù)不小于指定值的鏡像 --automated:僅顯示自動(dòng)構(gòu)建的鏡像 示例: 默認(rèn)搜索 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:~$ 搜索收藏?cái)?shù)大于10的nginx鏡像,并顯示詳細(xì)信息 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:~$ 使用條件查找收藏?cái)?shù)大于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(版本) 參數(shù)說明 -a, --all-tags:下載所有版本的鏡像 -q, --quiet:不顯示詳細(xì)信息 --disable-content-trust:不對(duì)下載的鏡像進(jìn)行校驗(yàn) --platform:指定下載鏡像服務(wù)器的平臺(tái)信息 示例 下載nginx默認(rèn)版本的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鏡像服務(wù)器 語法 docker push 本地鏡像:tag(版本) 參數(shù)說明 --disable-content-trust:不對(duì)鏡像進(jìn)行校驗(yàn) 示例 docker push blxt:5000/blxt/nginx 上傳nginx到私有倉庫 ## images 查看本地所有docker鏡像 語法 docker images 參數(shù) 鏡像:tag(版本) 參數(shù)說明 -a, --all:列出本地所有鏡像 --digests:顯示鏡像簡單信息說明 -f, --filter:根據(jù)相關(guān)條件過濾輸出 --format:使用Go模板進(jìn)行漂亮的打印搜索 --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:~$ 查看鏡像詳細(xì)信息 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 創(chuàng)建容器,并運(yùn)行 語法 docker run 參數(shù) 鏡像:tag(版本) 參數(shù)說明 -a, --attach: 指定標(biāo)準(zhǔn)輸入輸出內(nèi)容類型,可選 STDIN/STDOUT/STDERR 三項(xiàng) -d: 容器將在后臺(tái)運(yùn)行 -t: 給容器分配tty終端 --sig-proxy=true: 將所有接收到的信號(hào)代理到進(jìn)程(僅限非TTY模式) -i: 以交互模式運(yùn)行容器 --name: 給容器設(shè)置名稱 --dns: 為容器設(shè)置自定義dns服務(wù)器 --dns-search: 指定DNS搜索域名 --network: 為容器指定網(wǎng)卡 --network-alias: 為容器的網(wǎng)卡設(shè)置別名 --add-host: 在容器的hosts文件中添加記錄 --mac-address: 設(shè)置容器的MAC地址 --ip: 設(shè)置容器的IPV4地址 --ip6: 設(shè)置容器IPV6地址 --link-local-ip: 設(shè)置一個(gè)或多個(gè)容器的以太網(wǎng)設(shè)備的鏈接本地IPv4/IPv6地址 --rm: 容器退出時(shí)自動(dòng)刪除容器 -m, --memory: 設(shè)置容器內(nèi)存大小,最小4M --memory-swap: 設(shè)置容器的總內(nèi)存大小,包含交換分區(qū) -c, --cpu-shares: 設(shè)置cpu --cpus: 設(shè)置cpu數(shù)量 --entrypoint: 在容器運(yùn)行時(shí)執(zhí)行的默認(rèn)命令 --expose: 設(shè)置端口映射 -P: 將容器中所有端口映射到宿主機(jī) -p: 將容器的某個(gè)端口映射到宿主機(jī) --link: 將鏈接添加到另外一個(gè)容器 -v, --volume: 將宿主機(jī)的文件掛載到容器中 --volumes-from: 將宿主機(jī)的所有文件系統(tǒng)掛載到容器 -u,--user: 設(shè)置所使用的用戶名或UID以及指定命令的可選組名或GID -h: 指定容器主機(jī)名稱 -e: 設(shè)置環(huán)境變量 --env-file: 從指定文件中導(dǎo)入變量 參數(shù)還有很多,這里就不一一列舉了,上面是常用的參數(shù) 示例 后臺(tái)運(yùn)行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:~$ 后臺(tái)運(yùn)行nginx容器,并映射容器80端口到宿主機(jī)的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:~$ 創(chuàng)建nginx容器,并進(jìn)行交互式啟動(dòng) ubuntu@VM-35-226-ubuntu:~$ sudo docker run -it --name nginx nginx:latest /bin/bash root@4a518f4a2a01:/# ## history 查看鏡像形成過程 語法 docker history 參數(shù) 鏡像:tag(版本) 參數(shù) --format:使用Go模板進(jìn)行漂亮的打印搜索 -H, --human:以可讀格式打印大小和日期(默認(rèn)為true) --no-trunc:顯示形成詳細(xì)信息 -q, --quiet:只顯示鏡像ID 示例 默認(rèn)使用方法 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:~$ 顯示鏡像形成的詳細(xì)信息 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 啟動(dòng)容器 語法 docker start 參數(shù) 容器ID/容器名稱 參數(shù)說明 -a, --attach:附加STDOUT/STDERR和轉(zhuǎn)發(fā)信號(hào) --checkpoint:從快照進(jìn)行還原 --checkpoint-dir:使用自定義檢查點(diǎn)存儲(chǔ)目錄 --detach-keys:重寫用于分離容器的鍵序列 -i, --interactive:附加容器的STDIN 示例 啟動(dòng)使用create創(chuàng)建的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 關(guān)閉容器 語法 docker stop 參數(shù) 容器ID/容器名稱 參數(shù)說明 --time , -t:在多少時(shí)間后關(guān)閉容器 示例 默認(rèn)使用方法 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 參數(shù) 路徑 參數(shù)說明 --build-arg:設(shè)置創(chuàng)建時(shí)的變量 --cgroup-parent:容器的可選父cgroup --compress:使用gzip壓縮構(gòu)建上下文 --cpu-shares :設(shè)置 cpu 使用權(quán)重 --cpu-period :限制 CPU CFS周期 --cpu-quota :限制 CPU CFS配額 --cpuset-cpus :指定使用的CPU id --cpuset-mems :指定使用的內(nèi)存 id --disable-content-trus:跳過鏡像驗(yàn)證 --file , -f:指定dockerfile的名稱,默認(rèn)為dockerfile --force-rm:始終移除中間容器 --iidfile:將鏡像ID寫入文件 --isolation:使用容器隔離 --label:為鏡像設(shè)置元數(shù)據(jù) --memory , -m:設(shè)置內(nèi)存限制 --memory-swap:設(shè)置總內(nèi)存,包含交換內(nèi)存 --network:設(shè)置鏡像網(wǎng)絡(luò)信息 --no-cache:構(gòu)建鏡像不使用緩存 --output , -o:設(shè)置鏡像生成位置 --pull:始終嘗試提取圖像的較新版本 --quiet , -q: 禁止生成輸出并成功打印圖像ID --rm:成功構(gòu)建后刪除中間容器 --tag, -t: 鏡像的名字及標(biāo)簽 --ulimit :Ulimit配置 --shm-size :設(shè)置/dev/shm的大小,默認(rèn)值是64M 示例 使用當(dāng)前目錄下的dockerfile制作nginx:V1鏡像 docker build -t nginx:V1 . 創(chuàng)建nginx:V2鏡像,使用非當(dāng)前目錄下的dockerfile docker build -t nginx:V2 -f /opt/dockerfile 添加其他主機(jī)到容器文件中 docker build --add-host=docker:1.1.1.1 . 使用參數(shù)--squash創(chuàng)建名為test:1的鏡像 docker build --squash -t test:1 . ## attach 當(dāng)前shell連接運(yùn)行容器 語法 docker attach 參數(shù) 容器ID/容器名稱 參數(shù) --detach-keys: 覆蓋分離容器的鍵序列 --no-stdin:不要附上STDIN --sig-proxy:代理所有接收到的信號(hào)到進(jìn)程 示例 默認(rèn)使用方法 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命令返回給其調(diào)用者 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 保存當(dāng)前容器為鏡像/快照 語法 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 //創(chuàng)建容器 9566c54f3883b0a3b2f92dc94eb8520772d5d05826d7025be23212768f4d7967 ubuntu@VM-121-163-ubuntu:~$ sudo docker ps -a //查看容器狀態(tài) 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 容器與宿主機(jī)互相復(fù)制文件 語法 docker cp 容器ID/容器名:文件路徑 宿主機(jī)路徑 復(fù)制容器中文件到宿主機(jī) docker cp 宿主機(jī)路徑 容器ID/容器名:文件存儲(chǔ)路徑 復(fù)制宿主機(jī)文件到容器 參數(shù)說明 -a, --archive:復(fù)制文件同時(shí)復(fù)制文件用戶信息 -L, --follow-link:保持源目標(biāo)中的鏈接 示例 復(fù)制宿主機(jī)文件到容器的根目錄中 root@test:/jia/test# docker run -d --name nginx nginx:latest //創(chuàng)建并運(yùn)行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:/ //復(fù)制文件到容器 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 從容器中復(fù)制文件到宿主目錄 root@test:/jia/test# ls //列出目錄 root@test:/jia/test# docker cp nginx:/test . //復(fù)制容器中文件到當(dāng)前目錄 root@test:/jia/test# ls //列出目錄 test root@test:/jia/test# ## create 創(chuàng)建容器但不啟動(dòng)容器 語法 docker create 參數(shù) 鏡像:tag(版本) 參數(shù)說明 --add-host: 在容器的hosts文件中添加記錄 -a, --attach: 指定標(biāo)準(zhǔn)輸入輸出內(nèi)容類型,可選 STDIN/STDOUT/STDERR 三項(xiàng) --cap-add: 添加linux功能 --cap-drop: 刪除linux功能 --cidfile: 將容器ID寫入文件 -c, --cpu-shares: 設(shè)置cpu --device: 將主機(jī)設(shè)備添加到容器 --disable-content-trus: 不對(duì)鏡像進(jìn)行驗(yàn)證 --dns: 為容器設(shè)置自定義dns服務(wù)器‘ --dns-opt: 設(shè)置DNS選項(xiàng) --dns-search: 指定DNS搜索域名 --domainname: 設(shè)置容器域名 --env , -e: 設(shè)置環(huán)境變量 --env-file: 讀取變量文件進(jìn)行設(shè)置容器變量 --expose: 設(shè)置端口映射 --hostname , -h: 設(shè)置容器主機(jī)名 --init: 在容器內(nèi)運(yùn)行一個(gè)初始化程序,以轉(zhuǎn)發(fā)信號(hào)并獲取進(jìn)程 --ip: 設(shè)置容器的IPV4地址 --ip6: 設(shè)置容器IPV6地址 --isolation: 使用隔離技術(shù) --link: 將鏈接添加到宿主機(jī),也就是創(chuàng)建軟連接 --link-local-ip: 設(shè)置容器本地地址 --mac-address: 設(shè)置容器MAC地址 --memory , -m: 設(shè)置內(nèi)存 --memory-swap: 設(shè)置總內(nèi)存大小,包含交換內(nèi)存 --mount: 掛載文件系統(tǒng) --name: 設(shè)置容器名稱 --net: 將容器鏈接網(wǎng)絡(luò) --net-alias: 為容器網(wǎng)絡(luò)起別名 --network: 容器鏈接到網(wǎng)絡(luò) --network-alias: 為容器網(wǎng)絡(luò)起別名 --publish , -p: 將容器端口映射到宿主機(jī) --publish-all , -P: 將容器所有端口映射到宿主機(jī) --read-only: 將容器的根文件系統(tǒng)掛載為只讀 --restart: 容器退出時(shí)重新啟動(dòng)策略以應(yīng)用 --rm: 退出時(shí)自動(dòng)刪除容器 --stop-signal: 停止容器的信號(hào) --tty , -t: 分配終端 --user , -u: 設(shè)置運(yùn)行容器的用戶 --workdir , -w: 設(shè)置容器的工作目錄 參數(shù)還有很多,這里就不一一列舉了,上面是常用的參數(shù) 示例 創(chuàng)建nginx容器并啟動(dòng) root@test:/jia/test# docker create -t -i nginx:latest /bin/bash //創(chuàng)建容器 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 //啟動(dòng)容器 root@6f2cea61b80c:/# ls //列出容器當(dāng)前目錄 bin boot dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var root@6f2cea61b80c:/# 創(chuàng)建名為nginx的容器并進(jìn)行啟動(dòng) root@test:/jia/test# docker create --name nginx -t -i nginx:latest /bin/bash //創(chuàng)建容器 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 //啟動(dòng)容器并進(jìn)行鏈接 root@5487d362e25b:/# ls bin boot dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var root@5487d362e25b:/# 創(chuàng)建nginx容器并映射端口 root@test:/jia/test# docker create --name nginx -p 80:80-it nginx:latest /bin/bash //創(chuàng)建容器 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 //啟動(dòng)容器 nginx root@test:/jia/test# docker ps -a //查看容器狀態(tài) 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 查看容器改動(dòng) 語法 docker diff 容器 符號(hào)說明 A 添加了文件或目錄 D 文件或目錄已刪除 C 文件或目錄已更改 示例 默認(rèn)使用方法 root@test:/jia/test# docker diff nginx C /root A /root/.bash_history root@test:/jia/test# 復(fù)制test文件到nginx容器中查看你改動(dòng)情況 root@test:/jia/test# docker cp test nginx:/ //復(fù)制test文件到容器的根目錄 root@test:/jia/test# docker diff nginx A /test C /root A /root/.bash_history root@test:/jia/test# ## exec 在容器中執(zhí)行命令 語法 docker exec 參數(shù) 容器ID/容器名稱 命令 參數(shù)說明 --detach , -d:后臺(tái)運(yùn)行容器 --detach-keys:覆蓋分離容器的鍵序列 --env , -e:設(shè)置環(huán)境變量 --interactive , -i: 即使未連接STDIN也保持打開狀態(tài) --privileged:賦予命令擴(kuò)展權(quán)限 --tty , -t: 分配偽TTY --user , -u:指定運(yùn)行用戶 --workdir , -w:設(shè)置工作目錄 示例 使用容器打印“hello word” root@test:/jia/test# docker run --name centos -itd centos:latest //創(chuàng)建并啟動(dòng)容器 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 //設(shè)置工作目錄,并進(jìn)行打印目錄 /var/www/html root@test:/jia/test# ## login 登入docker鏡像源服務(wù)器 語法 docker login 參數(shù) 服務(wù)器地址 參數(shù)說明 -p, --password:指定登錄服務(wù)器用戶的密碼 -u, --username:指定登錄服務(wù)器用戶的賬戶 示例 默認(rèn)登錄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# 指定用戶名和密碼進(jìn)行登錄 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 退出登錄鏡像源服務(wù)器 語法 docker logout 鏡像倉庫地址 示例 退出dockerhub用戶登錄 root@jia:/etc# docker login -u blxt -p 'JKY@jjj.123' //登錄dockerhub服務(wù)器 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 輸出當(dāng)前容器的日志信息 語法 docker logs 參數(shù) 容器ID/容器名 參數(shù)說明 --details:查看日志詳細(xì)信息 --follow , -f:實(shí)時(shí)動(dòng)態(tài)的查看日志 --since:顯示生成日志的時(shí)間 --tail:顯示日志行數(shù) --timestamps , -t:顯示時(shí)間戳 --until:顯示某一時(shí)間段的日志信息 示例 默認(rèn)查看容器日志 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# 實(shí)時(shí)顯示nginx容器的日志 root@jia:/etc# docker run --name nginx -itd nginx:latest //創(chuàng)建并啟動(dòng)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服務(wù) <!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 //創(chuàng)建并啟動(dòng)容器,映射容器所有端口 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 參數(shù) 參數(shù)說明 --all , -a:查看所有容器 --filter , -f:根據(jù)相關(guān)條件進(jìn)行過濾 --format: 使用Go模板打印漂亮的容器 --last , -n:顯示第幾個(gè)創(chuàng)建的容器 --latest , -l:顯示最新創(chuàng)建的容器 --no-trunc:顯示詳細(xì)信息 --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:~# 查看正在運(yùn)行的容器 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 重新啟動(dòng)容器 語法 docker restart 容器名稱/容器ID 參數(shù) -t:設(shè)置重啟執(zhí)行時(shí)間 示例 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 參數(shù) 容器ID/容器名稱 參數(shù) --force , -f:根據(jù)條件進(jìn)行刪除容器,強(qiáng)制刪除正在運(yùn)行的容器 --link , -l:移除容器間的網(wǎng)絡(luò)連接,而非容器本身 -v, --volumes:刪除與容器關(guān)聯(lián)的文件系統(tǒng) 示例 刪除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容器正在運(yùn)行所以無法使用此參數(shù)刪除 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) //刪除容器失敗,因?yàn)槿萜髡谶\(yùn)行 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 參數(shù) 鏡像名稱/鏡像ID 參數(shù) --force , -f:強(qiáng)制刪除 --no-prune:不移除該鏡像的過程鏡像,默認(rèn)移除 示例 默認(rèn)刪除鏡像 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:~# 強(qiáng)制刪除正在運(yùn)行的鏡像 root@jia:~# docker run -itd --name nginx nginx //啟動(dòng)創(chuàng)建容器 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 //默認(rèn)刪除鏡像,刪除失敗,報(bào)錯(cuò)鏡像正在使用 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:~# 注意:當(dāng)強(qiáng)制刪除鏡像后,會(huì)直接刪除,但是會(huì)生成新的鏡像,容器不會(huì)被刪除 ## save 保存鏡像為tar包 語法 docker save 參數(shù) 鏡像名稱 文件目錄及名稱.tar 參數(shù)說明 --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# 將多個(gè)鏡像保存到一個(gè)文件 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進(jìn)行打包壓縮 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 查看容器中進(jìn)程信息 語法 docker top 參數(shù) 容器ID/容器名稱 示例 查看nginx容器中所有進(jìn)程信息 root@jia:/jia/jia# docker run --name nginx -itd nginx:latest //創(chuàng)建啟動(dòng)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 參數(shù) 參數(shù)說明 -f, --format:使用給定的Go模板格式化輸出 --kubeconfig:Kubernetes配置文件 示例 查看docker版本詳細(xì)信息 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服務(wù)器實(shí)時(shí)事件 語法 docker events 參數(shù) 參數(shù)說明 -f,--filter:根據(jù)條件過濾事件 --since :從指定的時(shí)間戳后顯示所有事件 --until :流水時(shí)間顯示到指定的時(shí)間為止 --format:使用給定的Go模板格式化輸出 示例 容器啟動(dòng)停止,使用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 //啟動(dòng)容器 nginx root@jia:/jia/jia# root@jia:~# docker events //查看實(shí)時(shí)事件,注意此命令不會(huì)自動(dòng)終止需要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文件導(dǎo)入docker為鏡像 語法 docker import 參數(shù) tar文件目錄名稱 導(dǎo)入的鏡像名稱:tag(版本) 參數(shù)說明 --change , -c: 將Dockerfile指令應(yīng)用于創(chuàng)建的映像 --message , -m: 設(shè)置導(dǎo)入圖像的提交消息 示例 將nginx.tar的鏡像包導(dǎo)入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# 導(dǎo)入使用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導(dǎo)出容器為tar文件 語法 docker export 參數(shù) 容器名稱/容器ID 參數(shù)說明 --output , -o:寫入文件 示例 導(dǎo)出centos容器并命名為centos.tar root@jia:/jia/jia# docker run --name centos -itd centos:latest //創(chuàng)建并啟動(dòng)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# 導(dǎo)出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# 導(dǎo)出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系統(tǒng)信息 語法 docekr info 參數(shù) 參數(shù)說明 --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 查看容器詳細(xì)信息 語法 docker inspect 參數(shù) 容器ID/容器名稱 參數(shù)說明 --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 殺死正在運(yùn)行的容器 語法 docker kill 參數(shù) 容器ID/容器名稱 參數(shù)說明 --signal , -s:向容器發(fā)送信號(hào) 示例 殺死正在運(yùn)行的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# 殺死正在運(yùn)行的nginx容器,發(fā)送kill信號(hào) 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 參數(shù) tar文件 參數(shù)說明 -i, --input:從tar存檔文件中讀取,而不是從STDIN中讀取 --quiet , -q:顯示簡要信息 示例 導(dǎo)入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 //創(chuàng)建并運(yùn)行 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# 容器暫停后會(huì)在狀態(tài)處加(Paused) ## unpause 取消容器暫停狀態(tài) 語法 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 給鏡像進(jìn)行打標(biāo)簽 語法 docker tag 源鏡像ID/源鏡像名稱:tag(版本) 目標(biāo)鏡像名稱:tag(版本) 示例 給centos鏡像重新打標(biāo)簽 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 阻塞運(yùn)行直到容器停止,然后打印出它的退出代碼 語法 docekr wait 容器ID/容器名稱 示例 root@jia:/jia/jia# docker run -itd --name nginx nginx //創(chuàng)建啟動(dòng)容器 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 //啟動(dòng)阻塞 root@jia:~# curl 172.18.0.2 //發(fā)現(xiàn)無法訪問容器nginx頁面上次只是給大家把命令的作用以及簡單使用列出來了(大家可以查看:docker命令總結(jié)(一)),那這篇文章會(huì)詳細(xì)介紹每條命令的參數(shù) ## search docker search:此命令是用來在倉庫中查找docker鏡像 語法: docker search 參數(shù) 鏡像名稱:tag(版本) 參數(shù)說明: --filter,-f:根據(jù)相關(guān)條件過濾輸出 --format:使用Go模板進(jìn)行漂亮的打印搜索 --limit:指定最大搜索結(jié)果,默認(rèn)25個(gè) --no-trunc:顯示完整的鏡像描述 --stars,-s:列出收藏?cái)?shù)不小于指定值的鏡像 --automated:僅顯示自動(dòng)構(gòu)建的鏡像 示例: 默認(rèn)搜索 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:~$ 搜索收藏?cái)?shù)大于10的nginx鏡像,并顯示詳細(xì)信息 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:~$ 使用條件查找收藏?cái)?shù)大于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(版本) 參數(shù)說明 -a, --all-tags:下載所有版本的鏡像 -q, --quiet:不顯示詳細(xì)信息 --disable-content-trust:不對(duì)下載的鏡像進(jìn)行校驗(yàn) --platform:指定下載鏡像服務(wù)器的平臺(tái)信息 示例 下載nginx默認(rèn)版本的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鏡像服務(wù)器 語法 docker push 本地鏡像:tag(版本) 參數(shù)說明 --disable-content-trust:不對(duì)鏡像進(jìn)行校驗(yàn) 示例 docker push blxt:5000/blxt/nginx 上傳nginx到私有倉庫 ## images 查看本地所有docker鏡像 語法 docker images 參數(shù) 鏡像:tag(版本) 參數(shù)說明 -a, --all:列出本地所有鏡像 --digests:顯示鏡像簡單信息說明 -f, --filter:根據(jù)相關(guān)條件過濾輸出 --format:使用Go模板進(jìn)行漂亮的打印搜索 --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:~$ 查看鏡像詳細(xì)信息 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 創(chuàng)建容器,并運(yùn)行 語法 docker run 參數(shù) 鏡像:tag(版本) 參數(shù)說明 -a, --attach: 指定標(biāo)準(zhǔn)輸入輸出內(nèi)容類型,可選 STDIN/STDOUT/STDERR 三項(xiàng) -d: 容器將在后臺(tái)運(yùn)行 -t: 給容器分配tty終端 --sig-proxy=true: 將所有接收到的信號(hào)代理到進(jìn)程(僅限非TTY模式) -i: 以交互模式運(yùn)行容器 --name: 給容器設(shè)置名稱 --dns: 為容器設(shè)置自定義dns服務(wù)器 --dns-search: 指定DNS搜索域名 --network: 為容器指定網(wǎng)卡 --network-alias: 為容器的網(wǎng)卡設(shè)置別名 --add-host: 在容器的hosts文件中添加記錄 --mac-address: 設(shè)置容器的MAC地址 --ip: 設(shè)置容器的IPV4地址 --ip6: 設(shè)置容器IPV6地址 --link-local-ip: 設(shè)置一個(gè)或多個(gè)容器的以太網(wǎng)設(shè)備的鏈接本地IPv4/IPv6地址 --rm: 容器退出時(shí)自動(dòng)刪除容器 -m, --memory: 設(shè)置容器內(nèi)存大小,最小4M --memory-swap: 設(shè)置容器的總內(nèi)存大小,包含交換分區(qū) -c, --cpu-shares: 設(shè)置cpu --cpus: 設(shè)置cpu數(shù)量 --entrypoint: 在容器運(yùn)行時(shí)執(zhí)行的默認(rèn)命令 --expose: 設(shè)置端口映射 -P: 將容器中所有端口映射到宿主機(jī) -p: 將容器的某個(gè)端口映射到宿主機(jī) --link: 將鏈接添加到另外一個(gè)容器 -v, --volume: 將宿主機(jī)的文件掛載到容器中 --volumes-from: 將宿主機(jī)的所有文件系統(tǒng)掛載到容器 -u,--user: 設(shè)置所使用的用戶名或UID以及指定命令的可選組名或GID -h: 指定容器主機(jī)名稱 -e: 設(shè)置環(huán)境變量 --env-file: 從指定文件中導(dǎo)入變量 參數(shù)還有很多,這里就不一一列舉了,上面是常用的參數(shù) 示例 后臺(tái)運(yùn)行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:~$ 后臺(tái)運(yùn)行nginx容器,并映射容器80端口到宿主機(jī)的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:~$ 創(chuàng)建nginx容器,并進(jìn)行交互式啟動(dòng) ubuntu@VM-35-226-ubuntu:~$ sudo docker run -it --name nginx nginx:latest /bin/bash root@4a518f4a2a01:/# ## history 查看鏡像形成過程 語法 docker history 參數(shù) 鏡像:tag(版本) 參數(shù) --format:使用Go模板進(jìn)行漂亮的打印搜索 -H, --human:以可讀格式打印大小和日期(默認(rèn)為true) --no-trunc:顯示形成詳細(xì)信息 -q, --quiet:只顯示鏡像ID 示例 默認(rèn)使用方法 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:~$ 顯示鏡像形成的詳細(xì)信息 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 啟動(dòng)容器 語法 docker start 參數(shù) 容器ID/容器名稱 參數(shù)說明 -a, --attach:附加STDOUT/STDERR和轉(zhuǎn)發(fā)信號(hào) --checkpoint:從快照進(jìn)行還原 --checkpoint-dir:使用自定義檢查點(diǎn)存儲(chǔ)目錄 --detach-keys:重寫用于分離容器的鍵序列 -i, --interactive:附加容器的STDIN 示例 啟動(dòng)使用create創(chuàng)建的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 關(guān)閉容器 語法 docker stop 參數(shù) 容器ID/容器名稱 參數(shù)說明 --time , -t:在多少時(shí)間后關(guān)閉容器 示例 默認(rèn)使用方法 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 參數(shù) 路徑 參數(shù)說明 --build-arg:設(shè)置創(chuàng)建時(shí)的變量 --cgroup-parent:容器的可選父cgroup --compress:使用gzip壓縮構(gòu)建上下文 --cpu-shares :設(shè)置 cpu 使用權(quán)重 --cpu-period :限制 CPU CFS周期 --cpu-quota :限制 CPU CFS配額 --cpuset-cpus :指定使用的CPU id --cpuset-mems :指定使用的內(nèi)存 id --disable-content-trus:跳過鏡像驗(yàn)證 --file , -f:指定dockerfile的名稱,默認(rèn)為dockerfile --force-rm:始終移除中間容器 --iidfile:將鏡像ID寫入文件 --isolation:使用容器隔離 --label:為鏡像設(shè)置元數(shù)據(jù) --memory , -m:設(shè)置內(nèi)存限制 --memory-swap:設(shè)置總內(nèi)存,包含交換內(nèi)存 --network:設(shè)置鏡像網(wǎng)絡(luò)信息 --no-cache:構(gòu)建鏡像不使用緩存 --output , -o:設(shè)置鏡像生成位置 --pull:始終嘗試提取圖像的較新版本 --quiet , -q: 禁止生成輸出并成功打印圖像ID --rm:成功構(gòu)建后刪除中間容器 --tag, -t: 鏡像的名字及標(biāo)簽 --ulimit :Ulimit配置 --shm-size :設(shè)置/dev/shm的大小,默認(rèn)值是64M 示例 使用當(dāng)前目錄下的dockerfile制作nginx:V1鏡像 docker build -t nginx:V1 . 創(chuàng)建nginx:V2鏡像,使用非當(dāng)前目錄下的dockerfile docker build -t nginx:V2 -f /opt/dockerfile 添加其他主機(jī)到容器文件中 docker build --add-host=docker:1.1.1.1 . 使用參數(shù)--squash創(chuàng)建名為test:1的鏡像 docker build --squash -t test:1 . ## attach 當(dāng)前shell連接運(yùn)行容器 語法 docker attach 參數(shù) 容器ID/容器名稱 參數(shù) --detach-keys: 覆蓋分離容器的鍵序列 --no-stdin:不要附上STDIN --sig-proxy:代理所有接收到的信號(hào)到進(jìn)程 示例 默認(rèn)使用方法 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命令返回給其調(diào)用者 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 保存當(dāng)前容器為鏡像/快照 語法 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 //創(chuàng)建容器 9566c54f3883b0a3b2f92dc94eb8520772d5d05826d7025be23212768f4d7967 ubuntu@VM-121-163-ubuntu:~$ sudo docker ps -a //查看容器狀態(tài) 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 容器與宿主機(jī)互相復(fù)制文件 語法 docker cp 容器ID/容器名:文件路徑 宿主機(jī)路徑 復(fù)制容器中文件到宿主機(jī) docker cp 宿主機(jī)路徑 容器ID/容器名:文件存儲(chǔ)路徑 復(fù)制宿主機(jī)文件到容器 參數(shù)說明 -a, --archive:復(fù)制文件同時(shí)復(fù)制文件用戶信息 -L, --follow-link:保持源目標(biāo)中的鏈接 示例 復(fù)制宿主機(jī)文件到容器的根目錄中 root@test:/jia/test# docker run -d --name nginx nginx:latest //創(chuàng)建并運(yùn)行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:/ //復(fù)制文件到容器 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 從容器中復(fù)制文件到宿主目錄 root@test:/jia/test# ls //列出目錄 root@test:/jia/test# docker cp nginx:/test . //復(fù)制容器中文件到當(dāng)前目錄 root@test:/jia/test# ls //列出目錄 test root@test:/jia/test# ## create 創(chuàng)建容器但不啟動(dòng)容器 語法 docker create 參數(shù) 鏡像:tag(版本) 參數(shù)說明 --add-host: 在容器的hosts文件中添加記錄 -a, --attach: 指定標(biāo)準(zhǔn)輸入輸出內(nèi)容類型,可選 STDIN/STDOUT/STDERR 三項(xiàng) --cap-add: 添加linux功能 --cap-drop: 刪除linux功能 --cidfile: 將容器ID寫入文件 -c, --cpu-shares: 設(shè)置cpu --device: 將主機(jī)設(shè)備添加到容器 --disable-content-trus: 不對(duì)鏡像進(jìn)行驗(yàn)證 --dns: 為容器設(shè)置自定義dns服務(wù)器‘ --dns-opt: 設(shè)置DNS選項(xiàng) --dns-search: 指定DNS搜索域名 --domainname: 設(shè)置容器域名 --env , -e: 設(shè)置環(huán)境變量 --env-file: 讀取變量文件進(jìn)行設(shè)置容器變量 --expose: 設(shè)置端口映射 --hostname , -h: 設(shè)置容器主機(jī)名 --init: 在容器內(nèi)運(yùn)行一個(gè)初始化程序,以轉(zhuǎn)發(fā)信號(hào)并獲取進(jìn)程 --ip: 設(shè)置容器的IPV4地址 --ip6: 設(shè)置容器IPV6地址 --isolation: 使用隔離技術(shù) --link: 將鏈接添加到宿主機(jī),也就是創(chuàng)建軟連接 --link-local-ip: 設(shè)置容器本地地址 --mac-address: 設(shè)置容器MAC地址 --memory , -m: 設(shè)置內(nèi)存 --memory-swap: 設(shè)置總內(nèi)存大小,包含交換內(nèi)存 --mount: 掛載文件系統(tǒng) --name: 設(shè)置容器名稱 --net: 將容器鏈接網(wǎng)絡(luò) --net-alias: 為容器網(wǎng)絡(luò)起別名 --network: 容器鏈接到網(wǎng)絡(luò) --network-alias: 為容器網(wǎng)絡(luò)起別名 --publish , -p: 將容器端口映射到宿主機(jī) --publish-all , -P: 將容器所有端口映射到宿主機(jī) --read-only: 將容器的根文件系統(tǒng)掛載為只讀 --restart: 容器退出時(shí)重新啟動(dòng)策略以應(yīng)用 --rm: 退出時(shí)自動(dòng)刪除容器 --stop-signal: 停止容器的信號(hào) --tty , -t: 分配終端 --user , -u: 設(shè)置運(yùn)行容器的用戶 --workdir , -w: 設(shè)置容器的工作目錄 參數(shù)還有很多,這里就不一一列舉了,上面是常用的參數(shù) 示例 創(chuàng)建nginx容器并啟動(dòng) root@test:/jia/test# docker create -t -i nginx:latest /bin/bash //創(chuàng)建容器 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 //啟動(dòng)容器 root@6f2cea61b80c:/# ls //列出容器當(dāng)前目錄 bin boot dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var root@6f2cea61b80c:/# 創(chuàng)建名為nginx的容器并進(jìn)行啟動(dòng) root@test:/jia/test# docker create --name nginx -t -i nginx:latest /bin/bash //創(chuàng)建容器 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 //啟動(dòng)容器并進(jìn)行鏈接 root@5487d362e25b:/# ls bin boot dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var root@5487d362e25b:/# 創(chuàng)建nginx容器并映射端口 root@test:/jia/test# docker create --name nginx -p 80:80-it nginx:latest /bin/bash //創(chuàng)建容器 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 //啟動(dòng)容器 nginx root@test:/jia/test# docker ps -a //查看容器狀態(tài) 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 查看容器改動(dòng) 語法 docker diff 容器 符號(hào)說明 A 添加了文件或目錄 D 文件或目錄已刪除 C 文件或目錄已更改 示例 默認(rèn)使用方法 root@test:/jia/test# docker diff nginx C /root A /root/.bash_history root@test:/jia/test# 復(fù)制test文件到nginx容器中查看你改動(dòng)情況 root@test:/jia/test# docker cp test nginx:/ //復(fù)制test文件到容器的根目錄 root@test:/jia/test# docker diff nginx A /test C /root A /root/.bash_history root@test:/jia/test# ## exec 在容器中執(zhí)行命令 語法 docker exec 參數(shù) 容器ID/容器名稱 命令 參數(shù)說明 --detach , -d:后臺(tái)運(yùn)行容器 --detach-keys:覆蓋分離容器的鍵序列 --env , -e:設(shè)置環(huán)境變量 --interactive , -i: 即使未連接STDIN也保持打開狀態(tài) --privileged:賦予命令擴(kuò)展權(quán)限 --tty , -t: 分配偽TTY --user , -u:指定運(yùn)行用戶 --workdir , -w:設(shè)置工作目錄 示例 使用容器打印“hello word” root@test:/jia/test# docker run --name centos -itd centos:latest //創(chuàng)建并啟動(dòng)容器 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 //設(shè)置工作目錄,并進(jìn)行打印目錄 /var/www/html root@test:/jia/test# ## login 登入docker鏡像源服務(wù)器 語法 docker login 參數(shù) 服務(wù)器地址 參數(shù)說明 -p, --password:指定登錄服務(wù)器用戶的密碼 -u, --username:指定登錄服務(wù)器用戶的賬戶 示例 默認(rèn)登錄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# 指定用戶名和密碼進(jìn)行登錄 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 退出登錄鏡像源服務(wù)器 語法 docker logout 鏡像倉庫地址 示例 退出dockerhub用戶登錄 root@jia:/etc# docker login -u blxt -p 'JKY@jjj.123' //登錄dockerhub服務(wù)器 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 輸出當(dāng)前容器的日志信息 語法 docker logs 參數(shù) 容器ID/容器名 參數(shù)說明 --details:查看日志詳細(xì)信息 --follow , -f:實(shí)時(shí)動(dòng)態(tài)的查看日志 --since:顯示生成日志的時(shí)間 --tail:顯示日志行數(shù) --timestamps , -t:顯示時(shí)間戳 --until:顯示某一時(shí)間段的日志信息 示例 默認(rèn)查看容器日志 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# 實(shí)時(shí)顯示nginx容器的日志 root@jia:/etc# docker run --name nginx -itd nginx:latest //創(chuàng)建并啟動(dòng)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服務(wù) <!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 //創(chuàng)建并啟動(dòng)容器,映射容器所有端口 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 參數(shù) 參數(shù)說明 --all , -a:查看所有容器 --filter , -f:根據(jù)相關(guān)條件進(jìn)行過濾 --format: 使用Go模板打印漂亮的容器 --last , -n:顯示第幾個(gè)創(chuàng)建的容器 --latest , -l:顯示最新創(chuàng)建的容器 --no-trunc:顯示詳細(xì)信息 --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:~# 查看正在運(yùn)行的容器 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 重新啟動(dòng)容器 語法 docker restart 容器名稱/容器ID 參數(shù) -t:設(shè)置重啟執(zhí)行時(shí)間 示例 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 參數(shù) 容器ID/容器名稱 參數(shù) --force , -f:根據(jù)條件進(jìn)行刪除容器,強(qiáng)制刪除正在運(yùn)行的容器 --link , -l:移除容器間的網(wǎng)絡(luò)連接,而非容器本身 -v, --volumes:刪除與容器關(guān)聯(lián)的文件系統(tǒng) 示例 刪除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容器正在運(yùn)行所以無法使用此參數(shù)刪除 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) //刪除容器失敗,因?yàn)槿萜髡谶\(yùn)行 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 參數(shù) 鏡像名稱/鏡像ID 參數(shù) --force , -f:強(qiáng)制刪除 --no-prune:不移除該鏡像的過程鏡像,默認(rèn)移除 示例 默認(rèn)刪除鏡像 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:~# 強(qiáng)制刪除正在運(yùn)行的鏡像 root@jia:~# docker run -itd --name nginx nginx //啟動(dòng)創(chuàng)建容器 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 //默認(rèn)刪除鏡像,刪除失敗,報(bào)錯(cuò)鏡像正在使用 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:~# 注意:當(dāng)強(qiáng)制刪除鏡像后,會(huì)直接刪除,但是會(huì)生成新的鏡像,容器不會(huì)被刪除 ## save 保存鏡像為tar包 語法 docker save 參數(shù) 鏡像名稱 文件目錄及名稱.tar 參數(shù)說明 --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# 將多個(gè)鏡像保存到一個(gè)文件 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進(jìn)行打包壓縮 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 查看容器中進(jìn)程信息 語法 docker top 參數(shù) 容器ID/容器名稱 示例 查看nginx容器中所有進(jìn)程信息 root@jia:/jia/jia# docker run --name nginx -itd nginx:latest //創(chuàng)建啟動(dòng)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 參數(shù) 參數(shù)說明 -f, --format:使用給定的Go模板格式化輸出 --kubeconfig:Kubernetes配置文件 示例 查看docker版本詳細(xì)信息 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服務(wù)器實(shí)時(shí)事件 語法 docker events 參數(shù) 參數(shù)說明 -f,--filter:根據(jù)條件過濾事件 --since :從指定的時(shí)間戳后顯示所有事件 --until :流水時(shí)間顯示到指定的時(shí)間為止 --format:使用給定的Go模板格式化輸出 示例 容器啟動(dòng)停止,使用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 //啟動(dòng)容器 nginx root@jia:/jia/jia# root@jia:~# docker events //查看實(shí)時(shí)事件,注意此命令不會(huì)自動(dòng)終止需要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文件導(dǎo)入docker為鏡像 語法 docker import 參數(shù) tar文件目錄名稱 導(dǎo)入的鏡像名稱:tag(版本) 參數(shù)說明 --change , -c: 將Dockerfile指令應(yīng)用于創(chuàng)建的映像 --message , -m: 設(shè)置導(dǎo)入圖像的提交消息 示例 將nginx.tar的鏡像包導(dǎo)入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# 導(dǎo)入使用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導(dǎo)出容器為tar文件 語法 docker export 參數(shù) 容器名稱/容器ID 參數(shù)說明 --output , -o:寫入文件 示例 導(dǎo)出centos容器并命名為centos.tar root@jia:/jia/jia# docker run --name centos -itd centos:latest //創(chuàng)建并啟動(dòng)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# 導(dǎo)出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# 導(dǎo)出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系統(tǒng)信息 語法 docekr info 參數(shù) 參數(shù)說明 --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 查看容器詳細(xì)信息 語法 docker inspect 參數(shù) 容器ID/容器名稱 參數(shù)說明 --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 殺死正在運(yùn)行的容器 語法 docker kill 參數(shù) 容器ID/容器名稱 參數(shù)說明 --signal , -s:向容器發(fā)送信號(hào) 示例 殺死正在運(yùn)行的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# 殺死正在運(yùn)行的nginx容器,發(fā)送kill信號(hào) 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 參數(shù) tar文件 參數(shù)說明 -i, --input:從tar存檔文件中讀取,而不是從STDIN中讀取 --quiet , -q:顯示簡要信息 示例 導(dǎo)入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 //創(chuàng)建并運(yùn)行 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# 容器暫停后會(huì)在狀態(tài)處加(Paused) ## unpause 取消容器暫停狀態(tài) 語法 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 給鏡像進(jìn)行打標(biāo)簽 語法 docker tag 源鏡像ID/源鏡像名稱:tag(版本) 目標(biāo)鏡像名稱:tag(版本) 示例 給centos鏡像重新打標(biāo)簽 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 阻塞運(yùn)行直到容器停止,然后打印出它的退出代碼 語法 docekr wait 容器ID/容器名稱 示例 root@jia:/jia/jia# docker run -itd --name nginx nginx //創(chuàng)建啟動(dòng)容器 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 //啟動(dòng)阻塞 root@jia:~# curl 172.18.0.2 //發(fā)現(xiàn)無法訪問容器nginx頁面

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI