以非root用户身份连接到docker容器 [英] Connect to docker container as user other than root

查看:252
本文介绍了以非root用户身份连接到docker容器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

默认情况下,运行

docker run -it [myimage]

OR

docker attach [mycontainer]

您以root用户身份连接到终端,但是我想以其他用户身份连接。这可能吗?

you connect to the terminal as root user, but I would like to connect as a different user. Is this possible?

推荐答案

对于 docker run

只需添加选项-user< user> 即可在启动Docker容器时更改为其他用户。

Simply add the option --user <user> to change to another user when you start the docker container.

docker run -it --user nobody busybox

对于 docker附件 docker exec

由于该命令用于附加/执行到现有进程中,因此它直接在此使用当前用户。

Since the command is used to attach/execute into the existing process, therefore it uses the current user there directly.

docker run -it busybox  # CTRL-P/Q to quit
docker attach <container id>  # then you have root user
/ # id
uid=0(root) gid=0(root) groups=10(wheel)

docker run -it --user nobody busybox # CTRL-P/Q to quit
docker attach <container id>  
/ $ id
uid=99(nobody) gid=99(nogroup)

如果您确实想附加到想要拥有的用户,则

If you really want to attach to the user you want to have, then


  1. 以该用户开始 run --user< user> 或使用 USER 在您的 Dockerfile 中提及

  2. 使用`su

  1. start with that user run --user <user> or mention it in your Dockerfile using USER
  2. change the user using `su

这篇关于以非root用户身份连接到docker容器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