5

I want to create Dockerfile that will be able to build three different images (one at the moment). Those images differs only in configuration files.

I was trying:

  • creating three different docker files (doesn't work because dockerfile needs to be named Dockerfile and be in context root
  • finding a way of passing parameters to docker build command - didn't find
  • using ONBUILD command - I created one main file and three specific ones which were copying specific conf file into same directory in image (it doesn't work because of the same reason as in first point)
  • passing docker file from stdin - it doesn't work because there is no context then (so I can't use ADD/COPY commands then)

I must say I run out of ideas:/ How do you handle that situation. In my opinion it should be a common issue.

user2963977
  • 572
  • 5
  • 17
  • 2
    Consider a single image with the runtime configuration passed in as environment settings. That way you can create an image in Dev, certify the same image in test and be confident it runs the same way in production. Creating a new binary artifact for every deployment is an anti-pattern in my opinion. – Mark O'Connor Jan 27 '15 at 22:51
  • I've posted a more general question: http://stackoverflow.com/questions/35733166/dockerfile-production-build-debug-test-environment – Vanuan Mar 01 '16 at 20:53

3 Answers3

5

Why not copy in your Dockerfile all 3 config files, and then docker run -e config=file1 (or file2 or file3) and use the value of config environment variable to get the required config file .Check the doc http://docs.docker.com/reference/commandline/cli/#run

You can also use the --env-file=[] Read in a line delimited file of environment variables of the docker run command.

You can check that environment variable is available with docker run -it -e mytag=abc123 ubuntu:latest env | grep mytag shows mytag=abc123

user2915097
  • 30,758
  • 6
  • 57
  • 59
0

If you want 3 different images, use 3 different Dockerfiles. The naming issue is annoying, but you can write a helper script that simply copies files and calls docker build e.g.

cp Dockerfile-cfg1 Dockerfile
docker build -t "cfg1" .
cp Dockerfile-cfg2 Dockerfile
docker build -t "cfg2" .

However, as @user2915097 suggests, a better solution is probably to have a single image and choose the config at run-time. This means less overhead of looking after and maintaining multiple images.

Adrian Mouat
  • 44,585
  • 16
  • 110
  • 102
0

I have added three different files:

test.Dockerfile
dev.Dockerfile
prod.Dockerfile

and I have run them using the file name:

docker build -t name/name -f test.Dockerfile .
docker build -t name/name -f dev.Dockerfile .
docker build -t name/name -f prod.Dockerfile .
Piotr Żak
  • 2,083
  • 6
  • 29
  • 42