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?
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.
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.
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!
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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With