Docker Conditional Assembly Image

I need to execute the same script two docker images.

My Dockerfile :

 FROM centos:6 ... 

and

 FROM centos:7 ... 

Is it possible to have one file and pass a parameter, for example:

 FROM centos:MYPARAMS 

and during build something like:

 docker build --no-cache MYPARAMS=6 . 

thanks

+5
source share
3 answers

As far as I know, this is not possible with Docker.

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

+4
source

Just to express this in the right context, now (since May 2017) you can achieve this with a clean docker from 05.17 ( https://github.com/moby/moby/pull/31352 )

The Docker file should look like (yes, the commands in that order):

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

Then build is called using

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

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

Helped me unlimitedly reduce the logic of creating images.

+4
source

At https://github.com/BITPlan/docker-stackoverflowanswers/tree/master/33351864 you will find a bash script "build" that works the way you want.

 wf@mars :~/source/docker/docker-stackoverflowanswers/33351864>./build -v 6 Sending build context to Docker daemon 3.584 kB Step 0 : FROM centos:6 6: Pulling from library/centos fa5be2806d4c: Pull complete ebdbe10e9b33: Downloading 4.854 MB/66.39 MB ... wf@mars :~/source/docker/docker-stackoverflowanswers/33351864>./build -v 7 Sending build context to Docker daemon 3.584 kB Step 0 : FROM centos:7 

The essential part is the document used here:

 # # parameterized dockerfile # dockerfile() { local l_version="$1" cat << EOF > Dockerfile FROM centos:$l_version EOF } 
+2
source

Source: https://habr.com/ru/post/1234545/


All Articles