我使用alpine(或基于alpine的图像)作为Dockerfile中的基本图像。我需要添加哪些指令来创建用户?
最终,我将使用这个用户运行我放入容器中的应用程序,这样根用户就不用运行了。
我使用alpine(或基于alpine的图像)作为Dockerfile中的基本图像。我需要添加哪些指令来创建用户?
最终,我将使用这个用户运行我放入容器中的应用程序,这样根用户就不用运行了。
Alpine使用adduser和addgroup命令创建用户和组(而不是useradd和usergroup)。
FROM alpine:latest
# Create a group and user
RUN addgroup -S appgroup && adduser -S appuser -G appgroup
# Tell docker that all future commands should run as the appuser user
USER appuser
adduser的标志是:
Usage: adduser [OPTIONS] USER [GROUP] Create new user, or add USER to GROUP -h DIR Home directory -g GECOS GECOS field -s SHELL Login shell -G GRP Group -S Create a system user -D Don't assign a password -H Don't create home directory -u UID User id -k SKEL Skeleton directory (/etc/skel)
添加新的用户官方文档
命令为adduser和addgroup。
下面是一个Docker模板,你可以在busybox环境(alpine)以及基于debian的环境(Ubuntu等)中使用:
ENV USER=docker
ENV UID=12345
ENV GID=23456
RUN adduser \
--disabled-password \
--gecos "" \
--home "$(pwd)" \
--ingroup "$USER" \
--no-create-home \
--uid "$UID" \
"$USER"
注意事项:
——disable -password防止提示输入密码 ——gecos“”在debian系统上绕过了“全名”等提示 ——home "$(pwd)"将用户的home设置为WORKDIR。你可能不想要这个。 ——no-create-home防止cruft从/etc/skel复制到目录中
这些应用程序的使用描述缺少adduser和addgroup代码中出现的长标记。
下面的长形式标志应该在alpine和debian衍生版本中都能工作:
adduser
BusyBox v1.28.4 (2018-05-30 10:45:57 UTC) multi-call binary.
Usage: adduser [OPTIONS] USER [GROUP]
Create new user, or add USER to GROUP
--home DIR Home directory
--gecos GECOS GECOS field
--shell SHELL Login shell
--ingroup GRP Group (by name)
--system Create a system user
--disabled-password Don't assign a password
--no-create-home Don't create home directory
--uid UID User id
需要注意的一点是,如果没有设置——ingroup,则会分配GID以匹配UID。如果与提供的UID对应的GID已经存在,adduser将失败。
addgroup
BusyBox v1.28.4 (2018-05-30 10:45:57 UTC) multi-call binary.
Usage: addgroup [-g GID] [-S] [USER] GROUP
Add a group or add a user to a group
--gid GID Group id
--system Create a system group
我在尝试编写自己的fixuid项目的替代方案以作为主机的UID/GID来运行容器时发现了这一切。
我的入口点助手脚本可以在GitHub上找到。
目的是将该脚本作为ENTRYPOINT的第一个参数,这将导致Docker从相关绑定挂载推断UID和GID。
可能需要一个环境变量“TEMPLATE”来确定从哪里推断权限。
(在写作的时候,我没有脚本的文档。它仍然在待办事项清单上!!)
有一个包阴影带来useradd和usermod。
Adduser有一些愚蠢的限制:
$ sudo adduser --disabled-password root
adduser: user 'root' in use
但是usermod没有:
$ sudo apk add shadow
$ sudo usermod --unlock root