Search code examples
docker

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


Solution

  • 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.