π· Photo: Buddy at the The Beard-Green Cemetery by Wendy Bayer
If you have a Dead Docker container that you can not remove with
the usual Docker commands, especially on Docker Desktop for Mac,
this post may help you finally remove it.
TL;DR: Something went sideways when removing this container and it is totally borked
A Dead container looks like this when revealed by a
docker ps --all that shows all containers and not just those
currently running...
% docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
ad624bb35668 19838108d46c "./entrypoint.sh" 2 weeks ago Dead
According to the
Docker documentation
a Dead container is...
A "defunct" container; for example, a container that was only partially removed because resources were kept busy by an external process. dead containers cannot be (re)started, only removed.
Normally to remove a stopped container, you use the
docker rm command (e.g docker rm ad624bb35668),
or to force the removal of a running container,
the docker rm -f command (e.g docker rm -f ad624bb35668).
If this does not remove the container, the next usual commands
are docker container prune which removes all stopped containers
and docker container prune -f which forces the pruning of running
containers as well.
Finally, the nuclear option to remove all of the Docker environment artifacts: docker system prune -af --volumes intermixed with restarts of Docker (Desktop for Mac) and reboots of your machine.
It is possible that none of these commands will work to remove the
Dead container.
In that case, there is this Stack Overflow post which suggested (most of) these commands above and offered a solution for Docker on linux.
Per the
recommended answer
in that Stack Overflow post, to remove a Dead container on linux,
you locate and remove its storage_driver data files.
π Note that this requires root (sudo) access
β© The objective is to execute a command...
sudo rm -rf /var/lib/docker/<storage_driver>/<dead-container-id>/The steps for this are...
- Find your
<dead-container-id>(e.g.ad624bb35668)...docker ps -a - Find your Docker
<storage_driver>(e.g.overlayfs)...docker info --format '{{.Driver}}' - Stop Docker (Daemon)
- List the files (you will need root access password)...
sudo ls -al /var/lib/docker/<storage_driver>/<dead-container-id>/
- Remove the files (you will need root access password)...
sudo rm -rf /var/lib/docker/<storage_driver>/<dead-container-id>/
- Restart Docker (Daemon)
π Although Docker Desktop for Mac runs linux
architecture images, Docker does not actually run
on the Mac itself but inside a linux Virtual Machine
(VM) running on your Mac. Thus the above linux
approach for removing a Dead container will not
work on Mac because its storage_driver data
files are in the virtual machine and not in your
Mac's native filesystem.
β© The approach to to remove a Dead container for Docker Desktop for Mac is to remove the Docker VM and have Docker Desktop for Mac recreate it when it starts.
The steps for this are...
- Quit Docker Desktop For Mac
- Find the proper
<vm-number>...For example here thels -al ~/Library/Containers/com.docker.docker/Data/vms/<vm-number>is0...drwxr-xr-x@ 3 alexuser staff 96 Nov 3 2024 . drwxr-xr-x@ 42 alexuser staff 1344 Nov 27 10:19 .. drwxr-xr-x@ 8 alexuser staff 256 Nov 27 10:19 0 - Move/Backup the VM...
For example here for
mv ~/Library/Containers/com.docker.docker/Data/vms/<vm-number>/data/Docker.raw ~/Desktop/Docker.raw.backup
<vm-number>0...mv ~/Library/Containers/com.docker.docker/Data/vms/0/data/Docker.raw ~/Desktop/Docker.raw.backup
- Start Docker Desktop For Mac
- The
Deadcontainer(s) should be gone...docker ps -a
π For more information on how Docker Desktop for Mac works, see this post Under the Hood: Demystifying Docker Desktop For Mac