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.)
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.
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”]
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With