以非 root 用户身份运行 docker containe 进程

Abh*_*ava 1 docker dockerfile

以非 root 用户身份在 docker 容器中运行进程的最佳方法是什么?在主机和 docker 组中添加用户,或者通过创建用户和组作为 Dockerfile 的一部分

Exa*_*a37 5

\n

以非 root 用户身份在 docker 容器中运行进程的最佳方法是什么?

\n
\n\n

是通过在 dockerfile 中创建一个非特权用户,其用户 ID 大于或等于1000,并确保该用户是启动 docker 容器时的默认用户。

\n\n
\n

在主机和docker组中添加用户

\n
\n\n

主机不与 docker 容器共享用户或组,仅共享内核。

\n\n
\n

或者通过创建用户和组作为 Dockerfile 的一部分

\n
\n\n

是的,正如我已经提到的,这是正确的方法。

\n\n

Dockerfile

\n\n
FROM debian:9\n\nRUN apt update && \\\n    apt upgrade -y && \\\n\n    # Adjust the user id 1000 to match the user id in your Linux host\n    useradd -m -u 1000 -s /usr/bin/bash stackoverflow\n\n# Ensures that the docker container always start with this user, unless we \n# explicit override it with docker run --user username ....\nUSER stackoverflow\n\n# The default dir when starting the docker container. \nWORKDIR /home/stackoverflow\n\nCMD bash\n\n
Run Code Online (Sandbox Code Playgroud)\n\n构建它:\n\n
sudo docker build --tag stackoverflow .\n
Run Code Online (Sandbox Code Playgroud)\n\n

Docker容器

\n\n

让我们首先在主机中创建一个文件以映射到 docker 容器:

\n\n
\xe2\x95\xad\xe2\x94\x80exadra37@laptop ~/Developer/StackOverflow/57331317  \n\xe2\x95\xb0\xe2\x94\x80\xe2\x9e\xa4  printf "\\nHost user:  $(id -un) with id: $(id -u)\\n" > usernames.txt\n\n\xe2\x95\xad\xe2\x94\x80exadra37@laptop ~/Developer/StackOverflow/57331317  \n\xe2\x95\xb0\xe2\x94\x80\xe2\x9e\xa4  ls -al\ntotal 16\ndrwxr-xr-x 2 exadra37 exadra37 4096 Aug  3 09:58 .\ndrwxr-xr-x 6 exadra37 exadra37 4096 Aug  2 18:59 ..\n-rw-r--r-- 1 exadra37 exadra37  173 Aug  2 19:18 Dockerfile\n-rw-r--r-- 1 exadra37 exadra37   82 Aug  3 09:59 usernames.txt\n\n\xe2\x95\xad\xe2\x94\x80exadra37@laptop ~/Developer/StackOverflow/57331317  \n\xe2\x95\xb0\xe2\x94\x80\xe2\x9e\xa4  cat usernames.txt \n\nHost user:  exadra37 with id: 1000\n\n
Run Code Online (Sandbox Code Playgroud)\n\n

使用我们刚刚在主机中创建的文件运行 docker 容器:

\n\n
\xe2\x95\xad\xe2\x94\x80exadra37@laptop ~/Developer/StackOverflow/57331317  \n\xe2\x95\xb0\xe2\x94\x80\xe2\x9e\xa4  sudo docker run --rm -v $PWD/usernames.txt:/home/stackoverflow/usernames.txt -it stackoverflow bash \n
Run Code Online (Sandbox Code Playgroud)\n\n

现在我们位于 docker 容器内:

\n\n
stackoverflow@367236be38d1:~$ ls -al\ntotal 28\ndrwxr-xr-x 1 stackoverflow stackoverflow 4096 Aug  3 08:58 .\ndrwxr-xr-x 1 root          root          4096 Aug  2 18:19 ..\n-rw-r--r-- 1 stackoverflow stackoverflow  220 May 15  2017 .bash_logout\n-rw-r--r-- 1 stackoverflow stackoverflow 3526 May 15  2017 .bashrc\n-rw-r--r-- 1 stackoverflow stackoverflow  675 May 15  2017 .profile\n-rw-r--r-- 1 stackoverflow stackoverflow   36 Aug  3 08:58 usernames.txt\n\nstackoverflow@367236be38d1:~$ cat usernames.txt \n\n\nHost user:  exadra37 with id: 1000\n
Run Code Online (Sandbox Code Playgroud)\n\n

正如我们在上面看到的,我们可以读取我们在主机中创建的文件,甚至可以保存到其中:

\n\n
stackoverflow@367236be38d1:~$ printf "\\nContainer user:  $(id -un) with id: $(id -u)\\n" >> usernames.txt\n
Run Code Online (Sandbox Code Playgroud)\n\n

让我们再次读取该文件:

\n\n
stackoverflow@367236be38d1:~$ cat usernames.txt \n\nHost user:  exadra37 with id: 1000\n\nContainer user:  stackoverflow with id: 1000\n
Run Code Online (Sandbox Code Playgroud)\n\n

正如我们所看到的,该文件包含另一行,并且我们可以看到,尽管用户名不同,但主机和容器用户的用户 ID 是相同的,这使得我们在主机和容器之间不会出现权限问题。容器。

\n\n

让我们手动确认容器中的用户名和 ID:

\n\n
stackoverflow@367236be38d1:~$ id -u\n1000\n\nstackoverflow@367236be38d1:~$ id -un\nstackoverflow\n
Run Code Online (Sandbox Code Playgroud)\n\n

现在在主机中:

\n\n
stackoverflow@367236be38d1:~$ exit\nexit\n\n\xe2\x95\xad\xe2\x94\x80exadra37@laptop ~/Developer/StackOverflow/57331317  \n\xe2\x95\xb0\xe2\x94\x80\xe2\x9e\xa4  id -u\n1000\n\n\xe2\x95\xad\xe2\x94\x80exadra37@laptop ~/Developer/StackOverflow/57331317  \n\xe2\x95\xb0\xe2\x94\x80\xe2\x9e\xa4  id -un\nexadra37\n
Run Code Online (Sandbox Code Playgroud)\n\n

正如我们所看到的,它们与我们保存到文件中的相同,并且您可以再次看到主机和 docker 容器之间相同的用户 ID,尽管用户名不同。对于linux来说重要的是用户id,用户名只是为了帮助我们人类。

\n\n

在完成之前,让我们确保主机中的文件反映了我们在 docker 容器中对其所做的更改:

\n\n
\xe2\x95\xad\xe2\x94\x80exadra37@laptop ~/Developer/StackOverflow/57331317  \n\xe2\x95\xb0\xe2\x94\x80\xe2\x9e\xa4  cat usernames.txt \n\nHost user:  exadra37 with id: 1000\n\nContainer user:  stackoverflow with id: 1000\n\n\xe2\x95\xad\xe2\x94\x80exadra37@laptop ~/Developer/StackOverflow/57331317  \n\xe2\x95\xb0\xe2\x94\x80\xe2\x9e\xa4  \n
Run Code Online (Sandbox Code Playgroud)\n