1

I have the Dockerfile file below:


FROM ubuntu:focal
LABEL maintainer="campos <joledal387@ncstorms.com>"

# 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?

campos
  • 153
  • 2
  • 12

2 Answers2

1

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
F1ko
  • 3,326
  • 1
  • 9
  • 24
1

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:

enter image description here

Tips & docs

JRichardsz
  • 14,356
  • 6
  • 59
  • 94
  • I did the test above and it works correctly. After creating the container with the command `docker volume ls` , I can see the volume `config_vol`. But in some Internet examples, it is not necessary to create a volume when the docker host folder and container folder are mapped. In this case `docker volume ls` returns nothing. – campos Aug 01 '21 at 16:52
  • An example where this occurs: `docker run -d --name=cont-db-01 --hostname=cont-db-01 \ -v /data/db/mysql:/var/lib/mysql \ --network=db-network \ -e MYSQL_ROOT_PASSWORD=root_password \ -e MYSQL_DATABASE=mydatabase \ -e MYSQL_USER=myuser \ -e MYSQL_PASSWORD=myuser_password \ mysql:5.7` – campos Aug 01 '21 at 16:53
  • I wanted `/testdir` and `/var` to be mounted on the docker host with all their content similar to the example above. How the example mapping works is a mystery to me. I can see everything in `/data/db/mysql`. – campos Aug 01 '21 at 17:00
  • When I remove `config_vol` as `docker volume rm config_vol`, the `/data/test` folder is preserved. Is there any way through docker cli to remove the volume and the folder associated with it? – campos Sep 02 '21 at 14:38