A Docker container is like a virtual machine. It essentially permits you to run a pre-bundled “Linux box” inside a container. The primary difference between a Docker container and a virtual machine is that a Docker container shares the Linux kernel with the host operating system, which implies it doesn’t have to “boot” the way a virtual machine would.

The major point of concern is non-persistent filesystem of a Docker Container. You can perform all the operations the way you do in a virtual machine but once you exit the Docker Container all modified data will be lost. In order to save changes you are required to create a new Docker images which includes all the changes you made. Ideally it is not a recommended way to create a new image every time you modify the data in a Docker Container. Alternatively you can provide a Docker Data Volume as a separate place to store the data.

In this post we will be covering the process to

  • Present Docker Data Volumes to a Docker Container.
  • Present persistence storage location on host to a Docker Container

A data volume is a specially-designated directory within one or more containers that bypasses Dockers Union File System. Data volumes provide several useful features for persistent or shared data:

  • Volumes are initialized when a container is created.
  • Data volumes can be shared and reused among containers.
  • Changes to a data volume are made directly.
  • Changes to a data volume will not be included when you update an image.
  • Data volumes persist even if the container itself is deleted.

Presenting Docker Data Volume to a Docker Container

The first step is to , create a new data volume container to store our volume. To create a new Docker Data Volume, execute the below command.

docker create -v /datavolumes –name persistentstorage¬† ubuntu

Executing above command will create a container named persistentstorage using ubuntu image and in the directory /datavolumes.

1

Now we can attach the Docker Data Volume to a new container using –volumes-from flag

docker run -t -i –volumes-from persistentstorage ubuntu /bin/bash

  • -t : calls a terminal from inside the container
  • -i : makes the terminal interactive.

2

You can locate the volume on the host by utilizing the docker inspect command.
$ docker inspect <docker_container_id>
The output will provide details on the container configurations including the volumes. The output should look something similar to the following:

3

Mount a host directory as a data volume

In addition to creating a volume using the -v flag you can also mount a directory from your docker host into a container. This helps administrator to share files between host machine and the Docker Container. It is not required to create a Docker Data Volume for mounting a host directory as a data volume. You can run a container of any Docker image and override one of its directories with the contents of a directory on the host system.

4

We’ve now covered the process to create data volume containers and mounting that data volume to different docker container images as well as how to share folders between the host filesystem and a Docker container. This solves the¬†major point of concern of non-persistent filesystem in a Docker Container. I hope this is informative for you. Thanks for Reading!!. Be social and share it in social media, if you feel worth sharing it