Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 527
Muzzstick Avatar asked Dec 23 '16 15:12

Muzzstick


People also ask

How do I specify volumes in Dockerfile?

In Dockerfile you can specify only the destination of a volume inside a container. e.g. /usr/src/app . When you run a container, e.g. docker run --volume=/opt:/usr/src/app my_image , you may but do not have to specify its mounting point ( /opt ) on the host machine.

How do I name a container in Docker compose?

In docker-compose, you can do this by setting the “container_name” property on any of your containers.

What is volume in Docker compose Yml?

When you execute a docker-compose command, the volumes directive in docker-compose. yml file mounts source directories or volumes from your computer at target paths inside the container. If a matching target path exists already as part of the container image, it will be overwritten by the mounted path.

What is named volume in docker?

There are two types of volumes to consider: Named volumes have a specific source from outside the container, for example, awesome:/bar . Anonymous volumes have no specific source, therefore, when the container is deleted, you can instruct the Docker Engine daemon to remove them.


2 Answers

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.

like image 109
Juliano Alves Avatar answered Sep 21 '22 15:09

Juliano Alves


Just search in the docker documentation for hosted mounted volumes:

version: '2'

services:
  web:
    image: modx
    environment:
      - MYSQL_PORT_3306_TCP=3306
      - MODX_DB_HOST=mysql:3306
    volumes: 
      - /home/muzzstick/dev/modxdev/html:/var/www/html

    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

Change /var/www/html to the directory where the html files will be inside the container. And also create the directory at the left in your host and give read permission to all users.

Regards

like image 36
Carlos Rafael Ramirez Avatar answered Sep 22 '22 15:09

Carlos Rafael Ramirez