docker compose 中的多阶段构建? [英] multi-stage build in docker compose?

查看:31
本文介绍了docker compose 中的多阶段构建?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在 docker-compose.yml 中指定多阶段构建?

How can I specify multi-stage build with in a docker-compose.yml?

对于每个变体(例如 dev、prod...),我有一个包含 2 个 docker 文件的多阶段构建:

For each variant (e.g. dev, prod...) I have a multi-stage build with 2 docker files:

  • dev: Dockerfile.base + Dockerfile.dev
  • 或 prod:Dockerfile.base + Dockerfile.prod
  • dev: Dockerfile.base + Dockerfile.dev
  • or prod: Dockerfile.base + Dockerfile.prod

文件 Dockerfile.base(所有变体通用):

File Dockerfile.base (common for all variants):

FROM python:3.6
RUN apt-get update && apt-get upgrade -y
RUN pip install pipenv pip
COPY Pipfile ./
# some more common configuration...

文件Dockerfile.dev:

FROM flaskapp:base
RUN pipenv install --system --skip-lock --dev
ENV FLASK_ENV development
ENV FLASK_DEBUG 1

文件Dockerfile.prod:

FROM flaskapp:base
RUN pipenv install --system --skip-lock
ENV FLASK_ENV production

没有 docker-compose,我可以构建为:

Without docker-compose, I can build as:

# Building dev
docker build --tag flaskapp:base -f Dockerfile.base .
docker build --tag flaskapp:dev -f Dockerfile.dev .
# or building prod
docker build --tag flaskapp:base -f Dockerfile.base .
docker build --tag flaskapp:dev -f Dockerfile.dev .

根据compose-file doc,我可以指定一个Dockerfile构建.

According to the compose-file doc, I can specify a Dockerfile to build.

# docker-compose.yml
version: '3'
services:
  webapp:
    build:
      context: ./dir
      dockerfile: Dockerfile-alternate

但是如何在 docker-compose.yml 中指定 2 个 Dockerfiles(用于多阶段构建)?

But how can I specify 2 Dockerfiles in docker-compose.yml (for multi-stage build)?

推荐答案

正如评论中提到的,多阶段构建涉及单个 Dockerfile 来执行多个阶段.您拥有的是一个通用的基础镜像.

As mentioned in the comments, a multi-stage build involves a single Dockerfile to perform multiple stages. What you have is a common base image.

您可以使用类似的语法将这些转换为非传统的多阶段构建(我说非传统是因为您不在层之间执行任何复制,而是仅使用 from 行从前一阶段中进行选择):

You could convert these to a non-traditional multi-stage build with a syntax like (I say non-traditional because you do not perform any copying between the layers and instead use just the from line to pick from a prior stage):

FROM python:3.6 as base
RUN apt-get update && apt-get upgrade -y
RUN pip install pipenv pip
COPY Pipfile ./
# some more common configuration...

FROM base as dev
RUN pipenv install --system --skip-lock --dev
ENV FLASK_ENV development
ENV FLASK_DEBUG 1

FROM base as prod
RUN pipenv install --system --skip-lock
ENV FLASK_ENV production

然后你可以使用 --target 语法构建一个或另一个阶段,或者像这样的组合文件:

Then you can build one stage or another using the --target syntax to build, or a compose file like:

# docker-compose.yml
version: '3.4'
services:
  webapp:
    build:
      context: ./dir
      dockerfile: Dockerfile
      target: prod

最大的缺点是当前的构建引擎会经历每个阶段,直到达到目标.构建缓存可能意味着这只是一个亚秒级的过程.而 BuildKit 将在 18.09 中完成实验并需要 docker-compose 的上游支持,它将更加智能地仅运行所需的命令来构建所需的目标.

The biggest downside is the current build engine will go through every stage until it reaches the target. Build caching can mean that's only a sub-second process. And BuildKit which is coming out of experimental in 18.09 and will need upstream support from docker-compose will be more intelligent about only running the needed commands to get your desired target built.

综上所述,我相信这是试图将方钉插入圆孔中.docker-compose 开发人员鼓励用户不要在 compose 文件本身中进行构建,因为它在 swarm 模式下不受支持.相反,推荐的解决方案是使用 CI/CD 构建服务器执行构建,并将这些映像推送到注册表.然后,您可以使用 docker-composedocker stack deploy 甚至一些 k8s 等价物运行相同的撰写文件,而无需重新设计您的工作流程.

All that said, I believe this is trying to fit a square peg in a round hole. The docker-compose developer is encouraging users to move away from doing the build within the compose file itself since it's not supported in swarm mode. Instead, the recommended solution is to perform builds with a CI/CD build server, and push those images to a registry. Then you can run the same compose file with docker-compose or docker stack deploy or even some k8s equivalents, without needing to redesign your workflow.

这篇关于docker compose 中的多阶段构建?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