1

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

asked Dec 7, 2024 at 7:34
2
  • Does this help? docs.docker.com/reference/cli/docker/volume/rm Commented 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 Commented Dec 7, 2024 at 7:46

1 Answer 1

0

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"

answered Dec 7, 2024 at 7:58
Sign up to request clarification or add additional context in comments.

2 Comments

No,you misunderstand my intentions.I want use "docker rm -v db" to remove the volume in one line of command while removing the container like it says in the hints
Oh,Now i understand,That's why I failed.Thank you so much,you save my day

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.