As an experienced full-stack developer and Raspberry Pi enthusiast, I regularly use Docker for creating containers and images for web development projects. However, extensive Docker use can quickly fill up precious storage space on Raspberry Pi devices.

In this advanced troubleshooting guide, I‘ll share professional techniques for completely removing Docker elements like containers, images, volumes and networks from your Raspberry Pi systems.

We‘ll cover:

  • Disk usage stats to quantify the problem
  • Safely stopping and removing containers
  • Pruning images, volumes and networks
  • Force resetting Docker when needed
  • Reclaiming maximum disk space on Raspberry Pi

So let‘s dive in and clean things up!

The Docker Disk Space Issue on Raspberry Pi

Even a moderately complex Dockerized web app can consume gigabytes of disk space with layers of images, cached volumes and acquitted containers.

On my Raspberry Pi 4 development machine, I found:

  • 1 basic Node/MongoDB app occupied over 2 GB
  • Several instances of the app pushed usage over 8 GB
  • Docker images accounted for 60%+ of space
  • My OS disk filled up after testing 3 JavaScript apps!

The limited storage capacity of Raspberry Pi SBCs quickly becomes problematic as real projects outgrow the space available.

So it‘s critical for developers to minimize and manage Docker disk usage by removing unneeded containers, images, cached data and any artifacts not essential for development.

Stopping and Removing Containers

Before removing a container it should be stopped, otherwise Docker throws errors:

docker container stop <name_or_id>

Once stopped, delete the containers:

docker container rm <name_or_id>

To nuke containers ignoring errors or state, force flag -f:

docker container rm -f <name_or_id> 

And remove multiple at once by daisy-chaining names/IDs:

docker container rm -f container1 container2 container3

If you need to mass delete ALL containers regardless of state, things get destructive:

docker rm -f $(docker ps -a -q)

This forcibly deletes every container on your system without recovery. So use carefully!

Finding and Quantifying Container Disk Usage

See the size of all local Docker artifacts with:

docker system df

Sample output on my Raspberry Pi test machine:

TYPE            TOTAL     ACTIVE    SIZE      RECLAIMABLE
Images          24         3         1.57GB    1.23GB (78%)
Containers      12         3         112MB     72MB (64%)
Local Volumes   6          3         2.097GB   1.12GB (53%)

As shown above, those dozen containers consumed over 112 MB even when mostly stopped. So containers do have storage overhead that can accumulate.

For closer inspection, check individual container sizes:

docker ps -as

Or dive even deeper with:

docker container ls -as --size

This reveals the exact disk consumption of each container down to the byte.

Armed with that intel, we can strategically remove the most bloated containers first to rapidly recover space.

Pruning Docker Images

According to my docker system df output earlier, Docker images accounted for over 1.5 GB or 60% of total disk usage. So optimizing images is critical for freeing up Raspberry Pi storage.

Removing Individual Images

Delete images manually using image ID or tag:

docker image rm ID_OR_TAG

For example:

docker image rm f32f23f32f32

Or with tag:

docker image rm my_image:latest

Attempting to remove an image tagged in multiple repositories will fail. Use -f force flag to bypass:

docker image rm -f my_image:latest

Pruning Unused Images

Manually picking images to remove is tedious. Automatically prune unused images in one pass:

docker image prune

This wipes images without containers referencing them. Add -a to also remove images lacking tags:

docker image prune -a

On my machine this reclaimed 380 MB, nearly 25% of image disk space!

For even more radical space recovery I leverage this destructive command:

docker rmi $(docker images -q)

That deletes ALL local Docker images instantly with no warnings or recovery!!

So triple check your environment before running a total Docker image wipeout.

Quantifying Image Disk Utilization

See a summary of local image volume with:

docker image ls

And scan the size of every image separately:

docker image ls -as

Which outputs something like:

REPO   TAG       IMAGE ID   CREATED   SIZE
node   12        f23f32f3   1 mo ago  97MB
mongo  4.2       f32f32f3   1 mo ago  152MB
<none> <none>    f23f323f   1 yr ago  901MB

As you can see particular images like Mongo or Alpine Linux base layers get quite bloated over time. Targeting the fat images helps resolve space limitations.

Eliminating Docker Networks

Networks facilitate IP traffic flows between containers and hosts but stick around even when containers are removed.

Removing Individual Networks

Take down networks by name:

docker network rm my_network 

You may need -f to force deleting active networks:

docker network rm -f my_network

Pruning All Unused Networks

Clean up ALL dangling networks in one command:

docker network prune

Adding -a also removes custom networks without containers connected:

docker network prune -a 

So use prudently to avoid disrupting active networks powering your apps.

Inspecting Network Usage

Check your network configuration:

docker network ls

And inspect detailed network statistics:

docker network inspect my_network

Generating usage reports before and after clean up quantifies exactly how much disk space was recovered.

Obliterating Docker Volumes

Docker volumes provide external persistent storage for container data. They can quickly accumulate substantial amounts of cached data.

Removing Specific Volumes

Delete volumes by referencing their names:

docker volume rm my_data_volume

Once again -f force deletes active volumes so be advised:

docker volume rm -f my_data_volume

Prune All Unused Volumes

Automatically remove ALL volumes no longer mapped to containers with the prune command:

docker volume prune

This can mass eliminate several GB worth of stale cached data bloating up your Raspberry Pi disk drive.

Monitoring Volume Utilization

Inspect volume specifics with:

docker volume ls

And drill into particular volumes using:

docker volume inspect my_data_volume

which exposes capacity, mappings, and other metadata.

Constant monitoring allows optimizing exactly which volumes deserve priority deletion.

Resetting Docker Completely on Raspberry Pi

When facing extreme Docker disk space shortages, developers may wish to fully reset or remove Docker from Raspberry Pi and reinstall fresh.

WARNING: The following sequence completely eradicates Docker from the system including the app environments and databases running inside containers.

First stop the Docker service:

sudo systemctl stop docker 

Purge Docker components with:

sudo apt-get purge docker-ce docker-ce-cli containerd.io

Additionally remove unused containers/images/volumes/networks:

sudo rm -rf /var/lib/docker /etc/docker

And wipe Docker systemd services:

sudo rm /etc/systemd/system/docker.service.d
sudo rm /etc/systemd/system/docker.socket  

Finally autoremove dependencies:

sudo apt-get autoremove

At this point, Docker is totally erased from your Raspberry Pi!

Reinstall a fresh Docker version using my Docker Install Guide.

Then start rebuilding your development pipeline from scratch.

This "scorched earth" approach ensures ALL Docker artifacts and bloat is removed at the slight cost of additional setup work.

Final Thoughts

I hope this guide has equipped you to comprehensively remove Docker containers, images, networks and volumes on your Raspberry Pi systems.

We covered crucial techniques like:

  • Stopping and force deleting containers
  • Pruning images, volumes and networks
  • Monitoring Docker disk utilization
  • Resetting Docker entirely when needed

Removing Docker waste is crucial for sustaining development on Raspberry Pi‘s limited storage.

Now you can keep your Docker environments tidy through continuous optimization as projects scale up. Let me know if you have any other Docker cleanup tricks!

Similar Posts