Docker

How can I backup a Docker-container with its data-volumes

25 September 2026 · 7 min read

How can I backup a Docker-container with its data-volumes

Docker containers, with their ephemeral nature, present a unique challenge when it comes to data persistence. While the container itself can be easily recreated, the data within it vanishes unless properly backed up. Losing valuable data can be detrimental, disrupting workflows and potentially causing significant setbacks. This guide provides a comprehensive overview of how to effectively backup your Docker containers and their associated data volumes, ensuring your data remains safe and accessible.

Using docker cp for Single-File Backups

The docker cp command offers a straightforward method for backing up individual files from a running container to your local machine. This is useful for grabbing configuration files or small datasets. However, it’s not ideal for backing up entire volumes, especially large ones.

For example, to copy a file named “config.json” from the container “my_app” to your current directory:

docker cp my_app:/path/to/config.json .

While simple, this approach has limitations. It requires the container to be running, and it doesn’t capture the entire volume’s state, making it unsuitable for complex applications.

Volume Mounting for Persistent Data

A more robust approach is to mount a host directory as a data volume within your container. This ensures data persists even after the container is stopped or removed. Backing up becomes as simple as copying the contents of the host directory.

When running your container, use the -v flag:

docker run -v /path/on/host:/path/in/container image_name

Now, any changes made within the container are reflected directly on your host machine, facilitating easy backups using standard file system tools.

Leveraging docker run --volumes-from for Backup

The --volumes-from flag allows you to create a dedicated backup container that mounts the volumes of another container. This enables you to perform backups without affecting the running application.

First, create a backup container:

docker create --volumes-from my_app --name my_app_backup ubuntu bash

Then, start the backup container and use tar to create an archive:

docker start -ai my_app_backup

tar cvf /backup.tar /path/to/data

Finally, copy the backup archive to your host:

docker cp my_app_backup:/backup.tar .

Employing Specialized Backup Tools

Several third-party tools simplify Docker backup and recovery, offering features like incremental backups, scheduling, and cloud integration. Duplicati, for instance, supports Docker backups and provides a user-friendly interface. Explore various options to find the tool that best suits your needs.

These tools often offer more advanced features compared to manual methods, making them a worthwhile investment for production environments.

Consider factors like backup frequency, storage location, and restoration speed when selecting a tool. Look for solutions that integrate seamlessly with your existing infrastructure.

Choosing the Right Backup Strategy

The optimal backup strategy depends on your specific needs and environment. Factors like data volume size, frequency of changes, and recovery time objectives (RTOs) will influence your choice.

  1. Regular Backups: Establish a consistent backup schedule. Daily backups are recommended for frequently changing data.
  2. Incremental Backups: To save storage space, consider incremental backups that only store changes since the last full backup.
  3. Offsite Storage: Store backups in a separate location to protect against data loss due to hardware failure or other disasters.

Learn more about advanced backup strategies.

Featured Snippet Optimization: Backing up Docker containers is crucial for safeguarding your data. Utilizing volume mounts, specialized backup tools, or the docker cp command ensures data persistence and facilitates easy recovery.

FAQ: Common Docker Backup Questions

  • Q: How often should I back up my Docker containers? A: The frequency depends on how often your data changes. Daily backups are a good starting point for critical data.
  • Q: Where should I store my backups? A: Store backups in a secure, offsite location to protect against data loss due to local incidents.

Protecting your Docker data is paramount. By implementing a robust backup strategy, you can mitigate the risks associated with data loss and ensure business continuity. Regularly backing up your containers and volumes, whether through manual methods or specialized tools, provides peace of mind and enables swift recovery in case of unexpected events. Evaluate your specific requirements and explore the various options discussed to create a tailored backup solution that safeguards your valuable data. Don’t wait until it’s too late – start protecting your Docker data today.

Explore related topics such as disaster recovery planning, data security best practices, and container orchestration for a more comprehensive understanding of managing containerized environments.

Question & Answer :
I’ve been using this Docker-image tutum/wordpress to demonstrate a Wordpress website. Recently I found out that the image uses volumes for the MySQL-data.

So the problem is this: If I want to backup and restore the container I can try to commit an image, and then later delete the container, and create a new container from the committed image. But if I do that the volume gets deleted and all my data is gone.

There must be some simple way to backup my container plus its volume-data but I can’t find it anywhere.

if I want to revert the container I can try to commit an image, and then later delete the container, and create a new container from the committed image. But if I do that the volume gets deleted and all my data is gone

As the docker user guide explains, data volumes are meant to persist data outside of a container filesystem. This also eases the sharing of data between multiple containers.

While Docker will never delete data in volumes (unless you delete the associated container with docker rm -v), volumes that are not referenced by any docker container are called dangling volumes. Those dangling volumes are difficult to get rid of and difficult to access.

This means that as soon as the last container using a volume is deleted, the data volume becomes dangling and its content difficult to access.

In order to prevent those dangling volumes, the trick is to create an additional docker container using the data volume you want to persist so that there will always be at least that docker container referencing the volume. This way you can delete the docker container running the wordpress app without losing the ease of access to that data volume content.

Such containers are called data volume containers.

There must be some simple way to back up my container plus volume data but I can’t find it anywhere.

back up docker images

To back up docker images, use the docker save command that will produce a tar archive that can be used later on to create a new docker image with the docker load command.

back up docker containers

You can back up a docker container by different means

  • by committing a new docker image based on the docker container current state using the docker commit command
  • by exporting the docker container file system as a tar archive using the docker export command. You can later on create a new docker image from that tar archive with the docker import command.

Be aware that those commands will only back up the docker container layered file system. This excludes the data volumes.

back up docker data volumes

To back up a data volume you can run a new container using the volume you want to back up and executing the tar command to produce an archive of the volume content as described in the docker user guide.

In your particular case, the data volume is used to store the data for a MySQL server. So if you want to export a tar archive for this volume, you will need to stop the MySQL server first. To do so you will have to stop the wordpress container.

back up the MySQL data

An other way is to remotely connect to the MySQL server to produce a database dump with the mysqldump command. However in order for this to work, your MySQL server must be configured to accept remote connections and also have a user who is allowed to connect remotely. This might not be the case with the wordpress docker image you are using.


Edit

Docker recently introduced Docker volume plugins which allow to delegate the handling of volumes to plugins implemented by vendors.

The docker run command has a new behavior for the -v option. It is now possible to pass it a volume name. Volumes created in that way are named and easy to reference later on, easing the issues with dangling volumes.

Edit 2

Docker introduced the docker volume prune command to delete all dangling volumes easily.