Remove a container and its anonymous volumes
In this step, you will learn how to remove a container and its associated anonymous volumes. When you create a container, Docker can create volumes to store data. Anonymous volumes are volumes that are not explicitly named when created. They are typically created when a VOLUME
instruction is present in the Dockerfile of the image.
Let's run a container that creates an anonymous volume. We will use the ubuntu
image again and specify a volume mount point using the -v
flag without a name before the colon.
docker run -d -v /data ubuntu sleep infinity
This command creates a container and an anonymous volume mounted at /data
inside the container. You should see the container ID printed.
<container_id>
Now, let's inspect the container to see the volume that was created. Replace <container_id>
with the actual ID of your running container.
docker inspect <container_id>
The output of docker inspect
is a large JSON object. Look for the "Mounts"
section. You should see an entry for the volume mounted at /data
, and the "Name"
field will be a long, randomly generated string, indicating it's an anonymous volume.
...
"Mounts": [
{
"Type": "volume",
"Source": "<volume_name>",
"Target": "/data",
"Consistency": "consistent",
"BindOptions": null,
"Mode": "",
"RW": true,
"Propagation": "rprivate"
}
],
...
You can also list all volumes using docker volume ls
. You should see the anonymous volume listed.
docker volume ls
The output will include the anonymous volume with its randomly generated name.
DRIVER VOLUME NAME
local <volume_name>
By default, when you remove a container, its anonymous volumes are not removed. Let's stop the container first. Replace <container_id>
with the actual ID of your running container.
docker stop <container_id>
You should see the container ID printed.
<container_id>
Now, remove the stopped container without removing its volumes. Replace <container_id>
with the actual ID of your stopped container.
docker rm <container_id>
You should see the container ID printed.
<container_id>
Check the volumes again using docker volume ls
. The anonymous volume should still be present.
docker volume ls
The output will still include the anonymous volume.
Now, let's create another container with an anonymous volume and then remove the container and its volume together.
docker run -d -v /data ubuntu sleep infinity
Get the new container ID.
<new_container_id>
Stop the new container. Replace <new_container_id>
with the actual ID.
docker stop <new_container_id>
You should see the container ID printed.
<new_container_id>
Now, remove the stopped container and its anonymous volumes using the -v
flag with docker rm
. Replace <new_container_id>
with the actual ID.
docker rm -v <new_container_id>
You should see the container ID printed.
<new_container_id>
Check the volumes again using docker volume ls
. The anonymous volume associated with the container you just removed should now be gone. The first anonymous volume you created should still be there.
docker volume ls
The output should only show the first anonymous volume you created.