How to Mount a Host Directory in a Docker Container?
If you want to mound a host directory in a Docker container, you have to main ways to do that:
Using the ADD
command:
The simplest way is to use the dockers ADD command as shown below:
ADD . /path/inside/docker/container
This command will copy the files from the current working directory to the path in the docker container specified in the command.
However, any changes made to this directory on the host after building the dockerfile will not show up in the container. This is because when building a container, docker compresses the directory into a .tar
and uploads that context into the container permanently.
Mount a volume
The second way to do this is to mount a volume. Due to docker trying to be as portable as possible, you cannot map a host directory to a docker container directory within a dockerfile, because the host directory can change depending on which machine you are running on. To map a host directory to a docker container directory you need to use the -v
flag when using docker run
docker run \
-v /tmp:/container/directory \
<container> \
ls /container/directory
If you are new to Docker, feel free to start with our Getting started logging guide.
-
How to Include Files outside of Docker’s Build Context?
You may have ended up in a situation where you wanted to include a file from outside of Docker's build context using the ADD command, but the ADD command requires the path to be within the build co...
Questions -
How to Mount a Host Directory in a Docker Container?
If you want to mound a host directory in a Docker container, you have to main ways to do that: Using the ADD command: The simplest way is to use the dockers ADD command as shown below: ADD . /path/...
Questions -
What Is the Difference between CMD and ENTRYPOINT in a Dockerfile?
There is a big confusion around similarity and lack of clarity in the difference between the CMD and ENTRYPOINT instruction in Docker. Let’s clear things up. See the example Let’s say we want to cr...
Questions -
How to Remove a Docker Image?
To remove one or more specific images, you can use the docker rmi command. How to list all images If you want to list all the images before removing any, you can do they by running the following co...
Questions
Make your mark
Join the writer's program
Are you a developer and love writing and sharing your knowledge with the world? Join our guest writing program and get paid for writing amazing technical guides. We'll get them to the right readers that will appreciate them.
Write for usBuild on top of Better Stack
Write a script, app or project on top of Better Stack and share it with the world. Make a public repository and share it with us at our email.
community@betterstack.comor submit a pull request and help us build better products for everyone.
See the full list of amazing projects on github