Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to dynamically change the docker's base image

I have two dockerfiles, which are exactly same with the only difference being the FROM statement. The problem I have is whenever I modify something in one file I have to do the same in another, because they have to be the same.

One solution for this would be to pass base image as parameter to docker build command. I don't know if that is possible.

Another would be to have some kind of include files mechanism. In that case I would implement all common steps in a separate file which I would just include in my Dockerfile.

like image 974
Dragan Nikolic Avatar asked Sep 23 '15 17:09

Dragan Nikolic


People also ask

Can Dockerfile take argument?

You can use the ARG command inside a Dockerfile to define the name of a parameter and its default value. This default value can also be overridden using a simple option with the Docker build command.

What is base image in Dockerfile?

A base image is the image that is used to create all of your container images. Your base image can be an official Docker image, such as Centos, or you can modify an official Docker image to suit your needs, or you can create your own base image from scratch. Parent topic: Docker.

How do I tag an image in Dockerfile?

Docker tags are just an alias for an image ID. The tag's name must be an ASCII character string and may include lowercase and uppercase letters, digits, underscores, periods, and dashes. In addition, the tag names must not begin with a period or a dash, and they can only contain 128 characters.


1 Answers

From Docker version 17.05, you can do something like this:

ARG MYAPP_IMAGE=myorg/myapp:latest
FROM $MYAPP_IMAGE

You can provide MYAPP_IMAGE as a command line paramether:

docker build -t container_tag --build-arg MYAPP_IMAGE=localimage:latest .

More info here: https://www.jeffgeerling.com/blog/2017/use-arg-dockerfile-dynamic-image-specification

like image 141
Anton Krosnev Avatar answered Sep 19 '22 19:09

Anton Krosnev