龙空技术网

定制基础容器镜像

BlueCore 411

前言:

目前咱们对“centos7定制”都比较看重,小伙伴们都需要学习一些“centos7定制”的相关资讯。那么小编也在网上搜集了一些有关“centos7定制””的相关内容,希望同学们能喜欢,各位老铁们快快来了解一下吧!

Docker官方hub上拉取的镜像,很多时候并不能满足自己公司项目所要使用的环境,需要基于官方镜像进行定制化。

拉取官方镜像

此次测试定制基础镜像使用的是CentOS官方基础镜像,TAG为:centos:centos7.9.2009

docker pull centos:centos7.9.2009
将本地镜像推送到本地镜像仓库(可不做)

推送本地搭建的harbor镜像仓库的目的主要是担心hub.docker.com的访问稳定性。

[root@harbor lab]# docker tag centos:centos7.9.2009 10.100.202.211/centos/centos:centos7.9.2009[root@harbor lab]# docker push 10.100.202.211/centos/centos:centos7.9.2009The push refers to repository [10.100.202.211/centos/centos]174f56854903: Pushed centos7.9.2009: digest: sha256:dead07b4d8ed7e29e98de0f4504d87e8880d4347859d839686a31da35a3b532f size: 529
启动容器

启动容器的目的在于接下来要执行的命令需要在容器中验证命令的有效性,以便通过dockerfile构建定制化容器镜像时可以顺利完成。

[root@harbor lab]# docker run -d -it --name centos-ini eeb6ee3f44bd 440cb009c419ffc72dd242afd650a1cbc244b345e8aef962167f88909f4389d7
修改容器默认软件源为阿里云

连接到容器中,执行以下命令修改软件源:

# 连接到刚刚启动的容器docker attach centos-ini# 修改软件源为阿里云[root@6edc8ad63088 yum.repos.d]# curl -o /etc/yum.repos.d/CentOS-Base.repo   % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current                                 Dload  Upload   Total   Spent    Left  Speed100  2523  100  2523    0     0    935      0  0:00:02  0:00:02 --:--:--   935# 因为服务器并非阿里云ECS实例,所以需要删除关于aliyuncs.com相关的域名地址[root@6edc8ad63088 /]# sed -i -e '/mirrors.cloud.aliyuncs.com/d' -e '/mirrors.aliyuncs.com/d' /etc/yum.repos.d/CentOS-Base.repo
通过dockerfile构建定制化容器镜像
# 创建目录mkdir centos-pdc && cd centos-pdc[root@harbor centos-pdc]# cat dockerfile FROM 10.100.202.211/centos/centos:centos7.9.2009RUN curl -o /etc/yum.repos.d/CentOS-Base.repo  && sed -i -e '/mirrors.cloud.aliyuncs.com/d' -e '/mirrors.aliyuncs.com/d' /etc/yum.repos.d/CentOS-Base.repoCMD "/bin/bash"# 基于dockerfile构建定制化容器镜像[root@harbor centos-pdc]# docker build -t centos-pdc:v1.0 .Sending build context to Docker daemon  2.048kBStep 1/3 : FROM 10.100.202.211/centos/centos:centos7.9.2009 ---> eeb6ee3f44bdStep 2/3 : RUN curl -o /etc/yum.repos.d/CentOS-Base.repo  && sed -i -e '/mirrors.cloud.aliyuncs.com/d' -e '/mirrors.aliyuncs.com/d' /etc/yum.repos.d/CentOS-Base.repo ---> Running in 8598ee505816  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current                                 Dload  Upload   Total   Spent    Left  Speed                                          100  2523  100  2523    0     0   1254      0  0:00:02  0:00:02 --:--:--  1254                                         Removing intermediate container 8598ee505816                                                                            ---> 3daf441e0efcStep 3/3 : CMD "/bin/bash" ---> Running in 81547977b798Removing intermediate container 81547977b798 ---> a298bd8361a2Successfully built a298bd8361a2Successfully tagged centos-pdc:v1.0[root@harbor centos-pdc]# docker imagesREPOSITORY                                          TAG              IMAGE ID       CREATED         SIZEcentos-pdc                                          v1.0             a298bd8361a2   4 minutes ago   204MB
推送构建的镜像到harbor仓库

上面根据dockerfile构建出的镜像,可以为其打tag后,push到私有化镜像仓库harbor存储,以便其他人直接使用该镜像。

[root@harbor centos-pdc]# docker tag centos-pdc:v1.0 10.100.202.211/centos/centos-pdc:20220718[root@harbor centos-pdc]# docker push 10.100.202.211/centos/centos-pdc:20220718The push refers to repository [10.100.202.211/centos/centos-pdc]6ba235257a6f: Pushed 174f56854903: Mounted from centos/centos 20220718: digest: sha256:091768246ae8da68092b36a6a69f6a096c6e56b85b22e51c4acf416c7b274358 size: 737

标签: #centos7定制