Sharing data between the host and the container
In the previous recipe, we used a named volume to persist application data. We also learned that named volumes can be used to share data between containers. In this recipe, we will use bind mounting to mount a Docker host directory to a container and then share data between the Docker host and container using that mount point.
Getting ready
Before we begin, ensure that the Docker daemon is running.
How to do it...
Perform the following steps:
Let's begin by creating a new directory called
data_share
in our home directory, as shown in the following code:
$ mkdir $HOME/data_share
- Create a new file in the
$HOME/data_share
directory of the Docker host and write some text in it:
$ echo "data sharing demo" > $HOME/data_share/demo.txt
- Now, launch a container by mounting the
$HOME/data_share
directory and then print the content of thedemo.txt
:
$ docker container run --rm \ -v $(HOME)/data_share:/data \ ubuntu cat /data/demo.txt...