Dockerfile制作基础系统镜像
一 目录规划
1.1 创建dockerfile目录
root@k8s-harbor-01:~# mkdir /data/dockerfile/{web/{nginx,tomcat,jdk},system/{centos,ubuntu,alpine}} -p
1.2 查看创建的结果
oot@k8s-harbor-01:~# tree /data/dockerfile/
/data/dockerfile/
├── system
│ ├── alpine
│ ├── centos
│ └── ubuntu
└── web
├── jdk
├── nginx
└── tomcat
8 directories, 0 files
1.3 当前系统版本
root@k8s-harbor-01:~# cat /etc/issue
Ubuntu 20.04.3 LTS \n \l
二 制作centos base image
2.1 pull centos image
root@k8s-harbor-01:~# docker pull centos:7.9.2009
2.2 编写Dockerfile
root@k8s-harbor-01:/data/dockerfile/system/centos# cat Dockerfile
FROM centos:7.9.2009
RUN yum -y install wget \
&& rm -rf /etc/yum.repos.d/* \
&& wget -P /etc/yum.repos.d/ http://mirrors.aliyun.com/repo/Centos-7.repo \
&& wget -P /etc/yum.repos.d/ http://mirrors.aliyun.com/repo/epel-7.repo \
&& yum clean all \
&& rm -rf /etc/localtime \
&& ln -snf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
2.3 编写构建镜像脚本
root@k8s-harbor-01:/data/dockerfile/system/centos# cat build-command.sh
#!/bin/bash
docker build -t 192.168.174.120/baseimages/centos-base:7.9.2009 .
#docker push 192.168.174.120/baseimages/centos-base:7.9.2009
2.4 构建镜像
root@k8s-harbor-01:/data/dockerfile/system/centos# bash build-command.sh
2.5 验证镜像
root@k8s-harbor-01:/data/dockerfile/system/centos# docker run -it --rm 192.168.174.120/baseimages/centos-base:7.9.2009 /bin/bash
[root@35515aac6c8c /]# date
Thu Dec 2 16:52:21 CST 2021
[root@35515aac6c8c /]# ls -l /etc/yum.repos.d/
total 8
-rw-r--r-- 1 root root 2523 Dec 26 2020 Centos-7.repo
-rw-r--r-- 1 root root 664 Dec 26 2020 epel-7.repo
[root@35515aac6c8c /]#
2.6 上传镜像到harbor
root@k8s-harbor-01:/data/dockerfile/system/centos# docker login 192.168.174.120
Authenticating with existing credentials...
WARNING! Your password will be stored unencrypted in /root/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store
Login Succeeded
root@k8s-harbor-01:/data/dockerfile/system/centos# docker push 192.168.174.120/baseimages/centos-base:7.9.2009
The push refers to repository [192.168.174.120/baseimages/centos-base]
a193b3efa9a7: Pushed
174f56854903: Pushed
7.9.2009: digest: sha256:0d0218033517022874a1e160f604362cb78ff1f8f77b28c680d7a209b5f9c590 size: 736
三 制作ubuntu base image
3.1 pull ubuntu image
root@k8s-harbor-01:~# docker pull ubuntu:20.04
3.2 准备软件源文件
root@k8s-harbor-01:/data/dockerfile/system/ubuntu# cat sources.list
deb http://mirrors.aliyun.com/ubuntu/ focal main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ focal main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ focal-security main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ focal-security main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ focal-updates main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ focal-updates main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ focal-proposed main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ focal-proposed main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ focal-backports main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ focal-backports main restricted universe multiverse
3.3 准备localtime
root@k8s-harbor-01:/data/dockerfile/system/ubuntu# cp /etc/localtime .
3.4 编写Dockerfile
root@k8s-harbor-01:/data/dockerfile/system/ubuntu# cat Dockerfile
FROM ubuntu:20.04
COPY sources.list /etc/apt/sources.list
COPY localtime /etc/localtime
3.5 编写构建镜像脚本
root@k8s-harbor-01:/data/dockerfile/system/ubuntu# cat build-command.sh
#!/bin/bash
docker build -t 192.168.174.120/baseimages/ubuntu-base:20.04 .
#docker push 192.168.174.120/baseimages/ubuntu-base:20.04
3.6 构建镜像
root@k8s-harbor-01:/data/dockerfile/system/ubuntu# ./build-command.sh
Sending build context to Docker daemon 6.144kB
Step 1/3 : FROM ubuntu:20.04
---> ba6acccedd29
Step 2/3 : COPY sources.list /etc/apt/sources.list
---> 15ab776afe11
Step 3/3 : COPY localtime /etc/localtime
---> 55ac560bfca5
Successfully built 55ac560bfca5
Successfully tagged 192.168.174.120/baseimages/ubuntu-base:20.04
3.7 验证镜像
root@k8s-harbor-01:/data/dockerfile/system/ubuntu# docker run -it --rm 192.168.174.120/baseimages/ubuntu-base:20.04 /bin/bash
root@91f8d0b54321:/# date
Thu Dec 2 17:25:30 CST 2021
root@91f8d0b54321:/# apt update
Get:1 http://mirrors.aliyun.com/ubuntu focal InRelease [265 kB]
Get:2 http://mirrors.aliyun.com/ubuntu focal-security InRelease [114 kB]
Get:3 http://mirrors.aliyun.com/ubuntu focal-updates InRelease [114 kB]
Get:4 http://mirrors.aliyun.com/ubuntu focal-proposed InRelease [267 kB]
Get:5 http://mirrors.aliyun.com/ubuntu focal-backports InRelease [108 kB]
Get:6 http://mirrors.aliyun.com/ubuntu focal/main Sources [1079 kB]
Get:7 http://mirrors.aliyun.com/ubuntu focal/multiverse Sources [208 kB]
Get:8 http://mirrors.aliyun.com/ubuntu focal/universe Sources [12.3 MB]
Get:9 http://mirrors.aliyun.com/ubuntu focal/restricted Sources [7198 B]
Get:10 http://mirrors.aliyun.com/ubuntu focal/main amd64 Packages [1275 kB]
Get:11 http://mirrors.aliyun.com/ubuntu focal/universe amd64 Packages [11.3 MB]
3.8 上传镜像到harbor
root@k8s-harbor-01:/data/dockerfile/system/ubuntu# docker login 192.168.174.120
Authenticating with existing credentials...
WARNING! Your password will be stored unencrypted in /root/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store
Login Succeeded
root@k8s-harbor-01:/data/dockerfile/system/ubuntu# docker push 192.168.174.120/baseimages/ubuntu-base:20.04
The push refers to repository [192.168.174.120/baseimages/ubuntu-base]
03929d067b08: Pushed
3e2abc630209: Pushed
9f54eef41275: Pushed
20.04: digest: sha256:a7459a5e9ac5a868e186bee3f4a530b9d8d505c339578779900842b128ed584c size: 943
四 制作alpine base image
4.1 pull alpine image
root@k8s-harbor-01:~# docker pull alpine:3.15
4.2 编写Dockerfile
root@k8s-harbor-01:/data/dockerfile/system/alpine# cat Dockerfile
FROM alpine:3.15
RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories \
&& apk add --no-cache tzdata \
&& ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
4.3 编写构建镜像脚本
root@k8s-harbor-01:/data/dockerfile/system/alpine# cat build-command.sh
#!/bin/bash
docker build -t 192.168.174.120/baseimages/alpine-base:3.15 .
#docker push 192.168.174.120/baseimages/alpine-base:3.15
4.4 构建镜像
root@k8s-harbor-01:/data/dockerfile/system/alpine# ./build-command.sh
Sending build context to Docker daemon 4.608kB
Step 1/2 : FROM alpine:3.15
---> c059bfaa849c
Step 2/2 : RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories && apk add --no-cache tzdata && ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
---> Running in fab7cd8023df
fetch https://mirrors.aliyun.com/alpine/v3.15/main/x86_64/APKINDEX.tar.gz
fetch https://mirrors.aliyun.com/alpine/v3.15/community/x86_64/APKINDEX.tar.gz
(1/1) Installing tzdata (2021e-r0)
Executing busybox-1.34.1-r3.trigger
OK: 9 MiB in 15 packages
Removing intermediate container fab7cd8023df
---> 5d928b01ab04
Successfully built 5d928b01ab04
Successfully tagged 192.168.174.120/baseimages/alpine-base:3.15
4.5 验证镜像
root@k8s-harbor-01:/data/dockerfile/system/alpine# docker run -it --rm 192.168.174.120/baseimages/alpine-base:3.15 sh
/ # date
Thu Dec 2 17:37:39 CST 2021
/ # apk update
fetch https://mirrors.aliyun.com/alpine/v3.15/main/x86_64/APKINDEX.tar.gz
fetch https://mirrors.aliyun.com/alpine/v3.15/community/x86_64/APKINDEX.tar.gz
v3.15.0-36-g1913711e11 [https://mirrors.aliyun.com/alpine/v3.15/main]
v3.15.0-38-g8d417c2896 [https://mirrors.aliyun.com/alpine/v3.15/community]
OK: 15829 distinct packages available
/ #
4.6 上传镜像到harbor
root@k8s-harbor-01:/data/dockerfile/system/alpine# docker push 192.168.174.120/baseimages/alpine-base:3.15
The push refers to repository [192.168.174.120/baseimages/alpine-base]
d1e64fe801c1: Pushed
646ffa5d43fe: Pushed
8d3ac3489996: Pushed
3.15: digest: sha256:2b46c4bb705a2f42064f017ec548efa773e17767be3bde7116d673425633d3df size: 942