The right way to change a root password in a Docker picture

When you deploy Docker containers based mostly on an official think about, you would possibly wish to set a root password for heightened safety.


You have most likely already learn that some Docker photos had been launched with null passwords. This might have simply led to severe safety points on any container you may need deployed with that pulled picture.

Though this is not typical, you may want your containers to deploy with a heightened sense of safety. Whereas this might not be the very best resolution for each container you’re employed with, it’s potential to alter a root person password on a picture. It may be difficult, as a result of some photos rely upon a set password for the basis person. Nevertheless, in the event you plan on doing quite a lot of in-house growth, you definitely do not wish to base these containers on photos with weak safety. 

SEE: Home windows 10 safety: A information for enterprise leaders (TechRepublic Premium)

To that finish, I wish to present you how one can change the basis password on an operating container after which commit that change the picture.

I am going to exhibit with the official CentOS picture. You need to have the ability to do that with any of the official Linux distribution photos from DockerHub (or any you created by yourself). I’ll assume you have already got Docker up and operating.

Deploying the container

The very first thing to do is deploy the CentOS container, based mostly on the official picture. That is carried out with the command:

docker run -it centos

When that command completes (it may need to first pull down the CentOS picture), you will end up on the bash immediate for the basis person. Problem the command:

cat /and so forth/shadow | grep root

You need to see the basis person would not embrace a hashed password (Determine A).

Determine A: Our passwordless root person.

Let’s change that. Problem the command:


When prompted, kind and confirm a brand new password for the basis person. When that completes, you’ll be able to situation the cat /and so forth/shadow | grep root command to see the basis person now has a hashed password.

Committing the change

Again at your common bash immediate (outdoors of the container), you must commit the change to the picture (in any other case you will simply deploy extra containers with the identical lack of password). To commit our change situation the command:


The place CONTAINER_ID is the ID of the container for which you modified the basis password, and NEW_IMAGE_NAME is a novel identify for the brand new picture. When you’re uncertain of what the ID is, situation the command docker ps -a. You do not have to make use of the complete container ID, simply the primary 4 characters will suffice.

Checking the brand new picture

With a view to see if this labored, deploy a brand new container with the brand new picture, like so:

docker run -it NEW_IMAGE_NAME

The place NEW_IMAGE_NAME is the brand new identify for the picture.

You may end up contained in the newly deployed container. Problem the command:

cat /and so forth/shadow | grep root

You need to see that the basis password is hashed (Determine B).

Determine B: Our root person password is conveniently hashed.

Exit out of that container, and also you at the moment are prepared to begin rolling out different containers, based mostly in your newly modified CentOS picture.


As I discussed, this technique may not work for each event. You would possibly run into an occasion the place somebody constructed a really particular picture (for a particular goal), and the basis password should stay unchanged. However for these containers you wish to deploy, that are based mostly on official base photos (similar to CentOS, Ubuntu, Debian, and so forth.) you’ll be able to change that root password to one thing robust, and relaxation simpler understanding that root password has been modified.

Cybersecurity Insider E-newsletter

Strengthen your group’s IT safety defenses by maintaining abreast of the newest cybersecurity information, options, and finest practices.
Delivered Tuesdays and Thursdays

Join right now

Join right now

Additionally see

Leave a Reply

Your email address will not be published. Required fields are marked *