[英]Docker does not mount volume as current user
I have a website that I'm trying to run inside Docker container.我有一个网站,我试图在 Docker 容器中运行。 To avoid file permission errors, I change the ID of user
www-data
to match my user id on host.为了避免文件权限错误,我更改了用户
www-data
的 ID 以匹配我在主机上的用户 ID。 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:启动容器后,我可以看到 ID 已更改:
However, inside container they belong to root:但是,在容器内部,它们属于 root:
Could this be because docker daemon runs as root and mounts the volume as such?这可能是因为 docker 守护程序以 root 身份运行并安装卷吗?
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.我应该注意到这个 Dockerfile 安装了 Supervisord(运行我的后台脚本),所以我不能用我的用户运行整个容器——这修复了权限,但容器内不能安装任何东西。
You should use the -u
flag for docker compose run
.您应该为
docker compose run
使用-u
标志。
Here an example:这里有一个例子:
setup:设置:
mkdir mihai
touch mihai/test.txt
docker-compose.yml docker-compose.yml
version: '3'
services:
test:
image: "alpine:latest"
volumes:
- "./mihai:/mihai"
Run normal运行正常
docker compose run test ls -l /mihai
gives output:给出 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:给出 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
.如果您在实际声明用户和组的地方使用自定义图像,那么您会看到比
501
和 dialout 更好的dialout
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.