利用DockerFile文件执行docker build自动构建基础镜像(Ubuntu)

官方说明: https://docs.docker.com/engine/reference/builder/

第一步:按照业务类型或系统类型等方式划分创建目录环境,方便后期镜像比较多的时候进行分类
[root@ubuntu2004 ~]#mkdir -pv /data/dockerfile/{app/{nginx,tomcat,jdk},system/{ubuntu,rocky,alpine}}
[root@ubuntu2004 ~]#tree /data/
/data/
└── dockerfile
    ├── app
    │   ├── jdk
    │   ├── nginx
    │   └── tomcat
    └── system
        ├── alpine
        ├── rocky
        └── ubuntu

9 directories, 0 files
第二步:从哪个父镜像继承,父镜像在官方文档  #FROM
[root@ubuntu2004 ~]#cd /data/dockerfile/system/ubuntu/
[root@ubuntu2004 ubuntu]#vim Dockerfile(可以不使用这个文件名,但是需要指定, Dockerfile是默认的)
FROM ubuntu:20.04                #去https://hub.docker.com/上搜寻ubuntu找到对应版本父镜像

#docker build -f                  #(如果不是Dockerfile,就需要用-f指定,需要放在当前目录下)

如果文件名是 Dockerfile,就不用-f指定
[root@ubuntu2004 ubuntu]#docker build -t ubuntu:20.04-20221018 .   #(-t起名称 . 当前路径)20.04: Sending build context to Docker daemon  2.048kB
Step 1/1 : FROM ubuntu:20.04
20.04: Pulling from library/ubuntu
Digest: sha256:626ffe58f6e7566e00254b638eb7e0f3b11d4da9675088f4781a50ae288f3322
Status: Downloaded newer image for ubuntu:20.04
 ---> ba6acccedd29
Successfully built ba6acccedd29
Successfully tagged ubuntu:20.04-20221018
第三步: #加说明性LABEL
[root@ubuntu2004 ubuntu]#vim Dockerfile 
FROM ubuntu:20.04
LABEL Auther=mengfanchao version=ubuntu20.04-v1.0 ORG=M50 
[root@ubuntu2004 ubuntu]#docker build -t ubuntu:20.04-20221018 . (会覆盖掉原来的)
这个build命令在修改Dockerfile后需要重新执行,实验环境可以把这个命令写个脚本带替
如:[root@ubuntu2004 ubuntu]#vim build.sh
#!/bin/bash
TAG=$1
docker build -t ubuntu:$TAG .
[root@ubuntu2004 ubuntu]#bash build.sh 20.04-20221018-v1.0
查看ubuntu制作镜像历史
[root@ubuntu2004 ubuntu]#docker history ubuntu:20.04-20221018
第四步:RUN  执行shell命令
RUN 指令用来在构建镜像阶段需要执行 FROM 指定镜像所支持的Shell命令. 
通常各种基础镜像一般都支持丰富的shell命令 
注意: RUN 可以写多个,每一个RUN指令都会建立一个镜像层,所以尽可能合并成一条指令,比如将多个 shell命令通过 && 连接一起成为在一条指令 
每个RUN都是独立运行的,和前一个RUN无关

