我正在一个码头容器中运行我的单核应用程序,并在GKE上运行k8s。
应用程序包含python &节点依赖项,前端包也包含webpack。
我们已经实现了CI/CD,它花费了5-6分钟的时间来构建和部署新版本的k8s集群。
主要目标是尽可能减少构建时间。书面Dockerfile是多阶段的。
Webpack正在花费更多的时间来生成我使用的已经很高的配置工作人员的bundle.To buid码头映像。
为了减少时间,我尝试使用Kaniko建筑商。
发行:
作为python代码的docker缓存层,它正在完美地工作。但是,当JS
或CSS
文件发生任何更改时,我们必须生成包。
当JS
& CSS
文件发生任何变化时,如果生成新的包,则使用缓存层。
是否有任何方法通过向docker文件传递一些值来分离、构建新的包或使用缓存。
这是我的码头文件:
FROM python:3.5 AS python-build
WORKDIR /app
COPY requirements.txt ./
RUN pip install -r requirements.txt &&\
pip3 install Flask-JWT-Extended==3.20.0
ADD . /app
FROM node:10-alpine AS node-build
WORKDIR /app
COPY --from=python-build ./app/app/static/package.json app/static/
COPY --from=python-build ./app ./
WORKDIR /app/app/static
RUN npm cache verify && npm install && npm install -g --unsafe-perm node-sass && npm run sass && npm run build
FROM python:3.5-slim
COPY --from=python-build /root/.cache /root/.cache
WORKDIR /app
COPY --from=node-build ./app ./
RUN apt-get update -yq \
&& apt-get install curl -yq \
&& pip install -r requirements.txt
EXPOSE 9595
CMD python3 run.py
发布于 2019-09-11 07:11:44
我建议为您的码头映像创建单独的构建管道,在这里,您知道npm和pip的需求不那么频繁。这将极大地提高速度,缩短进入npm和pip注册中心的时间。
使用专用的坞注册中心( 官方一号或类似于VMWare港或SonaType Nexus的东西)。
您可以将这些构建映像存储在注册表中,并在项目发生更改时使用它们。
就像这样:
python-builder:YOUR_TAG // [gitrev,date等]
docker build --no-cache -t python-builder:YOUR_TAG -f Dockerfile.python.build .
FROM python:3.5
WORKDIR /app
COPY requirements.txt ./
RUN pip install -r requirements.txt &&\
pip3 install Flask-JWT-Extended==3.20.0
js-builder:YOUR_TAG // [gitrev,date等]
docker build --no-cache -t js-builder:YOUR_TAG -f Dockerfile.js.build .
FROM node:10-alpine
WORKDIR /app
COPY app/static/package.json /app/app/static
WORKDIR /app/app/static
RUN npm cache verify && npm install && npm install -g --unsafe-perm node-sass
您的应用程序多阶段构建
docker build --no-cache -t app_delivery:YOUR_TAG -f Dockerfile.app .
FROM python-builder:YOUR_TAG as python-build
# Nothing, already "stoned" in another build process
FROM js-builder:YOUR_TAG AS node-build
ADD ##### YOUR JS/CSS files only here, required from npm! ###
RUN npm run sass && npm run build
FROM python:3.5-slim
COPY . /app # your original clean app
COPY --from=python-build #### only the files installed with the pip command
WORKDIR /app
COPY --from=node-build ##### Only the generated files from npm here! ###
RUN apt-get update -yq \
&& apt-get install curl -yq \
&& pip install -r requirements.txt
EXPOSE 9595
CMD python3 run.py
一个问题是:为什么要安装curl
并在最终的停靠映像中再次执行pip install -r requirements.txt
命令?每次触发apt-get update
并在不清理apt缓存/var/cache/apt
文件夹的情况下安装,都会产生更大的映像。
根据建议,使用带有选项--无缓存的docker命令来避免缓存结果:
docker build --no-cache -t your_image:your_tag -f your_dockerfile .
备注
您将有3个独立的Dockerfile,正如我上面所列出的。如果您更改了您的和node-npm
需求,则只构建Docker映像1和2,否则将为您的项目修复它们。如果任何依赖项需求发生变化,则更新所涉及的对接者映像,然后更新多级映像以指向最新构建的映像。
您应该始终只构建项目的源代码(CSS、JS、python)。通过这种方式,您还保证了可重复的构建。
要优化您的环境并在多阶段构建器中复制文件,请尝试使用virtualenv
进行python。
https://stackoverflow.com/questions/57883224
复制相似问题