Docker Tips #1: Beware the 'rm'
This begins a serie of docker tips.
Scenario
I want to remove a container from my host
Given
My very simple Dockerfile
built with docker build -t test .
Given
A container based on this image started with
When
I remove it: docker rm test_container
Then
I expect to have everything deleted, right? Wrong
Explanations
If you look at /var/lib/docker/vfs/dir/
you will have something like that
As you can see, the agileek_test.file is still here, without attached container, like a ghost folder on your system.
The Volume command
The fact that I specified VOLUME ["/data"]
in my Dockerfile tells docker to treat this folder differently.
Almost every time when you use the VOLUME command, later when you run it, you mount this volume (on the host, or linked to another container) with -v
But when you forgot to do this, docker automatically creates a folder in a Virtual File System (on ubuntu, /var/lib/docker/vfs) and doesn’t delete it, even when you remove your container.
You can see with docker inspect test_container
which folder has been created
but once you delete it, you’ve lost the information and this folder is “lost”
What can I do ?
The best practice is of course to always mount the folder that you declared with VOLUME in your Dockerfile.
The other good practice is to Read The Manual (Of course, I didn’t do it) because:
there is a -v option that Remove the volumes associated with the container
I really wished I found this before our server went full.