Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I become root in a docker container

Tags:

docker

I run this command on my local machine docker run -d --name SonarQube -p 9000:9000 -p 9092:9092 sonarqube

This takes the image of the branch from dockerhub and then creates a container of the image. Now I want to make some changes in the file but there is no editor in the container. I tried installing vi using apt-get but it says I need to be the root user to execute the command and when I write sudo it says command not found. How do I install the editor in the container?

I run this command to install vim sudo apt-get install vim

And this is the error which I get

bash: sudo: command not found

like image 746
dave Miller Avatar asked Mar 22 '26 01:03

dave Miller


1 Answers

Try to pass user to docker run command.

docker run -it --user root --name SonarQube -p 9000:9000 -p 9092:9092 sonarqube

But you might see same issue with this approach as this will start the container with root, not sonarqube user.

So I will recommend to go with the below approach.

FROM sonarqube
USER root
RUN apt-get update \
    && apt-get install -y vim
USER sonarqube
ENTRYPOINT ["./bin/run.sh"]

USER

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.

The developer can set a default user to run the first process with the Dockerfile USER instruction. When starting a container, the operator can override the USER instruction by passing the -u option.

-u="", --user="": Sets the username or UID used and optionally the groupname or GID for the specified command.

The followings examples are all valid:
--user=[ user | user:group | uid | uid:gid | user:gid | uid:group ]

reference-run

like image 84
Adiii Avatar answered Mar 24 '26 00:03

Adiii



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!