我正在使用一个Docker映像,它是使用USER命令构建的,以使用一个称为dev的非根用户。 在容器中,我是“dev”,但我想编辑/etc/hosts文件。

所以我需要是根。我正在尝试su命令,但要求我输入根密码。

Docker容器中root用户的默认密码是什么?


当前回答

'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的最终依赖。

其他回答

我可以用下面的命令让它工作。

root@gitnew:# docker exec -it --user $(username) $(containername) /bin/bash

有几种方法可以做到。

运行覆盖USER设置的Docker docker exec -u 0 -it containerName bash

or

docker exec -u root -it --workdir / <containerName> bash

在Docker文件的镜像构建过程中,设置必要的文件权限等 如果您的Linux映像中所有的包都可用,那么在dockerfile中USER实用程序之前输入chpasswin。

完整参考:http://muralitechblog.com/root-password-of-a-docker-container/

'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的最终依赖。

为了避免重新构建容器,请执行以下步骤:

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
docker exec -u 0 -it containername bash