Docker Hub 中99%的镜像都是从这个基础镜像(Scratch)中安装和配置需要的软件和配置构建出来的!
创建一个自己的 CentOS
1、查看官方镜像
2、编写自己的 DockerFile
初始化:
cd /home
mkdir dockerfile
vim dockerfile-centos
编写 dockerfile-centos
:
FROM centos
MAINTAINER mianbao<843818747@qq.com>
ENV MYPATH /usr/local
WORKDIR $MYPATH
RUN yum -y install vim \
&& yum -y install net-tools
EXPOSE 80
CMD echo $MYPATH
CMD echo "----end----"
CMD ["/bin/bash"]
3、通过这个文件构建镜像
# docker build -f dockerfile文件路径 -t 镜像名:[tag] 其中t表示target
# . 表示当前目录(输出路径)
[root@localhost dockerfile]$ docker build -f dockerfile-centos -t mycentos:1.0 .
3、报错和解决
3.1、报错一
报错一
docker警告:WARNING: IPv4 forwarding is disabled. Networking will not work._杰哥的技术杂货铺的博客-CSDN博客
报错:
—> [Warning] IPv4 forwarding is disabled. Networking will not work.
是没有开启转发, docker网桥配置完后,需要开启转发,不然容器启动后,就会没有网络,配置 /etc/sysctl.conf , 添加 net.ipv4.ip_forward=1
解决:
# 也可修改此文件:/usr/lib/sysctl.d/00-system.conf
cd /etc
# 备份
cp sysctl.conf sysctl.conf_bak20220701
# 添加配置
vim sysctl.conf
# 添加的配置
net.ipv4.ip_forward=1
# 重启 network 服务
systemctl restart network
# 重启 docker 服务
systemctl restart docker
# 查看是否修改成功
sysctl net.ipv4.ip_forward
# 如果返回为“net.ipv4.ip_forward = 1”则表示修改成功
3.2、报错二
报错二
报错:
Error: Failed to download metadata for repo ‘appstream’: Cannot prepare internal mirrorlist: No URLs in mirrorlist
错误:无法下载 repo ‘appstream’ 的元数据:无法准备内部镜像列表:镜像列表中没有 URL
解决:
- 检测是否能够
ping
通外网
ping -c3 www.baidu.com
- CentOS 已于2021年底停止维护而产生的问题。如果需要更新 CentOS,需要将镜像从 mirror.centos.org 更改为 vault.centos.org
1、进入到 yum 的 repos 目录,并备份
[root@localhost dockerfile]$ cd /etc/yum.repos.d/
[root@localhost yum.repos.d]$ ll
总用量 16
-rw-r--r--. 1 root root 2523 12月 26 2020 CentOS-Base.repo
-rw-r--r--. 1 root root 998 12月 11 2018 CentOS-SCLo-scl.repo
-rw-r--r--. 1 root root 971 10月 29 2018 CentOS-SCLo-scl-rh.repo
-rw-r--r--. 1 root root 2081 5月 7 22:12 docker-ce.repo
# 创建备份目录 /home/bak/etc/yum.repos.d/ 进行备份
[root@localhost yum.repos.d]$ cp -r /etc/yum.repos.d/* /home/bak/etc/yum.repos.d/
2、修改 centos 文件内容
# 官方临时镜像地址从 mirror.centos.org 移到 https://vault.centos.org
sed -i 's/mirrorlist/#mirrorlist/g' /etc/yum.repos.d/CentOS-*
sed -i 's|#baseurl=http://mirror.centos.org|baseurl=http://vault.centos.org|g' /etc/yum.repos.d/CentOS-*
# 测试
# sed -i 's|#baseurl=http://mirror.centos.org|baseurl=http://mirrors.aliyun.com|g' /etc/yum.repos.d/CentOS-*
3、更新yum源为阿里镜像
# https://mirrors.aliyun.com/repo/【Centos 版本】
$ wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo
4、清理之前的yum缓存,建立新的缓存 ( 此步报错请看报错三 )
# 清理之前的yum缓存
yum clean all
# 建立新的缓存
yum makecache
5、更新 yum 源软件包和内核
# 更新 yum 源软件包和内核
yum upgrade –y
# 重新安装 vim
yum -y install vim
3.3、报错三
报错三
建立新的缓存 yum makecache
报错:
http:// vault.centos.org/centos/7/sclo/x86_64/rh/repodata/repomd.xml: [Errno 14] HTTPS Error 404 - Not Found
原因:
http://vault.centos.org/centos/7/sclo/x86_64/rh/repodata/repomd.xml
这个地址 rh/
目录下什么文件都没有。
解决:
将配置文件中的这个地址换成阿里云的镜像地址。
[root@localhost yum.repos.d]# ll
总用量 16
-rw-r--r--. 1 root root 1759 7月 4 22:22 CentOS-Base.repo
-rw-r--r--. 1 root root 998 7月 4 22:17 CentOS-SCLo-scl.repo
-rw-r--r--. 1 root root 1107 7月 4 22:18 CentOS-SCLo-scl-rh.repo
-rw-r--r--. 1 root root 2081 5月 7 22:12 docker-ce.repo
1、检查 CentOS-Base.repo
文件中的地址是否都是阿里云的镜像地址: http://mirrors.aliyun.com/
# CentOS-Base.repo
#
# The mirror system uses the connecting IP address of the client and the
# update status of each mirror to pick mirrors that are updated to and
# 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, as a fall back you can try the
# remarked out baseurl= line instead.
#
[base]
name=CentOS-$releasever - Base - mirrors.aliyun.com
failovermethod=priority
baseurl=http://mirrors.aliyun.com/centos/$releasever/os/$basearch/
gpgcheck=1
gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7
#released updates
[updates]
name=CentOS-$releasever - Updates - mirrors.aliyun.com
failovermethod=priority
baseurl=http://mirrors.aliyun.com/centos/$releasever/updates/$basearch/
gpgcheck=1
gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7
#additional packages that may be useful
[extras]
name=CentOS-$releasever - Extras - mirrors.aliyun.com
failovermethod=priority
baseurl=http://mirrors.aliyun.com/centos/$releasever/extras/$basearch/
gpgcheck=1
gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7
#additional packages that extend functionality of existing packages
[centosplus]
name=CentOS-$releasever - Plus - mirrors.aliyun.com
failovermethod=priority
baseurl=http://mirrors.aliyun.com/centos/$releasever/centosplus/$basearch/
gpgcheck=1
enabled=0
gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7
#contrib - packages by Centos Users
[contrib]
name=CentOS-$releasever - Contrib - mirrors.aliyun.com
failovermethod=priority
baseurl=http://mirrors.aliyun.com/centos/$releasever/contrib/$basearch/
gpgcheck=1
enabled=0
gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7
2、检查 CentOS-SCLo-sclo.repo
文件中是否有报错的地址
# CentOS-SCLo-sclo.repo
#
# Please see http://wiki.centos.org/SpecialInterestGroup/SCLo for more
# information
[centos-sclo-sclo]
name=CentOS-7 - SCLo sclo
baseurl=http://mirror.centos.org/centos/7/sclo/$basearch/sclo/
#mirrorlist=http://#mirrorlist.centos.org?arch=$basearch&release=7&repo=sclo-sclo
gpgcheck=1
enabled=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-SIG-SCLo
[centos-sclo-sclo-testing]
name=CentOS-7 - SCLo sclo Testing
baseurl=http://buildlogs.centos.org/centos/7/sclo/$basearch/sclo/
gpgcheck=0
enabled=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-SIG-SCLo
[centos-sclo-sclo-source]
name=CentOS-7 - SCLo sclo Sources
baseurl=http://vault.centos.org/centos/7/sclo/Source/sclo/
gpgcheck=1
enabled=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-SIG-SCLo
[centos-sclo-sclo-debuginfo]
name=CentOS-7 - SCLo sclo Debuginfo
baseurl=http://debuginfo.centos.org/centos/7/sclo/$basearch/
gpgcheck=1
enabled=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-SIG-SCLo
3、检查 CentOS-SCLo-sclo.repo
文件中是否有报错的地址,可以看到报错的地址在这里,换成阿里云的镜像,镜像中存在repomd.xml文件即可。
# CentOS-SCLo-rh.repo
#
# Please see http://wiki.centos.org/SpecialInterestGroup/SCLo for more
# information
[centos-sclo-rh]
name=CentOS-7 - SCLo rh
# baseurl=http://vault.centos.org/centos/7/sclo/$basearch/rh/
# 上面的地址换成了下面这个地址
baseurl=http://mirrors.aliyun.com/centos/7/sclo/$basearch/rh/
# mirrorlist=http://#mirrorlist.centos.org?arch=$basearch&release=7&repo=sclo-rh
gpgcheck=1
enabled=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-SIG-SCLo
[centos-sclo-rh-testing]
name=CentOS-7 - SCLo rh Testing
baseurl=http://buildlogs.centos.org/centos/7/sclo/$basearch/rh/
gpgcheck=0
enabled=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-SIG-SCLo
[centos-sclo-rh-source]
name=CentOS-7 - SCLo rh Sources
baseurl=http://vault.centos.org/centos/7/sclo/Source/rh/
# baseurl=http://mirrors.aliyun.com/centos-vault/centos/7/sclo/Source/
gpgcheck=1
enabled=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-SIG-SCLo
[centos-sclo-rh-debuginfo]
name=CentOS-7 - SCLo rh Debuginfo
baseurl=http://debuginfo.centos.org/centos/7/sclo/$basearch/
gpgcheck=1
enabled=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-SIG-SCLo
最终结果:
4、还是存在报错二
最新的 centos:latest
镜像可能存在问题,更换为 centos:centos7
的镜像。
# 下载镜像 centos:centos7
[root@localhost docker]$ docker pull centos:centos7
centos7: Pulling from library/centos
2d473b07cdd5: Pull complete
Digest: sha256:c73f515d06b0fa07bb18d8202035e739a494ce760aa73129f60f4bf2bd22b407
Status: Downloaded newer image for centos:centos7
docker.io/library/centos:centos7
[root@localhost docker]$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos centos7 eeb6ee3f44bd 9 months ago 204MB
centos latest 5d0da3dc9764 9 months ago 231MB
FROM 修改为使用 centos:centos7
版本
sed "s/cen.*/centos:centos7/" dockerfile-centos > dockerfile-centos-7
5、构建镜像
4、通过这个文件 dockerfile-centos-7
构建镜像
[root@localhost dockerfile]$ cat dockerfile-centos-7
FROM centos:centos7
MAINTAINER mianbao<843818747@qq.com>
ENV MYPATH /usr/local
WORKDIR $MYPATH
RUN yum -y install vim \
&& yum -y install net-tools
EXPOSE 80
CMD echo $MYPATH
CMD echo "----end----"
CMD ["/bin/bash"]
[root@localhost dockerfile]$ docker build -f dockerfile-centos-7 -t mycentos:1.0 .
Sending build context to Docker daemon 3.072kB
Step 1/9 : FROM centos:centos7
# 由镜像ID得知使用的是本地已经下载了 centos:centos7 镜像。如果本地没有则去下载
---> eeb6ee3f44bd
......
Successfully built c333a356635e
Successfully tagged mycentos:1.0
[root@localhost dockerfile]$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mycentos 1.0 adfcc20188da 27 seconds ago 436MB
centos centos7 eeb6ee3f44bd 9 months ago 204MB
5、运行测试
[root@localhost dockerfile]$ docker run -it mycentos:1.0
[root@4634c75f0226 local]$ pwd
/usr/local # 工作目录。官方镜像是 / 默认是根目录
[root@4634c75f0226 local]$ vim test # 也能使用功能 vim 了
6、列出镜像地的变更历史
# docker history 镜像ID
[root@localhost dockerfile]$ docker history c333a356635e
IMAGE CREATED CREATED BY SIZE COMMENT
c333a356635e 26 minutes ago /bin/sh -c #(nop) CMD ["/bin/bash"] 0B
c9798bea2575 26 minutes ago /bin/sh -c #(nop) CMD ["/bin/sh" "-c" "echo… 0B
64352f6b6b9b 26 minutes ago /bin/sh -c #(nop) CMD ["/bin/sh" "-c" "echo… 0B
fc038771d877 26 minutes ago /bin/sh -c #(nop) EXPOSE 80 0B
b14cbe3e38b6 26 minutes ago /bin/sh -c yum -y install vim && yum -y ins… 232MB
9b3a136706ee 28 minutes ago /bin/sh -c #(nop) WORKDIR /usr/local 0B
c93b2bd0fb22 28 minutes ago /bin/sh -c #(nop) ENV MYPATH=/usr/local 0B
30a71766f1f1 28 minutes ago /bin/sh -c #(nop) MAINTAINER mianbao<843818… 0B
eeb6ee3f44bd 9 months ago /bin/sh -c #(nop) CMD ["/bin/bash"] 0B
<missing> 9 months ago /bin/sh -c #(nop) LABEL org.label-schema.sc… 0B
<missing> 9 months ago /bin/sh -c #(nop) ADD file:b3ebbe8bd304723d4… 204MB