Back Up (Docker) Containers
Reviewed in November 2026
Docker and related container tools like Podman have greatly simplified the work of sysadmins. Instead of installing dependencies on the operating system, they can now be isolated in a “container”. This helps with isolation and updates. At the same time, containers are relatively light because processes share the host OS and kernel.
Backing up data related to containers isn’t too different from any other application. This article shares rough steps and best practices.
Prerequisites
Section titled “Prerequisites”This tutorial assumes that you are using Docker or Podman, though similar container runtimes work in a similar way. It works with both rootless and rootful containers.
Step 1 – Gather Data to Back Up
Section titled “Step 1 – Gather Data to Back Up”Containers keep data in multiple places. To get a complete backup, you need to establish where the data needed to recreate a container is kept:
- Container configuration: The details needed to recreate a container or pod, equivalent to the
docker runcommand needed to launch a container. Often this is kept in adocker-compose.ymlfile (for small deployments), in an orchestration tool like Portainer, or in another deployment automation tool. - Mounted volumes: Any data inside a container is managed by the publisher of the image and will disappear if the container is updated or recreated. Thus it’s necessary to mount persistent folders outside the container. This can happen with named volumes (managed by the container runtime and usually kept in
/var/lib/docker/volumes/or a user’s home folder) or bind mounts (links to actual host folders). No matter where mounted folders are kept, they need to be included in the backup. - Databases: Many apps require a database to work and often launch a separate container for it. If you have multiple apps requiring a MySQL database, it’s generally simpler to consolidate them in one container and let multiple apps share one DB container. This makes backups easier and saves resources.
Step 2 – Back Up Volumes
Section titled “Step 2 – Back Up Volumes”With the locations known, you can use our CLI tutorial or Ansible role to set up Borgmatic and Borg. There is also a Docker image to run Borgmatic. No matter which installation method works best, you will always need to set up a Borgmatic config file (~/.config/borgmatic/config.yaml).
First, cover container configurations and volumes:
source_directories: - /root/docker-compose - /var/lib/docker/volumes/Step 3 – Back Up Databases
Section titled “Step 3 – Back Up Databases”Databases are slightly trickier to back up, since data might be kept in memory and just copying the data files is not sufficient for a clean backup. The simplest way to get a full database backup is Borgmatic’s database dump feature. It uses named pipes to dump the data, which doesn’t need space for intermediate files. See Databases for more.
The config below logs in to local database containers via their published ports as the root user and creates a backup of all databases:
commands: - before: action when: - create run: - echo "`date` - Starting backup."
postgresql_databases: - name: all hostname: 127.0.0.1 port: 5432 username: postgres password: ${POSTGRES_ROOT_PASSWORD}mysql_databases: - name: all hostname: 127.0.0.1 port: 33306 username: root password: ${MYSQL_ROOT_PASSWORD} options: "--column-statistics=0"Step 4 – Testing
Section titled “Step 4 – Testing”As a final step, run a backup and test restoring it so you know your process works and you can rely on it when you need it.
Conclusion
Section titled “Conclusion”This guide explained the high-level steps for backing up Docker and similar containers. As with every backup, it’s important to first establish where all the data is located and then choose appropriate backup strategies to get a complete backup.