This article is only relevant if you know about and use Docker volumes and have some fluency in bash. I'll explain the code as I go, if it helps.
## The Problem
Over the lifetime of a Docker host machine, it's like that orphaned volumes ( and other detritus ) will accumulate over time. You might also find yourself fumbling a configuration and orphaning a volume yourself.
Over the lifetime of a Docker host machine, it's likely that orphaned volumes and other detritus will accumulate over time. You might find yourself fumbling a configuration and orphaning a volume yourself. However we got here, we have a bunch of volumes and we need to know if any of them are important. In a perfect world, they'll have decent names.
However we got here, we have a bunch of volumes and we need to know if any of them are important. In a perfect world, they'll have decent names. We don't live in a perfect world.
We don't live in a perfect world.
## Make a list
Luckily, we have tools at our disposal to handle this. My thought process almost always starts with "Can I turn a list of the things I care about into a newline separated list?" If I can do that, I can start automating my troubleshooting.
Luckily, we have tools at our disposal to handle this. My thought process almost always starts with "Can I make a newline separated list of the things I care about?" If I can do that, I can start automating my troubleshooting.
Let's start with `docker volume ls`. This is how we list volumes, but the default output isn't quite what I'm looking for:
```
@ -29,7 +29,7 @@ local grafana-storage
local plausible_db-data
local plausible_event-data
```
This is human readable, and we could even do some slicing with `cut` or `awk`, but Docker gives us a flag that will take us exactly where we need to go: `--format`. Generally, Docker uses Go's `text/template` library to back this feature, and more specifically, individual flags (usually) [document](https://docs.docker.com/reference/cli/docker/volume/ls/#format) the template verbs available. Here, we want `Name`.
This is human readable, and we could even do some slicing with `cut` or `awk`, but Docker gives us a flag that will take us exactly where we need to go: `--format`. Docker uses Go's `text/template` library to power this feature and individual flags (usually) [document](https://docs.docker.com/reference/cli/docker/volume/ls/#format) the template verbs available. Here, we want `Name`.
And now we have a newline separated list of volume names.
We now have a newline separated list of volume names.
## Process of elimination
The next part is fairly straightforward. We loop over this list and ask Docker to create a temporary container based on alpine, with a single volume mounted at `/test/`.