VXD
VXD

Reputation: 3

docker-compose volume behaviour

Using docker-compose, there is an inconsistency in behaviour with volumes.

I'm using nginx:1.15-alpine, if I mount /var/log/nginx, nginx has no problem creating /var/log/nginx/error.log, but if I mount /var/log, nginx cannot create /var/log/nginx/error.log.

Works

webserver:
  image: nginx:1.15-alpine
  ports:
    - '80:80'
  volumes:
    - /mnt/fs/webserver/nginx:/etc/nginx/conf.d
    - /mnt/fs/webserver/log:/var/log/nginx

Does Not Work

webserver:
  image: nginx:1.15-alpine
  ports:
    - '80:80'
  volumes:
    - /mnt/fs/webserver/nginx:/etc/nginx/conf.d
    - /mnt/fs/webserver/log:/var/log

I know that it's not required to mount a directory containing no other directories, because I can successfully mount /var/www/html in the Wordpress container to a volume, /var/www/html contains sub directories.

When using the Does Not Work configuration, nginx has the following error:

nginx: [alert] could not open error log file: open() "/var/log/nginx/error.log" failed (2: No such file or directory)

Why does mounting /var/log/nginx work, yet mounting /var/log does not work?

Upvotes: 0

Views: 2696

Answers (2)

Jaydip Chabhadiya
Jaydip Chabhadiya

Reputation: 600

        Works
        ----------------------------------
        webserver:
          image: nginx:1.15-alpine
          ports:
            - '80:80'
          volumes:
            - /mnt/fs/webserver/nginx:/etc/nginx/conf.d
            - /mnt/fs/webserver/log:/var/log/nginx

        Does Not Work
        ----------------------------------
        webserver:
          image: nginx:1.15-alpine
          ports:
            - '80:80'
          volumes:
            - /mnt/fs/webserver/nginx:/etc/nginx/conf.d
            - /mnt/fs/webserver/log:/var/log

    -------------------------------

Both Work if you given proper permission to /mnt/fs/webserver/log
Just set permission sudo chmod 777 -R /mnt/fs/webserver/log
After that try rebuilding image it works for me...

If that not work try using 
sudo chown -R $USER:$USER /mnt/fs/webserver/log
or
sudo chown -R $USER:$UID /mnt/fs/webserver/log

Upvotes: 1

David Maze
David Maze

Reputation: 159790

If you create an empty directory on the host and mount it to /var/log on the container, then /var/log in the container will be the host's empty directory. If you then try to write to /var/log/nginx/error.log, nothing has created the intermediate nginx directory, and you get that "no such file or directory" error.

Either mount the volume directly on to the target directory, or create the intermediate directory in the host directory (with appropriate permissions) before launching the container:

mkdir /mnt/fs/webserver/log/nginx
chmod 0777 /mnt/fs/webserver/log/nginx

Upvotes: 1

Related Questions