2

Is it possible to provide a file to a docker container during the run operation to be used during startup of the container?

Essentially I want to provide a txt file similar to plugins.txt in jenkins docker to a tomcat server. That file should contain names and versions (name:version) of .war and .jar files to be downloaded during startup of the container. I expect of this setup to be more flexible to provide customized containers for customers rather than having customer specific images. But the big questions for me are:

  1. Can I provide such a file during startup of a container?
  2. How should I handle versioning of the file and/or container to be able to check what the customer specific container contains?
  3. Is there anything else I should consider?
st.huber
  • 1,481
  • 2
  • 24
  • 45

2 Answers2

8

You can mount individual files into a container as a volume:

docker run -v `pwd`/conf.txt:/app/conf.txt my_app

That needs to be run on the docker host itself, or under a User directory on Windows/MacOS. You can also create a named volume in docker where you can store these files and update via temporary containers.

With those volume mounts, you do need to be careful with permissions, uid's inside the container may not match your uid on the host. You also have the ability to mount the files read only with a simple :ro appended to the volume mount.

BMitch
  • 231,797
  • 42
  • 475
  • 450
4

There are 3 ways to provide config data to a container.

  1. At build time

    Add it to the Dockerfile. Eg:

    COPY ./config.txt /var/local/myapp/
    

    You need to create a new image everytime you change your config. It's also a bad idea to use this technique if you plan to store passwords, certificates or other secrets.

  2. Pass by environments

    docker run -e name1=version1 -e name2=version2 imagename
    

    This is a good solution for passing a few parameters. Not suitable to pass whole files though.

  3. Pass by files

    If you're using Docker for Mac/Windows or Docker Toolbox (VirtualBox), you can mount a local directory into your docker container as described by @BMitch.

    For production systems your host will be remote so you will need to copy the file to either the host or a volume on the host. Eg:

    # Copy to host
    docker-machine scp config.txt host-name:/dir/on/host
    

    or copy to a volume on the host which I described in this other answer.

The versioning can be handled by setting an environment variable. Eg:

docker run --name myservice -e VERS=2.0.1 myimage

which you can retrieve at run time using the docker api. Eg:

docker inspect --format "{{index (index .Config.Env) 0 }}" myservice

This prints

VERS=2.0.1
Community
  • 1
  • 1
Bernard
  • 16,149
  • 12
  • 63
  • 66