Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Docker Conditional build image

I have to execute the same script to two docker images.

My Dockerfile are:

FROM centos:6
...

and

FROM centos:7
...

Is it possibile to have a single file and pass a parameter, something like:

FROM centos:MYPARAMS

and during the build somethings like that:

docker build --no-cache MYPARAMS=6  .

Thank you

like image 837
Middle Avatar asked Oct 26 '15 17:10

Middle


2 Answers

Just to put this in right context, it is now (since May 2017) possible to achieve this with pure docker since 17.05 (https://github.com/moby/moby/pull/31352)

Dockerfile should look like (yes, commands in this order):

ARG APP_VERSION
ARG GIT_VERSION
FROM app:$APP_VERSION-$GIT_VERSION

Then build is invoked with

docker build --build-arg APP_VERSION=1 --build-arg GIT_VERSION=c351dae2 .

Docker will try to base the build on image app:1-c351dae2

Helped me immensely to reduce logic around building images.

like image 88
Jan Oudrnicky Avatar answered Oct 12 '22 15:10

Jan Oudrnicky


From my knowledge, this is not possible with Docker.

The alternative solution is to use a Dockerfile "template", and then parse it using the template library of your choice. (Or even using sed command)

like image 24
Maxime Avatar answered Oct 12 '22 13:10

Maxime