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

Den*_*nis 1 docker alpine-linux

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

更多背景详情:

我的 Dockerfile 从一个 alpine 基地开始,并添加了 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"]
Run Code Online (Sandbox Code Playgroud)

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 "$@"
Run Code Online (Sandbox Code Playgroud)

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

sudo docker run -it -e LOCAL_USER_ID=`id -u` -v `realpath ../..`:/ws django-runtime /bin/bash
Run Code Online (Sandbox Code Playgroud)

您会看到我正在传递我的主机 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$
Run Code Online (Sandbox Code Playgroud)

感谢任何人可以提供的任何指示。谢谢!

Den*_*nis 5

经过更多搜索,我在这里找到了我的问题的答案:访问 Docker 中的主机目录时权限被拒绝,这里:http : //www.projectatomic.io/blog/2015/06/using-volumes-with-docker-can-cause -问题与selinux/

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

因此命令行变为:

sudo docker run -it -e LOCAL_USER_ID=`id -u` -v `realpath ../..`:/ws:Z django-runtime /bin/bash
Run Code Online (Sandbox Code Playgroud)

像魅力一样工作。