Root password inside a Docker container

2020-01-30 02:10发布

I'm using a Docker image which was built using the USER command to use a non-root user called dev. Inside a container, I'm "dev", but I want to edit the /etc/hosts file.

So I need to be root. I'm trying the su command, but I'm asked to enter the root password.

What's the default root user's password inside a Docker container?

标签: docker
13条回答
Explosion°爆炸
2楼-- · 2020-01-30 02:48

I'd suggest a better solution is to give the --add-host NAME:IP argument to docker run when starting the container. That will update the /etc/hosts/ file without any need to become root.

Otherwise, you can override the the USER setting by giving the -u USER flag to docker run. I would advise against this however, as you shouldn't really be changing things in a running container. Instead, make your changes in a Dockerfile and build a new image.

查看更多
叛逆
3楼-- · 2020-01-30 02:54

try the following command to get the root access

$ sudo -i 
查看更多
We Are One
4楼-- · 2020-01-30 03:01
docker exec -u 0 -it containername bash
查看更多
爷的心禁止访问
5楼-- · 2020-01-30 03:02

When you start the container, you will be root but you won't know what root's pw is. To set it to something you know simply use "passwd root". Snapshot/commit the container to save your actions.

查看更多
劳资没心,怎么记你
6楼-- · 2020-01-30 03:03

Get a shell of your running container and change the root password:

docker exec -it <MyContainer> bash

root@MyContainer:/# passwd
Enter new UNIX password:
Retype new UNIX password:
查看更多
叼着烟拽天下
7楼-- · 2020-01-30 03:03

You can use the USER root command in your Dockerfile.

查看更多
登录 后发表回答