Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Backup a running Docker container?

Is it possible to backup a running Docker container? Is the export command suitable for doing that?

like image 507
Slava V Avatar asked Mar 29 '13 09:03

Slava V


People also ask

Can I backup a docker container?

To backup 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.

How do I save a docker container?

The usual way is at least through a docker commit: that will freeze the state of your container into a new image. But know that there is no reliable way to "save state" of container unlike virtual machine save state in Hyper-V or VMware. This is a downside also to docker.

What is container backup?

Container Backup Support protects volume data that was allocated by a storage plug-in that supports the Container Storage Interface (CSI) provided for Kubernetes.


2 Answers

Posted by one friend in comments

Hi Slava, sorry that your question was closed. For the record, Slava is talking about docker.io, a runtime for linux containers. Yes, docker export is a suitable approach. It will generate a tarball of your entire container filesystem state, and dump it on stdout. So

docker export $CONTAINER_ID > $CONTAINER_ID-backup.tar

will yield a usable tarball. You can re-import the tarball with

docker import - slava/$CONTAINER_ID-backup < $CONTAINER_ID-backup.tar

Note the original metadata (eg id of the original image) will be lost. This should be fixed in future versions of docker. – Solomon Hykes Apr 2 '13 at 6:35

Adding here so one can find from summary that question was answered. Thanks Solomon!

like image 198
JuliandotNut Avatar answered Oct 18 '22 07:10

JuliandotNut


export has some limitations: it won't export the data volume.

Here's data volume means:

  1. There's a VOLUME defined in the image's Dockerfile.
  2. The container is start with a parameter like this: -v /webapp

More about data: https://docs.docker.com/userguide/dockervolumes/

The way to handle this situation is start a new container with '--volumes-from' parameter to hook on that container, so you can visit the data volume.

Examples:

  1. Visit the data: (in a bash)

docker run -it --volumes-from target_container ubuntu bash

  1. Backup to host: (a postgres container)

docker run -it --volumes-from some_postgres -v /host/path:/container/path --rm ubuntu bash -c "tar -cvf /container/path/postgres-backup.tar /var/lib/postgresql/data"

like image 35
semicircle21 Avatar answered Oct 18 '22 07:10

semicircle21