Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Root password inside a Docker container

Tags:

docker

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?

like image 541
guillaume Avatar asked Feb 25 '15 14:02

guillaume


People also ask

What is the root password for docker container?

If you run docker run -it centos you start a new container and are inside it immediately as root. You could set then interactively a new root password with passwd . But this is a rare usecase, mostly used for first steps into containers.

How do I get root permission in docker?

Manage Docker as a non-root user By default that Unix socket is owned by the user root and other users can only access it using sudo . The Docker daemon always runs as the root user. If you don't want to preface the docker command with sudo , create a Unix group called docker and add users to it.

How do I pass a password in Dockerfile?

Simply use the --build-arg flag. So you can avoid to keep explicit password (or other sensible information) on the Dockerfile and pass them on the fly. docker build --build-arg user=capuccino -t test_arguments -f path/to/dockerfile . Hope it helps!


2 Answers

You can log into the Docker container using the root user (ID = 0) instead of the provided default user when you use the -u option. E.g.

docker exec -u 0 -it mycontainer bash 

root (id = 0) is the default user within a container. The image developer can create additional users. Those users are accessible by name. When passing a numeric ID, the user does not have to exist in the container.

from Docker documentation

Update: Of course you can also use the Docker management command for containers to run this:

docker container exec -u 0 -it mycontainer bash

like image 143
H6. Avatar answered Oct 15 '22 22:10

H6.


Eventually, I decided to rebuild my Docker images, so that I change the root password by something I will know.

RUN echo 'root:Docker!' | chpasswd 

or

RUN echo 'Docker!' | passwd --stdin root  
like image 37
guillaume Avatar answered Oct 15 '22 22:10

guillaume