Docker Volumes.

Kasturi NithinKasturi Nithin
3 min read

When docker containers are deleted , the data associated with the container is lost, this is referred to as ephemeral storage. This means the container is temporary and will be removed whenever it is deleted.

To ensure the data persistency beyond the lifecycle of the container, Docker uses Volumes or bond mounts.

There are three types of volumes:

  1. Anonymous Volumes

  2. Named Volumes

  3. Bind Mounts

To understand them in detail clone this github repository .This is a simple repo , which consists of a node.js file which logs the timestamp into log.txt file for every 5 seconds.

Anonymous Volumes:

Anonymous volumes are automatically created and managed by Docker. These volumes are useful when you need temporary storage that will be removed along with the container.

Build the docker image and run the container using the commands:

  1. docker build -t NAME-OF-IMAGE .

  2. docker run -v /path-in-container NAME-OF-IMAGE

In this case -v is used to create anonymous volume which is mounted on /data directory inside the container.

So ,when you run the docker container current time stamp is logged into log.txt file inside the container.

To see the content of log.txt file follow the commands in your command prompt.

  1. docker ps

  2. Copy the CONTAINER ID and execute the command “docker exec -it CONTAINER ID /bin/sh“

  3. “cd data”

  4. “cat log.txt“

  5. Then you will be able to find all the recorded logs till now.

Now, if you restart the container , the data inside the container persists but if you delete the container the data will be completely lost . After deleting the container if you try to access the log.txt it fails as illustrated below.

Named Volumes:

A named volume in Docker is a persistent storage mechanism that you explicitly name and manage. Unlike anonymous volumes, which are automatically created and cleaned up by Docker, named volumes allow you to refer to a specific storage location by name and reuse it across different containers. This makes it easier to share data between containers and persist data even after a container is deleted.

Build the docker image and run the container using the commands:

  1. docker build -t NAME-OF-IMAGE .

  2. docker run -v NAME-OF-VOLUME:/path-in-container NAME-OF-IMAGE

  • Name of volume: This is the name of the volume. If it doesn’t exist, Docker will create it.

  • path in container: This is the directory inside the container where the volume is mounted.

Now, if you delete the container, you can still get the data by using the name of the volume.

Bind Mounts:

Bind mounts allow you to specify a path on the host machine to link directly to a path inside the container. This gives the container access to the specified host directory or file.

Build the docker image and run the container using the commands:

  1. docker build -t NAME-OF-IMAGE .

  2. docker run -v path-to-volume-in-host:/path-in-container NAME-OF-IMAGE

  • path to volume in host : This is the name of the volume. That is to be created in host machine.

  • path in container : This is the directory inside the container where the volume is mounted.

When you run the container , you can notice that the data gets logged into the path which you have specified in the docker run command[path-to-volume-in-host].

0
Subscribe to my newsletter

Read articles from Kasturi Nithin directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Kasturi Nithin
Kasturi Nithin

Exploring something new.