docker基础手册

docker常用命令

1.查看当前镜像

[root@centos7-docker ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE nginx latest 08b152afcfae 3 weeks ago 133MB hello-world latest d1165f221234 5 months ago 13.3kB centos latest 300e315adb2f 8 months ago 209MB 

2.搜索镜像

语法:docker search [options] term

options说明:

  • –no-trunc:显示完整的镜像描述
  • -f <过滤条件>:列出收藏数不小于指定值的镜像
[root@centos7-docker ~]# docker search tomcat NAME DESCRIPTION STARS OFFICIAL AUTOMATED tomcat Apache Tomcat is an open source implementati… 3093 [OK] tomee Apache TomEE is an all-Apache Java EE certif… 91 [OK] dordoka/tomcat Ubuntu 14.04, Oracle JDK 8 and Tomcat 8 base… 57 [OK] kubeguide/tomcat-app Tomcat image for Chapter 1 30 consol/tomcat-7.0 Tomcat 7.0.57, 8080, "admin/admin" 18 [OK] cloudesire/tomcat Tomcat server, 6/7/8 15 [OK] aallam/tomcat-mysql Debian, Oracle JDK, Tomcat & MySQL 13 [OK] #第一列:仓库源名称 #第二列:描述 #第三列:星星 #第四列:是否是官方发布 #第五列:是否是自动构建 #查看星星50个,描述详细的tomcat [root@centos7-docker ~]# docker search -f stars=50 --no-trunc tomcat NAME DESCRIPTION STARS OFFICIAL AUTOMATED tomcat Apache Tomcat is an open source implementation of the Java Servlet and JavaServer Pages technologies 3093 [OK] tomee Apache TomEE is an all-Apache Java EE certified stack where Apache Tomcat is top dog. 91 [OK] dordoka/tomcat Ubuntu 14.04, Oracle JDK 8 and Tomcat 8 based docker container. 57 [OK] 

3.拉取镜像

语法:docker pull name:tag

例如:docker pull nginx #不加tag,默认拉取最新的

4.创建镜像之docker commit

当我们拉取下来的镜像不能满足需求的时候,想自己设计一个,两种方法

  • 从已经创建的容器中更新镜像,并且提交这个镜像
  • 使用dockerfile指令自个创建

查看当前系统镜像

[root@centos7-docker ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE zhanghaodong/ubuntu v1 f990b7b8a364 7 minutes ago 63.1MB ubuntu 18.04 39a8cfeef173 2 weeks ago 63.1MB nginx latest 08b152afcfae 3 weeks ago 133MB hello-world latest d1165f221234 5 months ago 13.3kB centos latest 300e315adb2f 8 months ago 209MB 

在centos:latest里安装httpd软件

[root@centos7-docker ~]# docker run -it centos /bin/bash [root@c7bb5f3a8293 /]# yum -y install httpd [root@c7bb5f3a8293 /]# rpm -qa | grep -i httpd httpd-tools-2.4.37-39.module_el8.4.0+778+c970deab.x86_64 centos-logos-httpd-85.8-1.el8.noarch httpd-filesystem-2.4.37-39.module_el8.4.0+778+c970deab.noarch httpd-2.4.37-39.module_el8.4.0+778+c970deab.x86_64 [root@c7bb5f3a8293 /]# exit exit 

这个时候退出了,如果再重新进的话,还是原来新的镜像,我需要安装httpd之后的镜像
查看刚才运行centos后的容器ID,进行提交

#看到刚才运行过的centos容器的ID是:c7开头的那个 [root@centos7-docker ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES c7bb5f3a8293 centos "/bin/bash" 3 minutes ago Exited (0) About a minute ago youthful_fermi #进行提交,这里报错了,仓库源首字母不能是大写的 [root@centos7-docker ~]# docker commit -m "install httpd soft" -a "Hejing" c7 Hejing/centos:v1 invalid reference format: repository name must be lowercase #修改后重新提交 [root@centos7-docker ~]# docker commit -m "install httpd soft" -a "Hejing" c7 hejing/centos:v1 sha256:0e4de65ea5c91925bd262ccb1ae547ae59aab296dc79ac6da1970056132579d9 #查看本地镜像 [root@centos7-docker ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE hejing/centos v1 0e4de65ea5c9 4 seconds ago 259MB zhanghaodong/ubuntu v1 f990b7b8a364 27 minutes ago 63.1MB ubuntu 18.04 39a8cfeef173 2 weeks ago 63.1MB nginx latest 08b152afcfae 3 weeks ago 133MB hello-world latest d1165f221234 5 months ago 13.3kB centos latest 300e315adb2f 8 months ago 209MB #进入安装httpd之后的镜像看一下,no problem [root@centos7-docker ~]# docker run -it hejing/centos:v1 /bin/bash [root@497b09e5d905 /]# rpm -qa | grep -i httpd httpd-tools-2.4.37-39.module_el8.4.0+778+c970deab.x86_64 centos-logos-httpd-85.8-1.el8.noarch httpd-filesystem-2.4.37-39.module_el8.4.0+778+c970deab.noarch httpd-2.4.37-39.module_el8.4.0+778+c970deab.x86_64 

5.构建镜像-Dockerfile指令介绍

dockerfile指令介绍

  • Docker通过对于在Dockerfile中的一系列指令的顺序解析实现自动的image的构建
    • 通过使用build指令,根据Dockerfile的描述来构建镜像
    • 通过源代码路径的方式
    • 通过标准输入流的方式

5.1 通过源代码路径

  • Dockerfile需要放置在项目的根目录的位置

  • 在构建的时候,Dockerfile client会把整个context打包发送到Docker server端,然后由server端负责build镜像,在构建成功后,会删除context目录

    docker build -t {镜像名字} {项目路径可以是相对路径}

    docker利用Dockerfile来构建新镜像之前,先介绍一下Dockerfile常用的指令

5.2 通过标准输入流

  • 通过标准输入流的方式获取Dockerfile的内容
  • client不会打包上传context目录,因此对于一些ADD、COPY、等涉及host本地文件复制的操作不支持

docker build -t {镜像名字} - < Dockerfile路径

5.3 build cache

  • Dcokerfile中的每一个指令执行完毕之后,都会提交一个image,这样保证指令之间不会由影响
  • Dockerfile会尽可能尝试重用已经构建的镜像
  • 可以通过在build命令中增加–no-cache的方式来禁用这个cache

5.4 Dockerfile指令:

  • 只支持Docker自己定义的一套指令,不支持自定义
  • 大小写不敏感,但是建议全部使用大写
  • 根据Dockerfile的内容顺序执行

5.5 FROM

  • FROM {base镜像}
  • 必须放在Dockerfile的第一行,表示从哪个base image开始构建

5.6 MAINTAINER

  • 可选的,标识作者

5.7 RUN

  • 每一个RUN指令都会是在一个新的container里面运行,并提交一个image作为下一个RUN的base image
  • 一个Dockerfile中可以包含多个RUN,按定义顺序执行
  • RUN支持两种运行方式:
    • RUN 这个会当作/bin/sh -c “cmd” 运行
    • RUN [“executable”,“arg1”,…],docker把他当作json的顺序来解析,因此必须使用双引号,而且executable需要是完整路径
  • RUN都是启动一个容器、执行命名、然后提交存储层文件变更。第一层RUN commond1的执行仅仅是当前进程,一个内存上的变化而已,其结果不会造成任何文件。而到第二层的时候,启动的是一个全新的容器,跟第一层的容器完全没关系,自然不可能继承前一层构建过程中的内存变化。而如果需要将两条命令或者多条命令联合起来执行需要加上&&。

5.8 CMD

  • CMD的作用是作为执行container时候的默认行为(容器默认启动命令)
  • 当运行container的时候声明了command,则不再用image中的CMD默认所定义的命令
  • 一个Dockerfile中只能有一个有效的CMD,当定义多个CMD的时候,只有最后一个才会有作用
  • CMD定义的三种方式:
    • CMD #这个会当作/bin/sh -c “cmd”来执行
    • CMD [“executable”,“arg1”,…]
    • CMD [“arg1”,“arg2”],这个时候CMD作为ENTRYPOINT的参数

5.9 EXPOSE声明端口

格式为:EXPOSE <端口1> [<端口2>...]

EXPOSE指令是声明运行时容器提供服务端口,这只是一个声明,在运行时并不会因为这个声明,应用就会开启这个端口的服务。在Dockerfile中写入这样的声明有两个好处:

  • 其一:帮助镜像使用者理解这个镜像服务的守护端口,以方便配置映射
  • 其二:在运行使用随机端口映射时,也就是docker run -P的时候,会自动映射EXPOSE的端口

5.10 ENTRYPOINT定义方式

  • entrypoint [“executable”,“arg1”,“arg2”],在这种定义方式下,CMD可以通过json方式来定义entrypoint的参数,可以通过在运行container的时候通过指定command的方式传递参数
  • entrypoint ,当作/bin/bash -c “cmd”运行命令

5.11 ADD & COPY

  • 当在源代码构建的方式下,可以通过ADD和COPY的方式,把host上的文件或者目录复制到image中
  • ADD和COPY的源必须在context路径下
  • 当src为网络URL的情况下,ADD指令可以把它下载到dest的指定位置,这个在任何build方式下都可以work
  • ADD相对COPY还有一个多的功能,能够进行自动解压压缩包

5.12 ENV

ENV key value

  • 用来设置环境变量,后续的RUN可以使用他创建的环境变量
  • 当创建基于该镜像的container的时候,会自动拥有设置的环境变量

5.13 WORKDIR

  • 用来指定当前工作目录(或者称为当前目录)
  • 当使用相对目录的情况下,采用上一个WORKDIR指定的目录作为基准

5.14 USER

  • 指定UID或者username,来决定运行RUN指令的用户

5.15 ONBUILD

  • ONBUILD作为一个trigger的标记,可以用来trigger任何Dockerfile中的指令
  • 可以定义多个ONBUILD指令
  • 当下一个镜像B使用镜像A作为base的时候,在FROM A指令前,会先按照顺序执行在构建A时候定义的ONBUILD指令

ONBUILD <DOCKERFILE 指令> <content>

5.16 VOLUME

  • 用来创建一个在image之外的mount point,用来在多个container之间实现数据共享
  • 运行使用json array的方式定义多个volume
  • VOLUME [“/var/data1″,”/var/data2”]
  • 或者plain text的情况下定义多个VOLUME指令

6.Dockefile实践

指令实践,let's do it

先看下本地image,选一个作为base image:

[root@centos7-docker ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE hejing/centos v1 0e4de65ea5c9 21 hours ago 259MB zhanghaodong/ubuntu v1 f990b7b8a364 21 hours ago 63.1MB ubuntu 18.04 39a8cfeef173 2 weeks ago 63.1MB nginx latest 08b152afcfae 3 weeks ago 133MB hejing/hello-world v1 d1165f221234 5 months ago 13.3kB hello-world latest d1165f221234 5 months ago 13.3kB centos latest 300e315adb2f 8 months ago 209MB centos 7 8652b9f0cb4c 9 months ago 204MB 

新建一个专门存放此demo的目录docker_demo,也就是Dockerfile所在的context:

[root@centos7-docker ~]# mkdir docker_demo [root@centos7-docker ~]# cd docker_demo/ [root@centos7-docker docker_demo]# touch Dockerfile [root@centos7-docker docker_demo]# pwd /root/docker_demo 

接下来编写Dockfile文件(D要大写,必须的)

这里以编译nginx提供web服务来构建新的image

1.下载ngixn源码包到docker_demo目录下:

下载地址:http://nginx.org/en/download.html

[root@centos7-docker docker_demo]# ll total 960 -rw-r--r--. 1 root root 0 Aug 13 23:10 Dockerfile -rw-r--r--. 1 root root 981687 Aug 13 23:14 nginx-1.12.2.tar.gz 

2.下面是编写好的Dockerfile v1版本:

[root@centos7-docker docker_demo]# cat Dockerfile # base image FROM centos # MAINTAINER MAINTAINER hejing # put nginx-1.12.2.tar.gz into /usr/local/src and unpack nginx # the ADD can unpack tar ADD nginx-1.12.2.tar.gz /usr/local/src # running required command RUN yum install -y gcc gcc-c++ glibc make autoconf openssl openssl-devel RUN yum install -y libxslt-devel gd gd-devel GeoIP GeoIP-devel pcre pcre-devel RUN useradd -M -s /sbin/nologin nginx # change dir to /usr/local/src/nginx-1.12.2 WORKDIR /usr/local/src/nginx-1.12.2 # execute command to complie nginx RUN ./configure --user=nginx --group=nginx --prefix=/usr/local/nginx --with-file-aio --with-http_ssl_module --with-http_realip_module --with-http_addition_module --with-http_xslt_module --with-http_image_filter_module --with-http_geoip_module --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_auth_request_module --with-http_random_index_module --with-http_secure_link_module --with-http_degradation_module --with-http_stub_status_module && make && make install EXPOSE 80 

3.查看docker_demo目录情况:

[root@centos7-docker docker_demo]# ll total 964 -rw-r--r--. 1 root root 1106 Aug 13 23:24 Dockerfile -rw-r--r--. 1 root root 981687 Aug 13 23:14 nginx-1.12.2.tar.gz 

4.执行docker build进行构建

命令:docker build -t centos_nginx:v1 .docker build -t centos_nginx:v1 /root/docker_demo

后面的.代表的是相对路径的当前目录,如果需要全路径就是上面的方式(就是找到Dockerfile文件)

[root@centos7-docker docker_demo]# docker build -t centos7_nginx:v1 . --no-cache Sending build context to Docker daemon 985.6kB Step 1/9 : FROM centos:7 ---> 8652b9f0cb4c Step 2/9 : MAINTAINER hejing ---> Running in e59af95193e1 Removing intermediate container e59af95193e1 ---> a5465d667dd3 Step 3/9 : ADD nginx-1.12.2.tar.gz /usr/local/src ---> 3ae678f3c162 Step 4/9 : RUN yum install -y gcc gcc-c++ glibc make autoconf openssl openssl-devel ---> Running in f0390567bfee ...... Step 5/9 : RUN yum install -y libxslt-devel gd gd-devel GeoIP GeoIP-devel pcre pcre-devel ---> Running in e1441bb1cd65 Step 6/9 : RUN useradd -M -s /sbin/nologin nginx ---> Running in d5151e31e9d6 Removing intermediate container d5151e31e9d6 ---> 8541c8342250 Step 7/9 : WORKDIR /usr/local/src/nginx-1.12.2 ---> Running in be68dcd91e6e Removing intermediate container be68dcd91e6e ---> 5b6f6148ab37 Step 8/9 : RUN ./configure --user=nginx....... Step 9/9 : EXPOSE 80 ---> Running in a9fb28cfabfa Removing intermediate container a9fb28cfabfa ---> 1ffc4a8e3106 Successfully built 1ffc4a8e3106 Successfully tagged centos7_nginx:v1 

构建成功,查看新构建的镜像:

[root@centos7-docker docker_demo]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE centos7_nginx v1 1ffc4a8e3106 8 minutes ago 673MB hejing/centos v1 0e4de65ea5c9 21 hours ago 259MB zhanghaodong/ubuntu v1 f990b7b8a364 21 hours ago 63.1MB ubuntu 18.04 39a8cfeef173 2 weeks ago 63.1MB nginx latest 08b152afcfae 3 weeks ago 133MB hejing/hello-world v1 d1165f221234 5 months ago 13.3kB hello-world latest d1165f221234 5 months ago 13.3kB centos latest 300e315adb2f 8 months ago 209MB centos 7 8652b9f0cb4c 9 months ago 204MB 

5.使用新构建的镜像启动一个container并开启nginx服务

[root@centos7-docker docker_demo]# docker run -d centos7_nginx:v1 /usr/local/nginx/sbin/nginx -g "daemon off"; 4d3a523292df47b59179470af76982583c4194de16c855c3907e055f1484b07d 

查看启动的container是否则正在运行:

[root@centos7-docker docker_demo]# docker ps -l CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 4d3a523292df centos7_nginx:v1 "/usr/local/nginx/sb…" 52 seconds ago Exited (1) 51 seconds ago trusting_swanson 

在这里启动刚才构建的镜像失败了,看了一下message日志,显示如下:

Aug 14 05:50:29 centos7-docker containerd: time="2021-08-14T05:50:29.471934866-04:00" level=info msg="shim disconnected" id=f194c83d48ecaefacf949f0bc401b3317e1dc90af34cc9d2fa4cc Aug 14 05:50:29 centos7-docker containerd: time="2021-08-14T05:50:29.472029803-04:00" level=error msg="copy shim log" error="reaf/fd/12: file already closed" Aug 14 05:50:29 centos7-docker kernel: docker0: port 1(veth8dd62fb) entered disabled state Aug 14 05:50:29 centos7-docker NetworkManager[5999]: <info> [1628934629.5061] manager: (veth201db61): new Veth device (/org/fretworkManager/Devices/180) Aug 14 05:50:29 centos7-docker kernel: docker0: port 1(veth8dd62fb) entered disabled state Aug 14 05:50:29 centos7-docker kernel: device veth8dd62fb left promiscuous mode Aug 14 05:50:29 centos7-docker kernel: docker0: port 1(veth8dd62fb) entered disabled state Aug 14 05:50:29 centos7-docker NetworkManager[5999]: <info> [1628934629.5259] device (veth8dd62fb): released from master device Aug 14 05:51:51 centos7-docker systemd: Stopping Docker Application C ontainer Engine... 

不晓得是怎么回事,看到了NetworManager,于是查看了一下host的网络服务,发现network服务和Neworkmanager都activing开启着,尝试停止了一下NetworkManager服务,再次重启镜像,发现成功了

这个问题可以详细的看一下这一篇博客http://www.cocoachina.com/articles/60742

虽然nginx服务开启了,但是port并没有进行映射到本机host,所以这个container并不能进行访问,重新启动一个进行映射端口的容器

[root@centos7-docker docker_demo]# docker run -d -p80:80 centos7_nginx:v1 /usr/local/nginx/sbin/nginx -g "daemon off;" [root@centos7-docker docker_demo]# docker ps -l CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 856bf5558811 centos7_nginx:v1 "/usr/local/nginx/sb…" 5 minutes ago Up 5 minutes 0.0.0.0:80->80/tcp, :::80->80/tcp jovial_heyrovsky 

再次查看端口信息(docker port container-ID即可):

[root@centos7-docker docker_demo]# docker port 85 80/tcp -> 0.0.0.0:80 80/tcp -> :::80 

浏览器访问:

docker基础手册_centos

6.于是基于Dockerfile的一个简单实例构建完成,现在基于这个Dockerfile文件依次添加其他的指令进行构建

6.1 添加ENV环境便令指令

[root@centos7-docker docker_demo]# cat Dockerfile # base image FROM centos:7 # # MAINTAINER MAINTAINER hejing # put nginx-1.12.2.tar.gz into /usr/local/src and unpack nginx # the ADD can unpack tar ADD nginx-1.12.2.tar.gz /usr/local/src # running required command RUN yum install -y gcc gcc-c++ glibc make autoconf openssl openssl-devel RUN yum install -y libxslt-devel gd gd-devel GeoIP GeoIP-devel pcre pcre-devel RUN useradd -M -s /sbin/nologin nginx # change dir to /usr/local/src/nginx-1.12.2 WORKDIR /usr/local/src/nginx-1.12.2 # execute command to complie nginx RUN ./configure --user=nginx --group=nginx --prefix=/usr/local/nginx --with-file-aio --with-http_ssl_module --with-http_realip_module --with-http_addition_module --with-http_xslt_module --with-http_image_filter_module --with-http_geoip_module --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_auth_request_module --with-http_random_index_module --with-http_secure_link_module --with-http_degradation_module --with-http_stub_status_module && make && make install ENV PATH /usr/local/nginx/sbin:$PATH EXPOSE 80 

然后进行构建:

[root@centos7-docker docker_demo]# docker build -t centos7_nginx:v2 . Sending build context to Docker daemon 985.6kB Step 1/10 : FROM centos:7 ---> 8652b9f0cb4c Step 2/10 : MAINTAINER hejing ---> Using cache ---> a5465d667dd3 Step 3/10 : ADD nginx-1.12.2.tar.gz /usr/local/src ---> Using cache ---> 3ae678f3c162 Step 4/10 : RUN yum install -y gcc gcc-c++ glibc make autoconf openssl openssl-devel ---> Using cache ---> b49fa8555f64 Step 5/10 : RUN yum install -y libxslt-devel gd gd-devel GeoIP GeoIP-devel pcre pcre-devel ---> Using cache ---> a81a9003b1bb Step 6/10 : RUN useradd -M -s /sbin/nologin nginx ---> Using cache ---> 8541c8342250 Step 7/10 : WORKDIR /usr/local/src/nginx-1.12.2 ---> Using cache ---> 5b6f6148ab37 Step 8/10 : RUN ./configure --user=nginx --group=nginx --prefix=/usr/local/nginx --with-file-aio --with-http_ssl_module --with-http_realip_module --with-http_addition_module --with-http_xslt_module --with-http_image_filter_module --with-http_geoip_module --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_auth_request_module --with-http_random_index_module --with-http_secure_link_module --with-http_degradation_module --with-http_stub_status_module && make && make install ---> Using cache ---> adc42b044d17 Step 9/10 : ENV PATH /usr/local/nginx/sbin:$PATH ---> Running in 5cba8cea3ff0 Removing intermediate container 5cba8cea3ff0 ---> 37426b9520d6 Step 10/10 : EXPOSE 80 ---> Running in 300ae44ca128 Removing intermediate container 300ae44ca128 ---> fad261761053 Successfully built fad261761053 Successfully tagged centos7_nginx:v2 

docker有个神奇的功能,上文中说到,docker会尽量以重构的镜像来构建新镜像。即Dockerfile会尽可能尝试重用已经构建的镜像,所以在构建v2的时候很快哦

docker在构建的过程中会采用缓存机制,在构建v2的时候就包含了很多using cache,所以构建的很快,如果你想重新构建的话,不想使用cache的话,那就在构建的时候加一个--no-cache

--no-cache Do not use cache when building the image

查看v2版本的镜像

[root@centos7-docker ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE centos7_nginx v2 fad261761053 9 minutes ago 673MB centos7_nginx v1 1ffc4a8e3106 59 minutes ago 673MB hejing/centos v1 0e4de65ea5c9 22 hours ago 259MB zhanghaodong/ubuntu v1 f990b7b8a364 22 hours ago 63.1MB ubuntu 18.04 39a8cfeef173 2 weeks ago 63.1MB nginx latest 08b152afcfae 3 weeks ago 133MB hello-world latest d1165f221234 5 months ago 13.3kB hejing/hello-world v1 d1165f221234 5 months ago 13.3kB centos latest 300e315adb2f 8 months ago 209MB centos 7 8652b9f0cb4c 9 months ago 204MB 

下面使用v2版本启动一个container

[root@centos7-docker ~]# docker run -d -p81:80 centos7_nginx:v2 nginx -g "daemon off;" 2c8d00cb0b3ca5808593bd5392cdfffbcbe7647a55a2cb77429ba29bfc8735bb [root@centos7-docker ~]# docker port 2c 80/tcp -> 0.0.0.0:81 80/tcp -> :::81 [root@centos7-docker ~]# docker ps -l CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 2c8d00cb0b3c centos7_nginx:v2 "nginx -g 'daemon of…" 30 seconds ago Up 29 seconds 0.0.0.0:81->80/tcp, :::81->80/tcp sharp_dewdney 

浏览器访问:

docker基础手册_docker_02

上述使用的命令是:

docker run -d -p81:80 centos7_nginx:v2 nginx -g "daemon off;"

没有用/usr/local/nginx/sbin/nginx来启动,是因为我们在Dockerfile文件中执行了ENV啊,添加了环境变量而已

ENV PATH /usr/local/nginx/sbin:$PATH

6.2 添加指令CMD

[root@centos7-docker docker_demo]# cat Dockerfile # base image FROM centos:7 # # MAINTAINER MAINTAINER hejing # put nginx-1.12.2.tar.gz into /usr/local/src and unpack nginx # the ADD can unpack tar ADD nginx-1.12.2.tar.gz /usr/local/src # running required command RUN yum install -y gcc gcc-c++ glibc make autoconf openssl openssl-devel RUN yum install -y libxslt-devel gd gd-devel GeoIP GeoIP-devel pcre pcre-devel RUN useradd -M -s /sbin/nologin nginx # change dir to /usr/local/src/nginx-1.12.2 WORKDIR /usr/local/src/nginx-1.12.2 # execute command to complie nginx RUN ./configure --user=nginx --group=nginx --prefix=/usr/local/nginx --with-file-aio --with-http_ssl_module --with-http_realip_module --with-http_addition_module --with-http_xslt_module --with-http_image_filter_module --with-http_geoip_module --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_auth_request_module --with-http_random_index_module --with-http_secure_link_module --with-http_degradation_module --with-http_stub_status_module && make && make install ENV PATH /usr/local/nginx/sbin:$PATH EXPOSE 80 CMD /bin/sh -c 'nginx -g "daemon off;"' 

然后进行构建:

[root@centos7-docker docker_demo]# docker build -t centos7_nginx:v3 . 

查看构建的镜像:

[root@centos7-docker docker_demo]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE centos7_nginx v3 034fca0d39d2 34 seconds ago 673MB centos7_nginx v2 fad261761053 25 minutes ago 673MB centos7_nginx v1 1ffc4a8e3106 About an hour ago 673MB 

基于v3镜像启动container:

[root@centos7-docker docker_demo]# docker run -d -p82:80 centos7_nginx:v3 95daa1ad57d7c26961e9d3a1804c836c46e52a68eb3923107fd6af0e756acb3d [root@centos7-docker docker_demo]# docker ps -l CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 95daa1ad57d7 centos7_nginx:v3 "/bin/sh -c '/bin/sh…" 7 seconds ago Up 6 seconds 0.0.0.0:82->80/tcp, :::82->80/tcp sharp_keller [root@centos7-docker docker_demo]# docker port 95 80/tcp -> 0.0.0.0:82 80/tcp -> :::82 

浏览器访问:

docker基础手册_centos_03

新增加的CMD /bin/sh -c 'nginx -g "daemon off;"'表示:

当启动一个container时默认运行的命令,如果在启动container时赋予了command的话,那么定义的CMD中的命令将不会被执行,而是会执行command的命令

6.3 添加ENTRYPOINT指令:

[root@centos7-docker docker_demo]# cat Dockerfile # base image FROM centos:7 # # MAINTAINER MAINTAINER hejing # put nginx-1.12.2.tar.gz into /usr/local/src and unpack nginx # the ADD can unpack tar ADD nginx-1.12.2.tar.gz /usr/local/src # running required command RUN yum install -y gcc gcc-c++ glibc make autoconf openssl openssl-devel RUN yum install -y libxslt-devel gd gd-devel GeoIP GeoIP-devel pcre pcre-devel RUN useradd -M -s /sbin/nologin nginx # change dir to /usr/local/src/nginx-1.12.2 WORKDIR /usr/local/src/nginx-1.12.2 # execute command to complie nginx RUN ./configure --user=nginx --group=nginx --prefix=/usr/local/nginx --with-file-aio --with-http_ssl_module --with-http_realip_module --with-http_addition_module --with-http_xslt_module --with-http_image_filter_module --with-http_geoip_module --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_auth_request_module --with-http_random_index_module --with-http_secure_link_module --with-http_degradation_module --with-http_stub_status_module && make && make install ENV PATH /usr/local/nginx/sbin:$PATH EXPOSE 80 ENTRYPOINT ["nginx"] CMD ["-g","daemon off;"] 

当ENTRYPOINT和CMD连用时,CMD的命令是ENTRYPOINT命令的参数,两者连用相当于nginx -g “daemon off;”

而当一起连用的时候命令格式最好一致(这里选择的都是json格式的是成功的,如果是sh模式可以试一下)

开始构建v4版本镜像:

[root@centos7-docker docker_demo]# docker build -t centos7_nginx:v4 . 

查看v4版本的镜像:

[root@centos7-docker docker_demo]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE centos7_nginx v4 586517a9d3a0 40 seconds ago 673MB centos7_nginx v3 034fca0d39d2 16 minutes ago 673MB centos7_nginx v2 fad261761053 41 minutes ago 673MB centos7_nginx v1 1ffc4a8e3106 2 hours ago 673MB 

基于v4镜像启动一个container:

[root@centos7-docker docker_demo]# docker run -d -p83:80 centos7_nginx:v4 9e28be9c461f5c0c5489c6fa56612c4fed7bb3f4e77f439479765535b6b3578d [root@centos7-docker docker_demo]# docker ps -l CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 9e28be9c461f centos7_nginx:v4 "nginx -g 'daemon of…" 5 seconds ago Up 3 seconds 0.0.0.0:83->80/tcp, :::83->80/tcp laughing_buck [root@centos7-docker docker_demo]# docker port 9 Error response from daemon: Multiple IDs found with provided prefix: 9 [root@centos7-docker docker_demo]# docker port 9e 80/tcp -> 0.0.0.0:83 80/tcp -> :::83 

浏览器访问:
docker基础手册_docker_04

6.4 测试command和CMD运行是否冲突,以哪个为准

[root@centos7-docker docker_demo]# cat Dockerfile # base image FROM centos:7 # # MAINTAINER MAINTAINER hejing # put nginx-1.12.2.tar.gz into /usr/local/src and unpack nginx # the ADD can unpack tar ADD nginx-1.12.2.tar.gz /usr/local/src # running required command RUN yum install -y gcc gcc-c++ glibc make autoconf openssl openssl-devel RUN yum install -y libxslt-devel gd gd-devel GeoIP GeoIP-devel pcre pcre-devel RUN useradd -M -s /sbin/nologin nginx # change dir to /usr/local/src/nginx-1.12.2 WORKDIR /usr/local/src/nginx-1.12.2 # execute command to complie nginx RUN ./configure --user=nginx --group=nginx --prefix=/usr/local/nginx --with-file-aio --with-http_ssl_module --with-http_realip_module --with-http_addition_module --with-http_xslt_module --with-http_image_filter_module --with-http_geoip_module --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_auth_request_module --with-http_random_index_module --with-http_secure_link_module --with-http_degradation_module --with-http_stub_status_module && make && make install ENV PATH /usr/local/nginx/sbin:$PATH EXPOSE 80 ENTRYPOINT ["nginx"] CMD ["-g","daemon on;"] 

CMD的命令修改为了后台,我们知道如果容器内的进程在后台运行那么容器将不会运行

现在构建v5版本镜像:

[root@centos7-docker docker_demo]# docker build -t centos7_nginx:v5 . 

查看v5版本镜像:

[root@centos7-docker docker_demo]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE centos7_nginx v5 00e0e71aa40f 42 seconds ago 673MB centos7_nginx v4 586517a9d3a0 8 minutes ago 673MB centos7_nginx v3 034fca0d39d2 24 minutes ago 673MB centos7_nginx v2 fad261761053 49 minutes ago 673MB centos7_nginx v1 1ffc4a8e3106 2 hours ago 673MB 

现在直接利用v5启动container,不加什么其他命令的话,查看container是否能够运行:

[root@centos7-docker docker_demo]# docker run -d -p85:80 centos7_nginx:v5 1e20de8048e2bfe43942cc908f4bfafae1005cd55fede6176b126f83d428fc9f [root@centos7-docker docker_demo]# docker ps -l CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 1e20de8048e2 centos7_nginx:v5 "nginx -g 'daemon on…" 4 seconds ago Exited (0) 4 seconds ago friendly_leakey 

可以看到容器exit了

下面这样启动container:

[root@centos7-docker docker_demo]# docker run -d -p85:80 centos7_nginx:v5 -g "daemon off;" 354bb397fb44792c52496e9ce25d189cc2f5177e5c86964043fccbe532a894c3 [root@centos7-docker docker_demo]# docker ps -l CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 354bb397fb44 centos7_nginx:v5 "nginx -g 'daemon of…" 5 seconds ago Up 4 seconds 0.0.0.0:85->80/tcp, :::85->80/tcp focused_turing [root@centos7-docker docker_demo]# docker port 354 80/tcp -> 0.0.0.0:85 80/tcp -> :::85 

可以看到在后面添加了command之-g "daemon off;",容器的启动丝毫没有受影响。

上问说过如果新加了command命令后,Dockfile中的CMD命令将不会生效,就是这样

浏览器访问:

docker基础手册_centos_05

6.5 添加VOLUME指令

[root@centos7-docker docker_demo]# cat Dockerfile # base image FROM centos:7 # # MAINTAINER MAINTAINER hejing # put nginx-1.12.2.tar.gz into /usr/local/src and unpack nginx # the ADD can unpack tar ADD nginx-1.12.2.tar.gz /usr/local/src # running required command RUN yum install -y gcc gcc-c++ glibc make autoconf openssl openssl-devel RUN yum install -y libxslt-devel gd gd-devel GeoIP GeoIP-devel pcre pcre-devel RUN useradd -M -s /sbin/nologin nginx # mount a dit to container VOLUME ["/data"] # change dir to /usr/local/src/nginx-1.12.2 WORKDIR /usr/local/src/nginx-1.12.2 # execute command to complie nginx RUN ./configure --user=nginx --group=nginx --prefix=/usr/local/nginx --with-file-aio --with-http_ssl_module --with-http_realip_module --with-http_addition_module --with-http_xslt_module --with-http_image_filter_module --with-http_geoip_module --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_auth_request_module --with-http_random_index_module --with-http_secure_link_module --with-http_degradation_module --with-http_stub_status_module && make && make install ENV PATH /usr/local/nginx/sbin:$PATH EXPOSE 80 ENTRYPOINT ["nginx"] CMD ["-g"] 

开始构建v6:

[root@centos7-docker docker_demo]# docker build -t centos7_nginx:v6 . 

查看v6版本镜像:

[root@centos7-docker docker_demo]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE centos7_nginx v6 68954acd27d5 39 seconds ago 673MB centos7_nginx v5 00e0e71aa40f 11 minutes ago 673MB centos7_nginx v4 586517a9d3a0 19 minutes ago 673MB centos7_nginx v3 034fca0d39d2 35 minutes ago 673MB centos7_nginx v2 fad261761053 About an hour ago 673MB centos7_nginx v1 1ffc4a8e3106 2 hours ago 673MB 

基于v6启动container:

[root@centos7-docker docker_demo]# docker run -d -p86:80 --name=nginx6 centos7_nginx:v6 -g "daemon off;" 7d587fb786ab27c93d8790951410b361445b5de78d4999eb4e2a33b55f33a232 [root@centos7-docker docker_demo]# docker ps -l CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 7d587fb786ab centos7_nginx:v6 "nginx -g 'daemon of…" 6 seconds ago Up 5 seconds 0.0.0.0:86->80/tcp, :::86->80/tcp nginx6 [root@centos7-docker docker_demo]# docker port 7d5 80/tcp -> 0.0.0.0:86 80/tcp -> :::86 

利用docker exec进入到container中,查看是否存在卷/data:

[root@centos7-docker docker_demo]# docker exec -it 7d5 /bin/bash [root@7d587fb786ab /]# ls -ld data/ drwxr-xr-x. 2 root root 6 Aug 14 11:06 data/ 

这个data目录挂载的目录对应本机host的这个目录

[root@centos7-docker _data]# pwd /var/lib/docker/volumes/2aee288aa9c7545e0f6e3ccbcb11a52217a33d0b56807ee40d301534ece62067/_data 

现在在本机host上的目录创建一个文件:

[root@centos7-docker _data]# pwd /var/lib/docker/volumes/2aee288aa9c7545e0f6e3ccbcb11a52217a33d0b56807ee40d301534ece62067/_data [root@centos7-docker _data]# touch 8888 

然后到container的data下查看是否有8888

[root@7d587fb786ab data]# ls 8888 

6.6 添加ONBUILD指令:

  • Dockerfile1中base image为A镜像,并在Dcokerfile1中定义ONBUILD指令,构建成镜像B镜像

  • Dockerfile2中base image为B镜像,构建成镜像C

  • 当使用镜像B启动container1不会执行ONBUILD中定义的内容,而使用C镜像启动container2则会执行ONBUILD定义的内容

  • dockerfile1中:A镜像-centos:7为base image,里面有OBNBUILD指令——形成B-v7版本

  • dockerfile2中:以B镜像v7为base image,构建镜像c-v8

  • 当使用v7启动container,不会执行ONBUILD

  • 当使用v8启动container,会执行ONBUILD

  • centos:7-----构建centos7_nginx:v7------构建centos7_nginx:v8

A: centos:7到 B: centos7_nginx:v7到 C: centos7_nginx:v8
Dockerfile中有ONBUILD指令 以此image启动的container不执行ONBUILD内容 以此image启动的container会执行ONBUILD内容
[root@centos7-docker docker_demo]# cat Dockerfile # base image FROM centos:7 # # MAINTAINER MAINTAINER hejing # put nginx-1.12.2.tar.gz into /usr/local/src and unpack nginx # the ADD can unpack tar ADD nginx-1.12.2.tar.gz /usr/local/src # running required command RUN yum install -y gcc gcc-c++ glibc make autoconf openssl openssl-devel RUN yum install -y libxslt-devel gd gd-devel GeoIP GeoIP-devel pcre pcre-devel RUN useradd -M -s /sbin/nologin nginx # mount a dit to container ONBUILD VOLUME ["/data"] # change dir to /usr/local/src/nginx-1.12.2 WORKDIR /usr/local/src/nginx-1.12.2 # execute command to complie nginx RUN ./configure --user=nginx --group=nginx --prefix=/usr/local/nginx --with-file-aio --with-http_ssl_module --with-http_realip_module --with-http_addition_module --with-http_xslt_module --with-http_image_filter_module --with-http_geoip_module --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_auth_request_module --with-http_random_index_module --with-http_secure_link_module --with-http_degradation_module --with-http_stub_status_module && make && make install ENV PATH /usr/local/nginx/sbin:$PATH EXPOSE 80 ENTRYPOINT ["nginx"] CMD ["-g"] 

使用上面的Dockfile构建镜像v7

docker build -t centos7_nginx:v7 . 

查看images

[root@centos7-docker docker_demo]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE centos7_nginx v7 003b917060be 38 seconds ago 673MB centos7_nginx v6 68954acd27d5 19 hours ago 673MB centos7_nginx v5 00e0e71aa40f 19 hours ago 673MB centos7_nginx v4 586517a9d3a0 19 hours ago 673MB centos7_nginx v3 034fca0d39d2 20 hours ago 673MB centos7_nginx v2 fad261761053 20 hours ago 673MB centos7_nginx v1 1ffc4a8e3106 21 hours ago 673MB 

运行v7版本镜像

[root@centos7-docker docker_demo]# docker run -d -p87:80 --name=nginx7 centos7_nginx:v7 -g "daemon off;" 99940fb7029d9198048f2edf2fa79f094b2e86482c9cb185ffaf377e625cdace [root@centos7-docker docker_demo]# docker ps -l CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 99940fb7029d centos7_nginx:v7 "nginx -g 'daemon of…" 20 seconds ago Up 19 seconds 0.0.0.0:87->80/tcp, :::87->80/tcp nginx7 [root@centos7-docker docker_demo]# docker port 999 80/tcp -> 0.0.0.0:87 80/tcp -> :::87 

现在进入到容器查看是否有/data

[root@centos7-docker docker_demo]# docker exec -it nginx7 /bin/bash [root@99940fb7029d nginx-1.12.2]# ll /data ls: cannot access /data: No such file or directory 

现在修改上面的Dockerfile1中的base imagem,并形成Dcokerfile2

[root@centos7-docker docker_demo]# cat Dockerfile # base image #FROM centos:7 FROM centos7_nginx:v7 ...... 

现在基于Dockerfile2,开始构建新镜像v8:

[root@centos7-docker docker_demo]# docker build -t centos7_nginx:v8 . 

查看v8版本镜像:

[root@centos7-docker docker_demo]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE centos7_nginx v8 d52a7cd5a1ed 10 seconds ago 976MB centos7_nginx v7 003b917060be 13 minutes ago 673MB centos7_nginx v6 68954acd27d5 19 hours ago 673MB centos7_nginx v5 00e0e71aa40f 20 hours ago 673MB centos7_nginx v4 586517a9d3a0 20 hours ago 673MB centos7_nginx v3 034fca0d39d2 20 hours ago 673MB centos7_nginx v2 fad261761053 20 hours ago 673MB centos7_nginx v1 1ffc4a8e3106 21 hours ago 673MB 

现在基于v8启动container:

[root@centos7-docker docker_demo]# docker run -d -p88:80 centos7_nginx:v8 -g "daemon off;" eeea3992637f39cb33532feb620f302ddd92c7c06528a5fea25a4fcee2fb2729 [root@centos7-docker docker_demo]# docker ps -l CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES eeea3992637f centos7_nginx:v8 "nginx -g 'daemon of…" 4 seconds ago Up 3 seconds 0.0.0.0:88->80/tcp, :::88->80/tcp sleepy_elbakyan [root@centos7-docker docker_demo]# docker port eee 80/tcp -> 0.0.0.0:88 80/tcp -> :::88 

进入container查看data是否存在:

[root@centos7-docker ~]# docker exec -it eee /bin/bash [root@eeea3992637f nginx-1.12.2]# ll /data total 0 

7.设置镜像标签

使用docker tag为镜像添加新的标签

查看本地镜像

[root@centos7-docker ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE hejing/centos v1 0e4de65ea5c9 17 minutes ago 259MB zhanghaodong/ubuntu v1 f990b7b8a364 44 minutes ago 63.1MB ubuntu 18.04 39a8cfeef173 2 weeks ago 63.1MB nginx latest 08b152afcfae 3 weeks ago 133MB hello-world latest d1165f221234 5 months ago 13.3kB centos latest 300e315adb2f 8 months ago 209MB 

下面给hello-world换一个标签

[root@centos7-docker ~]# docker tag d11 hejing/hello-world:v1 [root@centos7-docker ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE hejing/centos v1 0e4de65ea5c9 18 minutes ago 259MB zhanghaodong/ubuntu v1 f990b7b8a364 45 minutes ago 63.1MB ubuntu 18.04 39a8cfeef173 2 weeks ago 63.1MB nginx latest 08b152afcfae 3 weeks ago 133MB hejing/hello-world v1 d1165f221234 5 months ago 13.3kB hello-world latest d1165f221234 5 months ago 13.3kB centos latest 300e315adb2f 8 months ago 209MB #可以看到镜像ID为d11的有两个标签 

8.docker数据卷管理

docker数据卷备份和还原

8.1 利用镜像创建一个nginx服务容器,并挂载一个数据卷

[root@centos7-docker ~]# docker run -v /data -d -p80:80 --name=web1 centos7_nginx:v8 -g "daemon off;" f1be21798741dd02a3bf3073e4f35778829b9fd36726912db65271fae2c42bfc 

8.2 进入web1容器,向空的/data添加数据

[root@centos7-docker volumes]# docker exec -it f1 /bin/bash [root@f1be21798741 nginx-1.12.2]# cd /data/ [root@f1be21798741 data]# ls [root@f1be21798741 data]# mkdir hejing [root@f1be21798741 data]# vi hejing/web1.txt [root@f1be21798741 data]# cat hejing/web1.txt this is in web1 

8.3 数据卷备份

创建另外一个容器以上面web1容器作为共享数据卷,并将共享的数据卷进行tar压缩备份,并通过映射到本机host

命令解释:

  • –volumes-from web1:表示创建的容器基于web1容器内的数据卷(即两个容器共享数据卷),web1的数据卷为/data
  • tar cvf /backup/data.tar /data:创建的容器将/data进行压缩
  • -v /root/backup:/backup:将容器的/backup与本机的/root/backup进行映射

总:创建一个新容器数据卷基于共享web1容器的数据卷,将数据卷进行tar压缩到容器内某个目录,并映射到本机目录

[root@centos7-docker ~]# docker run -d --volumes-from web1 -v /root/backup:/backup centos:7 tar cvf /backup/data.tar /data faf11579491a0437a3a89b6ebd4ad87c4b4cc6c2702c57b10feb2baedf78aff2 [root@centos7-docker ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES f1be21798741 centos7_nginx:v8 "nginx -g 'daemon of…" 11 minutes ago Up 11 minutes 0.0.0.0:80->80/tcp, :::80->80/tcp web1 [root@centos7-docker ~]# docker ps -l CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES faf11579491a centos:7 "tar cvf /backup/dat…" 12 seconds ago Exited (0) 11 seconds ago amazing_chatelet [root@centos7-docker ~]# ls anaconda-ks.cfg backup docker_demo rpmbuild [root@centos7-docker ~]# ls backup/data.tar backup/data.tar [root@centos7-docker ~]# tar -xf backup/data.tar [root@centos7-docker ~]# ls anaconda-ks.cfg backup data docker_demo rpmbuild [root@centos7-docker ~]# cd data/hejing/ [root@centos7-docker hejing]# ls web1.txt [root@centos7-docker hejing]# cat web1.txt this is in web1 

9. docker镜像的导入导出

涉及到的命令:export、import、save、load

其中save和load针对的是image

export和import针对的是container

区别:容器的快照信息将丢弃历史记录和元数据信息(只保留容器当时的快照状态),而镜像的存储文件将保存完整记录。

9.1 docker save-镜像导出

格式:docker save [options] image [images]

[root@centos7-docker ~]# docker save --help Usage: docker save [OPTIONS] IMAGE [IMAGE...] Save one or more images to a tar archive (streamed to STDOUT by default) Options: -o, --output string Write to a file, instead of STDOUT 

-o和>表示输出到文件,xxx.tar是目标文件,centos7_nginx:v8是源镜像名,目标文件后跟image id也可以

#查看host本机镜像 [root@centos7-docker ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE centos7_nginx v8 d52a7cd5a1ed 2 hours ago 976MB centos7_nginx v7 003b917060be 2 hours ago 673MB centos7_nginx v6 68954acd27d5 21 hours ago 673MB centos7_nginx v5 00e0e71aa40f 21 hours ago 673MB centos7_nginx v4 586517a9d3a0 21 hours ago 673MB centos7_nginx v3 034fca0d39d2 22 hours ago 673MB centos7_nginx v2 fad261761053 22 hours ago 673MB centos7_nginx v1 1ffc4a8e3106 23 hours ago 673MB hejing/centos v1 0e4de65ea5c9 44 hours ago 259MB zhanghaodong/ubuntu v1 f990b7b8a364 44 hours ago 63.1MB ubuntu 18.04 39a8cfeef173 2 weeks ago 63.1MB nginx latest 08b152afcfae 3 weeks ago 133MB hejing/hello-world v1 d1165f221234 5 months ago 13.3kB hello-world latest d1165f221234 5 months ago 13.3kB centos latest 300e315adb2f 8 months ago 209MB centos 7 8652b9f0cb4c 9 months ago 204MB 方法1: [root@centos7-docker ~]# docker save -o centos7_nginx-v9.tar centos7_nginx:v8 [root@centos7-docker ~]# ls anaconda-ks.cfg backup centos7_nginx-v9.tar docker_demo rpmbuild 方法2: [root@centos7-docker ~]# docker save > centos7_nginx-v10.tar centos7_nginx:v8 [root@centos7-docker ~]# ls anaconda-ks.cfg backup centos7_nginx-v10.tar centos7_nginx-v9.tar docker_demo rpmbuild 方法3: [root@centos7-docker ~]# docker save > centos7_nginx-v11.tar d52 [root@centos7-docker ~]# ls anaconda-ks.cfg backup centos7_nginx-v10.tar centos7_nginx-v11.tar centos7_nginx-v9.tar docker_demo rpmbuild 

9.2 docker load-镜像导入

格式:docker load [options]

[root@centos7-docker ~]# docker load --help Usage: docker load [OPTIONS] Load an image from a tar archive or STDIN Options: -i, --input string Read from tar archive file, instead of STDIN -q, --quiet Suppress the load output 

其中-i和<表示从文件输入,会成功导入镜像及相关元数据,包括tag

[root@centos7-docker ~]# docker load -i centos7_nginx-v11.tar 或者 [root@centos7-docker ~]# docker load < centos7_nginx-v11.tar 58149cafb515: Loading layer [==================================================>] 6.121MB/6.121MB ce85612a8b78: Loading layer [==================================================>] 130.6MB/130.6MB 94a3447f3146: Loading layer [==================================================>] 130.5MB/130.5MB 929ffd789d18: Loading layer [==================================================>] 36.33MB/36.33MB Loaded image ID: sha256:d52a7cd5a1ed35bde142d453f36b0c542ecd130338f5518f142f458b6fd1159f #导入之后发现源仓库和tag都是<none> [root@centos7-docker ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE <none> <none> d52a7cd5a1ed 2 hours ago 976MB centos7_nginx v7 003b917060be 2 hours ago 673MB centos7_nginx v6 68954acd27d5 22 hours ago 673MB centos7_nginx v5 00e0e71aa40f 22 hours ago 673MB centos7_nginx v4 586517a9d3a0 22 hours ago 673MB centos7_nginx v3 034fca0d39d2 22 hours ago 673MB centos7_nginx v2 fad261761053 22 hours ago 673MB centos7_nginx v1 1ffc4a8e3106 23 hours ago 673MB #更改源仓库和tag [root@centos7-docker ~]# docker tag d52 centos7_nginx:v8 [root@centos7-docker ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE centos7_nginx v8 d52a7cd5a1ed 2 hours ago 976MB centos7_nginx v7 003b917060be 2 hours ago 673MB centos7_nginx v6 68954acd27d5 22 hours ago 673MB centos7_nginx v5 00e0e71aa40f 22 hours ago 673MB centos7_nginx v4 586517a9d3a0 22 hours ago 673MB centos7_nginx v3 034fca0d39d2 22 hours ago 673MB centos7_nginx v2 fad261761053 23 hours ago 673MB centos7_nginx v1 1ffc4a8e3106 23 hours ago 673MB 

9.3 docker export-容器导出

格式:docker export [options] CONTAINER

[root@centos7-docker ~]# docker export --help Usage: docker export [OPTIONS] CONTAINER Export a container's filesystem as a tar archive Options: -o, --output string Write to a file, instead of STDOUT 

docker export -o ubuntu-18.04.tar b75

其中-o表示输出到文件,ubuntu-18.04.tar为目标文件,b75是容器ID(写容器名也可以)

#查看运行的容器 [root@centos7-docker ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES b756c37a6552 ubuntu:18.04 "/bin/bash" About an hour ago Up 2 seconds nostalgic_mestorf [root@centos7-docker ~]# docker export -o ubuntu-18.04.tar b75 [root@centos7-docker ~]# ls backup centos7_nginx-v10.tar centos7_nginx-v11.tar centos7_nginx-v9.tar docker_demo rpmbuild ubuntu-18.04.tar 

9.4 docker import-容器导入

格式:docker import [OPTIONS] file|URL|- [REPOSITORY[:TAG]]

[root@centos7-docker ~]# docker import --help Usage: docker import [OPTIONS] file|URL|- [REPOSITORY[:TAG]] Import the contents from a tarball to create a filesystem image Options: -c, --change list Apply Dockerfile instruction to the created image -m, --message string Set commit message for imported image --platform string Set platform if server is multi-platform capable 

docker基础手册_centos_06

或者
docker基础手册_d3_07

9.5 总结区别

  • export命令导出的tar略小于save命令导出来的
  • export命令针对的是container,是从容器container中导出tar文件的,而save命令则是从镜像中导出的
  • 基于第二点,export导出的文件在import回去时,无法保留镜像所有历史(即每一层layer信息),不能进行回滚操作;而save是根据镜像而来的,所以导入导出的时候可以完成的保留每一层的信息

下图是save导出load导入的

docker基础手册_ubuntu_08

下图是export导出import导入的

docker基础手册_centos_09

9.6 建议

  • 若是只想备份image,用save和load
  • 若是在启动容器后,容器内容有变化,需要备份,则使用export和import

原文链接:https://blog.51cto.com/u_14442495/3650269

© 版权声明
THE END
喜欢就支持一下吧
点赞0 分享