If you are using Docker for anything more than just playing around, you are likely using volumes. Volumes are great, but its easy to f*ck something up and lose data. Don't get caught with your pants down, let's go over some ways to backup your Docker volumes!
Application Specific
You're going to hate me for this one, but the best way is to not backup Docker volumes. Instead, backup only the data you need, with the tools you're already using.
If you are running Postgres, you can simply backup your database using the pg_dump
command. If you are running mongodb, you can use the mongodump
command. These commands will usually give you a lot more control over what you're backing up and can even give you some consistency guarantees that a normal backup won't. If you can, use the right tool for the job!
There are also existing container solutions that wrap tools like pg_dump
that make it a bit easier. For example docker-pg-backup by Kartoza.
Creating a backup can be as simple as running a single command:
docker run --name="backups" --link db:db -v `pwd`/backups:/backups -d kartoza/pg-backup:$POSTGRES_MAJOR_VERSION-$POSTGIS_MAJOR_VERSION.${POSTGIS_MINOR_RELEASE}
Local Backup
Next is something that a lot of people would not consider a backup, but its good enough if you do some local development and don't need a super robust solution.
If your volume is named my-data
, you can copy everything to a new volume called my-data-backup
:
docker volume create --name my-data-backup
docker container run --rm -it \
-v my-data:/from \
-v my-data-backup:/to \
ubuntu bash -c "cd /from ; cp -av . /to"
This will create a new volume with the same data as the original. You can then use this new volume as a backup! You might have to change the /from
and /to
paths depending on what you're backing up.
Remember, this will not protect you from a catastrophic event like your server burning down 🤖🔥.
Hosting Provider
If you use a modern PaaS provider like Sliplane or Render, you can simply use the backup feature of them. This is great if you dont want to worry about managing a backup solution, but is potentially a single point of failure and can cost extra.
Sliplane for example offers free volume backups for production servers, with no limit on the size of the volumes.
Disclaimer: I'm the co-founder, so I obviously think this is good 😆
Upload to S3 with Offen
Offen is a self-hosted backup solution that can backup to a variety of different cloud providers, including S3. It can backup Docker volumes with a simple command:
docker run --rm \
-v data:/backup/data \
--env AWS_ACCESS_KEY_ID="<xxx>" \
--env AWS_SECRET_ACCESS_KEY="<xxx>" \
--env AWS_S3_BUCKET_NAME="<xxx>" \
--entrypoint backup \
offen/docker-volume-backup:v2
Offen is pretty awesome, we even use it at Sliplane to backup volumes. They have great documentation with a bunch of examples and support for a variety of different cloud providers. I would suggest simply backing it up to S3. Make sure to also use the client side encryption to keep your data safe!
The configuration documentation is here.
Conclusion
There you have it! 5 ways to backup your Docker volumes. Use the one that makes the most sense for you and your use case. If you have any other great solutions, please share them in the comments below!
Thanks for reading, and happy backing up!
Cheers, Jonas
Co-Founder of sliplane.io
Top comments (6)
where is the 5th way? 🤠 (see conclusion)
5th way are the suggestions from readers :P
the 5th way is memorizing every single bit 🧠
My 5th way is a script that takes the backup from the container, copies from the container to the local server and then uploads to a cloud storage
If you need to backup your containers, you're using them wrong. If that's your use case you should have a proper operating system, which have the tools required. Just my 2 bits.
Im talking about volumes here, not containers. Or wdym with that?