In partnership with

Docker containers are bad at persisting data.

Why?

Because they are designed to be easily disposed of and recreated.

When you remove a container, the data inside is also gone.

This is a huge problem if you are running something that needs data persistence, like a database.

Docker storage solves this problem.

Why Docker Storage

Think of it as a backup.

If you have played any mobile game in a guest mode...

When you delete the app… (usually when you realize that it's taking up too much of your time, when you could be doing something more productive)

Your progress will be gone because your data isn't saved anywhere other than your device.

But if you connect it to a game account, your account data is safe even if your device explodes.

Because your data is saved on the server, you can access your account from any other device.

It's the same thing in Docker.

  • The container is your local device. Saving data there isn't safe.

  • The host machine (that runs the containers) is the backup server. Saving data there is much safer.

Using Docker Storage

For example, let's say you are running a Postgres container.

To create a space for backup, create a volume (the most common Docker storage):

docker volume create myVolume

A volume is just a folder in the host machine. Docker manages the exact location.

When you run the container, attach the volume.

docker run --volume <volume-name>:<mount-path> postgres:17

What's mount-path?

It's the location where the data is stored in the container.

For a database, it's where your tables are stored.

The exact path is:

#mount-path: /var/lib/postgresql/data
docker run --volume myVolume:/var/lib/postgresql/data postgres:17

What this does is mount the volume to a folder in the container.

Whenever you change the folder, like creating new tables, you are actually writing to the volume.

Now, let’s say you need to upgrade your base image and remove the container. The data will not disappear. It’s safely stored in the volume.

When you need to use the data again, simply attach the volume to the new container.

docker run --volume myVolume:/var/lib/postgresql/data postgres:18

Fee from Anime Coders

PS: If you want to stay competitive as a developer by adding Docker to your resume, try Gacha Coders, a Gacha game that teaches you how to code.

Ghost: Free Postgres For Agents

Agents are desperate for ephemeral databases.

They spin up projects, fork environments, test ideas, and tear them down. Over and over. But every database on the market was designed for humans who provision once and stick around. Agents don't work that way.

Ghost is a database built for agents. Unlimited databases, unlimited forks, 1 TB of storage, and 100 compute hours per month. All free. Try it here.

Keep Reading