MMT
MMT

Reputation: 2111

Why does `docker-compose up` erase volumes?

I'm testing lots of services as containers, usually through docker-compose.

Since I'm new to docker this question can appear noobish, anyway.

At some point I had a docker-compose stack (few containers with volumes) running. I had to stop containers to re-use the same ports; I did that with docker stop command.

When I was ready to start my containers again I did:

$ docker-compose start
Starting jenkins   ... done
Starting gitlab-ce ... done
ERROR: No containers to start

I checked for containers and was surprised to see this:

$ docker-compose ps
Name   Command   State   Ports
------------------------------

So I ran:

$ docker-compose up
Creating volume "dockerpipelinejenkins_jenkins_home" with default driver
Creating volume "dockerpipelinejenkins_gitlab_logs" with default driver
Creating volume "dockerpipelinejenkins_gitlab_data" with default driver
Creating volume "dockerpipelinejenkins_gitlab_config" with default driver
Creating dockerpipelinejenkins_jenkins_1   ... done
Creating dockerpipelinejenkins_gitlab-ce_1 ... done
Attaching to dockerpipelinejenkins_jenkins_1, dockerpipelinejenkins_gitlab-ce_1

... and I was shocked to see that my volumes had been recreated, effectively erasing my data.

Why did docker compose erase the volumes, and will it happen every time when I stop docker containers using docker stop?

Upvotes: 6

Views: 5422

Answers (1)

BMitch
BMitch

Reputation: 263627

A stop will not remove volumes. In fact it cannot since the container would still exist and therefore holding the volume as in use:

$ docker run --name test-vol -v test-vol:/data busybox sleep 10

$ docker volume rm test-vol
Error response from daemon: unable to remove volume: remove test-vol: volume is in use - [1de0add7a8dd6e083326888bc02d9954bfc7b889310ac238c34ac0a5b2f16fbf]

A docker-compose down will remove the containers, but it will not remove the volumes unless you explicitly pass that option:

$ docker-compose down --help
Stops containers and removes containers, networks, volumes, and images
created by `up`.

By default, the only things removed are:

- Containers for services defined in the Compose file
- Networks defined in the `networks` section of the Compose file
- The default network, if one is used

Networks and volumes defined as `external` are never removed.

Usage: down [options]

Options:
    --rmi type          Remove images. Type must be one of:
                        'all': Remove all images used by any service.
                        'local': Remove only images that don't have a custom tag
                        set by the `image` field.
    -v, --volumes       Remove named volumes declared in the `volumes` section
                        of the Compose file and anonymous volumes
                        attached to containers.
    --remove-orphans    Remove containers for services not defined in the
                        Compose file

If you have stopped all running containers, and also run a prune, and pass the option to that prune to also delete volumes, then that would remove the volumes:

$ docker system prune --help

Usage:  docker system prune [OPTIONS]

Remove unused data

Options:
  -a, --all             Remove all unused images not just dangling ones
      --filter filter   Provide filter values (e.g. 'label=<key>=<value>')
  -f, --force           Do not prompt for confirmation
      --volumes         Prune volumes

In short, what you have described is not the default behavior of docker, and you have run some command to remove these volumes that was not included in the question.

Upvotes: 4

Related Questions