Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save brianjbayer/4688d6c915af5f1db98777e158972ce2 to your computer and use it in GitHub Desktop.

Select an option

Save brianjbayer/4688d6c915af5f1db98777e158972ce2 to your computer and use it in GitHub Desktop.
Remove Dead Docker Containers on Linux and Docker Desktop for Mac

How to Remove a Dead Docker Container (Docker Desktop for Mac)

The Fearless Zombie Hunter - Wend Bayer

πŸ“· 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.

What is a Dead Container

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.

Removing a Dead Container

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.

Removing a Dead Container 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...

  1. Find your <dead-container-id> (e.g. ad624bb35668)...
    docker ps -a
    
  2. Find your Docker <storage_driver> (e.g. overlayfs)...
    docker info --format '{{.Driver}}'
    
  3. Stop Docker (Daemon)
  4. List the files (you will need root access password)...
    sudo ls -al  /var/lib/docker/<storage_driver>/<dead-container-id>/
  5. Remove the files (you will need root access password)...
    sudo rm -rf /var/lib/docker/<storage_driver>/<dead-container-id>/
  6. Restart Docker (Daemon)

Removing a Dead Container on Docker Desktop for Mac

🍎 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...

  1. Quit Docker Desktop For Mac
  2. Find the proper <vm-number>...
    ls -al ~/Library/Containers/com.docker.docker/Data/vms/
    For example here the <vm-number> is 0...
    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
    
  3. Move/Backup the VM...
    mv ~/Library/Containers/com.docker.docker/Data/vms/<vm-number>/data/Docker.raw ~/Desktop/Docker.raw.backup
    For example here for <vm-number> 0...
    mv ~/Library/Containers/com.docker.docker/Data/vms/0/data/Docker.raw ~/Desktop/Docker.raw.backup
  4. Start Docker Desktop For Mac
  5. The Dead container(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


Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment