Py学习  »  docker

带webpack的Monolith docker应用程序

Harsh Manvar • 4 年前 • 787 次点击  

我在docker容器中运行monolith应用程序,在GKE上运行k8s。

应用程序包含python&node dependencies,还包含前端包的webpack。

主要目标是尽可能减少构建时间。写入的Dockerfile是多阶段的。

Webpack需要更多的时间来生成包。要生成docker映像,我正在使用已经很高的配置工作程序。

为了减少我尝试使用 Kaniko builder .

问题:

作为docker的python代码缓存层,它工作得很好。但是当 JS CSS

当有任何变化 JS公司 &安培; 如果生成新的包,则改为使用缓存层。

有没有办法通过向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
Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/55102
 
787 次点击  
文章 [ 1 ]  |  最新文章 4 年前
madduci
Reply   •   1 楼
madduci    4 年前

我建议为docker映像创建单独的构建管道,在这里您知道npm和pip的需求并不那么频繁。 这将极大地提高速度,减少访问npm和pip注册表的时间。

official one 或者类似的 VMWare harbor SonaType Nexus OSS ).

像这样的:

第一个Docker生成器 python builder:你的标签 [吉特里夫,日期等)

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

// [吉特里夫,日期等)

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 build命令和选项 --没有缓存 为了避免缓存结果:

docker build --no-cache -t your_image:your_tag -f your_dockerfile .

评论 :

你将有3个独立的dockerfile,如我上面列出的。 只有 如果你改变你的 python-pip node-npm 要求,否则请为项目固定它们。 如果任何依赖性需求改变,则更新所涉及的docker图像,然后更新多级图像以指向最新构建的图像。

要优化环境并跨多阶段生成器复制文件,请尝试使用 virtualenv