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

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

问题描述

默认运行时

docker run -it [myimage]

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 attachdocker 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
  2. 在您的 Dockerfile 中提及它
  3. 使用 `su 更改用户

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

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