Certainly! Below is an example docker-compose.yml file that can be used to run SonarQube with a PostgreSQL database. Before you start, make sure you have Docker and Docker Compose installed on your system. You will also need to allocate at least 2GB of RAM for the SonarQube container for it to run effectively.
version: "3"
services:
sonarqube:
image: sonarqube:lts
container_name: sonarqube
environment:
- SONARQUBE_JDBC_USERNAME=sonar
- SONARQUBE_JDBC_PASSWORD=sonar_pass
- SONARQUBE_JDBC_URL=jdbc:postgresql://db:5432/sonar
ports:
- "9000:9000"
networks:
- sonarnet
depends_on:
- db
volumes:
- sonarqube_data:/opt/sonarqube/data
- sonarqube_extensions:/opt/sonarqube/extensions
- sonarqube_logs:/opt/sonarqube/logs
- sonarqube_temp:/opt/sonarqube/temp
db:
image: postgres:12
container_name: sonarqube_db
environment:
- POSTGRES_USER=sonar
- POSTGRES_PASSWORD=sonar_pass
- POSTGRES_DB=sonar
volumes:
- postgresql_data:/var/lib/postgresql/data
networks:
- sonarnet
networks:
sonarnet:
driver: bridge
volumes:
sonarqube_data:
sonarqube_extensions:
sonarqube_logs:
sonarqube_temp:
postgresql_data:
Here are some points to note about this docker-compose configuration:
- This configuration uses the
lts (Long-Term Support) tag for SonarQube, which ensures you get a stable version. You can specify a different tag/version depending on your requirements.
- The service
sonarqube has the environment variables set up to connect to the PostgreSQL service called db.
- The
db service runs a PostgreSQL 12 database, which will hold SonarQube's data.
- The volumes specified under both
sonarqube and db ensure that data is persistent across Docker container restarts.
- The port
9000 is mapped to the host, which is the port SonarQube uses for the web interface.
- It's important to remember that
SONARQUBE_JDBC_PASSWORD and POSTGRES_PASSWORD should be secure passwords, especially if you're planning to use this setup in a production environment. Replace sonar_pass with a strong password of your choice.
- The
networks directive creates a custom network named sonarnet, allowing containers to communicate with each other.
To deploy SonarQube using this configuration:
- Create a
docker-compose.yml file with the above content.
- In the directory where you have saved the
docker-compose.yml file, run the following command to start the services:
docker-compose up -d
- Once everything is up and running, you should be able to access SonarQube at
http://localhost:9000.
Make sure to change any settings as per your environment or security requirements.