Installing sonar-scanner in Alpine Linux / Docker

In my company we are shifting from our local build machines to Docker Swarm. As such I am re-creating our build environment in a Dockerfile, where the image produced by it will be the Jenkins agent used for the job run.

As part of this I am also trying to install sonar-scanner. The relevant part is this:

RUN curl -s -L https://binaries.sonarsource.com/Distribution/sonar-scanner-cli/sonar-scanner-cli-3.3.0.1492-linux.zip -o sonarscanner.zip \
  && unzip -qq sonarscanner.zip \
  && rm -rf sonarscanner.zip \
  && mv sonar-scanner-3.3.0.1492-linux sonar-scanner

COPY sonar-scanner.properties sonar-scanner/conf/sonar-scanner.properties
ENV SONAR_RUNNER_HOME=sonar-scanner
ENV PATH $PATH:sonar-scanner/bin

It seems to be installed properly because when testing it is found, however… it also returns an error:

docker run -it jenkins:test sh
/ # sonar-scanner
sonar-scanner/bin/sonar-scanner: exec: line 66: /sonar-scanner/jre/bin/java: not found

But Java is present:

/ # java -version
openjdk version "1.8.0_191"
OpenJDK Runtime Environment (IcedTea 3.10.0) (Alpine 8.191.12-r0)
OpenJDK 64-Bit Server VM (build 25.191-b12, mixed mode)

And:

/ # ls -l /usr/bin/java
lrwxrwxrwx    1 root     root            35 Feb 19 07:26 /usr/bin/java -> ../lib/jvm/default-jvm/jre/bin/java

What am I missing in my setup in order for sonar-scanner to work w/out the error above?

Sonar-scanner uses embedded jre. You will see that in the bin/sonar-scanner.
SONAR_RUNNER_HOME is not used in run script.
uncomment echo commands in the startup that will report current setup.
Try setting JAVA_HOME in environment explicitly.

I have set the following in the Dockerfile, which helped.

RUN sed -i 's/use_embedded_jre=true/use_embedded_jre=false/g' /bin/sonar-scanner/bin/sonar-scanner
2 Likes

Since you don’t want to use the embedded JRE, you could use https://binaries.sonarsource.com/Distribution/sonar-scanner-cli/sonar-scanner-cli-3.3.0.1492.zip instead of sonar-scanner-cli-3.3.0.1492-linux.zip. It’s a 551KB download, it’ll reduce the size of the docker container. :wink:

1 Like

@degree I am coming across this issue too. Has there been an official fix? Is this specific to the alpine container? I can see the java executable in the downloaded zip yet the scanner still throws the error java not found. The embedded java doesn’t seem to work properly

Hi @martijnhjk ,

this is a rather old thread so i am not so sure about the origin. can you describe your observed behavior and the docker image that you are referring to?

There is now a official docker image using alpine that you could just use, or modify to your liking here

docker run -it --rm sonarsource/sonar-scanner-cli:4.5 --version
INFO: Scanner configuration file: /opt/sonar-scanner/conf/sonar-scanner.properties
INFO: Project root configuration file: NONE
INFO: SonarScanner 4.5.0.2216
INFO: Java 11.0.6 AdoptOpenJDK (64-bit)
INFO: Linux 5.9.1-arch1-1 amd64

Thanks man! you saved my day