To run SonarQube (sonarq) in a Docker container using your existing directory structure, you can utilize Docker's capabilities to map your local configuration, data, and logs to the container. Additionally, SonarQube requires a database (commonly PostgreSQL) to function properly. Below are detailed steps to help you set up SonarQube in Docker, including using Docker Compose for easier management.
Overview of Steps
- Prerequisites
- Prepare Configuration and Data Directories
- Set Up a PostgreSQL Database
- Create a
docker-compose.yml File
- Run SonarQube with Docker Compose
- Access SonarQube
1. Prerequisites
Before proceeding, ensure you have the following installed on your system:
2. Prepare Configuration and Data Directories
Assuming you have the SonarQube directory structure as shown, organize your directories to be used as Docker volumes. Here's a suggested structure based on your provided directories:
sonarqube-docker/
├── sonarq/ # Your existing SonarQube installation
│ ├── conf/
│ ├── data/
│ ├── logs/
│ ├── extensions/
│ └── ... # Other SonarQube directories
├── postgres-data/ # Directory for PostgreSQL data
└── docker-compose.yml # Docker Compose configuration
-
Create a Project Directory:
mkdir sonarqube-docker
cd sonarqube-docker
-
Copy Your SonarQube Files:
Place your existing SonarQube directory (sonarq/) inside sonarqube-docker/.
cp -r /path/to/your/sonarq ./sonarq
-
Create a Directory for PostgreSQL Data:
mkdir postgres-data
3. Set Up a PostgreSQL Database
SonarQube requires a database to store its data. PostgreSQL is a recommended option.
3.1. Create a Docker Network (Optional)
To allow SonarQube and PostgreSQL containers to communicate seamlessly, you can create a custom Docker network.
docker network create sonarqube-network
3.2. Set Up Environment Variables
Create a .env file to store environment variables for database configuration.
touch .env
Add the following to .env:
POSTGRES_USER=sonar
POSTGRES_PASSWORD=sonar
POSTGRES_DB=sonar
SONARQUBE_JDBC_URL=jdbc:postgresql://db:5432/sonar
Ensure to use strong passwords for production environments.
4. Create a docker-compose.yml File
Create a docker-compose.yml file in the sonarqube-docker/ directory with the following content:
version: '3.8'
services:
db:
image: postgres:15
container_name: sonarqube_db
environment:
- POSTGRES_USER=${POSTGRES_USER}
- POSTGRES_PASSWORD=${POSTGRES_PASSWORD}
- POSTGRES_DB=${POSTGRES_DB}
volumes:
- ./postgres-data:/var/lib/postgresql/data
networks:
- sonarqube-network
sonarqube:
image: sonarqube:10-community
container_name: sonarqube
depends_on:
- db
ports:
- "9000:9000"
environment:
- SONAR_JDBC_URL=${SONARQUBE_JDBC_URL}
- SONAR_JDBC_USERNAME=${POSTGRES_USER}
- SONAR_JDBC_PASSWORD=${POSTGRES_PASSWORD}
volumes:
- ./sonarq/conf:/opt/sonarqube/conf
- ./sonarq/data:/opt/sonarqube/data
- ./sonarq/logs:/opt/sonarqube/logs
- ./sonarq/extensions:/opt/sonarqube/extensions
networks:
- sonarqube-network
networks:
sonarqube-network:
external: true
name: sonarqube-network
Explanation of docker-compose.yml Components:
-
Services:
- db: Sets up a PostgreSQL database.
- Image: Uses the official PostgreSQL 15 image.
- Environment Variables: Configured via the
.env file.
- Volumes: Persists PostgreSQL data on the host machine.
- sonarqube: Sets up the SonarQube application.
- Image: Uses the official SonarQube 10 Community Edition image. Adjust the tag if you need a different edition or version.
- Depends_on: Ensures the database starts before SonarQube.
- Ports: Maps port
9000 of the container to port 9000 on the host.
- Environment Variables: Configures SonarQube to connect to the PostgreSQL database.
- Volumes: Maps your local SonarQube directories to the container for configurations, data, logs, and extensions.
-
Networks:
- sonarqube-network: A custom Docker network to allow communication between SonarQube and PostgreSQL.
Note: Ensure that the volume paths (./sonarq/conf, etc.) correctly point to your SonarQube directories.
5. Run SonarQube with Docker Compose
In the sonarqube-docker/ directory, execute the following command to start the services:
docker-compose up -d
- The
-d flag runs the containers in detached mode.
Monitor the Logs
To check if SonarQube and PostgreSQL are running correctly:
docker-compose logs -f
Look for logs indicating that both services have started successfully. SonarQube might take a few minutes to initialize.
6. Access SonarQube
Once the containers are up and running, access the SonarQube web interface by navigating to:
http://localhost:9000
Default Credentials
- Username:
admin
- Password:
admin
Security Tip: After the first login, it's recommended to change the default password.
Additional Tips
Persisting SonarQube Data
Ensure that your sonarq/data, sonarq/logs, etc., directories are correctly mapped to persist data outside the container. This way, you won't lose data when containers are recreated.
Customizing sonar.properties
If you need to customize SonarQube configurations, edit the sonarq/conf/sonar.properties file on the host machine. Changes will be reflected inside the container due to the volume mapping.
Managing Docker Resources
SonarQube, especially with certain plugins or large codebases, can be resource-intensive. Ensure your Docker environment has sufficient memory and CPU allocated.
Updating SonarQube
To update SonarQube:
-
Pull the Latest Image:
docker pull sonarqube:10-community
-
Recreate the Containers:
docker-compose down
docker-compose up -d
Note: Always back up your data before performing updates.
Troubleshooting
By following these steps, you should have a functional SonarQube instance running in Docker, utilizing your existing directory structure for configurations, data, and logs. This setup ensures data persistence and simplifies management through Docker Compose.