rajdeep@portfolio — zsh
$
building0%

press any key to skip

Data Persistence in Docker 🐳 Container

Rajdeep Sengupta

Published on : Aug 12, 2023

Introduction

Generally we don’t want to persist data or a dependency for a container.

But there are three main ways to do this.

  • Include them in the image
  • Volume mounts
  • Bind mounts

Including the things in the image is very simple. You can use a docker build command like this to prepare tour container.

This command updates the image and also installs iputils-ping package while building it.

Now let’s under stand binding an external volume. ( volume mount )

We can bind a volume to a single docker container. We can use volumes and mounts to safely persist the data.

Volumes and mounts allow us to specify a location where data should persist beyond the lifecycle of a single container. There is also a thing called “tempfs mount” but this does not persist data, this areas when we exit the container. A good example of tempfs mount can be storing credential files.

Now how to bind a volume ?

Step 1 : Create a volume.

You can use the below command to create a volume.

bash

docker volume create my-volume

Step 2 : Attaching the volume to your container

You can use one of these below commands to attach/ mount the volume you just created to a simple ubuntu container.

Note: We add —rm flag so that the container deletes after we exit form it.

bash

# Create a container and mount the volume into the container filesystem
docker run -it --rm --mount source=my-volume, destination=/my-data/ ubuntu: 22.04

# There is a similar (but shorter) syntax using -v which accomplishes the same
docker run -it --rm -v my-volume:/my-data buntu: 22.04

Step 3 : Storing data to the volume

Let’s create a simple hello.txt in the “my-data” folder, which is the attached volume. And now we can exit the app.

bash

# Now we can create and store the file into the location we mounted the volume

echo "Hello from the container!' > /my-data/hello. txt cat my-data/hello.txt

Step 4 : Creating a new container an testing

Now we can create a new container with the same volume.

If we do cat now on the same exact path we can see the file persists even this is a different new container.

bash

# Create a new container and mount the volume into the container filesystem

docker run -it --rm --mount source=my-volume, destination=/my-data/ ubuntu: 22.04 cat my-data/hello.txt # This time it succeeds!
exit

here is this data located? On linux it would be at /var/lib/docker/volumes... but remember, on docker desktop, Docker runs a linux virtual machine.

Now lets see Bind Mounts :

We can also mount a directory from the host system using a bind mount. This is simple.

You can run these set of commands do so. Here {PWD} is path of the directory.

bash

# Create a container that mounts a directory from the host filesystem into the container
docker run -it --rm --mount type=bind, source="${PWD]" /my-data, destination=/my-data ubuntu: 2

# Again, there is a similar (but shorter) syntax using -v which accomplishes the same
docker run -it --rm -v ${PWD} /my-data: /my-data ubuntu: 22.04
echo "Hello from the container!" > /my-data/hello.txt

# You should also be able to see the hello.txt file on your host system
cat my-data/hello.txt exit

Bind mounts can be nice if you want easy visibility into the data being stored, but there are a number of reasons outlined at Docker official website / Docs (including speed if you are running Docker Desktop on windows/mac) for why volumes are preferred.

All thanks to DevOpsDetective YT channel where I learn all of these. A channel I can refer anyone who is into DevOps. Follow me if you are into DevOps and Cloud native application.