I'm in the situation where I have a bunch of images which come from different sources, but I need to apply the same steps to them. Basically it looks like this:
# dockerfile 1
FROM source1
RUN foo
# dockerfile 2
FROM source2
RUN bar
# dockerfile 3
FROM source3
RUN baz
Given these images, I need to apply some common steps, eg copy certificates, install commonly-used packages, set environment variables, etc.
ENV http_proxy http://proxy-server-address
ENV https_proxy http://proxy-server-address
ADD certs/* /usr/local/share/ca-certificates/
RUN apt install something -y
RUN pip install something-else
# etc
Is there a good way to do this for all the images, without duplicating all the steps into each dockerfile? That would be a maintenance headache, since every time I change a step, I have to make sure I do it for all the files.
CodePudding user response:
yes
you can create a shared Dockerfile and take the source image as a build argument
FROM ${BASE_IMAGE}
# common steps
you can then call it for everyone of your builds with
docker build ... --build-args BASE_IMAGE=<img_name>
EDIT:
this means you call docker build twice for each image
once with the different dockerfiles foo, bar, baz and you should tag it as foo_intermediate
etc
and then with the common dockerfile were the base image is foo_intermediate
and tag it the way you need e.g. foo
CodePudding user response:
Best practice in this case is creating Dockerfile for base image where you put all the common steps.
In specific Dockerfiles you extend base image using FROM base
and put image specific directives.
See sample base image Dockerfile here and specific image Dockerfile extending it here.