docker - Docker 入口点用户切换
问题描述
我正在创建一个 docker 映像以用作其他应用程序的基础。要求是:
- 应用程序必须以非 root 用户身份运行
- 可选地,必须在执行应用程序之前加载证书
我创建了以下 Dockerfile
FROM node:14.15.1-alpine3.11
# Specify node/npm related envs
ENV NPM_CONFIG_LOGLEVEL=warn \
NO_UPDATE_NOTIFIER=1
# Change cwd for next commands
WORKDIR /home/node/code
# Set local registry
RUN echo "registry=http://192.168.100.175:4873" > /home/node/.npmrc && \
chown -R node:node /home/node && \
apk add --update --no-cache tzdata=2021a-r0 ca-certificates=20191127-r2 su-exec=0.2-r1
# Need root to update CA certificates in entrypoint.sh and then switch back to restricted user
USER root
COPY entrypoint.sh entrypoint.sh
ENTRYPOINT [ "./entrypoint.sh" ]
# Execute the service entrypoint
CMD ["sh"]
和entrypoint.sh
#!/bin/sh
DIR_CRT="/home/node/certificates"
if [ "$(ls -A ${DIR_CRT})" ]; then
cp -r "${DIR_CRT}/." /usr/local/share/ca-certificates/
update-ca-certificates
echo "******* Updated CA certificates *******"
fi
exec su-exec node "$@"
这似乎满足了要求,但我注意到,如果我在图像中打开一个 shell,node
即使我从参数中指定了一个不同的 shell,它也总是与用户在一起:
$ docker run --rm -it -u root docker.repo.asts.com/scc-2.0/app-tg:1.6.0-beta50 whoami
node
是否可以同时满足要求和与所需用户一起执行直接命令的可能性?
解决方案
docker run -u xxx
仅当您未使用exec
inentrypoint
更改 PID1 时才有效。例如
$ docker run --rm -it node:14.15.1-alpine3.11 whoami
root
$ docker run --rm -u node -it node:14.15.1-alpine3.11 whoami
node
使用后将exec su-exec node "$@"
用户更改为node
,您将无法-u xxx
再次使用。唯一的解决方案是像下一个那样覆盖入口点,但我看不到这里的含义:
docker run --rm -u root --entrypoint=/bin/sh xxx
但是,您仍然可以在现有容器中使用docker exec -u root
或docker exec -u node
获取该用户的外壳。
推荐阅读
- node.js - 如何使用 Nodejs、mongodb 和 express 在对象数组数组中使用计数查询?
- node.js - 会话未存储在护照中
- wordpress - 最大的内容绘画:现场数据和工具之间的差异
- security - 如何为私有 Web 应用实施最小访问控制?
- javascript - 带有chart.js的刻度线和X轴之间的空间
- ios - 如何快速制作嵌套的可折叠表格视图?
- google-cloud-speech - 谷歌云语音到文本不返回标点符号
- php - PHP/JSON-OOCSS:单击任何元素后如何永久修改 CSS 文件?
- react-native - React Native Navigation Library - 隐藏底部选项卡
- discord.js - discord.js中间隔使用的时间单位是什么?