简体   繁体   中英

Docker does not mount volume as current user

I have a website that I'm trying to run inside Docker container. To avoid file permission errors, I change the ID of user www-data to match my user id on host. This setup worked for me, but after some time it stopped working, and I cannot find reason why.

FROM php:7.4-fpm

ARG HOST_UID

# Not relevant lines skipped

RUN usermod -u $HOST_UID www-data
RUN groupmod -g $HOST_UID www-data

Then I build container with:

docker compose build --build-arg HOST_UID=$(id -u)

All files on host belong to my user:

在此处输入图像描述

After starting container, I can see that ID is changed:

在此处输入图像描述

However, inside container they belong to root:

在此处输入图像描述

Could this be because docker daemon runs as root and mounts the volume as such?

I should have noted that this Dockerfile installs Supervisord (to run my background scripts), so I cannot run whole container with my user – this fixes permissions but nothing can be installed inside container.

You should use the -u flag for docker compose run .

Here an example:

setup:

mkdir mihai
touch mihai/test.txt

docker-compose.yml

version: '3'

services:
  test:
    image: "alpine:latest"
    volumes:
      - "./mihai:/mihai"

Run normal

docker compose run test ls -l /mihai

gives output:

total 0
-rw-r--r--    1 root     root             0 Sep 18 11:39 test.txt

Run as user:

docker compose run -u "$(id -u):$(id -g)" test ls -l /mihai

gives output:

total 0
-rw-r--r--    1 501      dialout          0 Sep 18 11:39 test.txt

If you use a custom image where you actually declare user and group then you will see a nicer output than 501 and dialout .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM