Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Overwrite files with `docker run`

Tags:

docker

Maybe I'm missing this when reading the docs, but is there a way to overwrite files on the container's file system when issuing a docker run command?

Something akin to the Dockerfile COPY command? The key desire here is to be able to take a particular Docker image, and spin several of the same image up, but with different configuration files. (I'd prefer to do this with environment variables, but the application that I'm Dockerizing is not partial to that.)

like image 655
Breedly Avatar asked Feb 29 '16 17:02

Breedly


People also ask

How do I overwrite an entry docker run?

You can use one or combine both depending on how you want to run your container. One difference is that unlike CMD , you cannot override the ENTRYPOINT command just by adding new command line parameters. To override ENTRYPOINT you need to modify the docker run command following a specific syntax.

Does docker run override CMD?

An essential feature of a CMD command is its ability to be overridden. This allows users to execute commands through the CLI to override CMD instructions within a Dockerfile. A Docker CMD instruction can be written in both Shell and Exec forms as: Exec form: CMD [“executable”, “parameter1”, “parameter2”]


1 Answers

You have a few options. Using something like docker-compose, you could automatically build a unique image for each container using your base image as a template. For example, if you had a docker-compose.yml that look liked:

container0:
  build: container0

container1:
  build: container1

And then inside container0/Dockerfile you had:

FROM larsks/thttpd
COPY index.html /index.html

And inside container0/index.html you had whatever content you wanted, then running docker-compose build would generate unique images for each entry (and running docker-compose up would start everything up).

I've put together an example of the above here.

Using just the Docker command line, you can use host volume mounts, which allow you to mount files into a container as well as directories. Using my thttpd as an example again, you could use the following -v argument to override /index.html in the container with the content of your choice:

docker run -v index.html:/index.html larsks/thttpd

And you could accomplish the same thing with docker-compose via the volume entry:

container0:
  image: larsks/thttpd
  volumes:
    - ./container0/index.html:/index.html

container1:
  image: larsks/thttpd
  volumes:
    - ./container1/index.html:/index.html

I would suggest that using the build mechanism makes more sense if you are trying to override many files, while using volumes is fine for one or two files.

A key difference between the two mechanisms is that when building images, each container will have a copy of the files, while using volume mounts, changes made to the file within the image will be reflected on the host filesystem.

like image 192
larsks Avatar answered Oct 16 '22 19:10

larsks