Docker Data Volumes

Docker Data Volumes

 Docker Data Volumes


In docker, data volumes are useful for managing your data with Docker containers and host machines. Here you will find two way of managing data volumes on Docker containers.

  1. Using the data volume container.
  2. Using share data between the host and the Docker container

#1. Use Docker Data Volume Container

The Docker data volume container is the same as other containers, but they are just used for storage only. You can use storage of these containers in your containers. When you write data to your container's file system, it will be originally written to a storage container.

Create Data Volume Container:

$ docker create -v /data --name data_container centos

The above command will launch a container with the name “data_container” having data volume at /data. The container will stop immediately due to no service. Now create your application container with –volumes-from flag.

Use Data Volume to New Container:

$ docker run -it --volumes-from data_container centos /bin/bash

This will provide you shell access to the newly created container. Now use /data directory to create any number of files and directories on it.

[root@28b55eb5e032 /]# cd /data
[root@28b55eb5e032 /]# echo "Hello Docker" > file.txt

Now you can exit from the current instance. Also, you're remote this using the docker remove command. After that launch a new instance using the same data volume and try to access the files created under /data directory.

Verify Files on Data Volume:

Let’s launch another container with the same data volumes and check available files on /data directory.

$ docker run -it --volumes-from data_container centos /bin/bash


#2. Sharing Host Volume to Docker Container

You can also share data between the host machine and the docker container. For example, you can share /var/www directory of the host machine to /data directory. You can share any directory of the host machine to a container.

$ docker run -it -v /var/www:/data centos

Here /var/www is a directory on the host machine. Which is mounted on /data directory in Docker container.

Reactions

Post a Comment

0 Comments

close