Score:0

How to restore docker containers after snap remove docker

pt flag

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.

birgersp avatar
cn flag
I can't believe why this isn't better (excplicitly) documented anyhwere. The docker docs https://docs.docker.com/engine/install/ubuntu/#uninstall-docker-engine state that "Images, containers, volumes, or customized configuration files on your host are not automatically removed", though that is for `apt`. Some people will assume that it is the same for `snap` when from your experiments it clearly isn't.
Nagev avatar
pt flag
Agree, it should also be documented in docker pages like the [snap page](https://snapcraft.io/docker) besides the generic `snap` documentation. And note that it says that the automatic snapshots " are retained for 31 days before being deleted automatically".
Score:1
pt flag

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.

user535733 avatar
cn flag
Great answer. I learned something! Thanks for leading us through it.
Nagev avatar
pt flag
Thanks, now also managed to get the containers visible by cleaning up and reinstalling docker. They show up with `docker ps -a` but they cannot be started: `Error response from daemon: OCI runtime create failed: container with id exists:...` will see if I can fix the conflict and get them up an running normally again.
vn flag
With `saved` worth to [mention also `forget`](https://askubuntu.com/a/1354550/349837)
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.