Guides

How to Set Up SonarQube and PostgreSQL Using Docker

SonarQube is an open-source SAST platform for continuous inspection of code quality, ensuring clean, maintainable, and reliable code. By integrating it with Docker, you can effortlessly manage its deployment, making it portable and easily scalable...

L0WK3Y
· 5 min read
Send by email
@Infophreak 2025

Introduction

SonarQube is an open-source Static Application Security Testing (SAST) platform for continuous inspection of code quality, ensuring clean, maintainable, and reliable code. By integrating it with Docker, you can effortlessly manage its deployment, making it portable and easily scalable. In this guide, we’ll walk you through setting up SonarQube using Docker on any platform that supports Docker containers.


Prerequisites

Before we start, ensure you have the following installed:

  1. Docker (you can install Docker from the official Docker website)
  2. Minimum System Requirements:
    • 2GB RAM (minimum)
    • 2 CPUs
    • 5GB of free disk space

Docker Compose (if you prefer orchestrating multiple containers)

sudo apt-get update
sudo apt-get install docker-compose

Step 1: Pull SonarQube Docker Image

First, you need to pull the SonarQube Docker image from Docker Hub. You can do this with the following command:

docker pull sonarqube:community

To pull a specific version:

docker pull sonarqube:9.9-community


Step 2: Create Docker Volumes

To ensure data persists between restarts, let’s create named volumes:

docker volume create sonarqube_pgdata
docker volume create sonarqube_data
docker volume create sonarqube_logs
docker volume create sonarqube_conf

These volumes will store PostgreSQL data and SonarQube configuration, data, and logs.


Step 3: Set Up a Database with Docker

SonarQube requires a database to store its data. We’ll use PostgreSQL as it is fully supported.

docker run -d --name sonarqube_db \\
  --network sonarnet \\
  -e POSTGRES_USER=USERNAME \\
  -e POSTGRES_PASSWORD="PASSWORD" \\
  -e POSTGRES_DB=sonarqube \\
  -v sonarqube_pgdata:/var/lib/postgresql/data \\
  postgres:latest
  • d: Runs PostgreSQL in detached mode.
  • network sonarnet: Connects to the custom Docker network.
  • e POSTGRES_USER: Sets the database username.
  • e POSTGRES_PASSWORD: Sets the database password.
  • e POSTGRES_DB: Names the database as sonarqube.
  • v: Maps the Docker volume to a directory inside the container.

Step 4: Run SonarQube with Docker

To run SonarQube and link it to the database, execute the following command:

docker run -d --name sonarqube \\
  --network sonarnet \\
  -p 9000:9000 \\
  -e SONAR_JDBC_URL=jdbc:postgresql://sonarqube_db:5432/sonarqube \\
  -e SONAR_JDBC_USERNAME=USERNAME \\
  -e SONAR_JDBC_PASSWORD="PASSWORD" \\
  -v sonarqube_data:/opt/sonarqube/data \\
  -v sonarqube_conf:/opt/sonarqube/conf \\
  -v sonarqube_logs:/opt/sonarqube/logs \\
  sonarqube:community


Step 5: Access SonarQube

Open your browser and navigate to:

<http://localhost:9000>

The default login credentials are admin (You will be prompted to change these after logging in):

  • Username: admin
  • Password: admin

Step 6: Configure Projects

  1. Go to Projects → Create Project.
  2. Name your project and generate a token for authentication.
  3. Use the provided command to analyze your project:
sonar-scanner \\
  -Dsonar.projectKey=my_project \\
  -Dsonar.sources=. \\
  -Dsonar.host.url=http://localhost:9000 \\
  -Dsonar.login=<your_token>

Running SonarQube with Docker Compose (Optional)

If you prefer Docker Compose, create a docker-compose.yml file:

version: '3.9'

services:
  sonarqube:
    image: sonarqube:community
    container_name: sonarqube
    networks: [sonarnet]
    ports:
      - "9000:9000"
    environment:
      SONAR_JDBC_URL: jdbc:postgresql://sonarqube_db:5432/sonarqube
      SONAR_JDBC_USERNAME: sonarUser
      SONAR_JDBC_PASSWORD: sonarPassword
      SONAR_ES_BOOTSTRAP_CHECKS_DISABLE: "true"
    volumes:
      - sonarqube_data:/opt/sonarqube/data   
      - sonarqube_logs:/opt/sonarqube/logs
      - sonarqube_conf:/opt/sonarqube/conf
    depends_on:
      sonarqube_db:
        condition: service_healthy
    restart: always

  sonarqube_db:
    image: postgres:17.5-bookworm
    container_name: sonarqube_db
    networks: [sonarnet]
    environment:
      POSTGRES_USER: sonarUser
      POSTGRES_PASSWORD: sonarPassword
      POSTGRES_DB: sonarqube
    volumes:
      - sonarqube_pgdata:/var/lib/postgresql/data
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U sonarUser -d sonarqube"]
      interval: 10s
      timeout: 5s
      retries: 5
    restart: always

networks:
  sonarnet:
    driver: bridge

volumes:
  sonarqube_data:
  sonarqube_logs:
  sonarqube_pgdata:
  sonarqube_conf:   

Launch Docker Compose

Run the container with:

docker-compose up -d

You now have SonarQube running in Docker, fully accessible at http://localhost:9000, backed by a PostgreSQL database with persistent data storage. You can analyze your code and continuously monitor its quality, integrating it seamlessly into your CI/CD pipeline.


Step 7: Create a Local Project in SonarQube

img

Now that your SonarQube instance is running, you can create a local project to analyze your code without any external integrations. Follow these steps:

  1. Click on 'Create a Local Project':
    • From the dashboard, select the option Create a local project.
  2. Fill in the Project Information:
    • Choose a Project Key and a Display Name.
    • Click Next to continue.
    • Click "Use the global settings" and Create project.
    • Select Locally.
  3. Generate a Token:
    • SonarQube will prompt you to generate a token for authentication.
    • Save this token, as it will only be shown once.
    • Continue to the language selection your project is written in.
    • SonarQube will provide you with instructions and commands to analyze your local project (may look something like this, depending on which language you selected for your project).
sonar-scanner \
  -Dsonar.projectKey=<your_project_key> \
  -Dsonar.sources=. \
  -Dsonar.host.url=http://localhost:9000 \
  -Dsonar.login=<your_token>

After running the command, head back to the dashboard to view the results of your scan. Congratulations, you've setup your first project in SonarQube!