Docker Community Forums

Share and learn in the Docker community.

What is the correct way to reference a named volume within service section of docker-compose.yml?

I am having trouble setting up a volume properly in which I wish to replicate an existing mysql database. Specifically, I explicitly created a named volume on the command-line, yet when I attempt to reference this named volume in my docker-compose.yml I end up creating a new volume. This new volume’s name is a concatenation of the service name, an underscore, and my volume name. This new volume with the combined name is where my database ends up being created. What is the proper way to reference a named volume within a service section of the docker-compose.yml?

I thought it made sense to use a named volume for this, erequests-data, so I could easily identify the volume where the data resided. This volume was created via “docker volume create erequests-data”. My Dockerfile for the database includes SQL to initialize the database.

FROM mysql:5.6
ENV MYSQL_DATABASE=erequests MYSQL_USER=erequests MYSQL_PASSWORD=erequests MYSQL_ROOT_PASSWORD=docker
COPY erequests.sql /docker-entrypoint-initdb.d/

I then attempt to reference this named volume in the section of docker-compose.yml for the database.

version: "3"
services:
  erequests-db:
    image: erequests-db:testy
    environment:
      - MYSQL_DATABASE=erequests
      - MYSQL_USER=erequests
      - MYSQL_PASSWORD=erequests
    volumes:
      - type: volume
        source: erequests-data
        target: /var/lib/mysql
    restart: always
  phpmyadmin:
    image: phpmyadmin/phpmyadmin
    container_name: phpmyadmin
    environment:
      - PMA_ARBITRARY=1
    ports:
      - "8080:80"
    links:
      - erequests-db:mysql
volumes:
  erequests-data:

After executing “docker-compose up” I end up with the following volumes.

terrence@t:~/sandbox/erequests_db$ docker volume ls
DRIVER              VOLUME NAME
local               8e8988bd599a5fedc07efe94abddf93da223342829f0058812ae0d3ad80fda17
local               erequests-data
local               erequestsdb_erequests-data
terrence@t:~/sandbox/erequests_db$ sudo ls -l /var/lib/docker/volumes/erequestsdb_erequests-data/_data
^[[A^[[A^[[Atotal 110608
-rw-rw---- 1 999 docker       56 Apr  2 09:08 auto.cnf
drwx------ 2 999 docker     4096 Apr  2 09:08 erequests
-rw-rw---- 1 999 docker 12582912 Apr  2 09:13 ibdata1
-rw-rw---- 1 999 docker 50331648 Apr  2 09:13 ib_logfile0
-rw-rw---- 1 999 docker 50331648 Apr  2 09:08 ib_logfile1
drwx------ 2 999 docker     4096 Apr  2 09:08 mysql
drwx------ 2 999 docker     4096 Apr  2 09:08 performance_schema
terrence@t:~/sandbox/erequests_db$ sudo ls -l /var/lib/docker/volumes/erequests-data/_data
total 0
terrence@t:~/sandbox/erequests_db$

Thanks for any insights as to what I’m doing wrong.

There were two problems with my configuration. The first was that I needed to specify in the volumes section of the docker-compose.yml that the named volume was external (i.e., docker-compose did not need to create it):

...
volumes:
  erequests-data:
    external: true

The second problem is that I couldn’t use the long-syntax volume specification as shown in the docker-compose.yml with ‘version: “3”’; the version needs to be 3.2 or greater.