docker构建镜像缓慢
电脑版发表于:2023/2/14 17:46
前言
dockerfile 在加载环境的时候十分缓慢,有些时候还需要下载一些依赖,往往这些依赖是国外的,就算配置了国内加速下载环境也得几分钟。
所以,有没有什么办法避免每次都去重新构建一个环境呢,我想到的是将所有依赖的东西都整理成一个基础镜像,再将这个镜像上传到阿里云,每次重新拉去这个基础镜像,这样速度会不会快一点
实测:会快
构建基础镜像的DockerFile(net6.0 和 libgdiplus的环境)
FROM mcr.microsoft.com/dotnet/aspnet:6.0 AS base ENV TZ=Asia/Shanghai RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone # 切换apt源为网易 RUN echo "" > /etc/apt/sources.list \ && echo "deb http://mirrors.163.com/debian/ stretch main non-free contrib" >> /etc/apt/sources.list \ && echo "deb http://mirrors.163.com/debian/ stretch-updates main non-free contrib" >> /etc/apt/sources.list \ && echo "deb http://mirrors.163.com/debian/ stretch-backports main non-free contrib" >> /etc/apt/sources.list \ && echo "deb-src http://mirrors.163.com/debian/ stretch main non-free contrib" >> /etc/apt/sources.list \ && echo "deb-src http://mirrors.163.com/debian/ stretch-updates main non-free contrib" >> /etc/apt/sources.list \ && echo "deb-src http://mirrors.163.com/debian/ stretch-backports main non-free contrib" >> /etc/apt/sources.list \ && echo "deb http://mirrors.163.com/debian-security/ stretch/updates main non-free contrib" >> /etc/apt/sources.list \ && echo "deb-src http://mirrors.163.com/debian-security/ stretch/updates main non-free contrib" >> /etc/apt/sources.list # 安装gdiplus RUN apt-get update -y && apt-get install -y libgdiplus && apt-get clean && ln -s /usr/lib/libgdiplus.so /usr/lib/gdiplus.dll RUN sed -i 's/TLSv1.2/TLSv1/g' /etc/ssl/openssl.cnf
执行命令
docker build -f 【dockerfile路径】 -t basenet6:v1 .
完成后基础镜像就在本地构建好了
如何推送到阿里云,请看阿里云的文档,写的很详细
以前的dockerFile
FROM mcr.microsoft.com/dotnet/aspnet:6.0 AS base WORKDIR /app/Basicapplication EXPOSE 80 COPY . . ENV TZ=Asia/Shanghai RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone # 切换apt源为网易 RUN echo "" > /etc/apt/sources.list \ && echo "deb http://mirrors.163.com/debian/ stretch main non-free contrib" >> /etc/apt/sources.list \ && echo "deb http://mirrors.163.com/debian/ stretch-updates main non-free contrib" >> /etc/apt/sources.list \ && echo "deb http://mirrors.163.com/debian/ stretch-backports main non-free contrib" >> /etc/apt/sources.list \ && echo "deb-src http://mirrors.163.com/debian/ stretch main non-free contrib" >> /etc/apt/sources.list \ && echo "deb-src http://mirrors.163.com/debian/ stretch-updates main non-free contrib" >> /etc/apt/sources.list \ && echo "deb-src http://mirrors.163.com/debian/ stretch-backports main non-free contrib" >> /etc/apt/sources.list \ && echo "deb http://mirrors.163.com/debian-security/ stretch/updates main non-free contrib" >> /etc/apt/sources.list \ && echo "deb-src http://mirrors.163.com/debian-security/ stretch/updates main non-free contrib" >> /etc/apt/sources.list # 安装gdiplus RUN apt-get update -y && apt-get install -y libgdiplus && apt-get clean && ln -s /usr/lib/libgdiplus.so /usr/lib/gdiplus.dll RUN sed -i 's/TLSv1.2/TLSv1/g' /etc/ssl/openssl.cnf ENV ASPNETCORE_URLS=http://+:80 ENTRYPOINT ["dotnet", "BasicApplication.dll","--server.urls","http://*:80"]
用基础镜像的地址替换一大堆引用(现在的dockerfile)
FROM 【刚刚构建的阿里云基础镜像,记得加版本号】 WORKDIR /app/Basicapplication EXPOSE 80 COPY . . ENV ASPNETCORE_URLS=http://+:80 ENTRYPOINT ["dotnet", "BasicApplication.dll","--server.urls","http://*:80"]