DEV Community

Docker Volumes: An Introduction

Stephen Afam-Osemene on February 04, 2018

Docker volumes are very useful when we need to persist data in Docker containers or share data between containers. Docker volumes are important ...
Collapse
 
cduv profile image
DUVERGIER Claude

I don't understand that part:

If we start a container that creates a new volume, or mount an empty directory to a destination in the container that contains files, then the files in the destination are copied into the volume or empty directory.

It means that with the following image:

FROM busybox
RUN echo "foo" > /data/bar
VOLUME /data
CMD ["/bin/sh"]

Built and executed as follows

docker build -t my-sample-image .
mkdir -p /tmp/my-temp-dir
docker run -d --name my-sample \
    -v  /tmp/my-temp-dir:/data \
    my-sample-image

I would have a file bar in /tmp/my-temp-dir?
I don't know for Docker volumes but I doubt the container's /data/bar file which was created during the docker build (making /data/bar part of the Docker image basically) would be back-copied into host's /tmp/my-temp-dir directory...

Usually when you mount host's /tmp/my-temp-dir as container's /data/bar, any content of container/image's /data/bar is replaced. That's why so many images have a setup/fill part in their entrypoint's script.

Collapse
 
stephenafamo profile image
Stephen Afam-Osemene

You are correct, and I have edited the article to reflect this. Thank you.