Here are my instructions for running the container:
docker run -p 5432:5432 --name=db -e POSTGRES_PASSWORD=secret -d --mount type=volume,src=postgres_data,target=/var/lib/postgresql/data postgres
docker volume ls
Here is the result:
DRIVER VOLUME NAME
local postgres_data
And I verify that the container is running properly
And excute the
docker rm --help
command then prompts me that this cmmand can remove the anonymous volume associated with the container:
Options:
-f, --force Force the removal of a running container (uses SIGKILL)
-l, --link Remove the specified link
-v, --volumes Remove anonymous volumes associated with the container
Then I do this:
docker stop db
docker rm -v db
docker ps -a
Here is the result:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
Although the container has been removed, the volume is still there and I don't understand why:
docker volume ls
DRIVER VOLUME NAME
local postgres_data
how can i remove a container also removes the volume associated with the container
When you remove a container with the docker rm -v command, it will remove the container and any anonymous volumes associated with it. However, named volumes(whichpostgres_data in your case) are not removed by default, even if you use the -v flag.
Why postgres_data is still there: The volume postgres_data is a named volume, not an anonymous volume. Named volumes are designed to persist data even when a container is removed, and they need to be manually deleted.
Simply, you can use this command "docker volume rm postgres_data"