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:
mkdir ~/container–data
|
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:
|
docker run –dit –P —name ubuntu–test –v ~/container–data:/data ubuntu
|
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:
|
docker attach ID
|
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:
|
docker run —name mysql–test –v mysql–data:/var/lib/mysql –e MYSQL_ROOT_PASSWORD=passwd –d mysql:latest
|
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:
|
docker exec –it ID /bin/bash
|
Now, list out the contents of the container’s /var/lib/mysql directory with the command:
|
ls /var/lib/mysql
|
Make note of those contents and exit from the container with the command:
|
exit
|
Now, check the contents of the host’s mounted volume with the command:
|
sudo ls /var/lib/docker/volumes/mysql–data/_data
|
[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.
0 Comments