Sonarqube Server (Docker): Issues scanning a local repository

Hi everyone,

I would like to evaluate SonarQube as a solution to verify a local copy of a given GitHub repository with respect to security issues. The code mainly consists of Kotlin and TypeScript code.

For this purpose, I am running SonarQube Developer edition as a Docker image. After starting the server, logging in to the web interface (localhost:9000) and creating a new local project (Settings: Other (for Go, Python, PHP, …) and macOS), I tried to invoke the CLI as follows from within the directory in which the local copy of the repo exists:

docker run \
    --rm \
    -e SONAR_HOST_URL="http://localhost:9000"  \
    -e SONAR_TOKEN="sqp_f75foob2da1d88956472cd2af9794891c6b49d2b" \
    -v ".:/usr/src" \
    sonarsource/sonar-scanner-cli

However, I do receive the following error:

ERROR Failed to query server version: Call to URL [http://localhost:9000/api/v2/analysis/version] failed: Failed to connect to localhost/[0:0:0:0:0:0:0:1]:9000

I manually checked and http://localhost:9000/api/v2/analysis/version actually exists and responds with 2025.3.0.108892 in the browser.

Could you point me to where I need to change the configuration or what I am missing?

Thanks in advance!

Hey there!

When using Docker, localhost inside a container points to itself—not your host or other containers.

If you have SonarQube and the SonarScanner CLI in separate containers, the SonarScanner CLI can’t reach SonarQube at localhost:9000 unless you set up networking.

Solution: Use a custom Docker network

  1. Create a network:

    docker network create sonarnet
    
  2. Run SonarQube in that network:

    docker run -d --name sonarqube --network sonarnet -p 9000:9000 sonarqube:developer
    
  3. Run sonar-scanner-cli on the same network, using the container name:

    docker run --rm --network sonarnet \
      -e SONAR_HOST_URL="http://sonarqube:9000" \
      -e SONAR_TOKEN="YOUR_TOKEN" \
      -v "$(pwd):/usr/src" \
      sonarsource/sonar-scanner-cli
    
  • Use the container name, not localhost, in SONAR_HOST_URL.
1 Like

Hey Colin,

you are totally right. I don’t know how I missed that :slight_smile: I was basically just following the Sonarqube Documentation on using the CLI from within a Docker image and didn’t give it a second thought. I resolved the issue by just downloading the scanner cli zip and using it outside of a container.

Thanks for clarifying!

1 Like