Skip to content

Instantly share code, notes, and snippets.

@valarpirai
Last active June 18, 2025 11:24
Show Gist options
  • Save valarpirai/e3abc9c20a2a6453cbd70446456e8a8f to your computer and use it in GitHub Desktop.
Save valarpirai/e3abc9c20a2a6453cbd70446456e8a8f to your computer and use it in GitHub Desktop.
SonarQube setup using docker compose
services:
postgres-server:
image: postgres:17-alpine
container_name: postgres-server
restart: always
environment:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: root
volumes:
- pg-data:/var/lib/postgresql/data
configs:
- source: init-sonarqube-db.sh
target: /docker-entrypoint-initdb.d/init-sonarqube-db.sh
healthcheck:
test: ["CMD", "pg_isready", "-U", "postgres"]
interval: 5s
timeout: 5s
retries: 5
sonarqube:
image: sonarqube:community
container_name: sonarqube-server
restart: always
ports:
- "9080:9000"
environment:
SONAR_ES_BOOTSTRAP_CHECKS_DISABLE: true
SONAR_JDBC_URL: jdbc:postgresql://postgres-server/sonarqube
SONAR_JDBC_USERNAME: sonarqube
SONAR_JDBC_PASSWORD: sonar_password
volumes:
- sonarqube_data:/opt/sonarqube/data
- sonarqube_extensions:/opt/sonarqube/extensions
- sonarqube_logs:/opt/sonarqube/logs
depends_on:
postgres-server:
condition: service_healthy
configs:
init-sonarqube-db.sh:
content: |
#!/bin/bash
set -e
# Check if the sonarqube database exists, create it if it doesn't
psql -v ON_ERROR_STOP=1 --username "postgres" --dbname "postgres" -tAc "SELECT 1 FROM pg_database WHERE datname='sonarqube'" | grep -q 1 || \
psql -v ON_ERROR_STOP=1 --username "postgres" --dbname "postgres" -c "CREATE DATABASE sonarqube WITH ENCODING 'UTF8' TEMPLATE template0;"
# Execute SQL to create schema and user in the sonarqube database
psql -v ON_ERROR_STOP=1 --username "postgres" --dbname "sonarqube" <<-EOSQL
-- Create the sonarqube schema
CREATE SCHEMA IF NOT EXISTS sonarqube;
-- Create the sonarqube user
CREATE ROLE sonarqube WITH LOGIN PASSWORD 'sonar_password';
-- Grant privileges to the sonarqube user on the sonarqube schema
GRANT USAGE ON SCHEMA sonarqube TO sonarqube;
GRANT CREATE ON SCHEMA sonarqube TO sonarqube;
-- Grant privileges for the sonarqube user to manage objects in the schema
GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA sonarqube TO sonarqube;
GRANT ALL PRIVILEGES ON ALL SEQUENCES IN SCHEMA sonarqube TO sonarqube;
-- Set default privileges for future objects created in the schema
ALTER DEFAULT PRIVILEGES IN SCHEMA sonarqube
GRANT ALL ON TABLES TO sonarqube;
ALTER DEFAULT PRIVILEGES IN SCHEMA sonarqube
GRANT ALL ON SEQUENCES TO sonarqube;
EOSQL
volumes:
es_data:
pg-data:
sonarqube_data:
sonarqube_logs:
sonarqube_extensions:
@valarpirai
Copy link
Author

valarpirai commented Jun 18, 2025

Run
docker compose -f docker-compose-sonarqube.yml up -d

Open

http://localhost:9080/

To delete
docker compose -f docker-compose-sonarqube.yml down

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment