V
V
vldud2018-06-28 21:24:04
Docker
vldud, 2018-06-28 21:24:04

How to set user for volume in docker-compose.yml?

There is docker-compose.yml:

version: '3'
services:
    php:
        volumes:
            - "./web:/var/www/html"

Chown for ./web is root:root, the www-data user inside the container has no rights to this folder. You probably need to create a special user / group with rights to this folder and somehow pass it inside the container, but I don’t see a suitable one in the description of the keys for docker-compose.yml.

Answer the question

In order to leave comments, you need to log in

4 answer(s)
R
rustler2000, 2018-06-28
@rustler2000

This is not a volume, this is a mount and the rights to it are the same as on ./web.
Put chmod 777 locally or mount /etc/passwd /etc/shadow and customize the container so that the uid gid matches the host.

M
Max, 2018-06-29
@MaxDukov

I in a similar case threw /etc/passwd and /etc/groups into the container,
after that the link uid-name group-gid works.

V
vldud, 2018-07-05
@vldud

Max , it doesn't work. After prescribing a group and user, the server stops responding altogether. With additions according to your scheme (if, of course, I understood it correctly), it turns out like this:

version: '3'
services:
    web:
        image: nginx:alpine
        volumes:
            - "./etc/nginx/default.conf:/etc/nginx/conf.d/default.conf"
            - "./etc/ssl:/etc/ssl"
            - "./web:/var/www/html"
            - "./etc/nginx/default.template.conf:/etc/nginx/conf.d/default.template"
            - "/etc/passwd:/etc/passwd"
            - "/etc/groups:/etc/groups"
        ports:
            - "80:80"
            - "3000:443"
        environment:
            - NGINX_HOST=${NGINX_HOST}
        command: /bin/sh -c "envsubst '$$NGINX_HOST' < /etc/nginx/conf.d/default.template > /etc/nginx/conf.d/default.conf && nginx -g 'daemon off;'"
        restart: always
        user: "1001:1001"
        depends_on:
            - php
            - mysqldb
    php:
        build:
            context: .
            dockerfile: DockerfilePhpWithGit
        restart: always
        user: "1001:1001"
        volumes:
            - "./etc/php/php.ini:/usr/local/etc/php/conf.d/php.ini"
            - "./web:/var/www/html"
            - "/etc/passwd:/etc/passwd"
            - "/etc/groups:/etc/groups"

where 1001:1001 is the www-data:www-data user. A user with the same name and group is created in the php service

A
allter, 2019-01-10
@allter

In such a situation, I modified local users in the entrypoint.sh of the container, getting the uid from the rights of the mounted directory:

SRC_DIR=/path/to/mounted/volume
DIR=/path/to/dir
USER=www-data
GROU=www-data

$uid=$(stat -c '%u' $SRC_DIR)                                                                                                                    
$gid=$(stat -c '%g' $SRC_DIR)                                                                                                                    
echo $uid > /root/uid                                                                                                                          
echo $gid > /root/gid                                                                                                                          
usermod -u $uid $USER                                                                                                                                   
groupmod -g $gid $GROUP                                                                                                                                  
mkdir -p $DIR                                                                                                                                            
chown -R $USER:$GROUP $DIR

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question