Docker: 文件权限与 --volume 绑定挂载

Docker: file permissions with --volume bind mount

我遵循以下指南:https://denibertovic.com/posts/handling-permissions-with-docker-volumes/ 在我的容器中设置一个 --volume 绑定挂载并在来宾容器中创建一个与我的主机用户具有相同 UID 的用户 - 理论是这样的我的容器用户应该能够访问挂载。它对我不起作用,我正在寻找一些下一步尝试的指示。

更多背景详情:

我的 Dockerfile 从高山基地开始,并添加 python 开发包。它根据 denibertovic 的指南复制 entrypoint.sh 脚本。然后跳转到 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"]

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 "$@"

容器构建没有问题。 然后我使用以下命令行 运行 它:

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

您会看到我正在传递我的主机 UID 以映射到容器用户的 UID,并且我正在请求从我的本地工作目录到容器中的 /ws 挂载点的卷绑定挂载。

从容器内的 bash shell 我可以看到 /ws 属于与我自己的 'id' 相匹配的 'user' UID。但是,当我去列出 /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$

感谢任何人提供的任何建议。谢谢!

经过更多搜索,我在这里找到了问题的答案: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/

简而言之,问题出在卷装载的 SELinux 默认标签阻止对装载文件的访问。解决方案是在 -v 命令行参数中添加 ':Z' 尾部以强制 docker 对已挂载的文件设置适当的标志以允许访问。

命令行因此变成:

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

工作得很好。