Rider_BY Rider_BY - 14 days ago 6
PHP Question

How to mount volume in docker compose version '2' format

So I have a trouble with mount new format version 2 docker compose file.
I have the code in folder

wordpress
inside where is location
docker-compose
file also I have inside the folder code docker file like this:

FROM debian:jessie
VOLUME /var/www/wordpress


When I used old format like this:

application:
build: code
volumes:
- ./wordpress:/var/www/wordpress
- ./logs/wordpress:/var/www/wordpress/app/logs
tty: true
db:
image: mysql
ports:
- 3306:3306
environment:
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: somename
MYSQL_USER: root
MYSQL_PASSWORD: root
php:
build: php-fpm
ports:
- 9001:9001
volumes_from:
- application
links:
- db
nginx:
build: nginx
ports:
- 8080:80
links:
- php
volumes_from:
- application
volumes:
- ./logs/nginx/:/var/log/nginx
elk:
image: willdurand/elk
ports:
- 81:80
volumes:
- ./elk/logstash:/etc/logstash
- ./elk/logstash/patterns:/opt/logstash/patterns
volumes_from:
- application
- php
- nginx


When I started use version '2' the same as code as previous version I got any error so I reformat my compose file and move
dockerfile
from code folder to insider main folder where is location
docker-compose
file. My new version docker-compose became looks like as:

version: '2'
services:
web:
build: .
volumes:
- /wordpress:/var/www/wordpress
- /logs/wordpress:/var/www/wordpress/app/logs
tty: true

db:
image: mysql
ports:
- 3306:3306
environment:
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: somename
MYSQL_USER: root
MYSQL_PASSWORD: root
php:
build: php-fpm
ports:
- 9001:9001
volumes_from:
- web
links:
- db
nginx:
build: nginx
ports:
- 82:82
links:
- php
volumes_from:
- web
volumes:
- /logs/nginx/:/var/log/nginx
elk:
image: willdurand/elk
ports:
- 81:80
volumes:
- /elk/logstash:/etc/logstash
- /elk/logstash/patterns:/opt/logstash/patterns
volumes_from:
- web
- php
- nginx


Finally after reformat the code
docker-compose
was successfully build and up but when I open my
php
and
nginx
container inside both of them in /
var/www/worpdress
I have just empty folder app is not my wordpress project.
In which place I was mistake with settings mount project volume?
Thanks in advance.

Answer

The problem is with the way you are defining the local directories to be used for the volumes. In your previous version, you were using ./wordpress, while in the new one, you're using just /wordpress.

When referencing local directories for volume mappings, they always have to start with ./ - please try this:

version: '2'
services:
  web:
    build: .
    volumes:
      - ./wordpress:/var/www/wordpress
      - ./logs/wordpress:/var/www/wordpress/app/logs

One more thing: I recommend to always enclose the volume mappings in double quotes to avoid issues with space characters, e.g.:

version: '2'
services:
  web:
    build: .
    volumes:
      - "./wordpress:/var/www/wordpress"
      - "./logs/wordpress:/var/www/wordpress/app/logs"