06 - DockerFile 学习

    科技2024-11-01  17

    1.Dockerfile 简介  

       Dockerfile 是由一系列命令和参数构成的脚本,这些命令应用于操作系统(centos或者Ubuntu)基础镜像并最终创建的一个新镜像。

         我们前面通过手工方式,修改配置文件,或者添加、删除文件目录的方式,来构建一种新镜像;这种手工方式麻烦,容易出错,而且不能复用。

         这里的讲的Docker 用脚本方式来构建自动化,可复用的,高效率的创建镜像方式,是企业级开发的首选方式;

         再软件系统开发生命周期中,采用Dockerfile 来构建镜像:

        1、对于开发人员:可以为开发团队提供一个完全一致的开发环境;

        2、对于测试人员:可以直接拿开发时所构建的镜像或者通过Dockerfile文件构建一个新的镜像开始工作;

        3、对于运维人员:在部署时,可以实现应用的无缝移植。

    2.Dockerfile 常用指令

    FROM image_name:tag   定义使用哪个基础镜像启动构建流程:MAINTAINER user_info    声明镜像维护者信息 LABEL key value    镜像描述元信息(可以写多条)ENV key value  设置环境变量(可以写多条)RUN command 构建容器时需要运行的命令(可以写多条)WORKDIR path_dir  设置终端默认登录进来的工作目录EXPOSE port  当前容器对外暴露出的端口ADD source_dir/file dest_dir/file  将宿主机的文件复制到容器内,如果是一个压缩文件,将会在复制后自动解压COPY  source_dir/file dest_dir/file 和ADD相似,但是如果有压缩文件是不能解压VOLUME 创建一个可以从本机主机或者其他容器挂载的挂载点,一般用来存放数据库和需要保持的数据等CMD  指定容器启动时要运行的命令,假如有多个CMD,最后一个生效;ENTRYPOINT 指定容器启动时要运行的命令ONBUILD 当构建一个被继承的Dockerfile时运行的命令,父镜像在被子镜像继承后父镜像的onbuild被触发。可以把ONBUILD理解为一个触发器

    3.用Dockerfile构建自定义centos

    1、编写DockerFile

    文件名为:myCentosDockerFile 内容如下

    FROM centos MAINTAINER zuozhe<zuozhe@test.com> LABEL name="centos Image" build-date='20200907' ENV WORKPATH /home/ WORKDIR $WORKPATH RUN yum -y install net-tools RUN yum -y install vim EXPOSE 80 CMD /bin/bash

    2、构建 docker build -f DockFile文件名 -t 镜像名称:版本.

    docker build -f myCentosDockerFile -t tiankerui/mycentos:1.1 .

    [root@base210 dockerfile]# docker build -f myCentosDockerFile -t tiankeuri/mycentos:v1 . Sending build context to Docker daemon 2.048kB Step 1/9 : FROM centos latest: Pulling from library/centos 3c72a8ed6814: Pull complete Digest: sha256:76d24f3ba3317fa945743bb3746fbaf3a0b752f10b10376960de01da70685fbd Status: Downloaded newer image for centos:latest ---> 0d120b6ccaa8 Step 2/9 : MAINTAINER zuozhe<zuozhe@test.com> ---> Running in 5afa5cd39da8 Removing intermediate container 5afa5cd39da8 ---> 80f6ee0d6b0c Step 3/9 : LABEL name="centos Image" build-date='20200907' ---> Running in a4e565e476f0 Removing intermediate container a4e565e476f0 ---> 98a04a452949 Step 4/9 : ENV WORKPATH /home/ ---> Running in d1d5d48b9ef2 Removing intermediate container d1d5d48b9ef2 ---> 08916c745160 Step 5/9 : WORKDIR $WORKPATH ---> Running in 6d0fd1fa5dba Removing intermediate container 6d0fd1fa5dba ---> 5dc308ab5e74 Step 6/9 : RUN yum -y install net-tools ---> Running in 11c0cf867073 CentOS-8 - AppStream 2.7 MB/s | 5.8 MB 00:02 CentOS-8 - Base 2.0 MB/s | 2.2 MB 00:01 CentOS-8 - Extras 5.0 kB/s | 8.1 kB 00:01 Last metadata expiration check: 0:00:01 ago on Wed Oct 7 12:44:49 2020. Dependencies resolved. ================================================================================ Package Architecture Version Repository Size ================================================================================ Installing: net-tools x86_64 2.0-0.51.20160912git.el8 BaseOS 323 k Transaction Summary ================================================================================ Install 1 Package Total download size: 323 k Installed size: 1.0 M Downloading Packages: net-tools-2.0-0.51.20160912git.el8.x86_64.rpm 937 kB/s | 323 kB 00:00 -------------------------------------------------------------------------------- Total 362 kB/s | 323 kB 00:00 warning: /var/cache/dnf/BaseOS-f6a80ba95cf937f2/packages/net-tools-2.0-0.51.20160912git.el8.x86_64.rpm: Header V3 RSA/SHA256 Signature, key ID 8483c65d: NOKEY CentOS-8 - Base 1.6 MB/s | 1.6 kB 00:00 Importing GPG key 0x8483C65D: Userid : "CentOS (CentOS Official Signing Key) <security@centos.org>" Fingerprint: 99DB 70FA E1D7 CE22 7FB6 4882 05B5 55B3 8483 C65D From : /etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial Key imported successfully Running transaction check Transaction check succeeded. Running transaction test Transaction test succeeded. Running transaction Preparing : 1/1 Installing : net-tools-2.0-0.51.20160912git.el8.x86_64 1/1 Running scriptlet: net-tools-2.0-0.51.20160912git.el8.x86_64 1/1 Verifying : net-tools-2.0-0.51.20160912git.el8.x86_64 1/1 Installed: net-tools-2.0-0.51.20160912git.el8.x86_64 Complete! Removing intermediate container 11c0cf867073 ---> 358aa6df4073 Step 7/9 : RUN yum -y install vim ---> Running in 533cc1bef383 Last metadata expiration check: 0:00:04 ago on Wed Oct 7 12:44:49 2020. Dependencies resolved. ================================================================================ Package Arch Version Repository Size ================================================================================ Installing: vim-enhanced x86_64 2:8.0.1763-13.el8 AppStream 1.4 M Installing dependencies: gpm-libs x86_64 1.20.7-15.el8 AppStream 39 k vim-common x86_64 2:8.0.1763-13.el8 AppStream 6.3 M vim-filesystem noarch 2:8.0.1763-13.el8 AppStream 48 k which x86_64 2.21-12.el8 BaseOS 49 k Transaction Summary ================================================================================ Install 5 Packages Total download size: 7.8 M Installed size: 31 M Downloading Packages: (1/5): gpm-libs-1.20.7-15.el8.x86_64.rpm 221 kB/s | 39 kB 00:00 (2/5): vim-filesystem-8.0.1763-13.el8.noarch.rp 889 kB/s | 48 kB 00:00 (3/5): which-2.21-12.el8.x86_64.rpm 900 kB/s | 49 kB 00:00 (4/5): vim-enhanced-8.0.1763-13.el8.x86_64.rpm 2.6 MB/s | 1.4 MB 00:00 (5/5): vim-common-8.0.1763-13.el8.x86_64.rpm 8.6 MB/s | 6.3 MB 00:00 -------------------------------------------------------------------------------- Total 2.4 MB/s | 7.8 MB 00:03 Running transaction check Transaction check succeeded. Running transaction test Transaction test succeeded. Running transaction Preparing : 1/1 Installing : which-2.21-12.el8.x86_64 1/5 Installing : vim-filesystem-2:8.0.1763-13.el8.noarch 2/5 Installing : vim-common-2:8.0.1763-13.el8.x86_64 3/5 Installing : gpm-libs-1.20.7-15.el8.x86_64 4/5 Running scriptlet: gpm-libs-1.20.7-15.el8.x86_64 4/5 Installing : vim-enhanced-2:8.0.1763-13.el8.x86_64 5/5 Running scriptlet: vim-enhanced-2:8.0.1763-13.el8.x86_64 5/5 Running scriptlet: vim-common-2:8.0.1763-13.el8.x86_64 5/5 Verifying : gpm-libs-1.20.7-15.el8.x86_64 1/5 Verifying : vim-common-2:8.0.1763-13.el8.x86_64 2/5 Verifying : vim-enhanced-2:8.0.1763-13.el8.x86_64 3/5 Verifying : vim-filesystem-2:8.0.1763-13.el8.noarch 4/5 Verifying : which-2.21-12.el8.x86_64 5/5 Installed: gpm-libs-1.20.7-15.el8.x86_64 vim-common-2:8.0.1763-13.el8.x86_64 vim-enhanced-2:8.0.1763-13.el8.x86_64 vim-filesystem-2:8.0.1763-13.el8.noarch which-2.21-12.el8.x86_64 Complete! Removing intermediate container 533cc1bef383 ---> ff98aa7a481f Step 8/9 : EXPOSE 80 ---> Running in 7e0e74f216a6 Removing intermediate container 7e0e74f216a6 ---> ca2d6a5b776f Step 9/9 : CMD /bin/bash ---> Running in 830ceb01bd03 Removing intermediate container 830ceb01bd03 ---> 8b0f24987335 Successfully built 8b0f24987335 Successfully tagged tiankeuri/mycentos:v1

    查看镜像

    [root@base210 dockerfile]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE tiankeuri/mycentos v1 8b0f24987335 About a minute ago 295MB

    3、运行docker run -it 镜像ID

         跟之前运行一样

    4、查看镜像历史 docker history 镜像ID

    [root@base210 dockerfile]# docker history 8b0f24987335 IMAGE CREATED CREATED BY SIZE COMMENT 8b0f24987335 5 minutes ago /bin/sh -c #(nop) CMD ["/bin/sh" "-c" "/bin… 0B ca2d6a5b776f 5 minutes ago /bin/sh -c #(nop) EXPOSE 80 0B ff98aa7a481f 5 minutes ago /bin/sh -c yum -y install vim 48.1MB 358aa6df4073 5 minutes ago /bin/sh -c yum -y install net-tools 31.3MB 5dc308ab5e74 5 minutes ago /bin/sh -c #(nop) WORKDIR /home/ 0B 08916c745160 5 minutes ago /bin/sh -c #(nop) ENV WORKPATH=/home/ 0B 98a04a452949 5 minutes ago /bin/sh -c #(nop) LABEL name=centos Image b… 0B 80f6ee0d6b0c 5 minutes ago /bin/sh -c #(nop) MAINTAINER zuozhe<zuozhe@… 0B 0d120b6ccaa8 8 weeks ago /bin/sh -c #(nop) CMD ["/bin/bash"] 0B <missing> 8 weeks ago /bin/sh -c #(nop) LABEL org.label-schema.sc… 0B <missing> 8 weeks ago /bin/sh -c #(nop) ADD file:538afc0c5c964ce0d… 215MB

     

    Processed: 0.030, SQL: 8