How to backup and restore docker containers (without the original docker run command)

2k views Asked by At

I want to be able to "freeze" a container state into a file and then restore it later if the container is accidentally deleted. This is to prevent accidental container deletion.

I tried docker export to a tar file, then docker import, but it will create an image of the docker filesystem... not the container itself. I know that I can use docker run from this image but I am not the container creator and I don't know what command was used to create it originally.

I also tried to use docker-runc to recreate the container from its rootfs and config.json from /run/docker/libcontainerd/CONTAINER_ID/config.json, but I see that this will not be easy and I wanted a "not so hack-ish" solution.

Currently, the only way I know to achieve this is to create snapshots of the docker host VM and, if someone accidentally deletes a container, restore the snapshot. But the last time this happened, the last backup was taken 16 hours before, and I lost all data created after that (and the recovery process too a good amount of time).

I just wanted to save the container state into a file and be able to recreate it in case of deletion.

1

There are 1 answers

4
fernandezcuesta On BEST ANSWER

You can use docker autocompose to get a docker-compose.yml equivalent that will allow you to recreate the container together with the image you saved.