How to restore docker containers after snap remove docker

I removed docker with sudo snap remove docker. I didn't actually want to remove it, was just checking if I had installed it with snap, trusting that it would prompt me with (y/n) like apt does. But it just went ahead and removed docker. Fine. It was quick to reinstall it again.

But, where are my containers?! All the images and containers (docker ps -a) were gone! It took a long time to uninstall it, so I'm wondering if there's some backup or the files are still kept and the containers can be restored? Don't care about the images, just the containers. Thanks.

Checked /var/snap/docker/common/var-lib-docker/containers but the original containers are not there.


Solution 1:

Turns out that the operation that was taking a long time was indeed a backup, or in their terminology, a snapshot. From the documentation:

Snapshots are generated manually with the snap save command and automatically when a snap is removed (requires snapd 2.39+). A snapshot can be used to backup the state of your snaps, revert snaps to a previous state and to restore a fresh snapd installation to a previously saved state.

I checked my snapd version:

# snap --version
snap    2.52.1
snapd   2.52.1
series  16
ubuntu  21.04
kernel  5.11.0-40-generic

Then saw what snapshots were available:

# snap saved
Set  Snap    Age    Version  Rev   Size    Notes
1    docker  2h58m  20.10.8  1125  1.39GB  auto

Checked that snapshot was OK:

# snap check-snapshot 1
Snapshot #1 verified successfully.

Finally, restored the automatic snapshot:

# snap restore 1
Restored snapshot #1.

At this point docker ps -a still showed me the containers I have created after the latest install, rather than the ones I wanted to restore. Perhaps if I had not created new containers, it would just work. But I know that at least the container files have been restored, because I can find them under /var/snap/docker/common/var-lib-docker/overlay2/. This is better than nothing. Be careful with snap remove!

Restoring Original Containers

Let me recap how I got into this state:

  • ran sudo snap remove docker which wiped out important containers
  • ran sudo snap install docker to install docker again
  • ran docker-compose up which brought up two new containers
  • ran snap restore 1 to restore the automatic snapshot

At this point I was able to find the restored container files, but docker ps -a still did not show the actual containers. In summary, this is what I did to get the full containers back:

  • removed all visible containers and images after the second installation since I did not need them
  • removed docker again with sudo remove docker and noticed the automatic snapshot taking place again: "Save data of snap "docker" in automatic snapshot set #2".
  • restored the first snapshot again: snap restore 1

Now if I do docker ps -a I can see all of my containers again, in stopped state! There is one problem though, one of them won't start, with the message: Error: response from daemon: OCI runtime create failed: container with id exists:....

To work around this, the simplest way I found was to docker commit <container-id> and use the resulting image to start it up as a new container. After I saw that it was fine, I got rid of the broken one. All back to normal!

By the way, it's considered good practice to use volumes to persist data in containers. Images can also be saved to an archive.