Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use multi-stage docker files for outputting multiple images

Tags:

A new docker feature is to do something like this in the dockerfile

FROM php7-fpm as build
...

FROM build AS test
...

FROM test AS staging
...

As far as i know, the last FROM statement marks the final output image. How is it possible to have two final images from one intermdiate image?

Like

...
FROM build AS test
...
FROM test AS staging
...
FROM test AS prod

Test, staging and prod should not be discarded. I want to check in them into the repository.

like image 260
Jim Panse Avatar asked Feb 28 '18 11:02

Jim Panse


People also ask

How do I pull multiple images in Docker?

By default, docker pull pulls a single image from the registry. A repository can contain multiple images. To pull all images from a repository, provide the -a (or --all-tags ) option when using docker pull .

Can a Dockerfile build multiple images?

A multistage build allows you to use multiple images to build a final product. In a multistage build, you have a single Dockerfile, but can define multiple images inside it to help build the final image.

What is multi-stage Docker file?

With multi-stage builds, you use multiple FROM statements in your Dockerfile. Each FROM instruction can use a different base, and each of them begins a new stage of the build. You can selectively copy artifacts from one stage to another, leaving behind everything you don't want in the final image.

Why use multi-stage Docker builds?

A multistage Docker build process makes it possible to build out an application and then remove any unnecessary development tools from the container. This approach reduces the container's final size, but it is a complex process.


1 Answers

You can stop the build at a certain stage and tag them as you want.

docker build --target test -t starx/test:latest .
docker build --target staging -t starx/staging:latest .
docker build --target prod -t starx/prod:latest .

This way, you have different images and you can push each image individually.

like image 55
Starx Avatar answered Sep 30 '22 19:09

Starx