0
点赞
收藏
分享

微信扫一扫

#yyds干活盘点#20.使用DockerFile构建centos失败的解决方案

扶摇_hyber 2022-02-27 阅读 140

20.使用DockerFile构建centos失败的解决方案

我们在《#yyds干活盘点#19.使用DockerFile构建centos》博文中,构建centos镜像时,没有成功,经过不断的测试研究,终于解决了《#yyds干活盘点#19.使用DockerFile构建centos》中构建失败的问题

20.1. build镜像失败解决

参考博客:https://blog.csdn.net/weixin_46217160/article/details/122835466

原因如下:

image202202270151.png

注意点:

  • 这里跟宿主机是没有关系的,之前看这篇博文,我一直以为是宿主机的问题,后来重新启动centos镜像,并进入到容器后发现,是启动的centos容器中的/etc/yum.repos.d/目录下,有CentOS-Linux-.repo等文件,需要使用命令修改CentOS-Linux-.repo等文件的yum源地址。
    image202202270200.png
[root@iZ8vb6lqqya4rvreq9ra6gZ ~]# docker images
REPOSITORY            TAG                    IMAGE ID       CREATED          SIZE
doublewang/centos     latest                 232c1c83e393   15 minutes ago   537MB
test/centos           latest                 103a757a5681   3 days ago       231MB
tomcat_v              v1                     aed546e63257   13 days ago      384MB
tomcat                9.0.58-jdk8-corretto   41702c6af95e   3 weeks ago      379MB
nginx                 latest                 605c77e624dd   8 weeks ago      141MB
redis                 latest                 7614ae9453d1   2 months ago     113MB
mysql                 5.7                    c20987f18b13   2 months ago     448MB
centos                latest                 5d0da3dc9764   5 months ago     231MB
portainer/portainer   latest                 580c0e4e98b0   11 months ago    79.1MB
elasticsearch         7.6.2                  f29a1ee41030   23 months ago    791MB
[root@iZ8vb6lqqya4rvreq9ra6gZ ~]# docker run -it centos
[root@c0d3d4978494 /]# cd /etc/yum.repos.d/
[root@c0d3d4978494 yum.repos.d]# ls
CentOS-Linux-AppStream.repo          CentOS-Linux-Devel.repo             CentOS-Linux-Media.repo
CentOS-Linux-BaseOS.repo             CentOS-Linux-Extras.repo            CentOS-Linux-Plus.repo
CentOS-Linux-ContinuousRelease.repo  CentOS-Linux-FastTrack.repo         CentOS-Linux-PowerTools.repo
CentOS-Linux-Debuginfo.repo          CentOS-Linux-HighAvailability.repo  CentOS-Linux-Sources.repo
[root@c0d3d4978494 yum.repos.d]# cat CentOS-Linux-AppStream.repo 
# CentOS-Linux-AppStream.repo
#
# The mirrorlist system uses the connecting IP address of the client and the
# update status of each mirror to pick current mirrors that are geographically
# close to the client.  You should use this for CentOS updates unless you are
# manually picking other mirrors.
#
# If the mirrorlist does not work for you, you can try the commented out
# baseurl line instead.

[appstream]
name=CentOS Linux $releasever - AppStream
mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=AppStream&infra=$infra
#baseurl=http://mirror.centos.org/$contentdir/$releasever/AppStream/$basearch/os/
gpgcheck=1
enabled=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial
[root@c0d3d4978494 yum.repos.d]# 

解决方案如下:

修改DockerFile文件内容,并且最终的文件内容如下:

# 指定基础镜像
FROM centos
# 执行作者信息
MAINTAINER doubleWang<739455347@qq.com>
# 配置环境变量
ENV MYPATH /usr/local
# 设置工作目录
WORKDIR $MYPATH
# 修改centos中yum源的地址
RUN sed -i -e "s|mirrorlist=|#mirrorlist=|g" /etc/yum.repos.d/CentOS-*
RUN sed -i -e "s|#baseurl=http://mirror.centos.org|baseurl=http://vault.centos.org|g" /etc/yum.repos.d/CentOS-*
# 安装ifconfig命令支持
RUN yum update -y && yum install net-tools -y
# 安装clear命令支持
RUN dnf install ncurses -y
# 暴露端口
EXPOSE 80
# 输出工作目录信息
CMD echo $WORKDIR
# 输出构建完成信息
CMD echo "-------build end successful--------"

CMD /bin/bash

开始构建自己的centos镜像

执行构建镜像命令如下:

docker build -f dockerfile-centos -t doublewang/centos .

image202202270204.png

从上图可以看到,这次build成功了,已经开始构建了。

image202202270206.png

doublewang/centos 镜像构建成功!!

20.2. 测试安装好的命令

启动并测试doublewang/centos镜像
image202202270214.png

clear命令也是可以用的,我已经测试过了,由于clear执行后,看不到上面的命令执行情况,所以截图中,我只截了这一部分。

20.3. 拓展

我们还可以使用 docker history 命令查看每个镜像的执行历史。
image202202270219.png

------------------------------EOF-----------------------------

举报

相关推荐

0 条评论