簡體   English   中英

如何將用戶添加到 Docker 容器?

[英]How to add users to Docker container?

我有一個 docker 容器,里面運行着一些進程(uwsgi 和 celery)。 我想為這些進程創建一個 celery 用戶和一個 uwsgi 用戶,以及它們都屬於的工作組,以便分配權限。

我嘗試將RUN adduser uwsgiRUN adduser celery添加到我的 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] 

訣竅是使用useradd而不是它的交互式包裝器adduser 我通常使用以下方式創建用戶:

RUN useradd -ms /bin/bash newuser

它為用戶創建一個主目錄並確保 bash 是默認 shell。

然后您可以添加:

USER newuser
WORKDIR /home/newuser

到你的 dockerfile。 之后的每個命令以及交互式會話都將作為用戶newuser執行:

docker run -t -i image
newuser@131b7ad86360:~$

在調用 user 命令之前,您可能必須授予newuser執行您打算運行的程序的權限。

出於安全原因,在容器內使用非特權用戶是一個好主意。 它也有一些缺點。 最重要的是,從您的圖像派生圖像的人必須切換回 root 才能以超級用戶權限執行命令。

Ubuntu

Dockerfile中嘗試以下行:

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

useradd選項(參見: man useradd ):

  • -r , --system創建一個系統帳戶。 請參閱:創建系統帳戶的含義
  • -m , --create-home創建用戶的主目錄。
  • -d , --home-dir HOME_DIR新帳戶的主目錄。
  • -s , --shell SHELL新帳戶的登錄shell。
  • -g , --gid GROUP主要組的名稱或 ID。
  • -G , --groups GROUPS補充組列表。
  • -u , --uid UID指定用戶 ID。 請參閱: 了解 uid 和 gid 如何在 Docker 容器中工作
  • -p , --password PASSWORD新帳戶的加密密碼(例如ubuntu )。

設置默認用戶密碼

要設置用戶密碼,請將-p "$(openssl passwd -1 ubuntu)"添加到useradd命令。

或者,將以下行添加到您的Dockerfile

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

第一條shell 指令是確保在RUN之前啟用-o pipefail選項,其中包含管道。 閱讀更多: Hadolint:整理你的 Dockerfile

為了避免 adduser 的交互式問題,您可以使用以下參數調用它:

RUN adduser --disabled-password --gecos '' newuser

--gecos參數用於設置附加信息。 在這種情況下,它只是空的。

在帶有busybox的系統(如Alpine)上,使用

RUN adduser -D -g '' newuser

busybox adduser

從安全角度來看,在 docker 中添加用戶並在該用戶下運行您的應用程序是非常好的做法。 為此,我建議執行以下步驟:

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 項目文件、創建用戶組和用戶、為用戶分配項目文件夾的權限、切換到新創建的用戶並在該用戶下運行應用程序的完整示例。

你可以模仿開源的Dockerfile,例如:

節點: node12-github

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

超集: 超集-github

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

我認為這是遵循開源的好方法。

每個人都有自己的最愛,這是我的:

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

參考: man useradd ( alt )

RUN行將添加用戶和組app

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

如果要將圖像用作基礎圖像,請使用比app更具體的名稱。 順便說一句,如果您確實需要,請包含--shell /bin/bash


部分學分: Ryan M 的回答

或者,您可以這樣做。

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

第一個命令創建名為demo的組。 第二個命令創建演示用戶並將他添加到先前創建的演示組。

標志代表:

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

將此行添加到您的 Dockerfile(您可以通過這種方式運行任何 linux 命令)

RUN useradd -ms /bin/bash yourNewUserName

請在 dockerfile 中添加以下條目,以便在容器中創建 sudo 用戶。

RUN useradd -m  -s /bin/bash ubuntu
RUN usermod -aG sudo ubuntu && echo "ubuntu ALL=(ALL) NOPASSWD: ALL" > /etc/sudoers.d/ubuntu
RUN chmod 044 /etc/sudoers.d/ubuntu
USER ubuntu:ubuntu
WORKDIR /home/ubuntu

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM