一、使用Dockerfile创建centos/vim
1.创建Dockerfile文件(注意大小写)
然后进行编辑
#创建Dockerfile文件
[root@localhost ~]# touch Dockerfile
[root@localhost ~]# ls
anaconda-ks.cfg Dockerfile
#向Dockerfile文件中添加内容
[root@localhost ~]# vim Dockerfile
2.编辑内容如下:(注意源使用centos 7 版本,8已经不再维护)
FROM centos:7
MAINTAINER cbg<166092@qq.com>
ENV MYPATH /usr/local
WORKDIR $MYPATH
RUN yum -y install vim
EXPOSE 80
CMD echo $MYPATH
CMD echo "---end---"
CMD /bin/bash
3.执行Dockerfile文件,遇到下述问题
[root@localhost ~]# docker build -t mycentos:0.1 .
Sending build context to Docker daemon 18.43 kB
Step 1/9 : FROM centos:7
---> eeb6ee3f44bd
Step 2/9 : MAINTAINER cbg<166092@qq.com>
---> Using cache
---> cc539581e821
Step 3/9 : ENV MYPATH /usr/local
---> Using cache
---> d36ff5473472
Step 4/9 : WORKDIR $MYPATH
---> Using cache
---> efabcb41766b
Step 5/9 : RUN yum -y install vim
---> [Warning] IPv4 forwarding is disabled. Networking will not work.
---> Running in 013a5bba630a
Loaded plugins: fastestmirror, ovl
Determining fastest mirrors
Could not retrieve mirrorlist http://mirrorlist.centos.org/?release=7&arch=x86_64&repo=os&infra=container error was
14: curl#6 - "Could not resolve host: mirrorlist.centos.org; Unknown error"
One of the configured repositories failed (Unknown),
and yum doesn't have enough cached data to continue. At this point the only
safe thing yum can do is fail. There are a few ways to work "fix" this:
1. Contact the upstream for the repository and get them to fix the problem.
2. Reconfigure the baseurl/etc. for the repository, to point to a working
upstream. This is most often useful if you are using a newer
distribution release than is supported by the repository (and the
packages for the previous distribution release still work).
3. Run the command with the repository temporarily disabled
yum --disablerepo=<repoid> ...
4. Disable the repository permanently, so yum won't use it by default. Yum
will then just ignore the repository until you permanently enable it
again or use --enablerepo for temporary usage:
yum-config-manager --disable <repoid>
or
subscription-manager repos --disable=<repoid>
5. Configure the failing repository to be skipped, if it is unavailable.
Note that yum will try to contact the repo. when it runs most commands,
so will have to try and fail each time (and thus. yum will be be much
slower). If it is a very temporary problem though, this is often a nice
compromise:
yum-config-manager --save --setopt=<repoid>.skip_if_unavailable=true
Cannot find a valid baseurl for repo: base/7/x86_64
The command '/bin/sh -c yum -y install vim' returned a non-zero code: 1
上述问题,主要原因是在centos镜像内部没有网络
解决方法:
先检查下 ping 网络,看看是不是网络问题
1.可能是网络出现问题
vim etc/sysconfig/network-scripts/ifcfg-ens33
修改配置为ONBOOT=yes
重启网络服务:
service network restart && systemctl restart network
如果ping baidu.com还是有问题,再次重启网络服务,或者reboot虚拟机重启一下就好了
2.修改下DNS 配置
vim /etc/resolv.conf
nameserver 10.0.8.10 (自己的虚拟机ip地址)
(我使用8.8.8.8或者233.6.6.6都有问题)
但是以自己虚拟机ip作为中转就没问题,在镜像内也可以ping百度
3.添加路由转发功能
vi /etc/sysctl.conf
添加 net.ipv4.ip_forward=1
重启网络服务:
service network restart && systemctl restart network
以及重启docker
在测试docker run 镜像内部是否能ping baidu.com 的时候要注意添加版本号
#这里需要声明版本号,否则使用最新版
[root@localhost ~]# docker run -it centos:7 /bin/bash
[root@fbe098fb5c0e /]# ping baidu.com
PING baidu.com (39.156.66.10) 56(84) bytes of data.
64 bytes from 39.156.66.10 (39.156.66.10): icmp_seq=1 ttl=127 time=34.4 ms
此时再执行Dockerfile文件
(Dockerfile 文件名字必须是这个,否则需要添加-f属性)
最后的.不要忘记(当前路径下的Dockerfile)
[root@localhost ~]# docker build -t mycentos:0.1 .
即可正常执行下载vim
查看本地镜像
mycentos为新建镜像
[root@localhost mydockerfile]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mycentos 0.1 32b1088e8974 52 minutes ago 657 MB
docker.io/centos 7 eeb6ee3f44bd 17 months ago 204 MB
本文介绍了在使用Dockerfile创建centos/vim镜像时遇到的yum网络错误。主要原因是镜像内无网络,解决方法包括检查网络、修改DNS配置和添加路由转发。通过这些步骤,可以成功执行Dockerfile并构建包含vim的定制CentOS7镜像。

2489

被折叠的 条评论
为什么被折叠?