指定apt仓库(清华源,https://mirrors.tuna.tsinghua.edu.cn/help/ubuntu/)
[root@ubuntu2004 ubuntu]#vim Dockerfile  
FROM ubuntu:20.04
LABEL Auther=mengfanchao version=ubuntu20.04-v1.0 ORG=M50
RUN  sed -i "s@http://.*archive.ubuntu.com@https://mirrors.tuna.tsinghua.edu.cn@g" /etc/apt/sources.list && sed -i "s@http://.*security.ubuntu.com@https://mirrors.tuna.tsinghua.edu.cn@g" /etc/apt/sources.list
第五步:安装基础包     #RUN执行shell命令
[root@ubuntu2004 ubuntu]#vim Dockerfile 
FROM ubuntu:20.04
LABEL Auther=mengfanchao version=ubuntu20.04-v1.0 ORG=M50
RUN  sed -i "s@http://.*archive.ubuntu.com@http://mirrors.tuna.tsinghua.edu.cn@g" /etc/apt/sources.list && sed -i "s@http://.*security.ubuntu.com@http://mirrors.tuna.tsinghua.edu.cn@g" /etc/apt/sources.list && apt update && apt -y install wget curl net-tools iproute2 tcpdump telnet traceroute  nfs-common lrzsz tree iotop unzip zip vim tzdata

[root@ubuntu2004 ubuntu]#bash build.sh 20.04-20221018-v1.0
第六步:变更时区,\换行  #RUN执行shell命令
[root@ubuntu2004 ubuntu]#vim Dockerfile
FROM ubuntu:20.04
LABEL Auther=mengfanchao version=ubuntu20.04-v1.0 ORG=M50
RUN  sed -i "s@http://.*archive.ubuntu.com@http://mirrors.tuna.tsinghua.edu.cn@g" /etc/apt/sources.list && \
     sed -i "s@http://.*security.ubuntu.com@http://mirrors.tuna.tsinghua.edu.cn@g" /etc/apt/sources.list && \
     apt update && apt -y install wget curl net-tools iproute2 tcpdump telnet traceroute  nfs-common lrzsz tree iotop unzip zip vim tzdata && \
     ln -s /usr/share/zoneinfo/Asia/Shanghai  /etc/localtime

[root@ubuntu2004 ubuntu]#bash build.sh 20.04-20221018-v1.0
第七步:定义变量      #ENV
[root@ubuntu2004 ubuntu]#vim Dockerfile
FROM ubuntu:20.04
LABEL Auther=mengfanchao version=ubuntu20.04-v1.0 ORG=M50
RUN  sed -i "s@http://.*archive.ubuntu.com@http://mirrors.tuna.tsinghua.edu.cn@g" /etc/apt/sources.list && \
     sed -i "s@http://.*security.ubuntu.com@http://mirrors.tuna.tsinghua.edu.cn@g" /etc/apt/sources.list && \
     apt update && apt -y install wget curl net-tools iproute2 tcpdump telnet traceroute  nfs-common lrzsz tree iotop unzip zip vim tzdata && \
     ln -s /usr/share/zoneinfo/Asia/Shanghai  /etc/localtime

ENV CALSS=m50 DATE=2000-10-18

[root@ubuntu2004 ubuntu]#bash build.sh 20.04-20221018-v1.0
第八步:配置apt仓库,把宿主机文件复制到镜像中 #COPY
(文件要放在Dockerfile子目录或平级目录,不能放在父目录)

把宿主机sources.list文件复制到镜像中去
[root@ubuntu2004 ubuntu]#cp /etc/apt/sources.list /data/dockerfile/system/ubuntu/
[root@ubuntu2004 ubuntu]#ls
build.sh  Dockerfile  sources.list
[root@ubuntu2004 ubuntu]#sed -i 's/https/http/g' sources.list 
[root@ubuntu2004 ubuntu]#cat sources.list 
deb http://mirrors.tuna.tsinghua.edu.cn//ubuntu/ focal main restricted universe multiverse
deb-src http://mirrors.tuna.tsinghua.edu.cn//ubuntu/ focal main restricted universe multiverse

deb http://mirrors.tuna.tsinghua.edu.cn//ubuntu/ focal-security main restricted universe multiverse
deb-src http://mirrors.tuna.tsinghua.edu.cn//ubuntu/ focal-security main restricted universe multiverse

deb http://mirrors.tuna.tsinghua.edu.cn//ubuntu/ focal-updates main restricted universe multiverse
deb-src http://mirrors.tuna.tsinghua.edu.cn//ubuntu/ focal-updates main restricted universe multiverse

deb http://mirrors.tuna.tsinghua.edu.cn//ubuntu/ focal-proposed main restricted universe multiverse
deb-src http://mirrors.tuna.tsinghua.edu.cn//ubuntu/ focal-proposed main restricted universe multiverse

deb http://mirrors.tuna.tsinghua.edu.cn//ubuntu/ focal-backports main restricted universe multiverse
deb-src http://mirrors.tuna.tsinghua.edu.cn//ubuntu/ focal-backports main restricted universe multiverse

[root@ubuntu2004 ubuntu]#vim Dockerfile
FROM ubuntu:20.04
LABEL Auther=mengfanchao version=ubuntu20.04-v1.0 ORG=M50
COPY sources.list /etc/apt/sources.list
RUN  apt update && apt -y install wget curl net-tools iproute2 tcpdump telnet traceroute  nfs-common lrzsz tree iotop unzip zip vim tzdata && \
     rm -f /etc/localtime && ln -s /usr/share/zoneinfo/Asia/Shanghai  /etc/localtime

ENV CALSS=m50 DATE=2000-10-18

[root@ubuntu2004 ubuntu]#bash build.sh 20.04-20221018-v2.0
第九步:基于20.04-20221018-v2.0的镜像做,可以更快
[root@ubuntu2004 ubuntu]#vim Dockerfile
FROM ubuntu:20.04-20221018-v2.0
LABEL Auther=mengfanchao version=ubuntu20.04-v1.0 ORG=M50
COPY sources.list /etc/apt/sources.list
#RUN  apt update && apt -y install wget curl net-tools iproute2 tcpdump telnet traceroute  nfs-common lrzsz tree iotop unzip zip vim tzdata && \
#     rm -f /etc/localtime && ln -s /usr/share/zoneinfo/Asia/Shanghai  /etc/localtime

#ENV CALSS=m50 DATE=2000-10-18

[root@ubuntu2004 ubuntu]#bash build.sh 20.04-20221018-v3.0
Sending build context to Docker daemon  4.608kB
Step 1/3 : FROM ubuntu:20.04-20221018-v2.0
 ---> 8aac5a8645e6
Step 2/3 : LABEL Auther=mengfanchao version=ubuntu20.04-v1.0 ORG=M50
 ---> Running in ccddfd904f2e
Removing intermediate container ccddfd904f2e
 ---> 983f7c814300
Step 3/3 : COPY sources.list /etc/apt/sources.list
 ---> 4f57e4f432e9
Successfully built 4f57e4f432e9
Successfully tagged ubuntu:20.04-20221018-v3.0
运行容器,查看apt仓库配置是否正常
[root@ubuntu2004 ubuntu]#docker run -it --rm -e CLASS=m50 ubuntu:20.04-20221018-v3.0 cat /etc/apt/sources.list
deb http://mirrors.tuna.tsinghua.edu.cn//ubuntu/ focal main restricted universe multiverse
deb-src http://mirrors.tuna.tsinghua.edu.cn//ubuntu/ focal main restricted universe multiverse

deb http://mirrors.tuna.tsinghua.edu.cn//ubuntu/ focal-security main restricted universe multiverse
deb-src http://mirrors.tuna.tsinghua.edu.cn//ubuntu/ focal-security main restricted universe multiverse

deb http://mirrors.tuna.tsinghua.edu.cn//ubuntu/ focal-updates main restricted universe multiverse
deb-src http://mirrors.tuna.tsinghua.edu.cn//ubuntu/ focal-updates main restricted universe multiverse

deb http://mirrors.tuna.tsinghua.edu.cn//ubuntu/ focal-proposed main restricted universe multiverse
deb-src http://mirrors.tuna.tsinghua.edu.cn//ubuntu/ focal-proposed main restricted universe multiverse

deb http://mirrors.tuna.tsinghua.edu.cn//ubuntu/ focal-backports main restricted universe multiverse
deb-src http://mirrors.tuna.tsinghua.edu.cn//ubuntu/ focal-backports main restricted universe multiverse
第十步:写一个文件,并打包。然后写到Dockerfile文件中  ADD:复制和解包文件
[root@ubuntu2004 ubuntu]#echo docker website > index.html
[root@ubuntu2004 ubuntu]#tar cf index.html.tar index.html
[root@ubuntu2004 ubuntu]#ls
build.sh  Dockerfile  index.html  index.html.tar  sources.list
[root@ubuntu2004 ubuntu]#rm -rf index.html
[root@ubuntu2004 ubuntu]#ls
build.sh  Dockerfile  index.html.tar  sources.list
[root@ubuntu2004 ubuntu]#gzip index.html.tar
[root@ubuntu2004 ubuntu]#ls
build.sh  Dockerfile  index.html.tar.gz  sources.list

[root@ubuntu2004 ubuntu]#vim Dockerfile 
FROM ubuntu:20.04-20221018-v2.0
LABEL Auther=mengfanchao version=ubuntu20.04-v1.0 ORG=M50
COPY sources.list /etc/apt/sources.list
#RUN  apt update && apt -y install wget curl net-tools iproute2 tcpdump telnet traceroute  nfs-common lrzsz tree iotop unzip zip vim tzdata && \
#     rm -f /etc/localtime && ln -s /usr/share/zoneinfo/Asia/Shanghai  /etc/localtime

#ENV CALSS=m50 DATE=2000-10-18
ADD index.html.tar.gz /data/www

[root@ubuntu2004 ubuntu]#bash build.sh 20.04-20221018-v4.0
Sending build context to Docker daemon  5.632kB
Step 1/4 : FROM ubuntu:20.04-20221018-v2.0
 ---> 8aac5a8645e6
Step 2/4 : LABEL Auther=mengfanchao version=ubuntu20.04-v1.0 ORG=M50
 ---> Using cache
 ---> 983f7c814300
Step 3/4 : COPY sources.list /etc/apt/sources.list
 ---> Using cache
 ---> 4f57e4f432e9
Step 4/4 : ADD index.html.gz /data/www
 ---> 9eb8adfd8880
Successfully built 9eb8adfd8880
Successfully tagged ubuntu:20.04-20221018-v4.0

进入容器查看index.html文件
[root@ubuntu2004 ubuntu]#docker run -it --rm ubuntu:20.04-20221018-v4.0 bash
root@3df18bda768c:/# tree /data/
/data/
`-- www
    `-- index.html

1 directory, 1 file
root@3df18bda768c:/# cat /data/www/index.html 
docker website

总结

第一步:创建目录和文件
[root@ubuntu2004 ~]#mkdir -pv /data/dockerfile/{app/{nginx,tomcat,jdk},system/{ubuntu,rocky,alpine}}
第二步:写Dockerfile文件
[root@ubuntu2004 ~]#cd /data/dockerfile/system/ubuntu/
[root@ubuntu2004 ubuntu]#vim Dockerfile
FROM ubuntu:20.04
LABEL Auther=mengfanchao version=ubuntu20.04-v1.0 ORG=M50
COPY sources.list /etc/apt/sources.list    #要把宿主机文件sources.list放在当前目录下
RUN  apt update && apt -y install wget curl net-tools iproute2 tcpdump telnet traceroute  nfs-common lrzsz tree iotop unzip zip vim tzdata && \
     ln -s /usr/share/zoneinfo/Asia/Shanghai  /etc/localtime

ENV CALSS=m50 DATE=2000-10-18
ADD  index.html.tar.gz /data/www/          #要把宿主机压缩包文件放在当前目录下
第三步:创建镜像
[root@ubuntu2004 ubuntu]#docker build -t ubuntu:20.04-20221018 .
把这个命令写成脚本
如:[root@ubuntu2004 ubuntu]#vim build.sh
#!/bin/bash
TAG=$1
docker build -t ubuntu:$TAG .
[root@ubuntu2004 ubuntu]#bash build.sh 20.04-20221018-v1.0
第四步:进入容器并验证
[root@ubuntu2004 ubuntu]#docker run --it --rm ubuntu:20.04-20221018-v1.0 bash
root@6dae64d0300f:/# cat /etc/apt/sources.list
deb http://mirrors.tuna.tsinghua.edu.cn//ubuntu/ focal main restricted universe multiverse
deb-src http://mirrors.tuna.tsinghua.edu.cn//ubuntu/ focal main restricted universe multiverse
第五步:根据20.04-20221018-v1.0镜像制作v2.0,并注释一些内容,可加快速度。顺便创建账号和用户www
[root@ubuntu2004 ubuntu]#vim Dockerfile
FROM ubuntu:20.04-20221018-v1.0
LABEL Auther=mengfanchao version=ubuntu20.04-v1.0 ORG=M50
COPY sources.list /etc/apt/sources.list
#RUN  apt update && apt -y install wget curl net-tools iproute2 tcpdump telnet traceroute  nfs-common lrzsz tree iotop unzip zip vim tzdata && \
#     rm -f /etc/localtime && ln -s /usr/share/zoneinfo/Asia/Shanghai  /etc/localtime

#ENV CALSS=m50 DATE=2000-10-18
RUN groupadd -g 80 www && useradd -u 80 -g www -s /sbin/nologin -M www
ADD  index.html.tar /data/www/

[root@ubuntu2004 ubuntu]#bash build.sh 20.04-20221018-v2.0
[root@ubuntu2004 ubuntu]#docker run -it --rm ubuntu:20.04-20221018-v2.0 bash
root@baa6cc0d6ce5:/# cat /etc/apt/sources.list
deb http://mirrors.tuna.tsinghua.edu.cn//ubuntu/ focal main restricted universe multiverse
deb-src http://mirrors.tuna.tsinghua.edu.cn//ubuntu/ focal main restricted universe multiverse

第六步:查看镜像和容器
[root@ubuntu2004 ubuntu]#docker images
REPOSITORY   TAG                   IMAGE ID       CREATED              SIZE
ubuntu       20.04-20221018-v2.0   ea728cc2fcf5   About a minute ago   241MB
ubuntu       20.04-20221018-v1.0   2a2961527103   13 minutes ago       241MB
ubuntu       20.04                 ba6acccedd29   12 months ago        72.8MB
[root@ubuntu2004 ubuntu]#docker ps -a
CONTAINER ID   IMAGE          COMMAND                  CREATED          STATUS                      PORTS     NAMES
54a0c61d5abf   ba1022e37841   "/bin/sh -c 'apt upd…"   16 minutes ago   Exited (1) 14 minutes ago             reverent_curie
bb9a46b8cdf7   ba1022e37841   "/bin/sh -c 'apt upd…"   18 minutes ago   Exited (1) 17 minutes ago             funny_bouman