我正在使用一个Docker映像,它是使用USER命令构建的,以使用一个称为dev的非根用户。 在容器中,我是“dev”,但我想编辑/etc/hosts文件。
所以我需要是根。我正在尝试su命令,但要求我输入根密码。
Docker容器中root用户的默认密码是什么?
我正在使用一个Docker映像,它是使用USER命令构建的,以使用一个称为dev的非根用户。 在容器中,我是“dev”,但我想编辑/etc/hosts文件。
所以我需要是根。我正在尝试su命令,但要求我输入根密码。
Docker容器中root用户的默认密码是什么?
当前回答
为了避免重新构建容器,请执行以下步骤:
docker exec -u 0 -it containerName bash sudo visudo 在文件末尾添加如下内容:
your_username ALL=(ALL:ALL) ALL
在我的例子中,我使用ubuntu作为我的用户名,所以它是这样的:
ubuntu ALL=(ALL) NOPASSWD: ALL
保存文件,重新连接到docker容器,并将sudo连接到您的hearts大陆。
好处:如果你想更新docker镜像,你可以做以下事情:
docker commit containerName NewImage
其他回答
当使用-u选项时,您可以使用root用户(ID = 0)而不是提供的默认用户登录Docker容器。如。
docker exec -u 0 -it mycontainer bash
Root (id = 0)是容器中的默认用户。图像 开发人员可以创建其他用户。可以访问这些用户 的名字。当传递数字ID时,用户不必存在于 的容器中。
摘自Docker文档
更新:当然你也可以使用Docker容器管理命令来运行这个:
Docker容器exec -u 0 -它mycontainer bash
'ubuntu'用户的密码是'ubuntu'(至少在docker中ubuntu:14.04.03)。
注意:'ubuntu'是在容器启动后创建的,所以,如果你这样做:
docker run -i -t --entrypoint /bin/bash ubuntu
您将直接得到根提示符。在这里你可以强制修改root用户的密码,提交容器并将其标记为ubuntu:latest(使用-f),如下所示:
root@ec384466fbbb:~# passwd
Enter new UNIX password:
Retype new UNIX password:
passwd: password updated successfully
root@ec384466fbbb:~# exit
% docker commit ec3844
5d3c03e7d6d861ce519fe33b184cd477b8ad03247ffe19b2a57d3f0992d71bca
docker tag -f 5d3c ubuntu:latest
你必须重新构建你对ubuntu:latest的最终依赖。
我确实遇到了无法su到root的问题,因为我是作为无特权用户在容器中运行的。
但我不想像之前的答案所暗示的那样重建一个新的形象。
相反,我发现我可以使用'nsenter'作为根访问容器,参见:https://github.com/jpetazzo/nsenter
首先确定主机上容器的PID:
docker inspect --format {{.State.Pid}} <container_name_or_ID>
然后使用nsenter以root身份进入容器
nsenter --target <PID> --mount --uts --ipc --net --pid
尝试以下命令获得根访问权限
$ sudo -i
为了避免重新构建容器,请执行以下步骤:
docker exec -u 0 -it containerName bash sudo visudo 在文件末尾添加如下内容:
your_username ALL=(ALL:ALL) ALL
在我的例子中,我使用ubuntu作为我的用户名,所以它是这样的:
ubuntu ALL=(ALL) NOPASSWD: ALL
保存文件,重新连接到docker容器,并将sudo连接到您的hearts大陆。
好处:如果你想更新docker镜像,你可以做以下事情:
docker commit containerName NewImage