3

As part of my quest to completely understand how docker deals with volumes under the hood (followup from my previous post), I'm curious about how running docker-compose stop deals with volumes.

There are two possibilities that I suppose may happen:

  1. Volumes are unmounted after running docker-compose stop and then mounted again in lexographic order when running docker-compose start.
  2. Volumes stay mounted until the container is removed using docker-compose rm or a similar command.

I was curious as even restarting a stopped container will create an empty directory in my host folder. I have the following in my docker-compose.yml:

volumes:
- .:/var/www/app
- my_modules:/var/www/app/node_modules

So, from my understanding of the answer in the linked post above, the empty directory is created because of the following:

  1. Bind mount happens first (lexographic order), overwrites everything in /var/www/app
  2. Docker will try to mount node_modules, but since the directory no longer exists in the container (overwritten), it will mkdir node_modules to have a directory to mount the named volume.
  3. Since the bind mount already exists when the mkdir happens, I see a new, empty node_modules folder in my host.

This makes complete sense if the volumes are being mounted, but if 2) is true and they stay mounted even after running docker-compose stop, why would I still be seeing an empty directory being created in my host? I remove node-modules from my host in between the stop and up command for reproducing this behavior.

Community
  • 1
  • 1
rb612
  • 5,280
  • 3
  • 30
  • 68
  • 1
    Docker never removes anything. Period, you don't want your data lost. – Mike Doe May 26 '19 at 21:08
  • @emix thank you, however that's not quite what I'm asking. I'm asking about the implementation regarding whether stopping containers using `docker-compose` ends up unmounting (not talking about removing—I know it doesn't remove) the volume, and if not, why I'm seeing the behavior I'm seeing. – rb612 May 26 '19 at 21:10
  • I'm pretty sure you're not allowed to **rmdir**(2) a directory that's a mount point in use; so if you can remove the directory, the volume isn't mounted. – David Maze May 26 '19 at 21:30
  • @DavidMaze I can't do `docker volume rm node_modules`, for example, since it says that there's a container in use. Though i don't know if that means necessarily it's mounted. I'm wondering do you think that because remove it in my host after `stop`ping the container, when I restart, that deletion is reflected in the container and since it needs the directory to mount the named volume, it will run `mkdir` again regardless of if it's remounted? I know it's such a minor detail but the whole process I find intriguing. – rb612 May 26 '19 at 21:41

0 Answers0