Q:

Why is an extra volume being created when I start the Postgres container?

I create a volume as follows:

>docker volume create testapp-pgdata

I see it listed as follows:

>docker volume ls
  DRIVER              VOLUME NAME
  local               0a47653c820b1d32df3197ed0598eb0a6e7e64f0e80c4ea0461f64c345048f66
  local               testapp-pgdata  

I start the container using the following command:  

> docker-compose -f src/main/docker/postgresql.yml up -d  

The yml file looks as follows:
version: '2'
services:
    testapp-postgresql:
        image: postgres:10.4
        volumes:
            - testapp-pgdata:/var/lib/postgresql/data/
        environment:
            - POSTGRES_USER=testapp
            - POSTGRES_PASSWORD=
        ports:
            - 5432:5432
volumes:
    testapp-pgdata:

When the container starts up I see this in the sysout:

Creating volume "docker_testapp-pgdata" with default driver

When I list the voles again I see the following:

>docker volume ls
  DRIVER              VOLUME NAME
  local               0a47653c820b1d32df3197ed0598eb0a6e7e64f0e80c4ea0461f64c345048f66
  local               docker_testapp-pgdata
  local               testapp-pgdata  

When I inspect the two volumes there is not much difference:

> docker volume inspect testapp-pgdata
  [
     {
         "CreatedAt": "2018-08-26T09:04:34Z",
         "Driver": "local",
         "Labels": {},
         "Mountpoint": "/var/lib/docker/volumes/testapp-pgdata/_data",
         "Name": "testapp-pgdata",
         "Options": {},
         "Scope": "local"
     }
  ]
  > docker volume inspect docker_testapp-pgdata
  [
     {
         "CreatedAt": "2018-08-26T09:36:32Z",
         "Driver": "local",
         "Labels": null,
         "Mountpoint": "/var/lib/docker/volumes/docker_testapp-pgdata/_data",
         "Name": "docker_testapp-pgdata",
         "Options": null,
         "Scope": "local"
     }
  ]  

Now the question: why is the second volume (docker_testapp-pgdata) created as a near clone of the original volume (testapp-pgdata) when the container is started?
I am running Docker version 18.06.0-ce, build 0ffa825 on windows.

A:

When you issue you a docker-compose up, Docker Compose will by default create a volume, network and container with the name of projectname_servicename_number where projectname is the name of the directory the Docker compose file exists
In your case, Docker doesn't consider your existing volume, it will create a new volume. To make it consider your volume, you must add a key of external: true to make Docker ignore the volume creation. It will look like this:
version: '2'
services:
    testapp-postgresql:
        image: postgres:10.4
        volumes:
            - testapp-pgdata:/var/lib/postgresql/data/
        environment:
            - POSTGRES_USER=testapp
            - POSTGRES_PASSWORD=
        ports:
            - 5432:5432
volumes:
    testapp-pgdata:
        external: true

The external: true will tell Docker Compose not to create a new volume and use the volume with the provided name as is. Note that if the volume doesn't exist, it will fail with an error

