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
-
Does this help? docs.docker.com/reference/cli/docker/volume/rmadmcfajn– admcfajn2024年12月07日 07:42:35 +00:00Commented Dec 7, 2024 at 7:42
-
thank you,i know i can use "docker volume rm" to remove the volume,But what i want is remove the container the volume associated with the container at the same time季天成– 季天成2024年12月07日 07:46:35 +00:00Commented Dec 7, 2024 at 7:46
1 Answer 1
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"