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?
By default docker containers run as the
root
user.If you are still using the container you can use
exit
command to get back toroot
(default user) user instead of running the container again.Example -
There are a couple of ways to do it.
To run the Docker overriding the USER setting
or
Make necessary file permissions, etc., during the image build in the Docker file
If all the packages are available in your Linux image,
chpasswd
in the dockerfile before the USER utility.I had exactly this problem of not being able to su to root because I was running in the container as an unprivileged user.
But I didn't want to rebuild a new image as the previous answers suggest.
Instead I have found that I could access the container as root using 'nsenter', see: https://github.com/jpetazzo/nsenter
First determine the PID of your container on the host:
Then use nsenter to enter the container as root
The password is 'ubuntu' for the 'ubuntu' user (at least in docker for ubuntu :14.04.03).
NB: 'ubuntu' is created after the startup of the container so, if you just do this:
You'll get the root prompt directly. From there you can force the password change of root, commit the container and optionally tag it (with -f) to ubuntu:latest like this:
You must rebuild your eventual dependencies on ubuntu:latest.
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.from Docker documentation
Eventually, I decided to rebuild my Docker images, so that I change the root password by something I will know.
or