Search code examples
dockerdocker-composedockerfile

docker-compose volume is empty even from initialize


I try to create a docker-compose image for different website.

Everything is working fine except for my volumes.

Here is an exemple of the docker-compose.yml:

version: '2'

services:
  website:
    build: 
        context: ./dockerfiles/                                        
        args:                                                                      
            MYSQL_ROOT_PASSWORD: mysqlp@ssword 
    volumes:
        - ./logs:/var/log
        - ./html:/var/www
        - ./nginx:/etc/nginx
        - ./mysql-data:/var/lib/mysql
    ports:
        - "8082:80"
        - "3307:3306"

Anf here is my Dockerfile:

FROM php:5.6-fpm

ARG MYSQL_ROOT_PASSWORD

RUN export DEBIAN_FRONTEND=noninteractive; \
echo mysql-server mysql-server/root_password password $MYSQL_ROOT_PASSWORD | debconf-set-selections; \
echo mysql-server mysql-server/root_password_again  password $MYSQL_ROOT_PASSWORD | debconf-set-selections;

RUN apt-get update && apt-get install -y -q mysql-server php5-mysql nginx wget

EXPOSE 80 3306

VOLUME ["/var/www", "/etc/nginx", "/var/lib/mysql", "/var/log"]

Everything is working well, expect that all my folders are empty into my host volumes. I want to see the nginx conf and mysql data into my folders.

What am I doing wrong?

EDIT 1 : Actually the problem is that I want docker-compose to create the volume in my docker directory if it not exist, and to use this volume if it exist, as it is explain in https://stackoverflow.com/a/39181484 . But it doesn't seems to work.


Solution

  • The problem is that you're expecting files from the Container to be mounted on your host.

    This is not the way it works: it's the other way around:

    Docker mounts your host folder in the container folder you specify. If you go inside the container, you will see that where there were supposed to be the init files, there will be nothing (or whatever was in your host folder(s)), and you can write a file in the folder and it will show up on your host.

    Your best bet to get the init files and modify them for your container is to:

    • Create a container without mounting the folders (original container data will be there)
    • Run the container (the container will have the files in the right place from the installation of nginx etc...) docker run <image>
    • Copy the files out of the container with docker cp <container>:<container_folder>/* <host_folder>
    • Now you have the 'original' files from the container init on your host.
    • Modify the files as needed for the container.
    • Run the container mounting your host folders with the new files in them.

    EDIT: another way to export those files is to mount a folder somewhere else in the container (like /tmp), get into the container (docker exec), and move / copy the files from the container to that mounted folder. The files will appear on the host. Then quit the container, edit the files and re-mount the folder at the right mounting point.

    Notes: You might want to go inside the container with shell (docker run -it <image> /bin/sh) and zip up all the folders to make sure you got everything if there are nested folders, then docker cp ... the zip file

    Also, be careful about filesystem case sensitivity: on linux files are case sensitive. On Mac OS X, they're not. So if you have Init.conf and init.conf in the same folder, they will collide when you copy them to a Mac OS X host.