adrianmusante/sonarqube

By adrianmusante

Updated about 1 month ago

SonarQube community edition with addons

Image
Integration & delivery
Developer tools
4

50K+

adrianmusante/sonarqube repository overview

docker-sonarqube

SonarQube™ is an open source quality management platform that analyzes and measures code's technical quality. It enables developers to detect code issues, vulnerabilities, and bugs in early stages.

The packaged SonarQube Docker image is based on the official SonarQube Community Edition binaries, but it includes some additional features and plugins to enhance its functionality and usability.

Overview of SonarQube™

Add-ons

Pre-installed plugins:
Additional features:
  • Unattended migration
  • Health-Check command-line tool
  • Gravatar enabled by default
  • Refresh configuration from environment variables on startup. (Note: The admin user only is loaded in first startup)

Docker registry

The recommended way to get the SonarQube Docker Image is to pull the prebuilt image from the Docker Hub Registry.

To use a specific version, you can pull a versioned tag. You can view the list of available versions in the Docker Hub Registry.

Supported architectures are:

  • x86-64: linux/amd64
  • ARM64: linux/arm64

Configuration

How to use this image

SonarQube™ requires access to a PostgreSQL database to store information. You can use any PostgreSQL database server, either running in a separate container or on a remote host.

The repository includes an example docker-compose.yml file that shows how to run SonarQube with a PostgreSQL database using official PostgreSQL Docker image via Docker Compose.

The SonarQube instance will be accessible at http://localhost:9000 (or http://<your-docker-host-ip>:9000 if you are not running Docker locally). The port can be changed by modifying the ports section of the docker-compose.yml file. Also, you can change port inside the container by setting the SONARQUBE_PORT_NUMBER environment variable.

Environment variables

When you start the SonarQube image, you can adjust the configuration of the instance by passing one or more environment variables either on the docker-compose file or on the docker run command line.

The following sections describe the available environment variables for configuring the SonarQube instance.

