Dockerfile优化及本地私有仓库搭建
文章目录
引言:Docker 官方提供了一个搭建私有仓库的镜像 registry ,运行该镜像的容器并且对外暴露5000端口就ok了。通常我们在docker拉取的镜像都是在docker hub或quay.io等公有仓库获取,那么在实际工作中,每个公司如果使用到docker,那么肯定是要搭建自己的私有仓库。那么接下来就通过docker提供的registry镜像来搭建我们自己的私有仓库。
一、Dockerfile优化
未优化前
mkdir /nginx
FROM centos:7
RUN yum install -y gcc pcre pcre-devel devel zlib-devel make
ADD nginx-1.15.9.tar.gz /mnt
WORKDIR /mnt/nginx-1.15.9
#关闭debug日志
RUN sed -i 's/CFLAGS="$CFLAGS -g"/#CFLAGS="$CFLAGS -g"/g' auto/cc/gcc
RUN ./configure --prefix=/usr/local/nginx
RUN make
RUN make install
EXPOSE 80
VOLUME ["/usr/local/nginx/html"]
CMD ["/usr/local/nginx/sbin/nginx","-g","daemon off;"]
1.将不需要输出的指令丢入/dev/null(需要确定命令执行的是正确的)
减少不必要的缓存(丢到黑洞)删除不必要的缓存文件/磁盘文件
FROM centos:7
RUN yum install -y gcc pcre pcre-devel devel zlib-devel make &> /dev/null && yum clean all
ADD nginx-1.15.9.tar.gz /mnt
WORKDIR /mnt/nginx-1.15.9
#关闭debug日志
RUN sed -i 's/CFLAGS="$CFLAGS -g"/#CFLAGS="$CFLAGS -g"/g' auto/cc/gcc
RUN ./configure --prefix=/usr/local/nginx &> /dev/null
RUN make &> /dev/null
RUN make install &> /dev/null
RUN rm -rf /mnt/nginx-1.15.9
EXPOSE 80
VOLUME ["/usr/local/nginx/html"]
CMD ["/usr/local/nginx/sbin/nginx""-g","daemon off;"]
docker build -t nginx:v1 .
2.减少RUN构建
可以使用&&连接
FROM centos:7
ADD nginx-1.15.9.tar.gz /mnt
WORKDIR /mnt/nginx-1.15.9
RUN yum install -y gcc pcre pcre-devel devel zlib-devel make &> /dev/null && \
yum clean all && \
sed -i 's/CFLAGS="$CFLAGS -g"/#CFLAGS="$CFLAGS -g"/g' auto/cc/gcc && \
./configure --prefix=/usr/local/nginx &> /dev/null && \
make &> /dev/null && make install &> /dev/null &&\
rm -rf /mnt/nginx-1.15.9
EXPOSE 80
VOLUME ["/usr/local/nginx/html"]
CMD ["/usr/local/nginx/sbin/nginx","-g","daemon off;"]
docker build -f Dockerfile -t nginx:v3 .
3.多阶段构建(使用FROM命令生成多个镜像,将指定的镜像做为其镜像的基础镜像环境来构建)
FROM centos:7 as build
ADD nginx-1.15.9.tar.gz /mnt
WORKDIR /mnt/nginx-1.15.9
RUN yum install -y gcc pcre pcre-devel devel zlib-devel make &> /dev/null && \
yum clean all &&\
sed -i 's/CFLAGS="$CFLAGS -g"/#CFLAGS="$CFLAGS -g"/g' auto/cc/gcc && \
./configure --prefix=/usr/local/nginx &> /dev/null && \
make &>/dev/null && \
make install &>/dev/null && \
rm -rf /mnt/nginx-1.15.9
FROM centos:7
EXPOSE 80
VOLUME ["/usr/local/nginx/html"]
COPY --from=build /usr/local/nginx /usr/local/nginx
CMD ["/usr/local/nginx/sbin/nginx","-g","daemon off;"]
4.使用更为轻量级的linux发行版本
- debian
- alpine
- apt add
- Sandbox
- Busybox
二、本地私有仓库搭建
1.先下载registry镜像
docker pull registry
2.在daemon.json文件中添加私有镜像仓库的地址并重启
vim /etc/docker/daemon.json
{
"insecure-registries": ["192.168.223.30:5000"], #添加此段,“,”不可少,ip是本机IP地址
"registry-mirrors": ["https://4abdkxlk.mirror.aliyuncs.com"]
}
systemctl daemon-reload
systemctl restart docker.service
3.创建registry容器并开放端口
docker create -it registry /bin/bash
docker run -d -p 5000:5000 -v /data/registry:/tmp/registry registry
'//-p指定端口,一内一外;-v表示挂载,前者是宿主机,后者是容器'
4.给镜像打标签后上传到私有仓库
必须先给镜像打标签再上传,因为默认是传到公共仓库
打标签
docker tag nginx:latest 192.168.50.128:5000/nginx_123
上传
docker push 192.168.50.128:5000/nginx_123
5.获取私有仓库列表查看是否上传成功
curl -XGET http://192.168.50.128:5000/v2/_catalog
若成功会返回以下值
{"repositories":["nginx_123"]}
6.验证:从私有仓库下载镜像
先删除原有的镜像,再从本地仓库进行下载测试
#删除原有镜像
docker rmi 192.168.50.128:5000/nginx_123:latest
docker images 查看镜像
docker pull 192.168.50.128:5000/nginx_123