问题
I use Docker multi-stage build, specifically:
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:
In my case I have three Docker files.
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
and suppose that I build the above Dockerfile giving:
docker build -t my-shared-build -f path/to/shared/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) 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.
Problem
Now I would like to use Docker Compose to automate the execution of "process one" and "process two".
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) ?
回答1:
I think you should be able to do that just fine.
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
Assuming your Dockerfiles are in subdirectories my-shared-build, my-process-one, my-process-two this should build all 3 images (in order)
来源:https://stackoverflow.com/questions/54207856/docker-compose-and-external-images-multi-stage-builds