一、 Docker容器生命周期管理1. create用法
 命令解释:创建一个容器,但不启动,容器名称为 mynginx
 hecy-MacBook-Pro:~ hcy$ docker create --name mynginx nginx:latest
 Unable to find image ‘nginx:latest’ locally
 latest: Pulling from library/nginx
 a5a632f73cs8: Pull complete
 1ba02017c112: Pull complete
 33b17c923de: Pull complete
 Digest: sha256:5d3dsfsafadgaNHb410ad88e6fe77500c87d3970eca97f54dba
 Status: Downloaded newer image for nginx:latest
 3a9e561b7629c68dgjgergebe31df52685f08c98f58c940e1a164c1f8d77c
 2. run用法
 命令解释:创建一个容器并运行它
 可选参数:
 -a stdin: 指定标准输入输出内容类型,可选 STDIN/STDOUT/STDERR 三项;
 -d: 后台运行容器,并返回容器ID;
 -i: 以交互模式运行容器,通常与 -t 同时使用;
 -p: 端口映射,格式为:主机(宿主)端口:容器端口
 -t: 为容器重新分配一个伪输入终端,通常与 -i 同时使用;
 –name=“mynginx”: 为容器指定一个名称;
 –dns 8.8.8.8: 指定容器使用的DNS服务器,默认和宿主一致;
 –dns-search example.com: 指定容器DNS搜索域名,默认和宿主一致;
 -h “hcy”: 指定容器的hostname;
 -e username=“uhcy”: 设置环境变量;
 –env-file=[]: 从指定文件读入环境变量;
 –cpuset=“0-2” 或者 --cpuset=“0,1,2”: 绑定容器到指定CPU运行;
 -m :设置容器使用内存最大值;
 –net=“bridge”: 指定容器的网络连接类型,支持 bridge/host/none/container: 四种类型;
 –link=[]: 添加链接到另一个容器;
 –expose=[]: 开放一个端口或一组端口;
 hecy-MacBook-Pro:~ hcy$ docker run -it -d -p 755:22 -p 8888:80 --restart=always --name nginx nginx:latest
 feea481119cb053e84bf3bda6f1b2fdfd99f4d28775c4015f0dafa2f
 hecy-MacBook-Pro:~ hcy$ docker ps –a #查看容器进程
 CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
 feea481119cb nginx:latest “nginx -g 'daemon of…” About a minute ago Up About a minute 0.0.0.0:755->22/tcp, 0.0.0.0:8888->80/tcp nginx
 hecy-MacBook-Pro:~ hcy$3. start/stop/restart用法
 命令解释:容器的启动,关闭,重启
 hecy-MacBook-Pro:~ hcy$ docker ps
 CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
 feea481119cb nginx:latest “nginx -g 'daemon of…” 3 minutes ago Up 3 minutes 0.0.0.0:755->22/tcp, 0.0.0.0:8888->80/tcp nginx
 hecy-MacBook-Pro:~ hcy$ docker stop feea481119cb
 feea481119cb
 hecy-MacBook-Pro:~ hcy$ docker ps
 CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
 hecy-MacBook-Pro:~ hcy$ docker restrat feea481119cb
 docker: ‘restrat’ is not a docker command.
 See ‘docker --help’
 hecy-MacBook-Pro:~ hcy$ docker restart feea481119cb
 feea481119cb
 hecy-MacBook-Pro:~ hcy$ docker ps
 CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
 feea481119cb nginx:latest “nginx -g 'daemon of…” 3 minutes ago Up 2 seconds 0.0.0.0:755->22/tcp, 0.0.0.0:8888->80/tcp nginx
 hecy-MacBook-Pro:~ hcy$4. pause/unpause用法
 命令解释:容器的暂停或恢复暂停容器,暂停状态无法正常使用该容器。
 hecy-MacBook-Pro:~ hcy$ docker ps
 CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
 feea481119cb nginx:latest “nginx -g 'daemon of…” 4 minutes ago Up About a minute 0.0.0.0:755->22/tcp, 0.0.0.0:8888->80/tcp nginx
 hecy-MacBook-Pro:~ hcy$ docker pause feea481119cb
 feea481119cb
 hecy-MacBook-Pro:~ hcy$ docker ps
 CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
 feea481119cb nginx:latest “nginx -g 'daemon of…” 6 minutes ago Up 2 minutes (Paused) 0.0.0.0:755->22/tcp, 0.0.0.0:8888->80/tcp nginx
 hecy-MacBook-Pro:~ hcy$ docker unpause feea481119cb
 feea481119cb
 hecy-MacBook-Pro:~ hcy$ docker ps
 CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
 feea481119cb nginx:latest “nginx -g 'daemon of…” 6 minutes ago Up 2 minutes 0.0.0.0:755->22/tcp, 0.0.0.0:8888->80/tcp nginx
 hecy-MacBook-Pro:~ hcy$5. exec用法
 命令解释:在运行的容器中执行命令或者进去容器
 hecy-MacBook-Pro:~ hcy$ docker exec -it nginx /bin/bash
 root@feea481119cb:/# hostname
 feea481119cb
 root@feea481119cb:/#6. kill用法
 命令解释:终止一个运行中的容器
 参数:
 -s :向容器发送终止信号
 hecy-MacBook-Pro:~ hcy$ docker ps
 CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
 feea481119cb nginx:latest “nginx -g 'daemon of…” 14 minutes ago Up 10 minutes 0.0.0.0:755->22/tcp, 0.0.0.0:8888->80/tcp nginx
 hecy-MacBook-Pro:~ hcy$ docker kill nginx
 nginx
 hecy-MacBook-Pro:~ hcy$ docker ps
 CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
 hecy-MacBook-Pro:~ hcy$7. rm用法
 命令解释:删除一个或者多个容器
 参数:
 -f :通过终止信号强制删除一个运行中的容器
 -l :移除容器间的网络连接,而非容器本身
 -v :删除与容器关联的卷
 hecy-MacBook-Pro:~ hcy$ docker ps
 CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
 feea481119cb nginx:latest “nginx -g 'daemon of…” 16 minutes ago Up 41 seconds 0.0.0.0:755->22/tcp, 0.0.0.0:8888->80/tcp nginx
 hecy-MacBook-Pro:~ hcy$ docker rm nginx
 Error response from daemon: You cannot remove a running container feea481119cb053e84bf3bda6f1b29beaddb3f91899f4d28775c4015f0dafa2f. Stop the container before attempting removal or force remove
 hecy-MacBook-Pro:~ hcy$ docker stop nginx
 nginx
 hecy-MacBook-Pro:~ hcy$ docker rm nginx
 nginx
 hecy-MacBook-Pro:~ hcy$ docker ps -a
 CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
 hecy-MacBook-Pro:~ hcy$二、 Docker容器操作1. ps用法
 命令解释:列出容器
 参数:
 -a :显示所有的容器,包括未运行的容器。
 -f :根据条件过滤显示的内容
 –format :指定返回值的模版
 -l :显示最近创建的容器
 -n :列出最近创建的n个容器
 –no-trunc :不截断输出
 -q :静默模式,只显示容器编号
 -s :显示总的文件大小。
 hecy-MacBook-Pro:~ hcy$ docker ps
 CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
 hecy-MacBook-Pro:~ hcy$ docker ps -a
 CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
 feea481119cb nginx:latest “nginx -g 'daemon of…” 16 minutes ago Exited (137) About a minute ago nginx
 hecy-MacBook-Pro:~ hcy$2. inspect用法
 命令解释:获取容器/镜像的元数据
 参数:
 -f : 指定返回值的模版文件
 -s :显示在难过的文件大小
 –type :为指定类型返回json
 查看nginx:latest的元信息
 hecy-MacBook-Pro:~ hcy$ docker inspect nginx:latest
 [
 {
 “Id”: “sha256:568c4670fa800978e08e4a51132b995a54f8d5ae83ca133ef5546d092b864acf”,
 “RepoTags”: [
 “nginx:latest”
 ],
 “RepoDigests”: [
 “nginx@sha256:5d32f60db294b5deb55d078cd4feb410ad88e6fe77500c87d3970eca97f54dba”
 ],
 “Parent”: “”,
 “Comment”: “”,
 “Created”: “2020-11-27T22:21:45.207738888Z”,
 “Container”: “1455cab9c97f6945336dd97d5dfca0429eacbb739fcd362cc528ed8a175fcc7b”,
 “ContainerConfig”: {
 “Hostname”: “1455cab9c97f”,
 “Domainname”: “”,
 “User”: “”,
 “AttachStdin”: false,
 “AttachStdout”: false,
 “AttachStderr”: false,
 “ExposedPorts”: {
 “80/tcp”: {}
 },
 “Tty”: false,
 “OpenStdin”: false,
 “StdinOnce”: false,
 “Env”: [
 “PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin”,
 “NGINX_VERSION=1.15.7-1~stretch”,
 “NJS_VERSION=1.15.7.0.2.6-1~stretch”
 ],
 “Cmd”: [
 “/bin/sh”,
 “-c”,
 "#(nop) ",
 “CMD [“nginx” “-g” “daemon off;”]”
 ],
 “ArgsEscaped”: true,
 “Image”: “sha256:cf9409661039371fa3448619fb988625e4542108921c4c8f923c5bccc51601eb”,
 “Volumes”: null,
 “WorkingDir”: “”,
 “Entrypoint”: null,
 “OnBuild”: [],
 “Labels”: {
 “maintainer”: “NGINX Docker Maintainers docker-maint@nginx.com”
 },
 “StopSignal”: “SIGTERM”
 },
 “DockerVersion”: “17.06.2-ce”,
 “Author”: “”,
 “Config”: {
 “Hostname”: “”,
 “Domainname”: “”,
 “User”: “”,
 “AttachStdin”: false,
 “AttachStdout”: false,
 “AttachStderr”: false,
 “ExposedPorts”: {
 “80/tcp”: {}
 },
 “Tty”: false,
 “OpenStdin”: false,
 “StdinOnce”: false,
 “Env”: [
 “PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin”,
 “NGINX_VERSION=1.15.7-1~stretch”,
 “NJS_VERSION=1.15.7.0.2.6-1~stretch”
 ],
 “Cmd”: [
 “nginx”,
 “-g”,
 “daemon off;”
 ],
 “ArgsEscaped”: true,
 “Image”: “sha256:cf9409661039371fa3448619fb988625e4542108921c4c8f923c5bccc51601eb”,
 “Volumes”: null,
 “WorkingDir”: “”,
 “Entrypoint”: null,
 “OnBuild”: [],
 “Labels”: {
 “maintainer”: “NGINX Docker Maintainers docker-maint@nginx.com”
 },
 “StopSignal”: “SIGTERM”
 },
 “Architecture”: “amd64”,
 “Os”: “linux”,
 “Size”: 109116878,
 “VirtualSize”: 109116878,
 “GraphDriver”: {
 “Data”: {
 “LowerDir”: “/var/lib/docker/overlay2/b2a9c894aa016618fdae88f2cd152cf11f1f05461d927d4682efe60c4366f038/diff:/var/lib/docker/overlay2/a9bc2292493221b766a47272eb1d9d7c2b0049bda74a745dd82bc82443f5ec1f/diff”,
 “MergedDir”: “/var/lib/docker/overlay2/b9af4051377bdbc6e1435834a73ed8c23d5bb89bfef33eb199347ff315038f32/merged”,
 “UpperDir”: “/var/lib/docker/overlay2/b9af4051377bdbc6e1435834a73ed8c23d5bb89bfef33eb199347ff315038f32/diff”,
 “WorkDir”: “/var/lib/docker/overlay2/b9af4051377bdbc6e1435834a73ed8c23d5bb89bfef33eb199347ff315038f32/work”
 },
 “Name”: “overlay2”
 },
 “RootFS”: {
 “Type”: “layers”,
 “Layers”: [
 “sha256:ef68f6734aa485edf13a8509fe60e4272428deaf63f446a441b79d47fc5d17d3”,
 “sha256:ad5345cbb119f7c720123e3adf28b164143e4157ca6e46a629ca694e75f7825f”,
 “sha256:ece4f9fdef598687f23d39643bacbf2c609201b087b93bbae81b931da72d2a64”
 },
 “Metadata”: {
 “LastT
 agTime”: “0001-01-01T00:00:00Z”
 }
 }
 hecy-MacBook-Pro:~ hcy$
 获取容器的ip方法
 hecy-MacBook-Pro:~ hcy$ docker inspect -f {{.NetworkSettings.Networks.bridge.IPAddress}} nginx
 172.17.0.2
 或者
 hecy-MacBook-Pro:~ hcy$ docker inspect --format=’{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}’ nginx
 172.17.0.2
 hecy-MacBook-Pro:~ hcy$3. top用法
 命令解释:查看容器中运行的进程信息,支持ps的参数
 hecy-MacBook-Pro:~ hcy$ docker top nginx
 PID USER TIME COMMAND
 3208 root 0:00 nginx: master process nginx -g daemon off;
 3251 101 0:00 nginx: worker process
 hecy-MacBook-Pro:~ hcy$
 查看所有容器的进程信息
 hecy-MacBook-Pro:~ hcy$ for i in docker ps |grep Up|awk '{print $1}' ; do echo \ && docker top 4. logs用法
 命令解释:获取容器的日志
 参数:
 -f :跟踪日志输出
 –since :显示某个开始时间的所有日志
 -t :显示时间戳
 –tail :仅列出最新N条容器日志
 hecy-MacBook-Pro:~ hcy$ docker logs -f nginx
 192.168.2.1 - - [08/Dec/2020:03:22:35 +0000] “GET / HTTP/1.1” 200 612 “-” “Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.110 Safari/537.36” “-”
 2020/12/08 03:22:36 [error] 24#24: *1 open() “/usr/share/nginx/html/favicon.ico” failed (2: No such file or directory), client: 192.168.2.1, server: localhost, request: “GET /favicon.ico HTTP/1.1”, host: “192.168.1.3:8888”, referrer: “http://192.168.1.3:8888/”
 192.168.2.1 - - [08/Dec/2020:03:22:36 +0000] “GET /favicon.ico HTTP/1.1” 404 555 “http://192.168.1.3:8888/” “Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.110 Safari/537.36” “-”
 192.168.2.1 - - [08/Dec/2020:03:22:47 +0000] “GET / HTTP/1.1” 304 0 “-” “Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.110 Safari/537.36” “-”
 192.168.2.1 - - [08/Dec/2020:03:23:48 +0000] “GET / HTTP/1.1” 200 612 “-” “Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/12.0.2 Safari/605.1.15” “-”
 2020/12/08 03:23:48 [error] 24#24: *5 open() “/usr/share/nginx/html/favicon.ico” failed (2: No such file or directory), client: 192.168.2.1, server: localhost, request: “GET /favicon.ico HTTP/1.1”, host: “192.168.1.3:8888”, referrer: “http://192.168.1.3:8888/”
 192.168.2.1 - - [08/Dec/2020:03:23:48 +0000] “GET /favicon.ico HTTP/1.1” 404 153 “http://192.168.1.3:8888/” “Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/12.0.2 Safari/605.1.15” “-”
 查看容器nginx从2020/12/8的最新5条日志
 hecy-MacBook-Pro:~ hcy$ docker logs --since=“2020-12-08” --tail=5 nginx
 192.168.2.1 - - [08/Dec/2020:03:22:36 +0000] “GET /favicon.ico HTTP/1.1” 404 555 “http://192.168.1.3:8888/” “Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.110 Safari/537.36” “-”
 192.168.2.1 - - [08/Dec/2020:03:22:47 +0000] “GET / HTTP/1.1” 304 0 “-” “Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.110 Safari/537.36” “-”
 192.168.2.1 - - [08/Dec/2020:03:23:48 +0000] “GET / HTTP/1.1” 200 612 “-” “Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/12.0.2 Safari/605.1.15” “-”
 2020/12/08 03:23:48 [error] 24#24: *5 open() “/usr/share/nginx/html/favicon.ico” failed (2: No such file or directory), client: 192.168.2.1, server: localhost, request: “GET /favicon.ico HTTP/1.1”, host: “192.168.1.3:8888”, referrer: “http://192.168.1.3:8888/”
 192.168.2.1 - - [08/Dec/2020:03:23:48 +0000] “GET /favicon.ico HTTP/1.1” 404 153 “http://192.168.1.3:8888/” “Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/12.0.2 Safari/605.1.15” “-”
 hecy-MacBook-Pro:~ hcy$5. events用法
 命令解释:从服务器获取实时事件
 参数:
 -f :根据条件过滤事件;
 –since :从指定的时间戳后显示所有事件;
 –until :流水时间显示到指定的时间为止;
 获取2020/12/8的所有事件
 hecy-MacBook-Pro:~ hcy$ docker events --since=“2020-12-08”
 2020-12-08T09:46:57.832351102+08:00 image pull nginx:latest (maintainer=NGINX Docker Maintainers docker-maint@nginx.com, name=nginx)
 2020-12-08T09:46:57.938764857+08:00 container create 3a9e561b7629c6880c776a9331dcc9d052685f08c98f58c940e1a164c1f8d77c (image=nginx:latest, maintainer=NGINX Docker Maintainers docker-maint@nginx.com, name=mynginx)
 2020-12-08T10:23:52.553078826+08:00 container create feea481119cb053e84bf3bda6f1b29beaddb3f91899f4d28775c4015f0dafa2f (image=nginx:latest, maintainer=NGINX Docker Maintainers docker-maint@nginx.com, name=nginx)
 2020-12-08T10:23:52.678445332+08:00 network connect 2651d136cf62ba9dc0ebe674eca0c1d9f73cf4230e345aec01be0ddd797715bb (cnotallow=feea481119cb053e84bf3bda6f1b29beaddb3f91899f4d28775c4015f0dafa2f, name=bridge, type=bridge)
 2020-12-08T10:23:53.133906581+08:00 container start feea481119cb053e84bf3bda6f1b29beaddb3f91899f4d28775c4015f0dafa2f (image=nginx:latest, maintainer=NGINX Docker Maintainers docker-maint@nginx.com, name=nginx)
 2020-12-08T10:25:07.949445750+08:00 container destroy 3a9e561b7629c6880c776a9331dcc9d052685f08c98f58c940e1a164c1f8d77c (image=nginx:latest, maintainer=NGINX Docker Maintainers docker-maint@nginx.com, name=mynginx)
 如果指定的时间是到秒级的,需要将时间转成时间戳。如果时间为日期的话,可以直接使用,如–since=“2020-01-01”。6. wait用法
 命令说明:阻塞运行知道容器停止,然后打印出它的退出代码
 hecy-MacBook-Pro:~ hcy$ docker wait nginx7. attach用法
 命令解释:连接到正在运行中的容器
 –sig-proxy:防止CTRL+D 或 CRTL+C退出容器
 hecy-MacBook-Pro:~ hcy$ docker attach --sig-proxy=false nginx
 192.168.2.1 - - [08/Dec/2020:03:37:47 +0000] “GET / HTTP/1.1” 200 612 “-” “Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/12.0.2 Safari/605.1.15” “-”8. port用法
 命令解释:列出指定的容器的端口映射,或查找将PRIVATE_PORT NAT到面向公众的端口。
 hecy-MacBook-Pro:~ hcy$ docker port nginx
 22/tcp -> 0.0.0.0:755
 80/tcp -> 0.0.0.0:8888
 hecy-MacBook-Pro:~ hcy$9. export用法
 命令解释:将文件系统作为一个tar归档文件导出到STDOUT。保存当前镜像状态。
 hecy-MacBook-Pro:~ hcy$ docker export -o nginx-hcy.tar nginx
 hecy-MacBook-Pro:~ hcy$ ls nginx-hcy.tar
 nginx-hcy.tar
 hecy-MacBook-Pro:~ hcy$三、 Docker容器rootfs命令1. commit用法
 命令解释:从容器创建一个新的镜像,提交到镜像库。
 参数:
 -a :提交的镜像作者;
 -c :使用Dockerfile指令来创建镜像;
 -m :提交时的说明文字;
 -p :在commit时,将容器暂停。
 hecy-MacBook-Pro:~ hcy$ docker commit -a “hcy” -m “hcy commit nginx test” nginx hcy:v1
 sha256:6ec4ef7718a8b311f261e8eb12f2b55a42f631e75ad7cd7a5c105620c6abd7ce
 hecy-MacBook-Pro:~ hcy$ docker images hcy:v1
 REPOSITORY TAG IMAGE ID CREATED SIZE
 hcy v1 6ec4ef7718a8 10 seconds ago 109MB
 hecy-MacBook-Pro:~ hcy$2. cp用法
 命令说明:用于容器与主机之间的数据拷贝
 参数说明:
 -L :保持源目标中的链接
 hecy-MacBook-Pro:~ hcy$ echo “nginxtest” >> test
 hecy-MacBook-Pro:~ hcy$ docker cp test nginx:/
 hecy-MacBook-Pro:~ hcy$ docker exec -it nginx /bin/bash
 root@96ddce20c551:/# cat test
 nginxtest
 root@96ddce20c551:/#3. diff用法
 命令说明:检查容器里文件结构的更改
 hecy-MacBook-Pro:~ hcy$ docker diff nginx
 C /run
 A /run/nginx.pid
 C /var
 C /var/cache
 C /var/cache/nginx
 A /var/cache/nginx/client_temp
 A /var/cache/nginx/fastcgi_temp
 A /var/cache/nginx/proxy_temp
 A /var/cache/nginx/scgi_temp
 A /var/cache/nginx/uwsgi_temp
 C /root
 A /root/.bash_history
 A /test
 hecy-MacBook-Pro:~ hcy$四、 Docker镜像仓库1. login/logout用法
 命令解释:登陆或登出docker镜像仓库
 参数:
 -u :登陆的用户名
 -p :登陆的密码
 因为我下载了docker客户端,直接命令输入
 hecy-MacBook-Pro:~ hcy$ docker login –u hcy –p *******
 Authenticating with existing credentials…
 Login Succeeded
 hecy-MacBook-Pro:~ hcy$
 2. pull用法
 命令解释:从镜像库拉取镜像活着更新指定镜像
 参数:
 -a :拉取所有 tagged 镜像
 –disable-content-trust :忽略镜像的校验,默认开启
 hecy-MacBook-Pro:~ hcy$ docker pull nginx
 3. push用法
 命令解释:将本地的镜像上传到镜像仓库,前提要先登录镜像仓库
 参数:
 –disable-content-trust :忽略镜像的校验,默认开启
 hecy-MacBook-Pro:~ hcy$ docker push hcy:v1
 The push refers to repository [docker.io/library/hcy]
 fae8ad5437e1: Preparing
 ece4f9fdef59: Preparing
 ad5345cbb119: Preparing
 ef68f6734aa4: Preparing
 denied: requested access to the resource is denied
 hecy-MacBook-Pro:~ hcy$
 4. search
 命令解释:从docker hub 中查找镜像
 参数:
 –automated :只列出 automated build类型的镜像;
 –no-trunc :显示完整的镜像描述;
 -s :列出收藏数不小于指定值的镜像。
 hecy-MacBook-Pro:~ hcy$ docker search nginx
 NAME DESCRIPTION STARS OFFICIAL AUTOMATED
 nginx Official build of Nginx. 10502 [OK]
 jwilder/nginx-proxy Automated Nginx reverse proxy for docker con… 1483 [OK]
 richarvey/nginx-php-fpm Container running Nginx + PHP-FPM capable of… 660 [OK]
 jrcs/letsencrypt-nginx-proxy-companion LetsEncrypt container to use with nginx as p… 450 [OK]
 kong Open-source Microservice & API Management la… 257 [OK]
 webdevops/php-nginx Nginx with PHP-FPM 118 [OK]
 五、 Docker本地镜像管理1. images用法
 命令解释:列出本地镜像
 参数
 -a :列出本地所有的镜像(含中间映像层,默认情况下,过滤掉中间映像层);
 –digests :显示镜像的摘要信息;
 -f :显示满足条件的镜像;
 –format :指定返回值的模板文件;
 –no-trunc :显示完整的镜像信息;
 -q :只显示镜像ID。
 hecy-MacBook-Pro:~ hcy$ docker images
 REPOSITORY TAG IMAGE ID CREATED SIZE
 hcy v1 6ec4ef7718a8 5 hours ago 109MB
 nginx latest 568c4670fa80 10 days ago 109MB
 hecy-MacBook-Pro:~ hcy$
 2. tag用法
 命令说明:标记本地镜像,将其归入某一个仓库
 hecy-MacBook-Pro:~ hcy$ docker tag 6ec4ef7718a8 hecy/hcy:v1
 hecy-MacBook-Pro:~ hcy$ docker images hecy/hcy:v1
 REPOSITORY TAG IMAGE ID CREATED SIZE
 hecy/hcy v1 6ec4ef7718a8 5 hours ago 109MB
 hecy-MacBook-Pro:~ hcy$
 3. history用法
 命令解释:查看指定镜像的创建历史
 参数:
 -H :以可读的格式打印镜像大小和日期,默认为true;
 –no-trunc :显示完整的提交记录;
 -q :仅列出提交记录ID。
 hecy-MacBook-Pro:~ hcy$ docker history hecy/hcy:v1
 IMAGE CREATED CREATED BY SIZE COMMENT
 6ec4ef7718a8 5 hours ago nginx -g daemon off; 108B hcy commit nginx test
 568c4670fa80 10 days ago /bin/sh -c #(nop) CMD [“nginx” “-g” “daemon… 0B
 10 days ago /bin/sh -c #(nop) STOPSIGNAL [SIGTERM] 0B
 10 days ago /bin/sh -c #(nop) EXPOSE 80/tcp 0B
 10 days ago /bin/sh -c ln -sf /dev/stdout /var/log/nginx… 22B
 10 days ago /bin/sh -c set -x && apt-get update && apt… 53.8MB
 10 days ago /bin/sh -c #(nop) ENV NJS_VERSION=1.15.7.0.… 0B
 10 days ago /bin/sh -c #(nop) ENV NGINX_VERSION=1.15.7-… 0B
 3 weeks ago /bin/sh -c #(nop) LABEL maintainer=NGINX Do… 0B
 3 weeks ago /bin/sh -c #(nop) CMD [“bash”] 0B
 3 weeks ago /bin/sh -c #(nop) ADD file:dab9baf938799c515… 55.3MB
 hecy-MacBook-Pro:~ hcy$
 4. build用法
 命令解释:用于Dockerfile创建镜像
 参数
 –build-arg=[] :设置镜像创建时的变量;
 –cpu-shares :设置 cpu 使用权重;
 –cpu-period :限制 CPU CFS周期;
 –cpu-quota :限制 CPU CFS配额;
 –cpuset-cpus :指定使用的CPU id;
 –cpuset-mems :指定使用的内存 id;
 –disable-content-trust :忽略校验,默认开启;
 -f :指定要使用的Dockerfile路径;
 –force-rm :设置镜像过程中删除中间容器;
 –isolation :使用容器隔离技术;
 –label=[] :设置镜像使用的元数据;
 -m :设置内存最大值;
 –memory-swap :设置Swap的最大值为内存+swap,”-1"表示不限swap;
 –no-cache :创建镜像的过程不使用缓存;
 –pull :尝试去更新镜像的新版本;
 –quiet, -q :安静模式,成功后只输出镜像 ID;
 –rm :设置镜像成功后删除中间容器;
 –shm-size :设置/dev/shm的大小,默认值是64M;
 –ulimit :Ulimit配置。
 –tag, -t: 镜像的名字及标签,通常 name:tag 或者 name 格式;可以在一次构建中为一个镜像设置多个标签。
 –network: 默认 default。在构建期间设置RUN指令的网络模式
 使用当前目录的 Dockerfile 创建镜像,标签为 runoob/ubuntu:v1。
 docker build -t runoob/ubuntu:v1 .
 使用URL github.com/creack/docker-firefox 的 Dockerfile 创建镜像。
 docker build github.com/creack/docker-firefox
 也可以通过 -f Dockerfile 文件的位置:
 docker build -f /path/to/a/Dockerfile .
 在 Docker 守护进程执行 Dockerfile 中的指令前,首先会对 Dockerfile 进行语法检查,有语法错误时会返回:
 $ docker build -t test/myapp .
 Sending build context to Docker daemon 2.048 kB
 Error response from daemon: Unknown instruction: RUNCMD
 5. save用法
 命令解释:将指定镜像保存成tar归档文件
 hecy-MacBook-Pro:~ hcy$ docker ps
 CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
 96ddce20c551 nginx:latest “nginx -g 'daemon of…” 6 hours ago Up 5 hours 0.0.0.0:755->22/tcp, 0.0.0.0:8888->80/tcp nginx
 hecy-MacBook-Pro:~ hcy$ docker save -o nginx_hcy_v1.tar nginx
 hecy-MacBook-Pro:~ hcy$ ls nginx_hcy_v1.tar
 nginx_hcy_v1.tar
 hecy-MacBook-Pro:~ hcy$
 6. import
 命令解释:从归档文件中创建镜像
 参数
 -c :应用docker 指令创建镜像;
 -m :提交时的说明文字;
 hecy-MacBook-Pro:~ hcy$ docker import nginx_hcy_v1.tar hecy/hcy:v1
 sha256:7afc0a247e015d0984f67a8b694c7c8e8363138c7ca6e4c3c69b583c2322774e
 hecy-MacBook-Pro:~ hcy$
 7. rmi
 命令解释:删除本地一个或多个镜像
 参数:
 -f :强制删除;
 –no-prune :不移除该镜像的过程镜像,默认移除;
 hecy-MacBook-Pro:~ hcy$ docker images
 REPOSITORY TAG IMAGE ID CREATED SIZE
 hecy/hcy v1 7afc0a247e01 About a minute ago 113MB
 nginx latest 568c4670fa80 10 days ago 109MB
 hecy-MacBook-Pro:~ hcy$ docker rmi 7afc0a247e01
 Untagged: hecy/hcy:v1
 Deleted: sha256:7afc0a247e015d0984f67a8b694c7c8e8363138c7ca6e4c3c69b583c2322774e
 Deleted: sha256:c0b54e54ff0bfd2330f0a541c02bf5706674cc3d2b0c7899457a5d543acaacff
 hecy-MacBook-Pro:~ hcy$
 六、 查看docker版本信息1. info用法
 命令解释:显示docker系统信息,包括镜像和容器数
 hecy-MacBook-Pro:~ hcy$ docker info
 Containers: 1
 Running: 1
 Paused: 0
 Stopped: 0
 Images: 2
 Server Version: 18.09.0
 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 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: 468a545b9edcd5932818eb9de8e72413e616e86e
 runc version: 69663f0bd4b60df09991c08812a60108003fa340
 init version: fec3683
 Security Options:
 seccomp
 Profile: default
 Kernel Version: 4.9.125-linuxkit
 Operating System: Docker for Mac
 OSType: linux
 Architecture: x86_64
 CPUs: 2
 Total Memory: 1.952GiB
 Name: linuxkit-025000000001
 ID: 4CPI:PVLC:WBJL:QQPX:JP2X:B7RP:MGYE:SA7Y:ULC5:YOSL:RAYE:EMFD
 2. version用法
 命令解释:显示docker版本信息
 参数:
 -f :指定返回值的模板文件。
 hecy-MacBook-Pro:~ hcy$ docker version
 Client: Docker Engine - Community
 Version: 18.09.0
 API version: 1.39
 Go version: go1.10.4
 Git commit: 4d60db4
 Built: Wed Nov 7 00:47:43 2020
 OS/Arch: darwin/amd64
 Experimental: false
 Server: Docker Engine - Community
 Engine:
 Version: 18.09.0
 API version: 1.39 (minimum version 1.12)+
 Go version: go1.10.4
 Git commit: 4d60db4
 Built: Wed Nov 7 00:55:00 2020
 OS/Arch: linux/amd64
 Experimental: false
 hecy-MacBook-Pro:~ hcy$