Reputation: 305
I have the Dockerfile file below:
FROM ubuntu:focal
LABEL maintainer="campos <[email protected]>"
# Update the system and install the necessary packages
RUN apt update && apt -y install nano
# Create directory
RUN mkdir /testdir
RUN touch /testdir/file.txt
RUN chmod 777 /testdir/file.txt
VOLUME ["/testdir/"]
# Copy the message "Hello World!"
COPY hello.txt /testdir/file.txt
CMD /bin/bash
I create the image from the Dockerfile:
docker build -t imgtest .
Then I create three containers. In the first example, when I enter the container, I can see the file.txt file in /testdir
. In the second example, when mapping the volume /data /test:/testdir
, I can't see the file.txt file. In the third example, when mapping /data /var:/var
, I can't see the files in the folder.
Example 1:
docker run -d -it --name cont-test-01 --hostname cont-test-01 --restart=always imgtest
docker exec -it cont-test-01 /bin/bash
Example 2:
docker run -d -it --name cont-test-02 --hostname cont-test-02 --restart=always -v /data/test:/testdir imgtest
docker exec -it cont-test-02 /bin/bash
Example 3:
docker run -d -it --name cont-test-03 --hostname cont-test-03 --restart=always -v /data/var:/var imgtest
docker exec -it cont-test-03 /bin/bash
How can I change the Dockerfile so that files from volumes I created and from system folders like /var
appear in the container and mapped folder of the docker host?
Upvotes: 1
Views: 1457
Reputation: 16554
AS @F1ki said, if you use the default volume feature -v /source:/target
, the content of target will be replaced with /source. Just new files created by your container after the startup in /target will be available in your /source folder
But if your goal is to have the already existent and new files of your container in a folder on your host, you could use a more advanced volume feature:
volume creation(config_vol)
docker volume create --driver local \
--opt type=none \
--opt device=/data/test \
--opt o=bind \
config_vol
use the created volume
docker run -it --name cont-test-04 -v config_vol:/testdir imgtest
After that, you could see your file.txt in your host folder /data/test
I tested with /var
and I was able to get all its folder/files on my host dir:
Upvotes: 1
Reputation: 4274
In the first example, when I enter the container, I can see the file.txt file in /testdir
So the first example seems to work exactly as you expect it to work.
In the second example, when mapping the volume /data /test:/testdir, I can't see the file.txt file.
Well that is because you are overwriting the folder:
$ docker run -d -it --name cont-test-02 --hostname cont-test-02 --restart=always -v /data/test:/testdir imgtest
By executing -v /data/test:/testdir
you are mounting everything under /data/test
on your host to the container /testdir
, effectively deleting everything that has been in /testdir
before that. When using something like -v /data/test:/testdir2
you would have /testdir
and /testdir2
accessible in your container at the same time.
In the third example, when mapping /data /var:/var, I can't see the files in the folder.
Which files in what folder are you referring to?
As explained above by using -v /data/var:/var
you are mounting everything from your host /data/var
to the container /var
. If you do not see any files inside the containers /var
than you most likely do not have any files in /data/var
on your host.
Execute the following on your host and you should see a file called bar.txt
inside the containers /var
folder:
mkdir -p /data/var/
echo "foo" > bar.txt
docker run -d -it --name cont-test-03 --hostname cont-test-03 --restart=always -v /data/var:/var imgtest
Upvotes: 1