Categories
Technology

Root password of a Docker container

Steps and user commands to
gain Root User privileges for Docker containers

How to set Root password of a Docker container

Often we will come across situations where the default user setting in docker container will be not be sufficient to perform operations and we will need root user privileges

To kill a process, inspect or edit some system files, import network certificates or even to install some basic tools..

 
We have few options to accomplish this..
 
 
Easiest of all:
 
docker exec -u 0 -it <container-name> bash
In the above command, we try to gain a bash terminal with “0” User.
 
By default, “0” denotes root user. You can also choose to specify, simply as “root”.
 
docker exec -u root -it --workdir / <container-name> bash
 
I always find the above command handy in most situations to execute on a running container.
 
 
For more on Docker’s “exec” command, please refer here:
https://docs.docker.com/engine/reference/commandline/exec/
 
 
Other options that are available are during the container image build:
 
 
Make necessary file permissions, etc., during the image build in the Docker file
 
 
 
If all the packages are available in your Linux image, chpasswdin the dockerfile before the USER utility
 
 
Check out the post on best practices on building and operating containers.. 
 
Hope this helps.. Happy Coding.!!