1

How to have default config file (that I copy to image during build) that I can override with -v option (or else), during container run?

The details:

Imagine I have following directives in Dockerfile:

COPY nginx.conf /etc/nginx/nginx.conf
VOLUME ["/etc/nginx/nginx.conf"]

I want that nginx used copied nginx.conf if no (single file) volume specified - like a default config, that always in the image. And if I specify volume - container will use provided config file instead of copied to image.

So main idea is to have default config that I can override.

Of course following solution is not working, and gives an error. But may be you know how to achieve similar behavior?

nwinkler
  • 52,665
  • 21
  • 154
  • 168
pro100sanya
  • 147
  • 1
  • 9
  • your `VOLUME` directive will erase what you just copied using `COPY` it seems to me... – user2915097 Mar 20 '17 at 18:15
  • Nope, docker will raise an error during image build: `Step 20/25 : VOLUME /etc/nginx/nginx.conf cannot mount volume over existing file, file exists` – pro100sanya Mar 21 '17 at 07:15
  • You can put another layer on it and again do the COPY nginx.conf /etc/nginx/nginx.conf with your desired configuration. – h__ Mar 21 '17 at 07:43
  • Possible duplicate of [Mounting nginx conf as a docker volume causes system error boot2docker](http://stackoverflow.com/questions/31763172/mounting-nginx-conf-as-a-docker-volume-causes-system-error-boot2docker) – nwinkler Mar 21 '17 at 07:44
  • Please see the linked question for an example of how to mount the config file into the container. I suggest you remove the `VOLUME` line from your Dockerfile and try it without that. You should still be able to mount a local file into the container when running it. – nwinkler Mar 21 '17 at 07:45
  • @nwinkler Seem to be it is working, thank you. If you will provide detailed answer I will accept it. or i can answer this question by myself. What do you prefer? – pro100sanya Mar 21 '17 at 09:09
  • 1
    Posted as an answer, please see below. Glad to hear that it's working! – nwinkler Mar 21 '17 at 09:16
  • @nwinkler Thank you! – pro100sanya Mar 21 '17 at 09:25

1 Answers1

1

It should work if you remove the VOLUME part from your Dockerfile. You can then overwrite the default configuration file when you run the container.

So in your Dockerfile, just put this:

COPY nginx.conf /etc/nginx/nginx.conf

Then when you run the container, map in a local file with your configuration changes:

docker run -v ./nginx.conf:/etc/nginx/nginx.conf:ro ...

This will allow you to provide a default configuration, but will also let you override it if required.

nwinkler
  • 52,665
  • 21
  • 154
  • 168