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.
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
|Category||Requirements, Conventions or Software Version Used|
|System||Any Linux distro|
|Other||Privileged access to your Linux system as root or via the
# – requires given linux commands to be executed with root privileges either directly as a root user or by use of
$ – requires given linux commands to be executed as a regular non-privileged user
Share data between Docker container and host
Consider the following example.
- 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
- Next, we run a docker container and use the
-voption 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
$PWD/environment variable which returns a full path to a current working directory:
# docker run -v $PWD/data1:/opt/data1 -it debian /bin/bash
- The result of the above command is that we can now access our previously created file
file1from 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.
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.
If you are encountering the following error:
FATA 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
# docker run --privileged=true -v $PWD/data1:/opt/data1 -it debian /bin/bash