2

I am tracking down two incidents I have seen this week, where data was lost after the container was recreated, no worries, the data was restored =)

Just for clarity, I know about what the documentation says and the options that come with compose. I searched around and some people do ask about related issues, but they are normally about the -v option or if data was inside the container RW layer vs volume.

However, I am more curious about the language from the doc:

-v, --volumes       Remove named volumes **declared in the `volumes` section**
                    of the Compose file and anonymous volumes
                    attached to containers.

More specifically, I am searching for the expected, documented, behavior of the down instruction when volumes are declared in the Dockerfile, but not in the docker-compose file, volumes section.

Example of related question on the same subject, in a case: Docker-Compose persistent data MySQL this questions is also confusing because the OP does declare the volume within the volumes section.

Victor
  • 3,520
  • 3
  • 38
  • 58

2 Answers2

1

A volume declared in a Dockerfile is an "anonymous volume", so it would be removed.

See this example: https://gist.github.com/dnephin/0aa8e8962ebcdcebff1cec7315a224dd

dnephin
  • 25,944
  • 9
  • 55
  • 45
  • 2
    The "anonymous volumes" referred to in the documentation are the ones in the `volumes:` section of a service in the docker-compose file – MatTheWhale Aug 17 '17 at 21:46
  • I don't know why the downvote, especially because this is what I used to think. But this is not what I am observing: I mean, the quedtion was why it is being removed, even without the option being set. – Victor Aug 17 '17 at 21:47
  • @MatTheWhale If you look at the code https://github.com/docker/compose/blob/1.15.0/compose/project.py#L314, you'll see it does pass `v=True` to the container remove, which should remove anonymous volumes. I also just tried it out, and it did remove the volume: https://gist.github.com/dnephin/0aa8e8962ebcdcebff1cec7315a224dd – dnephin Aug 17 '17 at 21:58
  • Nice, I will check this code. but still not quite what I was searching. But might have the answer there. – Victor Aug 17 '17 at 22:04
0

I just tested it. Neither docker-compose down nor docker-compose down -v removed the volume when it was declared in the Dockerfile.

docker-compose down followed by docker volume prune does remove it, however.

MatTheWhale
  • 967
  • 6
  • 16
  • docker-compose down & docker volume prune for sure.. but it is troublesome. Have you seen the related question I put. I was able to replicate that too. Only with named and bound volumes the data seems to be really persistent. Especially with an image upgrade..I wonder if it stricted to upgrades, or if it is a system dependent setting or config. (testing on compose 3.2, debian) – Victor Aug 17 '17 at 21:57
  • `docker volume prune` is going to remove every volume that is not referenced by a running container, so that may be too aggresive. `docker-compose down -v` is the correct way to remove them. – dnephin Aug 17 '17 at 22:01
  • That is incorrect, `docker volume prune` only removes dangling volumes. As long as the volume is referenced by a container it will not be removed, regardless of whether or not the container is running. `docker-compose down -v` is for removing anonymous volumes declared in `docker-compose.yml`, which would otherwise persist if `docker-compose down` was used instead – MatTheWhale Aug 17 '17 at 22:12
  • 1
    So far: My conclusion, based on experimentation is: named volumes in the global volume area in the compose file are safe, as do are the bound ones (of course). However, inside the service volume area, when using an anonymous volume it can get lost either way, with or without the -v - if the docker-compose down is used. My tests started with the mysql official image, with other 3 services. In the case of this mysql image, the behavior was that, as it was in the OP's question -> the one I used as example. – Victor Aug 17 '17 at 22:52
  • If you run `docker-compose down` (without -v) first, then yes, you will lose the link to the anonymous volume, so trying to run `docker-compose down -v` after that, there's no longer any reference to the anonymous volume and you wont be able to remove it. If you use `-v` the first time it will remove it. – dnephin Aug 21 '17 at 15:11