我有一个docker容器,里面运行着一些进程(uwsgi和芹菜)。为了分配权限,我想为这些进程创建一个芹菜用户和一个uwsgi用户,以及它们都属于的工作组。

我尝试将RUN adduser uwsgi和RUN adduser芹菜添加到我的Dockerfile中,但这导致了问题,因为这些命令提示输入(我已经在下面发布了构建的响应)。

将用户添加到Docker容器中,从而为容器中运行的工作者设置权限的最佳方法是什么?

我的Docker映像是基于官方Ubuntu14.04基础构建的。

下面是运行adduser命令时Dockerfile的输出:

Adding user `uwsgi' ...
Adding new group `uwsgi' (1000) ... 
Adding new user `uwsgi' (1000) with group `uwsgi' ... 
Creating home directory `/home/uwsgi' ...
Copying files from `/etc/skel' ... 
[91mEnter new UNIX password: Retype new UNIX password: [0m 
[91mpasswd: Authentication token manipulation error
passwd: password unchanged
[0m 
[91mUse of uninitialized value $answer in chop at /usr/sbin/adduser line 563.
[0m 
[91mUse of uninitialized value $answer in pattern match (m//) at /usr/sbin/adduser line 564.
[0m 
Try again? [y/N] 
Changing the user information for uwsgi
Enter the new value, or press ENTER for the default
    Full Name []: 
Room Number []:     Work Phone []:  Home Phone []:  Other []: 
[91mUse of uninitialized value $answer in chop at /usr/sbin/adduser line 589.
[0m 
[91mUse of uninitialized value $answer in pattern match (m//) at /usr/sbin/adduser line 590.
[0m 
Is the information correct? [Y/n] 
---> 258f2f2f13df 
Removing intermediate container 59948863162a 
Step 5 : RUN adduser celery 
---> Running in be06f1e20f64 
Adding user `celery' ...
Adding new group `celery' (1001) ... 
Adding new user `celery' (1001) with group `celery' ... 
Creating home directory `/home/celery' ...
Copying files from `/etc/skel' ... 
[91mEnter new UNIX password: Retype new UNIX password: [0m 
[91mpasswd: Authentication token manipulation error
passwd: password unchanged
[0m 
[91mUse of uninitialized value $answer in chop at /usr/sbin/adduser line 563.
[0m 
[91mUse of uninitialized value $answer in pattern match (m//) at /usr/sbin/adduser line 564.
[0m 
Try again? [y/N] 
Changing the user information for celery
Enter the new value, or press ENTER for the default
    Full Name []:   Room Number []:     Work Phone []: 
Home Phone []:  Other []: 
[91mUse of uninitialized value $answer in chop at /usr/sbin/adduser line 589.
[0m 
[91mUse of uninitialized value $answer in pattern match (m//) at /usr/sbin/adduser line 590.
[0m 
Is the information correct? [Y/n] 

当前回答

或者你也可以这样做。

RUN addgroup demo && adduser -DH -G demo demo

第一个命令创建名为demo的组。第二个命令创建演示用户并将其添加到前面创建的演示组。

Flags代表:

-G Group
-D Don't assign password
-H Don't create home directory

其他回答

你可以模仿开源Dockerfile,例如:

节点:node12-github

RUN groupadd --gid 1000 node \
    && useradd --uid 1000 --gid node --shell /bin/bash --create-home node

超集:superset-github

RUN useradd --user-group --create-home --no-log-init --shell /bin/bash 
    superset

我认为这是遵循开源的好方法。

在docker中添加user并在该用户下运行应用程序是非常好的安全实践。要做到这一点,我建议采取以下步骤:

FROM node:10-alpine

# Copy source to container
RUN mkdir -p /usr/app/src

# Copy source code
COPY src /usr/app/src
COPY package.json /usr/app
COPY package-lock.json /usr/app

WORKDIR /usr/app

# Running npm install for production purpose will not run dev dependencies.
RUN npm install -only=production    

# Create a user group 'xyzgroup'
RUN addgroup -S xyzgroup

# Create a user 'appuser' under 'xyzgroup'
RUN adduser -S -D -h /usr/app/src appuser xyzgroup

# Chown all the files to the app user.
RUN chown -R appuser:xyzgroup /usr/app

# Switch to 'appuser'
USER appuser

# Open the mapped port
EXPOSE 3000

# Start the process
CMD ["npm", "start"]

以上步骤是复制NodeJS项目文件、创建用户组和用户、为项目文件夹分配用户权限、切换到新创建的用户并在该用户下运行应用程序的完整示例。

或者你也可以这样做。

RUN addgroup demo && adduser -DH -G demo demo

第一个命令创建名为demo的组。第二个命令创建演示用户并将其添加到前面创建的演示组。

Flags代表:

-G Group
-D Don't assign password
-H Don't create home directory

Ubuntu

试试Dockerfile中的以下代码行:

RUN useradd -rm -d /home/ubuntu -s /bin/bash -g root -G sudo -u 1001 ubuntu
USER ubuntu
WORKDIR /home/ubuntu

用户添加选项(参见:man Useradd):

-r, --system Create a system account. see: Implications creating system accounts -m, --create-home Create the user's home directory. -d, --home-dir HOME_DIR Home directory of the new account. -s, --shell SHELL Login shell of the new account. -g, --gid GROUP Name or ID of the primary group. -G, --groups GROUPS List of supplementary groups. -u, --uid UID Specify user ID. see: Understanding how uid and gid work in Docker containers -p, --password PASSWORD Encrypted password of the new account (e.g. ubuntu).

设置默认用户密码

在useradd命令中添加-p "$(openssl passwd -1 ubuntu)",设置用户密码。

或者在Dockerfile中添加以下代码行:

SHELL ["/bin/bash", "-o", "pipefail", "-c"]
RUN echo 'ubuntu:ubuntu' | chpasswd

第一条shell指令是确保在运行管道之前启用-o pipefail选项。Hadolint:检测您的Dockerfile。

每个人都有自己最喜欢的,这是我的:

RUN useradd --user-group --system --create-home --no-log-init app
USER app

参考:man useradd (alt)

上面的RUN行将添加用户和组应用程序:

root@ef3e54b60048:/# id app
uid=999(app) gid=999(app) groups=999(app)

如果映像要重用为基本映像,请使用比app更具体的名称。顺便说一句,如果确实需要,可以包含——shell /bin/bash。


部分功劳:Ryan M