Mattias Svensson
Mattias Svensson

Reputation: 187

docker-compose mounted volume remain

I'm using docker-compose in one of my projects. During development i mount my source directory to a volume in one of my docker services for easy development. At the same time, I have a db service (psql) that mounts a named volume for persistent data storage.

I start by solution and everything is working fine

$ docker-compose up -d

When I check my volumes I see the named and "unnamed" (source volume).

$ docker volume ls
DRIVER              VOLUME NAME
local               226ba7af9689c511cb5e6c06ceb36e6c26a75dd9d619360882a1012cdcd25b72
local               myproject_data

The problem I experience is that, when I do

$ docker-compose down
...
$ docker volume ls
DRIVER              VOLUME NAME
local               226ba7af9689c511cb5e6c06ceb36e6c26a75dd9d619360882a1012cdcd25b72
local               myproject_data

both volumes remain. Every time I run

$ docker-compose down
$ docker-compose up -d

a new volume is created for my source mount

$ docker volume ls
DRIVER              VOLUME NAME
local               19181286b19c0c3f5b67d7d1f0e3f237c83317816acbdf4223328fdf46046518
local               226ba7af9689c511cb5e6c06ceb36e6c26a75dd9d619360882a1012cdcd25b72
local               myproject_data

I know that this will not happen on my deployment server, since it will not mount the source, but is there a way to not make the mounted source persistent?

Upvotes: 2

Views: 4487

Answers (2)

Richard Torcato
Richard Torcato

Reputation: 2762

Just remove volumes with the down command:

docker-compose down -v

Upvotes: 2

Tito
Tito

Reputation: 9044

You can use the --rm option in docker run. To use it with docker-compose you can use

docker-compose rm -v after stopping your containers with docker-compose stop

If you go through the docs about Data volumes , its mentioned that

Data volumes persist even if the container itself is deleted.

So that means, stopping a container will not remove the volumes it created, whether named or anonymous.

Now if you read further down to Removing volumes

A Docker data volume persists after a container is deleted. You can create named or anonymous volumes. Named volumes have a specific source form outside the container, for example awesome:/bar. Anonymous volumes have no specific source. When the container is deleted, you should instruct the Docker Engine daemon to clean up anonymous volumes. To do this, use the --rm option, for example:

$ docker run --rm -v /foo -v awesome:/bar busybox top

This command creates an anonymous /foo volume. When the container is removed, the Docker Engine removes the /foo volume but not the awesome volume.

Upvotes: 2

Related Questions