问题描述
我有一个 ECS Fargate 容器,它运行具有非 root 权限的 nodejs 应用程序,并且还安装到容器内 /.user_data 上的 EFS。
我遵循了 this AWS 教程。我的设置几乎相似。
这是 Docker 文件:
FROM node:12-buster-slim
RUN apt-get update && \
apt-get install -y build-essential \
wget \
python3 \
make \
gcc \
libc6-dev \
git
# delete old user
RUN userdel -r node
# Run as a non-root user
RUN addgroup "new_user_group" && \
useradd "new_user" --gid "new_user_group" \
--home-dir "/home/new_user"
RUN git clone https://github.com/test-app.git /home/new_user/app
RUN chown -R new_user:new_user_group /home/new_user
RUN mkdir -p /home/new_user/.user_data
RUN chown -R new_user:new_user_group /home/new_user/.user_data
RUN chmod -R 755 /home/new_user/
workdir /home/new_user/app
RUN npm install
RUN npm run build
EXPOSE 1880
USER new_user
CMD [ "npm","start" ]
当 Node 应用程序尝试在 /.user_data 内部写入时,我收到读写权限被拒绝错误。
如果我以 root 身份运行容器,应用程序就可以读/写数据。
我尝试使用 UID 和权限向 EFS 添加访问点,但这也无济于事。
感谢任何帮助。
请注意:Dockerfile 在我的本地机器上运行良好。
解决方法
更新
阅读这篇博文 - Developers guide to using Amazon EFS with Amazon ECS and AWS Fargate – Part 2 > POSIX permissions
可能与分配给 ECS 任务的 IAM 角色的 IAM 策略相关。
"...如果 AWS 策略不允许 ClientRootAccess 操作,您的用户将被压缩到一个预定义的 UID:GID,即 65534:65534。从现在开始,标准 POSIX 权限适用:什么这个用户可以做的事情是由POSIX文件系统权限决定的。例如,一个由除65534:65534以外的任何UID:GID拥有的666(rw为所有者,rw为每个人)的文件夹将允许这个保留用户创建一个文件. 但是,除 65534:65534 以外的任何 UID:GID 拥有的文件夹为 644(所有者为 rw,所有人为 r)将不允许此被压缩的用户创建文件。”
确保您的 root-dir 权限设置为 777
。这样任何 UID 都可以读/写这个目录。
为了不那么宽松,将 root-dir 设置为 755
,默认设置为 see the docs。这为 root 用户提供了 read-write-execute
,为组提供了 read-execute
,为所有其他用户提供了 read-execute
。
如果没有对其父目录(目录)的读取权限,则用户 (UID) 无法访问(读取)子目录。
您可以使用 Docker 轻松测试,这是一个快速示例
创建一个 Dockerfile -
FROM ubuntu:20.04
# Fetch values from ARGs that were declared at the top of this file
ARG APP_NAME
ARG APP_ARTIFACT_DIR
ARG APP_HOME_DIR="/app"
ARG APP_USER_NAME="appuser"
ARG APP_GROUP_ID="appgroup"
# Define workdir
ENV HOME="${APP_HOME_DIR}"
WORKDIR "${HOME}"
RUN apt-get update -y && apt-get install tree
# Define env vars
ENV PATH="${HOME}/.local/bin:${PATH}"
# Run as a non-root user
RUN addgroup "${APP_GROUP_ID}" && \
useradd "${APP_USER_NAME}" --gid "${APP_GROUP_ID}" --home-dir "${HOME}" && \
chown -R ${APP_USER_NAME} .
RUN mkdir -p rootdir && \
mkdir -p rootdir/subdir && \
touch rootdir/root.file rootdir/subdir/sub.file && \
chown -R root:root rootdir && \
chmod 600 rootdir rootdir/root.file && \
chmod -R 775 rootdir/subdir
您应该使用 chmod 600
和 chmod -R 775
,尝试不同的权限集,例如 777
和 644
,看看它是否有意义。
构建镜像、运行容器并测试权限 -
docker build boyfromnorth .
docker run --rm -it boyfromnorth bash
root@e0f043d9884c:~$ su appuser
$ ls -la
total 12
drwxr-xr-x 1 appuser root 4096 Jan 30 12:23 .
drwxr-xr-x 1 root root 4096 Jan 30 12:33 ..
drw------- 3 root root 4096 Jan 30 12:23 rootdir
$ ls rootdir
ls: cannot open directory 'rootdir': Permission denied