Customizable environment variables
NameDescriptionDefault Value
SONARQUBE_DEBUGEnable debug mode.no
SONARQUBE_LOG_LEVELSet the log level. Supported values are: TRACE, DEBUG, INFO.INFO (If SONARQUBE_DEBUG is enabled, the log level is set to DEBUG)
SONARQUBE_LOG_ROLLING_POLICYSet the log rolling policy. Use time:yyyy-MM-dd for daily rotation, size:10MB for size-based rotation, or none to disable log rotation.size:10MB
SONARQUBE_LOG_MAX_FILESSet the maximum number of files to keep. This property is ignored if SONARQUBE_LOG_ROLLING_POLICY is none.3
SONARQUBE_MOUNTED_PROVISIONING_DIRDirectory for SonarQube initial provisioning./sonarqube-provisioning
SONARQUBE_DATA_TO_PERSISTFiles to persist relative to the SonarQube installation directory. To provide multiple values, separate them with a whitespace.${SONARQUBE_DATA_DIR} ${SONARQUBE_EXTENSIONS_DIR} ${SONARQUBE_LOGS_DIR}
SONARQUBE_PORT_NUMBERSonarQube Web application port number.9000
SONARQUBE_ELASTICSEARCH_PORT_NUMBERSonarQube Elasticsearch application port number.9001
SONARQUBE_START_TIMEOUTTimeout for the application to start in seconds.300
SONARQUBE_SKIP_BOOTSTRAPWhether to perform initial bootstrapping for the application.no
SONARQUBE_SKIP_MIGRATIONPerforms migration when the version of SonarQube is updated. Otherwise, if the migration is skipped the system will not be operational without performing a manual step.no
SONARQUBE_WEB_CONTEXTSonarQube prefix used to access to the application./
SONARQUBE_WEB_URLHTTP(S) URL of the SonarQube server, such as https://yourhost.yourdomain/sonar. This value is used i.e. to create links in emails or Pull-Request decoration.nil
SONARQUBE_MAX_HEAP_SIZEMaximum heap size for SonarQube services (CE, Search and Web).nil
SONARQUBE_MIN_HEAP_SIZEMinimum heap size for SonarQube services (CE, Search and Web).nil
SONARQUBE_CE_JAVA_ADD_OPTSAdditional Java options for Compute Engine.nil
SONARQUBE_ELASTICSEARCH_JAVA_ADD_OPTSAdditional Java options for Elasticsearch.nil
SONARQUBE_WEB_JAVA_ADD_OPTSAdditional Java options for Web.nil
SONARQUBE_EXTRA_PROPERTIESComma separated list of properties to be set in the sonar.properties file, e.g. my.sonar.property1=property_value,my.sonar.property2=property_value.nil
SONARQUBE_EXTRA_SETTINGSComma separated list of settings to be set in Administration -> Configuration -> General Settings, e.g. sonar.lf.enableGravatar=false,sonar.lf.logoUrl=https://mysonar.com/logo.nil
SONARQUBE_USERNAMESonarQube user name.admin
SONARQUBE_PASSWORDSonarQube user password.Admin.123456
SONARQUBE_EMAILSonarQube user e-mail address.[email protected]
SONARQUBE_SMTP_HOSTSonarQube SMTP server host.nil
SONARQUBE_SMTP_PORT_NUMBERSonarQube SMTP server port number.nil
SONARQUBE_SMTP_USERSonarQube SMTP server user.nil
SONARQUBE_SMTP_PASSWORDSonarQube SMTP server user password.nil
SONARQUBE_SMTP_PROTOCOLSonarQube SMTP server protocol to use.nil
SONARQUBE_EMAIL_FROM_ADDRESSEmails will come from this address, e.g. [email protected].$SONARQUBE_EMAIL
SONARQUBE_EMAIL_FROM_NAMEEmails will come from this address name, e.g. SonarQube.nil
SONARQUBE_DATABASE_HOSTDatabase server host.$SONARQUBE_DEFAULT_DATABASE_HOST
SONARQUBE_DATABASE_PORT_NUMBERDatabase server port.5432
SONARQUBE_DATABASE_NAMEDatabase name.sonarqube_db
SONARQUBE_DATABASE_USERDatabase user name.sonarqube
SONARQUBE_DATABASE_PASSWORDDatabase user password.nil
SONARQUBE_PR_PLUGIN_RESOURCES_URLBase URL used to load the images for the PR comments. If the variable is defined as empty the image links are referenced to sonar.core.serverBaseURL.https://cdn.jsdelivr.net/gh/mc1arke/sonarqube-community-branch-plugin@${SONARQUBE_PR_PLUGIN_VERSION}/src/main/resources/static

Note

It is possible to provide environment variables using the _FILE suffix. The value will be read from the file specified by the environment variable, following the standard Docker secrets handling mechanism. This allows you to securely inject sensitive data (such as passwords) into the container without exposing them directly in environment variables.
Read-only environment variables
NameDescriptionValue
SONARQUBE_HOMESonarQube installation directory./opt/sonarqube
SONARQUBE_DATA_DIRDirectory for SonarQube data files.${SONARQUBE_HOME}/data
SONARQUBE_EXTENSIONS_DIRDirectory for SonarQube extensions.${SONARQUBE_HOME}/extensions
SONARQUBE_CONF_DIRDirectory for SonarQube configuration files.${SONARQUBE_HOME}/conf
SONARQUBE_CONF_FILEConfiguration file for SonarQube.${SONARQUBE_CONF_DIR}/sonar.properties
SONARQUBE_LOGS_DIRDirectory for SonarQube log files.${SONARQUBE_HOME}/logs
SONARQUBE_LOG_FILESonarQube log file.${SONARQUBE_LOGS_DIR}/sonar.log
SONARQUBE_TMP_DIRDirectory for SonarQube temporary files.${SONARQUBE_HOME}/temp
SONARQUBE_PID_DIRSonarQube directory for PID file.${SONARQUBE_HOME}/pids
SONARQUBE_BIN_DIRSonarQube directory for binary executables.${SONARQUBE_HOME}/bin/linux-x86-64
SONARQUBE_VOLUME_DIRSonarQube directory for mounted configuration files./sonarqube
SONARQUBE_DAEMON_USERSonarQube system user.sonarqube
SONARQUBE_DAEMON_USER_IDSonarQube system user ID.1001
SONARQUBE_DAEMON_GROUPSonarQube system group.sonarqube
SONARQUBE_DAEMON_GROUP_IDSonarQube system group.1001
SONARQUBE_DEFAULT_DATABASE_HOSTDefault database server host.postgresql
Persisting your application

