Install SonarQube with Docker, Deploy and Add Code for Analysis

Tiempo de lectura: 2 minutos

Reading Time: 2 minutes

Today we are going to see how we can install SonarQube using Docker for deployment on a machine. Additionally, we will add a sample code to analyze.

Docker…

Let’s create a new project within SonarQube. To do this, go to Create Project > Manual, and fill in the requested information:

Once all the information is filled, click on Set Up.

Now choose Locally.

Generate a new Token:

This token will not be recoverable, so you need to save or copy it to use it in the next step.

Now, within the project, create a file called sonar-project.properties.

sonar.projectKey=Test
sonar.projectName=Test
sonar.projectVersion=1.0

sonar.sources=.

sonar.host.url=http://sonarqube:9000
sonar.login=GENERATED_TOKEN

I used sonar.host.url=http://sonarqube:9000 assuming that the installation is on localhost (using the container name (hostname) within the Docker network and pointing to the local port).

In GENERATED_TOKEN, add the previously generated token.

Now we can analyze the code using SonarQube. To do this, we need to run the following command:

docker run -d --name sonarqube -p 9000:9000 sonarqube

This will start the SonarQube container on port 9000. Once the container is up and running, you can access SonarQube by opening the following URL in your web browser:

http://localhost:9000

Login to SonarQube using the credentials you provided during the setup process.

Once you are logged in, you can create a new project and analyze the code. Follow the steps below:

  1. Click on Projects in the top menu.
  2. Click on the Create Project button.
  3. Select the previously created project from the list.
  4. Click on the Analyze button.

This will start the analysis process and generate a report with the code quality metrics.

That’s it! You have successfully installed SonarQube using Docker and analyzed your code.

Leave a Comment