dave Miller
dave Miller

Reputation: 344

How do I become root in a docker container

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

Upvotes: 4

Views: 26444

Answers (4)

AnilPatil
AnilPatil

Reputation: 51

You can run below command for running container as root:

docker exec --user root -it <CONT-NAME> /bin/bash

Upvotes: 4

AnilPatil
AnilPatil

Reputation: 51

You can run with cont-name command for running container as root:

#docker exec --user root -it containername /bin/bash

Upvotes: 0

Jason Woods
Jason Woods

Reputation: 544

According to the Sonorqube Docker info at https://hub.docker.com/_/sonarqube/ the config files are in bind mounted persistent volumes. You can change the configs in a local directory

The images contain the SonarQube installation at /opt/sonarqube. You can use bind-mounted persistent volumes to override selected files or directories, for example:

sonarqube_conf:/opt/sonarqube/conf: configuration files, such as sonar.properties

or you can specify them on the command line

> $ docker run -d --name sonarqube \  
>     -p 9000:9000 \  
>     -v /path/to/conf:/opt/sonarqube/conf \  
>     -v /path/to/data:/opt/sonarqube/data \  
>     -v /path/to/logs:/opt/sonarqube/logs \  
>     -v /path/to/extensions:/opt/sonarqube/extensions \  
>     sonarqube

You should try not to edit files directly in a Docker container. If you really need to get the file in a running container, try editing the file locally and Docker cp it into the container

https://docs.docker.com/engine/reference/commandline/cp/

docker cp [OPTIONS] CONTAINER:SRC_PATH DEST_PATH|-

If you still really want to sudo in the container, see this SO post, it might help.

Upvotes: 3

Adiii
Adiii

Reputation: 59896

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

Upvotes: 10

Related Questions