DEV Community

Cover image for Making backup of a docker volume
Atul Anand Oraon
Atul Anand Oraon

Posted on

Making backup of a docker volume

Making a backup of the database

Docker Volume img

You can use it for any type of container not just for the database.

💡️ Don't be afraid of the --rm flag

It is not removing your container it just removes

🐧️alpine Linux container for using the cp command.

Which is a Linux command

The command will be like this.


docker run --rm -v volume_id:/volume -v /path/to/backup/directory:/backup alpine cp -a /volume/. /backup/
Enter fullscreen mode Exit fullscreen mode

SIMPLE Explanation

  • docker run : run a command in a new container.
  • --rm : remove the container after it exits.
  • -v volume_id:/volume : mount the volume with id volume_id to /volume in the container.
  • -v /path/to/backup/directory:/backup : mount the host[our computer or server] directory /path/to/backup/directory to /backup in the container.
  • alpine : the image to use, its a lightweight Linux image.
  • cp -a /volume/. /backup/ : copy the contents of the volume to the backup directory on the host machine where it is being run.

So if you run locally it will be on your computer else

it will be on the server if you run it on the server.

[BOOKISH EXPLANATION] Specific questions regarding the command

  • :/volume and :/backup:

    • In the command docker run --rm -v 7ade41ba2f490a4ccbe15b442baa7071a8bf57c4a0cb5cf250bbad845994ef4f:/volume -v /path/to/backup/directory:/backup alpine cp -a /volume/. /backup/, the :/volume and :/backup parts indicate the mount points within the container. When you use the -v option with Docker, you specify the volume binding in the format host_path:container_path. So 7ade41ba2f490a4ccbe15b442baa7071a8bf57c4a0cb5cf250bbad845994ef4f:/volume means you're mounting the Docker volume 7ade41ba2f490a4ccbe15b442baa7071a8bf57c4a0cb5cf250bbad845994ef4f to /volume inside the container, and /path/to/backup/directory:/backup means you're mounting the host directory /path/to/backup/directory to /backup inside the container.
  • alpine:

    • alpine is the name of the Docker image being used to run the temporary container. In this case, it's an Alpine Linux image, which is commonly used due to its small size and efficiency for tasks like this.
  • cp -a /volume/. /backup/:

    • This is the cp command that copies files.
    • -a is an option that preserves the original attributes of the files being copied, such as permissions and timestamps.
    • /volume/ is the source directory from which files are being copied.
    • . at the end indicates that the entire contents of the source directory (including hidden files) are being copied.
    • /backup/ is the destination directory where the files are being copied to.

This command effectively mounts the Docker volume and a backup directory into an Alpine

Linux container, then copies the contents of the Docker volume to the backup directory

within the container. This allows you to create a backup of the data stored in the Docker volume.

Top comments (0)