前面我们聊了docker的基本概念、架构、镜像、网络、数据卷,回顾请参考https://www.cnblogs.com/qiuhom-1874/category/1766327.html;今天这篇博客主要是对前面博客中的常用命令做一个总结补充说明;
在前面的博客中我们说过docker是基于LXC之上封装来实现容器的,其核心就是利用内核的资源名称空间和Cgroup实现资源的管控和隔离;这里要补充一点的是docker容器把资源抽象成对象;作为客户端,docker这个命令其实就是在对这些抽象出来的资源对象做增删查改的操作;docker的API是RESTful风格的API,所以它支持类似像http协议里的GET、POST、PUT、DELETE等操作;RESTful风格的API有这样的特点;1、每一个URI代表1种资源;2、支持类似http里的GET、POST、PUT、DELETE;GET用来获取资源,POST用来新建资源(也可以用于更新资源),PUT用来更新资源,DELETE用来删除资源;3、通过操作资源的表现形式来操作资源;4、资源的表现形式是XML或者HTML;5、客户端与服务端之间的交互在请求之间是无状态的,从客户端到服务端的每个请求都必须包含理解请求所必需的信息。这也是docker客户端和服务端使用http或https协议通信的原因吧!!
了解了上面docker的API风格,我们接下来再说说docker的对象;docker的对象大概有这样几个,1、镜像(image),2、容器(container),3、网络(network),4、卷(volumes),5、插件(plugins);除上5种还有就是其他对象;其中最为核心的就image,container,network;对于docker来说运行一容器的最最基础的是需要一镜像,其次就是网络,对于容器来讲,我们运行容器的目的就是为了提供服务,而绝大部分服务都是基于网络的,所以真正产生价值的是把服务通过网络提供给客户;所以网络是必不可少的资源;
镜像相关操作
镜像搜索
[root@node1 ~]# docker search nginx NAME DESCRIPTION STARS OFFICIAL AUTOMATED nginx Official build of Nginx. 13255 [OK] jwilder/nginx-proxy Automated Nginx reverse proxy for docker con… 1812 [OK] richarvey/nginx-php-fpm Container running Nginx + PHP-FPM capable of… 775 [OK] linuxserver/nginx An Nginx container, brought to you by LinuxS… 113 bitnami/nginx Bitnami nginx Docker Image 83 [OK] tiangolo/nginx-rtmp Docker image with Nginx using the nginx-rtmp… 74 [OK] alfg/nginx-rtmp NGINX, nginx-rtmp-module and FFmpeg from sou… 64 [OK] jc21/nginx-proxy-manager Docker container for managing Nginx proxy ho… 61 nginxdemos/hello NGINX webserver that serves a simple page co… 50 [OK] jlesage/nginx-proxy-manager Docker container for Nginx Proxy Manager 44 [OK] nginx/nginx-ingress NGINX Ingress Controller for Kubernetes 32 privatebin/nginx-fpm-alpine PrivateBin running on an Nginx, php-fpm & Al… 25 [OK] schmunk42/nginx-redirect A very simple container to redirect HTTP tra… 18 [OK] nginxinc/nginx-unprivileged Unprivileged NGINX Dockerfiles 16 centos/nginx-112-centos7 Platform for running nginx 1.12 or building … 13 blacklabelops/nginx Dockerized Nginx Reverse Proxy Server. 13 [OK] centos/nginx-18-centos7 Platform for running nginx 1.8 or building n… 13 raulr/nginx-wordpress Nginx front-end for the official wordpress:f… 12 [OK] nginx/nginx-prometheus-exporter NGINX Prometheus Exporter 12 mailu/nginx Mailu nginx frontend 7 [OK] sophos/nginx-vts-exporter Simple server that scrapes Nginx vts stats a… 7 [OK] bitnami/nginx-ingress-controller Bitnami Docker Image for NGINX Ingress Contr… 5 [OK] ansibleplaybookbundle/nginx-apb An APB to deploy NGINX 1 [OK] wodby/nginx Generic nginx 1 [OK] centos/nginx-110-centos7 Platform for running nginx 1.10 or building … 0 [root@node1 ~]#
提示:docker search 命令是从dockerhub中检索对应镜像的命令;该命令有个-s选项,是用于指定镜像的星级;通过使用-s选项可以筛选出我们指定星级以上的镜像;
[root@node1 ~]# docker search -s 5000 nginx Flag --stars has been deprecated, use --filter=stars=3 instead NAME DESCRIPTION STARS OFFICIAL AUTOMATED nginx Official build of Nginx. 13255 [OK] [root@node1 ~]# docker search -s 500 nginx Flag --stars has been deprecated, use --filter=stars=3 instead NAME DESCRIPTION STARS OFFICIAL AUTOMATED nginx Official build of Nginx. 13255 [OK] jwilder/nginx-proxy Automated Nginx reverse proxy for docker con… 1812 [OK] richarvey/nginx-php-fpm Container running Nginx + PHP-FPM capable of… 775 [OK] [root@node1 ~]#
镜像下载
[root@node1 ~]# docker pull nginx Using default tag: latest latest: Pulling from library/nginx afb6ec6fdc1c: Pull complete b90c53a0b692: Pull complete 11fa52a0fdc0: Pull complete Digest: sha256:30dfa439718a17baafefadf16c5e7c9d0a1cde97b4fd84f63b69e13513be7097 Status: Downloaded newer image for nginx:latest docker.io/library/nginx:latest [root@node1 ~]# docker image pull centos:7 7: Pulling from library/centos 524b0c1e57f8: Pull complete Digest: sha256:e9ce0b76f29f942502facd849f3e468232492b259b9d9f076f71b392293f1582 Status: Downloaded newer image for centos:7 docker.io/library/centos:7 [root@node1 ~]#
提示:docker pull和docker image pull两个命令都是从dockerhub仓库中下载指定镜像;这里需要提示一下,docker的镜像是镜像名+版本组成的;如果我们只指定了镜像名称没有指定版本,默认是latest版本(最新版);
查看镜像列表
[root@node1 ~]# docker image ls REPOSITORY TAG IMAGE ID CREATED SIZE nginx latest 9beeba249f3e 13 days ago 127MB centos 7 b5b4d78bc90c 3 weeks ago 203MB [root@node1 ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE nginx latest 9beeba249f3e 13 days ago 127MB centos 7 b5b4d78bc90c 3 weeks ago 203MB [root@node1 ~]#
提示:docker image ls 等同docker images 这两个命令都是用来查看本地仓库所有镜像列表;
除了上面的两种操作外,还有其他操作,我们可以通过docker image --help 来查看对镜像的命令帮助;如下
[root@node1 ~]# docker image --help Usage: docker image COMMAND Manage images Commands: build Build an image from a Dockerfile history Show the history of an image import Import the contents from a tarball to create a filesystem image inspect Display detailed information on one or more images load Load an image from a tar archive or STDIN ls List images prune Remove unused images pull Pull an image or a repository from a registry push Push an image or a repository to a registry rm Remove one or more images save Save one or more images to a tar archive (streamed to STDOUT by default) tag Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE Run 'docker image COMMAND --help' for more information on a command. [root@node1 ~]#
提示:build子命令是基于dockerfile来构建镜像;history:显示镜像的历史制作过程;import:从tarball导入内容以创建文件系统镜像;inspect:显示镜像的详细信息;load:指定本地一个tar包格式的文件从标准输入导入镜像;prune:移除未使用的镜像;push:把指定镜像推送到仓库;rm:删除一个或多个镜像;save:导出一个或多个镜像到本地指定tar文件中;默认是从标准输出导出;tag:对指定镜像新建一个标签;
查看镜像历史
[root@node1 ~]# docker image history nginx IMAGE CREATED CREATED BY SIZE COMMENT 9beeba249f3e 13 days ago /bin/sh -c #(nop) CMD ["nginx" "-g" "daemon… 0B <missing> 13 days ago /bin/sh -c #(nop) STOPSIGNAL SIGTERM 0B <missing> 13 days ago /bin/sh -c #(nop) EXPOSE 80 0B <missing> 13 days ago /bin/sh -c ln -sf /dev/stdout /var/log/nginx… 22B <missing> 13 days ago /bin/sh -c set -x && addgroup --system -… 57.6MB <missing> 13 days ago /bin/sh -c #(nop) ENV PKG_RELEASE=1~buster 0B <missing> 13 days ago /bin/sh -c #(nop) ENV NJS_VERSION=0.3.9 0B <missing> 13 days ago /bin/sh -c #(nop) ENV NGINX_VERSION=1.17.10 0B <missing> 13 days ago /bin/sh -c #(nop) LABEL maintainer=NGINX Do… 0B <missing> 2 weeks ago /bin/sh -c #(nop) CMD ["bash"] 0B <missing> 2 weeks ago /bin/sh -c #(nop) ADD file:7780c81c33e6cc5b6… 69.2MB [root@node1 ~]#
提示:以上默认是显示部分命令,如果要显示命令全部可以使用 --no-trunc选项;
查看镜像详细信息
[root@node1 ~]# docker image inspect nginx [ { "Id": "sha256:9beeba249f3ee158d3e495a6ac25c5667ae2de8a43ac2a8bfd2bf687a58c06c9", "RepoTags": [ "nginx:latest" ], "RepoDigests": [ "nginx@sha256:30dfa439718a17baafefadf16c5e7c9d0a1cde97b4fd84f63b69e13513be7097" ], "Parent": "", "Comment": "", "Created": "2020-05-15T20:15:52.366876108Z", "Container": "ed0a25109c2acf3dcb0b4926cad00692e01717b8417d36e50928aa6f03a711ca", "ContainerConfig": { "Hostname": "ed0a25109c2a", "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.17.10", "NJS_VERSION=0.3.9", "PKG_RELEASE=1~buster" ], "Cmd": [ "/bin/sh", "-c", "#(nop) ", "CMD ["nginx" "-g" "daemon off;"]" ], "ArgsEscaped": true, "Image": "sha256:23edbb1066877bcc22193518edb32ee3a50a18d52787c4f45f9a8bca95d329eb", "Volumes": null, "WorkingDir": "", "Entrypoint": null, "OnBuild": null, "Labels": { "maintainer": "NGINX Docker Maintainers <docker-maint@nginx.com>" }, "StopSignal": "SIGTERM" }, "DockerVersion": "18.09.7", "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.17.10", "NJS_VERSION=0.3.9", "PKG_RELEASE=1~buster" ], "Cmd": [ "nginx", "-g", "daemon off;" ], "ArgsEscaped": true, "Image": "sha256:23edbb1066877bcc22193518edb32ee3a50a18d52787c4f45f9a8bca95d329eb", "Volumes": null, "WorkingDir": "", "Entrypoint": null, "OnBuild": null, "Labels": { "maintainer": "NGINX Docker Maintainers <docker-maint@nginx.com>" }, "StopSignal": "SIGTERM" }, "Architecture": "amd64", "Os": "linux", "Size": 126773960, "VirtualSize": 126773960, "GraphDriver": { "Data": { "LowerDir": "/var/lib/docker/overlay2/4728ce1723abfab65d8065858c616360f45e258d53e7573be8e0f7bbe338be5d/diff:/var/lib/docker/overlay2/e998de0cd64ffe2b0410fb4595cbc3358b1b28626c8d356c6c6ac0ef97234a31/diff", "MergedDir": "/var/lib/docker/overlay2/d9c40ef0bb9226e6a84b720b9c6ce18ed46b4f61caab567b5ee13a801dbc7ef5/merged", "UpperDir": "/var/lib/docker/overlay2/d9c40ef0bb9226e6a84b720b9c6ce18ed46b4f61caab567b5ee13a801dbc7ef5/diff", "WorkDir": "/var/lib/docker/overlay2/d9c40ef0bb9226e6a84b720b9c6ce18ed46b4f61caab567b5ee13a801dbc7ef5/work" }, "Name": "overlay2" }, "RootFS": { "Type": "layers", "Layers": [ "sha256:ffc9b21953f4cd7956cdf532a5db04ff0a2daa7475ad796f1bad58cfbaf77a07", "sha256:2f4accd375d93db49e5a47c9bebe4e0dd3cef35f765f5cd36840a986435affc9", "sha256:6c7de695ede33d90077f01d60ec29e6a51552a3e350757018ff1b1ecd6cee0bf" ] }, "Metadata": { "LastTagTime": "0001-01-01T00:00:00Z" } } ] [root@node1 ~]#
提示:docker image inspect 是显示指定镜像的详细信息;其中-f选项可以只显示指定字段;起着过滤功能
[root@node1 ~]# docker image inspect -f {{.RootFS}} nginx {layers [sha256:ffc9b21953f4cd7956cdf532a5db04ff0a2daa7475ad796f1bad58cfbaf77a07 sha256:2f4accd375d93db49e5a47c9bebe4e0dd3cef35f765f5cd36840a986435affc9 sha256:6c7de695ede33d90077f01d60ec29e6a51552a3e350757018ff1b1ecd6cee0bf] } [root@node1 ~]# docker image inspect -f {{.RootFS.Type}} nginx layers [root@node1 ~]# docker image inspect -f {{.RootFS.Layers}} nginx [sha256:ffc9b21953f4cd7956cdf532a5db04ff0a2daa7475ad796f1bad58cfbaf77a07 sha256:2f4accd375d93db49e5a47c9bebe4e0dd3cef35f765f5cd36840a986435affc9 sha256:6c7de695ede33d90077f01d60ec29e6a51552a3e350757018ff1b1ecd6cee0bf] [root@node1 ~]#
提示:-f指定字段必须从“.”开始且需要用双大括号把字段括起来;如果一级字段是下还有二级字段,可以使用“.”加二级字段名称来表示显示二级字段的值;以此类推;
删除镜像
[root@node1 ~]# docker pull busybox Using default tag: latest latest: Pulling from library/busybox d9cbbca60e5f: Pull complete Digest: sha256:836945da1f3afe2cfff376d379852bbb82e0237cb2925d53a13f53d6e8a8c48c Status: Downloaded newer image for busybox:latest docker.io/library/busybox:latest [root@node1 ~]# docker pull alpine Using default tag: latest latest: Pulling from library/alpine cbdbe7a5bc2a: Pull complete Digest: sha256:9a839e63dad54c3a6d1834e29692c8492d93f90c59c978c1ed79109ea4fb9a54 Status: Downloaded newer image for alpine:latest docker.io/library/alpine:latest [root@node1 ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE nginx latest 9beeba249f3e 13 days ago 127MB busybox latest 78096d0a5478 2 weeks ago 1.22MB centos 7 b5b4d78bc90c 3 weeks ago 203MB alpine latest f70734b6a266 5 weeks ago 5.61MB [root@node1 ~]# docker image rm centos:7 Untagged: centos:7 Untagged: centos@sha256:e9ce0b76f29f942502facd849f3e468232492b259b9d9f076f71b392293f1582 Deleted: sha256:b5b4d78bc90ccd15806443fb881e35b5ddba924e2f475c1071a38a3094c3081d Deleted: sha256:edf3aa290fb3c255a84fe836109093fbfeef65c08544f655fad8d6afb53868ba [root@node1 ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE nginx latest 9beeba249f3e 13 days ago 127MB busybox latest 78096d0a5478 2 weeks ago 1.22MB alpine latest f70734b6a266 5 weeks ago 5.61MB [root@node1 ~]#
提示:删除镜像的前提是该镜像没有运行成容器;docker image rm 等同docker rmi命令;如果需要强行删除已经运行为容器的镜像,可以使用-f选项来指定;
[root@node1 ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE alpine latest f70734b6a266 5 weeks ago 5.61MB [root@node1 ~]# docker run --name a1 -d alpine d0abe1efe7138dfb409574a785e77b4941a50534a23740d6f7d2f1af36d05589 [root@node1 ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES d0abe1efe713 alpine "/bin/sh" 4 seconds ago Exited (0) 2 seconds ago a1 [root@node1 ~]# docker image rm alpine Error response from daemon: conflict: unable to remove repository reference "alpine" (must force) - container d0abe1efe713 is using its referenced image f70734b6a266 [root@node1 ~]# docker rmi alpine Error response from daemon: conflict: unable to remove repository reference "alpine" (must force) - container d0abe1efe713 is using its referenced image f70734b6a266 [root@node1 ~]# docker image rm -f alpine Untagged: alpine:latest Untagged: alpine@sha256:9a839e63dad54c3a6d1834e29692c8492d93f90c59c978c1ed79109ea4fb9a54 Deleted: sha256:f70734b6a266dcb5f44c383274821207885b549b75c8e119404917a61335981a [root@node1 ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE [root@node1 ~]#
提示:只要运行为容器,不管容器是否是运行状态,删除镜像都需要先删除容器在删除镜像,否则就需要用-f来指定强制删除镜像;通常不建议这么干;
给镜像打标签
[root@node1 ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE alpine latest f70734b6a266 5 weeks ago 5.61MB [root@node1 ~]# docker image tag alpine:latest alpine:v1 [root@node1 ~]# docker image tag alpine:latest alpine:v2 [root@node1 ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE alpine latest f70734b6a266 5 weeks ago 5.61MB alpine v1 f70734b6a266 5 weeks ago 5.61MB alpine v2 f70734b6a266 5 weeks ago 5.61MB [root@node1 ~]# docker tag alpine alpine:v3 [root@node1 ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE alpine latest f70734b6a266 5 weeks ago 5.61MB alpine v1 f70734b6a266 5 weeks ago 5.61MB alpine v2 f70734b6a266 5 weeks ago 5.61MB alpine v3 f70734b6a266 5 weeks ago 5.61MB [root@node1 ~]#
提示:docker image tag 和docker tag 等同;都是用于给镜像打标签;打标签的意思类似给镜像取别名的意思;
移除未使用的镜像
[root@node1 ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE nginx latest 9beeba249f3e 13 days ago 127MB alpine latest f70734b6a266 5 weeks ago 5.61MB alpine v1 f70734b6a266 5 weeks ago 5.61MB alpine v2 f70734b6a266 5 weeks ago 5.61MB alpine v3 f70734b6a266 5 weeks ago 5.61MB [root@node1 ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES d0abe1efe713 alpine "/bin/sh" 11 minutes ago Exited (0) 11 minutes ago a1 [root@node1 ~]# docker image prune -a WARNING! This will remove all images without at least one container associated to them. Are you sure you want to continue? [y/N] y Deleted Images: untagged: alpine:latest untagged: alpine:v1 untagged: alpine:v2 untagged: nginx:latest untagged: nginx@sha256:30dfa439718a17baafefadf16c5e7c9d0a1cde97b4fd84f63b69e13513be7097 deleted: sha256:9beeba249f3ee158d3e495a6ac25c5667ae2de8a43ac2a8bfd2bf687a58c06c9 deleted: sha256:8fb6373b4cca3383756d7fd7843dd92f95827e5f2913609e09a9621dcddb3752 deleted: sha256:8b09841626797a03a9fe5e73aa38aeacf9ff0ce85a3004236ff35234eec3b35c deleted: sha256:ffc9b21953f4cd7956cdf532a5db04ff0a2daa7475ad796f1bad58cfbaf77a07 Total reclaimed space: 126.8MB [root@node1 ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE alpine v3 f70734b6a266 5 weeks ago 5.61MB [root@node1 ~]#
提示:-a表示移除全部未使用的镜像;如果不想交互式确认可以使用-f选项,-f表示强制删除,不询问;
把指定镜像推送到仓库
[root@node1 ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE linux1874/myimg latest e408b1c6e04f 6 days ago 1.22MB linux1874/myimg v0.1 e408b1c6e04f 6 days ago 1.22MB alpine v3 f70734b6a266 5 weeks ago 5.61MB [root@node1 ~]# docker image push linux1874/myimg The push refers to repository [docker.io/linux1874/myimg] Get https://registry-1.docker.io/v2/: net/http: request canceled (Client.Timeout exceeded while awaiting headers) [root@node1 ~]# docker image push linux1874/myimg The push refers to repository [docker.io/linux1874/myimg] 4d567d38fed1: Layer already exists 1079c30efc82: Layer already exists latest: digest: sha256:6c2f6b7a0df5ca0a46cd46d858e9fd564169471e6715c0155027ac77672508f6 size: 734 4d567d38fed1: Layer already exists 1079c30efc82: Layer already exists v0.1: digest: sha256:6c2f6b7a0df5ca0a46cd46d858e9fd564169471e6715c0155027ac77672508f6 size: 734 [root@node1 ~]#
提示:如果是往自己私有仓库里推送镜像,需要先登录,然后在推镜像;这里需要注意一点镜像的命名需要同仓库一样;如果是顶级仓库则没有“/”分割;如果推送镜像没有指定版本,默认会把本地指定镜像的所有版本的推送到仓库;
镜像导出
[root@node1 ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE linux1874/myimg latest e408b1c6e04f 6 days ago 1.22MB linux1874/myimg v0.1 e408b1c6e04f 6 days ago 1.22MB alpine v3 f70734b6a266 5 weeks ago 5.61MB [root@node1 ~]# docker save alpine > alpine.tar.gz [root@node1 ~]# ls alpine.tar.gz [root@node1 ~]#
提示:docker save默认是把镜像输出到标准输出,所以我们导出时需要用重定向的方式存储到某个文件中,通常这个文件是tar格式的文件;
镜像导入
[root@node1 ~]# scp alpine.tar.gz 192.168.0.22:/root The authenticity of host '192.168.0.22 (192.168.0.22)' can't be established. ECDSA key fingerprint is SHA256:EG9nua4JJuUeofheXlgQeL9hX5H53JynOqf2vf53mII. ECDSA key fingerprint is MD5:57:83:e6:46:2c:4b:bb:33:13:56:17:f7:fd:76:71:cc. Are you sure you want to continue connecting (yes/no)? yes Warning: Permanently added '192.168.0.22' (ECDSA) to the list of known hosts. alpine.tar.gz 100% 5750KB 31.9MB/s 00:00 [root@node1 ~]#
[root@docker_node1 ~]# ip a s ens33 2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 00:0c:29:22:36:7f brd ff:ff:ff:ff:ff:ff inet 192.168.0.22/24 brd 192.168.0.255 scope global ens33 valid_lft forever preferred_lft forever inet6 fe80::20c:29ff:fe22:367f/64 scope link valid_lft forever preferred_lft forever [root@docker_node1 ~]# ls alpine.tar.gz [root@docker_node1 ~]# docker images WARNING: Error loading config file: /root/.docker/config.json: EOF REPOSITORY TAG IMAGE ID CREATED SIZE linux1874/myimg v0.1 e408b1c6e04f 6 days ago 1.22MB wordpress latest c3fa1c8546fb 4 weeks ago 540MB mysql 5.7 f965319e89de 4 weeks ago 448MB httpd 2.4.37-alpine dfd436f9a5d8 17 months ago 91.8MB [root@docker_node1 ~]# docker load -i alpine.tar.gz WARNING: Error loading config file: /root/.docker/config.json: EOF 3e207b409db3: Loading layer 5.879MB/5.879MB Loaded image: alpine:v3 [root@docker_node1 ~]# docker images WARNING: Error loading config file: /root/.docker/config.json: EOF REPOSITORY TAG IMAGE ID CREATED SIZE linux1874/myimg v0.1 e408b1c6e04f 6 days ago 1.22MB wordpress latest c3fa1c8546fb 4 weeks ago 540MB mysql 5.7 f965319e89de 4 weeks ago 448MB alpine v3 f70734b6a266 5 weeks ago 5.61MB httpd 2.4.37-alpine dfd436f9a5d8 17 months ago 91.8MB [root@docker_node1 ~]#
有关容器的操作
[root@node1 ~]# docker container --help WARNING: Error loading config file: /root/.docker/config.json: EOF Usage: docker container COMMAND Manage containers Commands: attach Attach local standard input, output, and error streams to a running container commit Create a new image from a container's changes cp Copy files/folders between a container and the local filesystem create Create a new container diff Inspect changes to files or directories on a container's filesystem exec Run a command in a running container export Export a container's filesystem as a tar archive inspect Display detailed information on one or more containers kill Kill one or more running containers logs Fetch the logs of a container ls List containers pause Pause all processes within one or more containers port List port mappings or a specific mapping for the container prune Remove all stopped containers rename Rename a container restart Restart one or more containers rm Remove one or more containers run Run a command in a new container start Start one or more stopped containers stats Display a live stream of container(s) resource usage statistics stop Stop one or more running containers top Display the running processes of a container unpause Unpause all processes within one or more containers update Update configuration of one or more containers wait Block until one or more containers stop, then print their exit codes Run 'docker container COMMAND --help' for more information on a command. [root@node1 ~]#
提示:docker container 操作大概有这么多;其中run ,exec,kill,port,logs,inspect用的比较多;其余命令其实我们看子命令就大概知道什么意思;如果需要查看某个子命令的具体用法和选项,我们可以加上子命令 然后用--help来查看帮助即可;如下
[root@node1 ~]# docker container logs --help WARNING: Error loading config file: /root/.docker/config.json: EOF Usage: docker container logs [OPTIONS] CONTAINER Fetch the logs of a container Options: --details Show extra details provided to logs -f, --follow Follow log output --since string Show logs since timestamp (e.g. 2013-01-02T13:23:37) or relative (e.g. 42m for 42 minutes) --tail string Number of lines to show from the end of the logs (default "all") -t, --timestamps Show timestamps --until string Show logs before a timestamp (e.g. 2013-01-02T13:23:37) or relative (e.g. 42m for 42 minutes) [root@node1 ~]#
提示:以上就是查看logs指明的用法和选项说明;
创建容器
[root@node1 ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE linux1874/myimg latest e408b1c6e04f 6 days ago 1.22MB linux1874/myimg v0.1 e408b1c6e04f 6 days ago 1.22MB alpine v3 f70734b6a266 5 weeks ago 5.61MB [root@node1 ~]# docker container create alpine Unable to find image 'alpine:latest' locally latest: Pulling from library/alpine Digest: sha256:9a839e63dad54c3a6d1834e29692c8492d93f90c59c978c1ed79109ea4fb9a54 Status: Downloaded newer image for alpine:latest 13bcdb5067a31389acd75b8218f73c9d759590b81e178084de3c8110af330d0b [root@node1 ~]# docker container ls CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES [root@node1 ~]# docker container ls -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 13bcdb5067a3 alpine "/bin/sh" 17 seconds ago Created tender_grothendieck d0abe1efe713 alpine "/bin/sh" 49 minutes ago Exited (0) 49 minutes ago a1 [root@node1 ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES [root@node1 ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 13bcdb5067a3 alpine "/bin/sh" 3 minutes ago Created tender_grothendieck d0abe1efe713 alpine "/bin/sh" 53 minutes ago Exited (0) 53 minutes ago a1 [root@node1 ~]#
提示:用create命令创建容器,容器不会自动运行;查看容器用ls命令,其中ls不加选项表示查看运行状态的容器列表,-a表示查看所有状态容器列表;docker container ls 就等同docker ps 都用于查看运行态容器列表;
创建并运行容器
[root@node1 ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES [root@node1 ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE linux1874/myimg latest e408b1c6e04f 6 days ago 1.22MB linux1874/myimg v0.1 e408b1c6e04f 6 days ago 1.22MB alpine latest f70734b6a266 5 weeks ago 5.61MB alpine v3 f70734b6a266 5 weeks ago 5.61MB [root@node1 ~]# docker run --name a1 -d linux1874/myimg 6be4be4e54690bc10fb07b12cc00c3173636ed95bd456f04ed2cca6cb8cc93e9 [root@node1 ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 6be4be4e5469 linux1874/myimg "/bin/sh -c '/bin/ht…" 4 seconds ago Up 2 seconds a1 [root@node1 ~]#
提示:docker run 等同docker container run;其中--name表示指定容器的的名称;-d表示后台运行,不占据现有终端;用run命令表示运行一容器,如果本地没有镜像,它默认会从dockerhub上去拖镜像,然后直接创建并运行容器;如果有镜像,则直接创建并运行为容器;
停止、启动、重启容器
[root@node1 ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 6be4be4e5469 linux1874/myimg "/bin/sh -c '/bin/ht…" 4 minutes ago Up 4 minutes a1 [root@node1 ~]# docker stop a1 a1 [root@node1 ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 6be4be4e5469 linux1874/myimg "/bin/sh -c '/bin/ht…" 4 minutes ago Exited (137) 5 seconds ago a1 [root@node1 ~]# docker start a1 a1 [root@node1 ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 6be4be4e5469 linux1874/myimg "/bin/sh -c '/bin/ht…" 4 minutes ago Up 3 seconds a1 [root@node1 ~]# docker restart a1 a1 [root@node1 ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 6be4be4e5469 linux1874/myimg "/bin/sh -c '/bin/ht…" 5 minutes ago Up 6 seconds a1 [root@node1 ~]#
提示:以上命令都支持后面跟多个容器,表示同时操作多个容器;docker start;docker stop ;docker restart;这三个命令等同docker contaier start,docker container stop ,docker container restart;停止容器除了正常用stop停止外,还可以使用kill命令停止容器;两者不同的是一个是给docker进程发送15号信号,正常停止容器,一个是发送9号信号,强杀容器;如下
提示:--rm选项表示启动一次性容器,容器停止后,自动删除容器;
查看容器日志
[root@node1 ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES [root@node1 ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE linux1874/myimg latest e408b1c6e04f 6 days ago 1.22MB linux1874/myimg v0.1 e408b1c6e04f 6 days ago 1.22MB nginx stable-alpine ab94f84cc474 5 weeks ago 21.3MB alpine latest f70734b6a266 5 weeks ago 5.61MB alpine v3 f70734b6a266 5 weeks ago 5.61MB [root@node1 ~]# docker run --name n1 -d nginx:stable-alpine a859fda7c6021a1e5398f744826d5a62257eb1d272b622ea32dac39f83009fc9 [root@node1 ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES a859fda7c602 nginx:stable-alpine "nginx -g 'daemon of…" 3 seconds ago Up 2 seconds 80/tcp n1 [root@node1 ~]# docker container inspect -f {{.NetworkSettings.Networks.bridge.IPAddress}} n1 172.17.0.2 [root@node1 ~]# curl http://172.17.0.2 <!DOCTYPE html> <html> <head> <title>Welcome to nginx!</title> <style> body { 35em; margin: 0 auto; font-family: Tahoma, Verdana, Arial, sans-serif; } </style> </head> <body> <h1>Welcome to nginx!</h1> <p>If you see this page, the nginx web server is successfully installed and working. Further configuration is required.</p> <p>For online documentation and support please refer to <a href="http://nginx.org/">nginx.org</a>.<br/> Commercial support is available at <a href="http://nginx.com/">nginx.com</a>.</p> <p><em>Thank you for using nginx.</em></p> </body> </html> [root@node1 ~]# elinks -dump http://172.17.0.2 Welcome to nginx! If you see this page, the nginx web server is successfully installed and working. Further configuration is required. For online documentation and support please refer to [1]nginx.org. Commercial support is available at [2]nginx.com. Thank you for using nginx. References Visible links 1. http://nginx.org/ 2. http://nginx.com/ [root@node1 ~]# docker logs n1 172.17.0.1 - - [29/May/2020:16:20:15 +0000] "GET / HTTP/1.1" 200 612 "-" "curl/7.29.0" "-" 172.17.0.1 - - [29/May/2020:16:20:27 +0000] "GET / HTTP/1.1" 200 612 "-" "ELinks/0.12pre6 (textmode; Linux; -)" "-" [root@node1 ~]# docker container logs n1 172.17.0.1 - - [29/May/2020:16:20:15 +0000] "GET / HTTP/1.1" 200 612 "-" "curl/7.29.0" "-" 172.17.0.1 - - [29/May/2020:16:20:27 +0000] "GET / HTTP/1.1" 200 612 "-" "ELinks/0.12pre6 (textmode; Linux; -)" "-" [root@node1 ~]#
提示:docker container logs等同docker logs命令,用于查看指定容器的日志信息;跟踪某个容器的日志信息可以使用-f选项;该选项同tail -f选项一样;
删除容器
[root@node1 ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES d6bd7c0a29bb nginx:stable-alpine "nginx -g 'daemon of…" 3 seconds ago Up 3 seconds 80/tcp n1 [root@node1 ~]# docker container rm n1 Error response from daemon: You cannot remove a running container d6bd7c0a29bb4ea4cb60266727bd534fa086c8a70831dfea00791034bb2a84c4. Stop the container before attempting removal or force remove [root@node1 ~]# docker container rm -f n1 n1 [root@node1 ~]#
提示:删除容器必须得是停止状态的容器才可正常删除;如果删除正在运行的容器需要用到-f选项;docker rm 等同docker container rm 用于删除容器;
附加到现运行的容器终端
提示:容器运行服务和传统系统上运行服务端方式有点不同,传统运行服务都是把服务运行到后台,而容器运行服务通常是把服务运行为前台;所以我们进入到容器内部终端是可以直接看到nginx访问日志的输出的;docker attach命令等同docker container attach;
通过上面对镜像和容器的一些基本操作,总结为一点docker客户端命令就是对docker的资源对象做增删查改操作,每个资源对象后面都有一些子命令支持对该资源的操作;我们可以使用docker +资源对象+--help 来查看该资源对象支持那些操作,每个子命令的用法可以使用docker + 资源对象 + 子命令 + --help来查看具体资源对象子命令的操作命令用法和选项的说明;有关网络和卷的操作我这里就不多去演示;有兴趣的朋友可以试试docker + 资源对象 + --help去了解每个资源对象的用法吧;