Book a free call

How to Run SonarQube with Docker Compose

Run SonarQube locally with Docker Compose: the compose file, the three parts it needs, the first scan, and how to fix the usual startup errors.

Hertiv engineering teamReviewed by a senior engineer
7 min read
How to Run SonarQube with Docker Compose
On this page

Short answer

Running SonarQube with Docker Compose means starting SonarQube and its PostgreSQL database as containers from one compose file, instead of installing Java, a database and SonarQube by hand. You write a file with two services, start it with one command, log in on port 9000, and run your first scan from the build.

What does running SonarQube with Docker Compose mean?

Running SonarQube with Docker Compose means starting SonarQube and its database as containers from one file, instead of installing Java, a database and SonarQube by hand on a server. SonarQube is a static analysis tool: it reads your code and reports bugs, security issues and duplicated code without running the program. Docker Compose is the tool that starts, links and stops the containers together.

The setup needs three parts: SonarQube itself, a PostgreSQL database that stores the scan results, and the search index that runs inside the SonarQube container. The compose file describes all three, so a new team member can start the same setup with one command. That is the main advantage over a manual install: the setup is written down, versioned and repeatable.

PartContainer imageWhat it does
SonarQubesonarqube:lts-communityRuns the web app and the analysis engine
PostgreSQLpostgres:15Stores projects, issues and scan history
Search indexBuilt into SonarQubeIndexes code so the interface can search it

Before you write the file, make sure your machine can run it. SonarQube needs at least 2 GB of memory for the app container and a database volume that survives restarts. If you already run builds in a pipeline, read how to set up CI/CD first, so the scan step has a pipeline to live in.

What do you need before you start?

You need Docker Engine and the Compose plugin installed, plus a machine with at least 4 GB of free memory and 10 GB of free disk. SonarQube's search index uses Elasticsearch, which needs the host setting vm.max_map_count set to at least 262144. Without it, the container starts and then stops with an error about virtual memory.

You also need two free ports: 9000 for the SonarQube web interface and 5432 for the database, unless you map them to other ports. Keep the database port private: only the SonarQube container should reach it, not the internet. Choose a strong database password and keep it in a file the compose setup reads, not in a shared chat message.

Finally, decide which version to run. The long-term-support image, tagged lts-community, is the safe choice for a team that wants stability over new features. Pin the image tag in the compose file so an update cannot change your setup without you noticing.

How do you write the compose file?

The compose file has two services: app and db. The app service uses the SonarQube image, maps port 9000, and sets three environment values: the database address, the database user and the database password. The db service uses the PostgreSQL image and stores its data in a named volume, so scan history survives a restart.

Give the app service a memory limit of at least 2 GB and the database at least 1 GB. Add a health check on the database so SonarQube only starts after PostgreSQL accepts connections. Without it, the app can fail its first start and need a manual restart.

SettingValueWhy it matters
SONAR_JDBC_URLjdbc:postgresql://db:5432/sonarTells SonarQube where the database is
SONAR_JDBC_USERNAMEsonarDatabase user for the app
SONAR_JDBC_PASSWORDA strong passwordKeeps the database closed to others
Volume sonar_dataNamed volumeKeeps scan history after restarts
Port mapping9000:9000Opens the web interface on the host

Keep the file in the same repository as the code it scans, or in a small infrastructure repository the team owns. A compose file that lives only on one person's laptop is not a setup the team can rely on.

How do you start it and log in?

You start SonarQube in five steps, and the first start takes a few minutes while the database is created.

  1. Start the containers. Open a terminal in the folder with the compose file and run docker compose up -d.
  2. Watch the logs. Run docker compose logs -f sonarqube and wait until the line saying SonarQube is up appears.
  3. Open the interface. Go to http://localhost:9000 in a browser.
  4. Log in. Use the default user admin and the default password admin.
  5. Change the password at once. Then create a project and a token for your first scan.

If the web page does not load, check the logs first: the two usual causes are the memory setting on the host and a port already in use. Do not delete the database volume to fix a login problem. The volume holds the scan history, and deleting it only wipes data without fixing the cause.

How do you run your first scan?

A scan has three parts: the project in SonarQube, the token, and the scanner that runs in your build. Create the project in the web interface, copy the token it gives you, and store the token as a secret in your build system, never in the code.

For a JavaScript or TypeScript project, run the scanner from the project folder with the project key and the server address. For Maven or Gradle projects, the scanner runs as a plugin step in the normal build. The first scan takes longer than later ones because it analyses every file; later scans only re-check what changed.

