2

How can I prevent docker-compose up from copying saved volumes from the previous session into a container? In effect, I want Docker Compose to behave like docker run which discards the contents of private volumes when its container exits.

Derek Mahar
  • 801
  • 2
  • 8
  • 15

2 Answers2

2

It seems that no option exists to prevent docker-compose up from using volumes from a previous session. The best alternative I could think of is to stop the containers and then remove the containers and their private volumes:

docker-compose stop
docker-compose rm -fv

Note that unlike docker-compose down --volumes, this preserves named volumes.

Derek Mahar
  • 801
  • 2
  • 8
  • 15
0

Try using docker-compose up --force-recreate. See https://docs.docker.com/compose/reference/up/ for more details.

I think that problem is that docker-compose up is re-using existing containers, so existing volumes are re-used as well (there is no actual copying of the data).

  • Using `--force-recreate` was my first thought, but while this option does cause DC to recreate the containers, it still reuses the existing volumes and does not reset their contents. You can see this by repeatedly running `docker-compose up --force-recreate` and `docker-compose stop` and observing that not only do the number of containers and volumes remain constant, but that DC doesn't clear the contents of the private volumes. (You can examine the contents of the private volumes by running a command like `docker run -it --volumes-from previous-container_1 ubuntu /bin/bash.) – Derek Mahar May 16 '16 at 14:39
  • As an alternative, I can use `docker-compose down --volumes` to stop container and then remove the containers and their volumes. This way Docker Compose won't have any saved volumes from a previous session to copy into a new session. – Derek Mahar May 16 '16 at 16:14
  • Except this also deletes named volumes that I would like to preserve. :-( I want to delete only the private volumes. – Derek Mahar May 16 '16 at 16:21