当我使用 Alpine 作为基础镜像时,如何添加用户?
How do I add a user when I'm using Alpine as a base image?
我正在使用 alpine
(或基于 Alpine 的图像)作为我的 Dockerfile 中的基础图像。我需要添加哪些说明来创建用户?
最终我将使用此用户 运行 我将放入容器的应用程序,这样 root 用户就不会。
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"
注意以下几点:
--disabled-password
防止提示输入密码
--gecos ""
在基于 Debian 的系统上绕过 "Full Name" 等的提示
--home "$(pwd)"
将用户的家设置为 WORKDIR。 你可能不想要这个。
--no-create-home
防止 cruft 从 /etc/skel
复制到目录中
这些应用程序的使用说明缺少 adduser and addgroup 代码中的长标志。
下面的长格式标志应该在 alpine 和 debian-derivatives 中都有效:
添加用户
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会失败。
添加组
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。
My entrypoint helper script 可以在 GitHub.
上找到
目的是将该脚本作为 ENTRYPOINT
的第一个参数添加到前面,这将导致 Docker 从相关的绑定挂载中推断出 UID 和 GID。
可能需要一个环境变量 "TEMPLATE" 来确定应该从哪里推断权限。
(在撰写本文时,我没有脚本的文档。它仍在待办事项列表中!!)
有包 shadow
带来 useradd
& usermod
。
adduser
有一些愚蠢的限制:
$ sudo adduser --disabled-password root
adduser: user 'root' in use
但usermod
不是:
$ sudo apk add shadow
$ sudo usermod --unlock root
我正在使用 alpine
(或基于 Alpine 的图像)作为我的 Dockerfile 中的基础图像。我需要添加哪些说明来创建用户?
最终我将使用此用户 运行 我将放入容器的应用程序,这样 root 用户就不会。
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"
注意以下几点:
--disabled-password
防止提示输入密码--gecos ""
在基于 Debian 的系统上绕过 "Full Name" 等的提示--home "$(pwd)"
将用户的家设置为 WORKDIR。 你可能不想要这个。--no-create-home
防止 cruft 从/etc/skel
复制到目录中
这些应用程序的使用说明缺少 adduser and addgroup 代码中的长标志。
下面的长格式标志应该在 alpine 和 debian-derivatives 中都有效:
添加用户
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会失败。
添加组
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。
My entrypoint helper script 可以在 GitHub.
上找到目的是将该脚本作为 ENTRYPOINT
的第一个参数添加到前面,这将导致 Docker 从相关的绑定挂载中推断出 UID 和 GID。
可能需要一个环境变量 "TEMPLATE" 来确定应该从哪里推断权限。
(在撰写本文时,我没有脚本的文档。它仍在待办事项列表中!!)
有包 shadow
带来 useradd
& usermod
。
adduser
有一些愚蠢的限制:
$ sudo adduser --disabled-password root
adduser: user 'root' in use
但usermod
不是:
$ sudo apk add shadow
$ sudo usermod --unlock root