Once the scan finishes, open the project dashboard and look at the issues by severity: blockers first, then critical issues, then the rest. A good first goal is to fix the blockers and agree a quality gate, which is the set of rules a scan must pass before code merges. Teams that review code together usually start with what code review covers so the scan results have a review step to feed into.

Example (hypothetical): a first scan for a small team

Example (hypothetical): a team of five developers has a web app with about 120,000 lines of code and wants to scan it on every pull request.

The first scan takes 8 minutes. Later scans only check changed files and take about 2 minutes each. The team runs about 15 scans a day, so scan time is 15 times 2 minutes, which is 30 minutes a day spread across the team, and nobody waits long for a result.

Storage grows slowly. The database starts at about 2 GB and each analysis adds about 50 MB of history. After 60 analyses, that is 60 times 50 MB, which is 3,000 MB, or about 3 GB, so the database holds about 5 GB in total. The team sets the volume to 20 GB and checks disk use once a month.

The first scan finds 4 blocker issues and 22 critical issues. The team fixes the blockers in the first week and adds a quality gate that blocks merges with new blockers. After a month, new blockers have stopped reaching the main branch, which is the result the team actually wanted: not a clean dashboard, but fewer bugs slipping through.

What are the common mistakes?

The most common mistakes are running SonarQube without a database, using the default password, and scanning without a quality gate. Each one is easy to avoid if the compose file and the first scan are set up with care.

  • Running the embedded database. SonarQube starts without PostgreSQL, but the embedded database is for evaluation only and wipes its data on restart. Always add the db service.
  • Forgetting vm.max_map_count. The search index fails and the container stops. Set the host value before the first start.
  • Leaving admin and admin as the password. Anyone who reaches port 9000 can change the settings. Change it on first login.
  • Mapping the database port to the host. Only SonarQube needs the database. Keep the port private.
  • Scanning once and stopping. A scan that never runs again is a dashboard, not a safety net. Put the scan in the pipeline.
  • Treating every issue as equal. Fix blockers first, then agree which rules the team enforces. To compare scanners and other tooling, see the list of DevOps tools for startups.

Key takeaways

  • SonarQube and its database run as containers from one compose file, which makes the setup repeatable
  • The host needs vm.max_map_count of at least 262144, or the search index fails on first start
  • Pin the lts-community image tag and keep the database in a named volume so history survives restarts
  • Change the default admin password at once and store the scan token as a build secret, never in code
  • Fix blockers first, add a quality gate, and scan on every pull request so new blockers stop slipping through

Frequently asked questions

How much memory does SonarQube need in Docker?

Give the app container at least 2 GB and the database at least 1 GB, on a machine with 4 GB free. The search index also needs the host setting vm.max_map_count set to at least 262144, or the container stops with a virtual memory error.

Can I run SonarQube without PostgreSQL?

SonarQube starts with its embedded database, but that database is for evaluation only and wipes its data on restart. For any real use, add a PostgreSQL service to the compose file and point SonarQube at it with the SONAR_JDBC settings.

What is the default SonarQube login?

The default user is admin and the default password is admin. Change the password on first login, because anyone who can reach port 9000 can otherwise change the settings and read the scan results.

How long does the first scan take?

The first scan analyses every file, so it takes longer than later scans, often several minutes for a mid-size codebase. Later scans only re-check changed files and usually take about two minutes each.

Why does SonarQube stop right after starting?

The two usual causes are the vm.max_map_count host setting and a port already in use. Check the container logs first: the error message names the cause, and deleting the database volume does not fix either one.

Do I need a quality gate?

A quality gate is the set of rules a scan must pass before code merges, such as no new blocker issues. Without one, the scan is only a dashboard. With one, it stops bad code from reaching the main branch.

Found this useful? Share it.
Hertiv engineering team

Written from projects we build for clients: software products in production. Every post is reviewed by a senior engineer before publishing and updated when practice changes.

About HertivHow we work

Setting up code quality checks for your team?

A senior engineer replies within one business day. Or see our DevOps service.

Book a free call

Keep reading

All posts

Tell us what you want to build

A senior engineer reads every request and replies within one business day with questions, a rough range, or a call link.

  • NDA on request before any details
  • You own all code, models and IP
  • US, UK and EU time-zone overlap

Your email app should open with your message ready to send. If it does not, write to [email protected].

Prefer email? Write to [email protected]
Estimate costBook a call