Docker组合和外部映像的多阶段构建 [英] Docker compose and external images multi-stage builds

查看:101
本文介绍了Docker组合和外部映像的多阶段构建的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 Docker多阶段构建,具体是:


使用外部图片作为舞台



阶段构建,您
不仅限于从您先前在
Dockerfile中创建的阶段复制。您可以使用COPY --from指令从
单独的映像进行复制,方法是使用本地映像名称,本地或在Docker注册表中可用的
的标签或标签ID。 Docker客户端在必要时提取
映像,并从那里复制工件。语法
是:

Use an external image as a "stage"

When using multi-stage builds, you are not limited to copying from stages you created earlier in your Dockerfile. You can use the COPY --from instruction to copy from a separate image, either using the local image name, a tag available locally or on a Docker registry, or a tag ID. The Docker client pulls the image if necessary and copies the artifact from there. The syntax is:

在我的情况下,我有三个Docker文件。

In my case I have three Docker files.

一个Dockerfile只是定义了一个映像,我将其用作构建阶段以在其他两个Dockerfile中共享:

One Dockerfile simply defines an image, which I use as a build stage to share among two other Dockerfiles:

FROM ubuntu:bionic

## do some heavy computation that will result in some files that I need to share in 2 Dockerfiles

,并假设我构建了上述Dockerfile:

and suppose that I build the above Dockerfile giving:

docker build -t my-shared- build -f path / to / shared / Dockerfile。

所以现在我有一个 my-shared-build <我在两个容器之间共享的/ code>映像,它们的dockerfile看起来像:

So now I have a my-shared-build image that I share between two containers, their dockerfiles look like:

FROM ubuntu:bionic

# do something

COPY --from=my-shared-build some/big/folder /some/big/folder

CMD ["/my-process-one"]



FROM ubuntu:bionic

# do something

COPY --from=my-shared-build some/big/folder /some/big/folder

CMD ["/my-process-two"]

我可以构建并运行它们。

And I can build and run them.

因此,我现在要做的是:

So to recap, what I currently do is:

1)构建共享图像
2)构建过程一图像
3)构建过程二图像

1) Build the shared image 2) Build the process one image 3) Build the process two image

现在我可以运行处理一个和处理两个容器。

now I can just run the "process one" and "process two" containers.

现在,我想要使用Docker Compose来自动执行进程一和进程二。

Now I would like to use Docker Compose to automate the execution of "process one" and "process two".

所以问题是:如何在Docker Compose中指定我需要首先构建共享映像,然后然后构建其他映像(然后运行它们的容器)?

So the question is: how can I specify in the Docker Compose that I need to first build the shared image and then build the other images (and then run their containers) ?

推荐答案

我认为您应该能够做到这一点。

I think you should be able to do that just fine.

docker-compose:

docker-compose:

version: '3'
services:
    my-shared-build:
        image: my-shared-build:latest
        build: my-shared-build

    my-process-one:
        image: my-process-one:latest
        build: my-process-one
        depends_on:
            - my-shared-build

    my-process-two:
        image: my-process-two:latest
        build: my-process-two
        depends_on:
            - my-shared-build
            - my-process-one

假设您的Dockerfile位于子目录my-shared-build,my-process中-一个,我的进程-两个,这应该构建所有3张图像(按顺序)

Assuming your Dockerfiles are in subdirectories my-shared-build, my-process-one, my-process-two this should build all 3 images (in order)

这篇关于Docker组合和外部映像的多阶段构建的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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