Running a SonarQube Server and Scanner Client with Docker
Tags: sonarqube, docker, codesmells, softwaremetrics
Published: 2021-06-17 03:38:59

## Goal

Running a SonarQube Server and Scanner Client using docker images 

### Tech Requirements

- The **Docker** must be installed;
- To give `sudo` or `root` privileges to any user with Docker:

```console
$ sudo usermod -aG docker <yourusername>
```

### Required knowledge

- knowing how to use a command line in ubuntu linux;

## Step 1: Check docker service status

```console
$ sudo service docker status
```
Or
```console
$ sudo systemctl status docker
```

The result should be similar to the one shown below, showing that the service is up and running:

```shell
● docker.service - Docker Application Container Engine
     Loaded: loaded (/lib/systemd/system/docker.service; enabled; vendor preset>
     Active: active (running) since Wed 2021-06-16 09:32:06 -03; 12h ago
TriggeredBy: ● docker.socket
       Docs: https://docs.docker.com
   Main PID: 1242 (dockerd)
      Tasks: 15
     Memory: 79.0M
     CGroup: /system.slice/docker.service
```

The *docker service status* must be **active (running)**. You can also use the command `docker --version` for check docker installation:

```console
$ docker --version
Docker version 20.10.7, build f0df350
```

## Step 2: Pull image and run sonarqube server

Pull the latest image version of the sonarqube:
 
```console
$ docker pull sonarqube:latest
```

Start the server by running:

```console
$ docker run -d --name sonarqube \
-p 9000:9000 sonarqube:latest
```

Wait a few moments and when your instance is up and running, Log in to http://localhost:9000 using System Administrator credentials:

```
    login: admin
    password: admin
```

By default, the image will use an embedded H2 database that is not suited for production. Set up a database by following the "Installing the Database" section of [Install SonarQube Server](https://docs.sonarqube.org/latest/setup/install-server/).

## Configuring your project

Create a configuration file in your project's root directory called `sonar-project.properties`:

```properties
# must be unique in a given SonarQube instance
sonar.projectKey=myproject

# --- optional properties ---

# defaults to project key
sonar.projectName=My project
# defaults to 'not provided'
sonar.projectVersion=1.0
 
# Path is relative to the sonar-project.properties file. Defaults to .
sonar.sources=.
 
# Encoding of the source code. Default is default system encoding
sonar.sourceEncoding=UTF-8
```
You can find more properties about testing, coverage, etc. on the website [Analysis Parameters](https://docs.sonarqube.org/latest/analysis/analysis-parameters/).

## Analyzing a Project 

* Updated copy of [Get started in 2 minutes](https://docs.sonarqube.org/latest/setup/get-started-2-minutes/)

Now that you're logged in to your local SonarQube instance, let's analyze a project:

1. Click the **Add project** button.

2. Give your project a **Project key** and a **Display name** and click the **Set Up** button.

3. Under **Provide a token**, select *Generate a token*. Give your token a name, click the Generate button, and click **Continue**. **Write down your generated token, it will be used for login: d32ede54513ec7b92589139aaaa5781c121a9303**.

4. Select your project's main language under **Run analysis on your project**, and follow the instructions to analyze your project. Here you'll **download and execute a Scanner on your code** (if you're using Maven or Gradle, the Scanner is automatically downloaded).

```console
$ sonar-scanner \
  -Dsonar.projectKey=myproject \
  -Dsonar.sources=. \
  -Dsonar.host.url=http://localhost:9000 \
  -Dsonar.login=d32ede54513ec7b92589139aaaa5781c121a9303
```
You can add an organization: `-Dsonar.organization=myorganization`.

### Analyzing with Docker

You don't need download sonar-scanner client, we will use the image to execute the command `sonar-scanner-cli`, linking with the running **sonarqube server**.

```console
$ docker run --rm --link sonarqube \
-e SONAR_HOST_URL="http://sonarqube:9000" \
-e SONAR_LOGIN="d32ede54513ec7b92589139aaaa5781c121a9303" \
-v "${YOUR_REPO}:/usr/src" \
sonarsource/sonar-scanner-cli
```

Replace the variable `${YOUR_REPO}` with the full path of the project's root directory (e.g., `/home/user/myproject`).

## To know more
- https://www.digitalocean.com/community/tutorials/how-to-install-and-use-docker-on-ubuntu-20-04
- https://docs.sonarqube.org/latest/setup/get-started-2-minutes/
- https://hub.docker.com/_/sonarqube
- https://docs.sonarqube.org/latest/setup/install-server/
- http://localhost:9000/documentation/analysis/scan/sonarscanner/

## Me
{% user tacianosilva %}