简体   繁体   English

Docker:具有 --volume 绑定挂载的文件权限

[英]Docker: file permissions with --volume bind mount

I'm following the guidelines from: https://denibertovic.com/posts/handling-permissions-with-docker-volumes/ to setup a --volume bind mount in my container and creating a user in the guest container with the same UID as my host user - the theory being that my container user should be able to access the mount.我正在遵循以下指南: https ://denibertovic.com/posts/handling-permissions-with-docker-volumes/ 在我的容器中设置 --volume 绑定安装并在来宾容器中创建一个用户作为我的主机用户的 UID - 理论是我的容器用户应该能够访问安装。 It's not working for me and I'm looking for some pointers to try next.它对我不起作用,我正在寻找一些指示以尝试下一步。

More background details:更多背景详情:

My Dockerfile starts from an alpine base and adds python dev packages.我的 Dockerfile 从一个 alpine 基地开始,并添加了 python 开发包。 It copies across an entrypoint.sh script per guidelines from denibertovic.它按照来自 denibertovic 的指南在 entrypoint.sh 脚本中复制。 It then jumps to the entrpoint.sh script.然后跳转到 entrpoint.sh 脚本。

FROM alpine

RUN apk update
RUN apk add bash
RUN apk add python3
RUN apk add python3-dev
RUN apk add su-exec

COPY entrypoint.sh /usr/local/bin/entrypoint.sh
RUN chmod +x  /usr/local/bin/entrypoint.sh

ENTRYPOINT ["/usr/local/bin/entrypoint.sh"]

The entrpoint.sh script adds a user to the container with the UID passed in as an environment variable. entrpoint.sh 脚本使用作为环境变量传入的 UID 将用户添加到容器中。

#!/bin/bash

# Add local user
# Either use the LOCAL_USER_ID if passed in at runtime or
# fallback

USER_ID=${LOCAL_USER_ID:-9001}

echo "Starting with UID : $USER_ID"
adduser -s /bin/bash -u $USER_ID -H -D user
export HOME=/home/user

su-exec user "$@"

The container builds no problem.容器构建没有问题。 I then run it with the following command line:然后我使用以下命令行运行它:

sudo docker run -it -e LOCAL_USER_ID=`id -u` -v `realpath ../..`:/ws django-runtime /bin/bash

You'll see that I'm passing in my host UID to be mapped to the container user's UID and I'm asking for a volume bind mount from my local working directory to the /ws mountpoint in the container.您会看到我正在传递我的主机 UID 以映射到容器用户的 UID,并且我正在请求从我的本地工作目录到容器中的 /ws 安装点的卷绑定安装。

From the bash shell inside the container I can see that /ws is owned by the 'user' UID matching my own 'id'.从容器内的 bash shell,我可以看到 /ws 归与我自己的“id”匹配的“user”UID 所有。 However, when I go to list the contents of /ws I get a Permission Denied error as follows:但是,当我去列出 /ws 的内容时,我收到一个 Permission Denied 错误,如下所示:

[dleclair@localhost runtime]$ sudo docker run -it -e LOCAL_USER_ID=`id -u` -v `realpath ../..`:/ws django-runtime /bin/bash
[sudo] password for dleclair:
Starting with UID : 1000
bash-5.0$ id
uid=1000(user) gid=1000(user) groups=1000(user)
bash-5.0$ ls -la .
total 0
drwxr-xr-x    1 root     root            27 Feb  8 09:15 .
drwxr-xr-x    1 root     root            27 Feb  8 09:15 ..
-rwxr-xr-x    1 root     root             0 Feb  8 09:15 .dockerenv
drwxr-xr-x    1 root     root            18 Feb  8 07:44 bin
drwxr-xr-x    5 root     root           360 Feb  8 09:15 dev
drwxr-xr-x    1 root     root            91 Feb  8 09:15 etc
drwxr-xr-x    2 root     root             6 Jan 16 21:52 home
drwxr-xr-x    1 root     root            17 Jan 16 21:52 lib
drwxr-xr-x    5 root     root            44 Jan 16 21:52 media
drwxr-xr-x    2 root     root             6 Jan 16 21:52 mnt
drwxr-xr-x    2 root     root             6 Jan 16 21:52 opt
dr-xr-xr-x  119 root     root             0 Feb  8 09:15 proc
drwx------    2 root     root             6 Jan 16 21:52 root
drwxr-xr-x    1 root     root            21 Feb  8 07:44 run
drwxr-xr-x    1 root     root            21 Feb  8 08:22 sbin
drwxr-xr-x    2 root     root             6 Jan 16 21:52 srv
dr-xr-xr-x   13 root     root             0 Feb  8 01:58 sys
drwxrwxrwt    2 root     root             6 Jan 16 21:52 tmp
drwxr-xr-x    1 root     root            19 Feb  8 07:44 usr
drwxr-xr-x    1 root     root            19 Jan 16 21:52 var
drwxrwxr-x    5 user     user           111 Feb  8 02:15 ws
bash-5.0$
bash-5.0$
bash-5.0$ cd /ws
bash-5.0$ ls -la
ls: can't open '.': Permission denied
total 0
bash-5.0$

Appreciate any pointers anyone can offer.感谢任何人可以提供的任何指示。 Thanks!谢谢!

After more searching I found the answer to my problem here: Permission denied on accessing host directory in Docker and here: http://www.projectatomic.io/blog/2015/06/using-volumes-with-docker-can-cause-problems-with-selinux/ .经过更多搜索,我在这里找到了我的问题的答案: 访问 Docker 中的主机目录时权限被拒绝,这里: http : //www.projectatomic.io/blog/2015/06/using-volumes-with-docker-can-cause -问题与selinux/

In short, the problem was with the SELinux default labels for the volume mount blocking access to the mounted files.简而言之,问题在于卷挂载的 SELinux 默认标签阻止了对挂载文件的访问。 The solution was to add a ':Z' trailer to the -v command line argument to force docker to set the appropriate flags against the mounted files to allow access.解决方案是在 -v 命令行参数中添加一个 ':Z' 预告片,以强制 docker 针对已安装的文件设置适当的标志以允许访问。

The command line therefore became:因此命令行变为:

sudo docker run -it -e LOCAL_USER_ID=`id -u` -v `realpath ../..`:/ws:Z django-runtime /bin/bash

Worked like a charm.像魅力一样工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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