Latest
Docker 2 min read

Run SonarScanner with Docker

Run SonarScanner with Docker
Docker · November 21, 2026

Version note: this guide uses SonarQube Server 2026.1 LTA documentation. Choose a supported patched release and check its requirements. These deployment commands were not tested on a live host.

What this guide covers

A scanner container reads your source code and sends an analysis report to an existing SonarQube server. You do not install SonarQube inside the application you want to analyze.

Before you start

You need Docker, a reachable SonarQube HTTPS address, a project key and an analysis token. This example is for the command-line scanner and a JavaScript or TypeScript project. Use the Maven, Gradle or .NET scanner for projects that require build integration.

Step 1: Prepare the project

From the repository root, install dependencies and run your project’s tests and coverage command. Create sonar-project.properties:

sonar.projectKey=payments-service
sonar.projectName=Payments service
sonar.sources=src
sonar.tests=test
sonar.sourceEncoding=UTF-8
sonar.javascript.lcov.reportPaths=coverage/lcov.info
sonar.exclusions=**/node_modules/**,**/dist/**

Change the key, name and directories to match your project. Remove the LCOV setting if the project does not produce that report. Add .scannerwork/ to .gitignore.

Step 2: Choose the scanner image

Select an official scanner image compatible with the server. Record its digest, which identifies the exact image contents:

read -r -p 'Approved image, sonarsource/sonar-scanner-cli@sha256:...: ' SCANNER_IMAGE
export SCANNER_IMAGE
case "$SCANNER_IMAGE" in
  sonarsource/sonar-scanner-cli@sha256:*) ;;
  *) printf '%s\n' 'Use an approved digest-qualified scanner image.' >&2; exit 1 ;;
esac
docker pull "$SCANNER_IMAGE"
git rev-parse HEAD

Use a verified digest from the official registry; do not type the ... example literally. The image user must be able to read the checkout and write .scannerwork. Fix permissions for the dedicated workspace rather than running the scanner as root.

Step 3: Run the analysis

Replace the server URL and run this from the repository root in Bash:

set +x
export SONAR_HOST_URL='https://sonar.example.com'
read -r -s -p 'Project analysis token: ' SONAR_TOKEN
printf '\n'
export SONAR_TOKEN
(
  trap 'unset SONAR_TOKEN' EXIT
  docker run --rm \
    --env SONAR_HOST_URL \
    --env SONAR_TOKEN \
    --env SONAR_USER_HOME=/tmp/.sonar \
    --mount "type=bind,source=$PWD,target=/usr/src" \
    --workdir /usr/src \
    "$SCANNER_IMAGE" \
    -Dsonar.qualitygate.wait=true \
    -Dsonar.qualitygate.timeout=300
)
scan_status=$?
unset SONAR_TOKEN
if [ "$scan_status" -ne 0 ]; then
  printf 'Analysis or quality-gate wait failed: %s\n' "$scan_status" >&2
  exit "$scan_status"
fi

The checkout is mounted at /usr/src. The container is removed after the run, while files it wrote in the checkout remain. The scanner waits for the quality gate and fails the command if analysis or the gate fails. In CI, inject the token through protected secret settings rather than an interactive prompt.

Step 4: Check connection and file errors

Inside a container, localhost means that container, not the host running SonarQube. Use the server’s reachable HTTPS name. For an internal certificate authority, mount and configure the truststore supported by your scanner version; do not disable certificate checks.

On Fedora, a dedicated checkout may need a container-compatible SELinux label. Use the documented bind-mount labeling options only on that checkout, not your whole home directory. A remote Docker daemon also needs the source files on its own host.

Check the result

Open the project and confirm the current commit, source files, coverage import and gate result. Use the Background Tasks page if upload succeeded but no new result appears. A timeout means the result is unknown, not passed.

References

PB
Written by

Parul Bedi

Notes and guides by Parul Bedi.

Up next

Run a Node.js Web Server in Docker

Docker November 24, 2026 1 min read
Open post

Conversation

0 comments

No comments yet. Start the conversation below.

Leave a comment

Replying to a comment.
Be kind. Markdown is not supported in comments.