Home > Software > How to Deploy SonarQube with Docker Compose: A Step-by-Step Guide

How to Deploy SonarQube with Docker Compose: A Step-by-Step Guide

Anastasios Antoniadis

Master the deployment of SonarQube using Docker Compose with our expert guide. Enhance your code quality and security by setting up this powerful code analysis tool effortlessly in your development environment.

Docker (1)

SonarQube is an open-source platform for continuous inspection of code quality. It performs automatic reviews with static code analysis to detect bugs, code smells, and security vulnerabilities in over 20 programming languages. Deploying SonarQube using Docker Compose can streamline the setup process, enabling developers and DevOps teams to integrate code quality checks into their CI/CD pipeline efficiently. This guide provides a comprehensive walkthrough on deploying SonarQube and its database using Docker Compose.

Prerequisites

Before beginning, ensure you have:

  • Docker installed on your system.
  • Docker Compose installed on your system.
  • Basic knowledge of Docker and Docker Compose.

Step 1: Create a Docker Compose File

First, create a directory for your SonarQube deployment. This directory will contain your Docker Compose file (docker-compose.yml) and any additional configuration files or directories you may need.

mkdir sonarqube-docker && cd sonarqube-docker

Create the docker-compose.yml file:

touch docker-compose.yml

Open this file in a text editor and add the following configuration:

version: '3'

services:
  sonarqube:
    image: sonarqube:latest
    container_name: sonarqube
    ports:
      - "9000:9000"
    environment:
      - SONARQUBE_JDBC_URL=jdbc:postgresql://db:5432/sonar
      - SONARQUBE_JDBC_USERNAME=sonar
      - SONARQUBE_JDBC_PASSWORD=sonar
    volumes:
      - sonarqube_data:/opt/sonarqube/data
      - sonarqube_extensions:/opt/sonarqube/extensions
      - sonarqube_logs:/opt/sonarqube/logs
    depends_on:
      - db

  db:
    image: postgres:12-alpine
    container_name: sonarqube_db
    environment:
      - POSTGRES_USER=sonar
      - POSTGRES_PASSWORD=sonar
      - POSTGRES_DB=sonar
    volumes:
      - postgresql:/var/lib/postgresql
      - postgresql_data:/var/lib/postgresql/data

volumes:
  sonarqube_data:
  sonarqube_extensions:
  sonarqube_logs:
  postgresql:
  postgresql_data:

Configuration Explained:

  • sonarqube: Configures the SonarQube service using the latest SonarQube Docker image. It sets up environment variables for database connectivity and maps volumes for data persistence.
  • db: Sets up a PostgreSQL database for SonarQube. The environment variables define the database user, password, and database name, which must match those provided to the SonarQube service.
  • volumes: Maps persistent storage for SonarQube data, extensions, logs, and PostgreSQL data to ensure that your configurations and data are retained across container restarts.

Step 2: Launch SonarQube

With your docker-compose.yml file ready, start the SonarQube and PostgreSQL services by running:

docker compose up -d

This command pulls the necessary Docker images and starts the containers in detached mode.

Step 3: Access SonarQube

After a few moments, SonarQube will be up and running. Access the SonarQube web interface by navigating to http://your-server-ip:9000 in a web browser. The default credentials are admin for both username and password.

Conclusion

Deploying SonarQube with Docker Compose offers a straightforward method for setting up a comprehensive code quality inspection platform. Following the steps outlined in this guide, you can quickly get a SonarQube instance running on your own server, integrating continuous code quality checks into your development process. Docker Compose simplifies the management of SonarQube services, making it easy to maintain, backup, and upgrade your instance as your project evolves, ensuring your code remains clean, efficient, and secure.

Anastasios Antoniadis
Follow me
0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x