How to share data between a Docker container and host system using volumes

The easiest way to share data between a Docker container and the host system is to use Docker’s volumes. In this guide, we will go through the step by step instructions of sharing files between a Docker container and host system using Docker volumes via the command line on Linux.

DID YOU KNOW?
Docker volumes work similarly to bind mounts, but are the preferred method for sharing data between a host system and Docker container because outside applications are not able to access the files and modify them.

In this tutorial you will learn:

  • How to use volumes to share data between a Docker container and host system

Sharing data from the host system to a Docker container

Sharing data from the host system to a Docker container
Software Requirements and Linux Command Line Conventions
Category Requirements, Conventions or Software Version Used
System Any Linux distro
Software Docker
Other Privileged access to your Linux system as root or via the sudo command.
Conventions # – requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command
$ – requires given linux commands to be executed as a regular non-privileged user

Share data between Docker container and host



Consider the following example.

  1. First, on a host system we create a directory with a single file we would like to share it with a docker container:
    # mkdir data1
    # echo "Docker volume share" > data1/file1
    
  2. Next, we run a docker container and use the -v option to mount a local host system directory data1 to the container’s directory /opt/data1. Please note that if the destination does not exist, it will be created by the docker command. Furthermore, docker only accepts a full path to a local host system directory and for this reason we need to prefix the data1 directory with $PWD/ environment variable which returns a full path to a current working directory:
    # docker run -v $PWD/data1:/opt/data1 -it debian /bin/bash
    
  3. The result of the above command is that we can now access our previously created file file1 from within a container:
    # cat /opt/data1/file1 
    Docker volume share
    

That’s all there is to it. You can now share any files you want between the host system and Docker containers by using the mounted volume. If you are encountering any errors, please see the troubleshooting section below.

Conclusion

In this guide, we learned how to share data between a host system and Docker container by using volumes. This is the best and easiest way to share data between the two systems on Linux, only requiring a few short steps.



Troubleshooting

If you are encountering the following error:

FATA[0000] Error response from daemon: cannot bind mount volume: data1 volume paths must be absolute.

This means you need to supply a full path to both source and destination directories. Hint: the full path always starts with /.

You may also encounter this permission denied error:

# ls /opt/data1/ 
ls: cannot open directory /opt/data1/: Permission denied

This error is caused by SElinux running on your local host system. The following two solutions will help to solve this issue. First, solution is to disable SElinux on your local host system.

# setenforce 0

Since disabling the SElinux may hinder an integrity of your host system it may be easier to give an extended privileges to your container instead, with a docker --privileged=true option:

# docker run --privileged=true -v $PWD/data1:/opt/data1 -it debian /bin/bash


Comments and Discussions
Linux Forum