How to name a volume using a docker-compose.yml file?

Muzzstick picture Muzzstick · Dec 23, 2016 · Viewed 16.5k times · Source

I'm new to Docker and I'm trying to find out how to set the name of the created data volume. Currently the directory is automatically named as a long hash under /var/libs/docker which is far from user friendly.

I'm attempting to set up a development environment for MODX as shown here: https://github.com/modxcms/docker-modx

Currently my docker-compose.yml file is as follows:

web:
  image: modx
  links:
    - db:mysql
  ports:
    - 80:80
db:
  image: mysql
  environment:
    MYSQL_ROOT_PASSWORD: example
  ports:
    - 3306:3306
  command: mysqld --sql-mode=NO_ENGINE_SUBSTITUTION
myadmin:
  image: phpmyadmin/phpmyadmin
  links:
    - db:db
  ports:
    - 8080:8080

This works perfectly but I'm unsure as to how to name the data volume that I would edit directly with my IDE.

(As a side question, does it have to be created under /var/libs/docker ? Or is there a way of setting it to a directory in my home folder?)

Update: Thanks to the help from @juliano I've updated my docker-compose.yml file to:

version: '2'

services:
    web:
      image: modx
      volumes: 
        - html:/home/muzzstick/dev/modxdev
      links:
        - db:mysql
      ports:
        - 80:80
    db:
      image: mysql
      environment:
        MYSQL_ROOT_PASSWORD: example
      ports:
        - 3306:3306
      command: mysqld --sql-mode=NO_ENGINE_SUBSTITUTION
    myadmin:
      image: phpmyadmin/phpmyadmin
      links:
        - db:db
      ports:
        - 8080:8080

volumes:
    html:
        external: false

Unfortunately this seems to stop the web container from running. db and myadmin containers show they're running ok. There weren't any errors... if I type docker start docker_web_1 it appears to start but docker ps -a shows it exited as soon as it started.

Update 2

Running docker-compose up -d appears to run without issue. But then as you can see below, the web container exits as soon as it's created.

    CONTAINER ID        IMAGE                   COMMAND                  CREATED             STATUS                     PORTS                            NAMES
a1dd6d8ac94e        modx                    "/entrypoint.sh apach"   10 seconds ago      Exited (1) 5 seconds ago                                    docker_web_1
ee812ae858dc        phpmyadmin/phpmyadmin   "/run.sh phpmyadmin"     10 seconds ago      Up 5 seconds               80/tcp, 0.0.0.0:8080->8080/tcp   docker_myadmin_1
db496134e0cf        mysql                   "docker-entrypoint.sh"   11 seconds ago      Up 10 seconds              0.0.0.0:3306->3306/tcp           docker_db_1

Update 3 OK the error logs for this container shows:

error: missing MODX_DB_HOST and MYSQL_PORT_3306_TCP environment variables
Did you forget to --link some_mysql_container:mysql or set an external db
with -e MODX_DB_HOST=hostname:port?

This error appears to be originating from https://github.com/modxcms/docker-modx/blob/master/apache/docker-entrypoint.sh#L15-L20

Could it be something like linking is handled differently in docker-compose version 2?

Answer

Juliano Alves picture Juliano Alves · Dec 23, 2016

To create a named data volume using the version 2 of compose files you will have a separated area:

version: '2'

services:
  db:
    image: postgres
    volumes:
      - amazingvolume:/var/lib/postgresql/data

volumes:
  amazingvolume:
    external: true

So you can define the volume name (amazingvolume), if it's external or not and under your service (db in this example) you can define which directory you gonna mount.