在Docker中进行多阶段构建? [英] multi-stage build in docker compose?

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

问题描述

如何在 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

  • 或产品: 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 .

根据撰写文件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中指定2个Dockerfile .yml (用于多阶段构建)?

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-compose docker 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中进行多阶段构建?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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