If you remove the container all your data will be lost, and the next time you run the image the database will be reinitialized. To avoid this loss of data, you should mount a volume that will persist even after the container is removed.

For persistence you should mount a directory at the /sonarqube path. If the mounted directory is empty, it will be initialized on the first run. Additionally you should mount a volume for persistence of the PostgreSQL data.

To avoid inadvertent removal of volumes, you can mount host directories as data volumes. Alternatively you can make use of volume plugins to host the volume data.

Following with the example included in this repository using Docker Compose, you can change the docker-compose.yml file to mount host directories instead of using Docker named volumes. Below is an example of how to do this:

   sonarqube-db:
     ...
     volumes:
-      - sonarqube_db:/var/lib/postgresql
+      - /path/to/sonarqube/db:/var/lib/postgresql
   ...
   sonarqube:
     ...
     volumes:
-      - sonarqube:/sonarqube
+      - /path/to/sonarqube/data:/sonarqube
   ...
-volumes:
-  sonarqube_db:
-    driver: local
-  sonarqube:
-    driver: local

When using host directories for persistence, keep in mind that this container runs as a non-root user. Therefore, any mounted files and directories must have the correct permissions for UID 1001. It is recommended to create the directory and set the correct permissions or ownership before running Docker for the first time, to ensure Docker does not create it with incompatible permissions.

If the permissions are not set properly, you may see an error message like the following in the container logs: mkdir: cannot create directory ‘/sonarqube/data’: Permission denied

You can set the ownership to 1001:1001 with the following commands:

$ mkdir -p /path/to/sonarqube
$ chown -R 1001:1001 /path/to/sonarqube

Alternatively, you can set the permissions to 777 (read, write, and execute for everyone):

$ mkdir -p /path/to/sonarqube
$ chmod -R 777 /path/to/sonarqube
Health check

The SonarQube image includes a health check command that verifies if the SonarQube web application is up and running. The health check tries to connect to the SonarQube and analyzes the state of the application by querying the /api/system/status endpoint. The health check will be successful when the application status is UP or any other status provided via the -s option of the health-check command line tool (see below).

$ health-check -h
Utility to check if SonarQube is healthy.

Usage: health-check [options] ...
Some of the options include:
    -u <HEALTH_CHECK_URL>       URL used to check the status of SonarQube. (Optional)

    -s <STATUS>                 Repeat this option to add more valid status. Possible status:
                                  - STARTING: Server initialization is ongoing
                                  - UP: SonarQube instance is up and running (always added as valid)
                                  - DOWN: Instance is up but not running (e.g., due to migration failure)
                                  - RESTARTING: Restart has been requested
                                  - DB_MIGRATION_NEEDED: Database migration required
                                  - DB_MIGRATION_RUNNING: Database migration in progress

    -h                          display this help and exit

Example:
    - health-check
    - health-check -u http://my-host:9000/api/system/status
    - health-check -s STARTING -s RESTARTING
    - health-check -s DB_MIGRATION_NEEDED -s DB_MIGRATION_RUNNING

This command don't run by default. You can run it manually inside the container or you can add it to your orchestration tool (e.g., Docker Compose, Kubernetes, etc.) to monitor the health of the SonarQube instance.

For example, to add the health check to your docker-compose.yml file, you can use the following configuration:

services:
  sonarqube:
    # ...
    healthcheck:
      test: health-check
      start_period: 3m
      start_interval: 10s
      interval: 1m
      timeout: 10s
      retries: 3

Important

The health check will fail if the SonarQube instance is not fully started, including the case when a database migration is required or is in progress. If you set the `SONARQUBE_SKIP_MIGRATION` environment variable to `yes`, it is recommended to use the `-s DB_MIGRATION_NEEDED -s DB_MIGRATION_RUNNING` options to consider these states as healthy.

Issues

If you have any problems with or questions about this image, please contact me through a GitHub issue.

Tag summary

Content type

Image

Digest

sha256:d88067915

Size

1.1 GB

Last updated

about 1 month ago

docker pull adrianmusante/sonarqube:main