Bobcares

How to share data between Docker Container and Host?

by | Sep 9, 2020

Docker volumes help us to share data between a Docker container and a host system.

As a part of our Server Management Services, we help our Customers with Docker related tasks regularly.

Let us today discuss the possible steps to perform this task.

How to share data between the docker container and host?

By default, the data created inside the Docker container is only available within the container. Also, it is available only while the container is running.

Docker volumes can be used to share files between a host system and the Docker container.

Let us today discuss the steps to make data inside the container accessible on the host machine.

Creating the Host Data Volume

The first step here is to create a new directory to house the volume. To do this, open a terminal window and issue the command:

We must ensure that the newly-created directory is in a location the Docker user can access with read-write privilege.

Now, we need to mount a volume inside it. Let’s say we are going to deploy a container, based on the official Ubuntu image, that contains a directory called /data. To deploy such a container that attaches the internal /data directory to a volume within the host directory ~/container-data, we can issue the command:

 

Once the command completes, we will be given a container ID. We can also find the container ID with the command below:

docker ps -a

 

Testing the Volume

Let’s now test this volume. We can access the newly-deployed container with the command:

Where ID is the first four characters of the deployed container. Issue the command ls / to verify that the /data directory added to the Ubuntu container. Let’s create a test file in that directory with the command:

touch /data/test

After creating this test file, open another terminal window on the host machine and issue the command ls ~/container-data. We will see the test file within that directory.

Database Volume

Let’s now discuss the steps to create a volume for a database. We can do this by first deploying a MySQL database Docker container and instructing it to use a persistent storage volume named mysql-data. This can be done with the command:

In the above command, the -e switch informs docker what follows is an environment variable.

To verify the contents, access the bash prompt for the container with the command:

Now, list out the contents of the container’s /var/lib/mysql directory with the command:

Make note of those contents and exit from the container with the command:

Now, check the contents of the host’s mounted volume with the command:

The listing of both directories will be the same. Now have a MySQL-based container which includes persistent storage mounted in a volume on a host computer.

[Need any further assistance with Docker errors? – We’re available 24*7]

 

Conclusion

In short, Docker volumes help us to share data between a Docker container and a host system. Today, we saw how our Support Engineers perform this task.

 

 

PREVENT YOUR SERVER FROM CRASHING!

Never again lose customers to poor server speed! Let us help you.

Our server experts will monitor & maintain your server 24/7 so that it remains lightning fast and secure.

GET STARTED

var google_conversion_label = "owonCMyG5nEQ0aD71QM";

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *

Never again lose customers to poor
server speed! Let us help you.