Multiple images, one Dockerfile
You can use a docker-compose
file using the target
option:
version: '3.4'
services:
img1:
build:
context: .
target: img1
img2:
build:
context: .
target: img2
using your Dockerfile
with the following content:
FROM alpine as img1
COPY file1.txt .
FROM alpine as img2
COPY file2.txt .
You can build multiple images from one Docker file by running docker build
for each build stage name
want to pick out of the Dockerfile (FROM alpine as name
).
By default docker builds the image from the last command in the Dockerfile, however you can target the build to an intermediate layer. Extending from the example Dockerfile in the question, one can:
docker build -t image1 --target img1 .
ref: https://docs.docker.com/engine/reference/commandline/build/#specifying-target-build-stage---target