docker多阶段构建离线yum源(修改)

有需要就会有需求

一、目录结构

二、Dockerfile

#########################################################################################
FROM centos:7.4.1708 as build0
ENV EPEL_RELEASE=http://mirrors.aliyun.com/repo/epel-7.repo \
    DOCKER_REPO=https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
COPY pkg.list /
RUN mkdir /rpms \
    && yum install -y  yum-utils \
    && yum-config-manager --add-repo $EPEL_RELEASE \
    && yum-config-manager --add-repo $DOCKER_REPO \
    && yum install -y --downloadonly --downloaddir=/rpms $(cat /pkg.list)

#########################################################################################
FROM centos:7.5.1804 as build1
ENV EPEL_RELEASE=http://mirrors.aliyun.com/repo/epel-7.repo \
    DOCKER_REPO=https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
COPY pkg.list /
RUN mkdir /rpms \
    && yum install -y  yum-utils \
    && yum-config-manager --add-repo $EPEL_RELEASE \
    && yum-config-manager --add-repo $DOCKER_REPO \
    && yum install -y --downloadonly --downloaddir=/rpms $(cat /pkg.list)

#########################################################################################
FROM centos:7.6.1810 as build2
ENV EPEL_RELEASE=http://mirrors.aliyun.com/repo/epel-7.repo \
    DOCKER_REPO=https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
COPY pkg.list /
RUN mkdir /rpms \
    && yum install -y  yum-utils  createrepo \
    && yum-config-manager --add-repo $EPEL_RELEASE \
    && yum-config-manager --add-repo $DOCKER_REPO \
    && yum install -y --downloadonly --downloaddir=/rpms $(cat /pkg.list)

COPY --from=build0 /rpms/* /rpms/
COPY --from=build1 /rpms/* /rpms/
RUN createrepo /rpms/

#########################################################################################	
FROM nginx:alpine
#RUN mkdir /usr/share/nginx/html/rpms
COPY --from=build2 /rpms/ /usr/share/nginx/html/rpms/
COPY index.html /usr/share/nginx/html/
COPY nginx.conf /etc/nginx/conf.d/default.conf

EXPOSE 80

三、pkg.list

docker-ce-19.03.4
docker-python 
docker-compose 
python-chardet 
python-requests
chrony audit rsync jq git tcpdump nc bind-utils net-tools ipvsadm graphviz tree lrzsz

四、nginx.conf

server {
    listen 80;
    server_name localhost;
    location / {
        root /usr/share/nginx/html;
        index index.html;
        autoindex on;
    }
    
    error_page 500 502 503 504  /50x.html;
    location = 50x.html {
        root /usr/share/nginx/html;
    }
}

五、index.html

<!DOCTYPE html>
<html>
	<head>
		<title>Wise2C Yum Repo for Docker/K8S/Ceph/NFS installation</title>
<style>
    body {
	            width: 35em;
			            margin: 0 auto;
					            font-family: Tahoma, Verdana, Arial, sans-serif;
							        }
</style>
	</head>
	<body>
		<h1>Wise2C Yum Repo for Docker/K8S/Ceph/NFS installation</h1>
		<p>If you see this page, the nginx web server is successfully installed and
		working. Further configuration is required.</p>

		<p>For online documentation and support please refer to
		<a href="http://nginx.org/">nginx.org</a>.<br/>
		Commercial support is available at
		<a href="http://nginx.com/">nginx.com</a>.</p>

		<p><em>Thank you for using nginx.</em></p>

		<br />
		<br />

		<a href = "rpms" >Enter the RPM folder </a><br />

		<br />
		<br />

		<p><em>Please create the file wise2c.repo as below and then move to /etc/yum.repos.d/</em></p>

	<script type="text/javascript">
		  var ip = location.host;
  var comment = "###############################################";
  document.write(comment.fontcolor("Red"));
  document.write("<pm><em></em></p>");
  document.write("[wise2c]"+"<br>");
  document.write("name=wise2c"+"<br>");
  document.write("baseurl=http://"+(ip)+"/rpms"+"<br>");
  document.write("enabled=1"+"<br>");
  document.write("gpgcheck=0"+"<br>");
  document.write("<pm><em></em></p>");
  document.write(comment.fontcolor("Red"));
	</script>

	</body>
</html>

六、最终效果:

docker build -t offline-yum:v0.1 .
docker run -d --name offline-yum -p 80:80 offline-yum:v0.1