Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SonarScanner in Docker cannot find java

I am trying to integrate SonarQube into my CI process. I tried to install SonarScanner in Docker, but it appears it cannot find java tho it can be run

Dockerfile

# Docker, AWS CLI, Terraform, NodeJS
FROM docker:18-dind
RUN echo "1"
RUN echo "http://dl-cdn.alpinelinux.org/alpine/edge/testing" >> /etc/apk/repositories
RUN apk --no-cache add openjdk11 --repository=http://dl-cdn.alpinelinux.org/alpine/edge/community
RUN apk update
RUN apk add awscli nodejs nodejs-npm wget unzip jq openjdk11
RUN wget https://releases.hashicorp.com/terraform/0.11.13/terraform_0.11.13_linux_amd64.zip
RUN unzip terraform_0.11.13_linux_amd64.zip
RUN mv terraform /usr/local/bin
RUN npm i -g sequelize sequelize-cli mysql2
RUN wget https://binaries.sonarsource.com/Distribution/sonar-scanner-cli/sonar-scanner-cli-4.2.0.1873.zip
RUN unzip sonar-scanner-cli-4.2.0.1873.zip
RUN ls sonar-scanner-4.2.0.1873
ENV PATH="/sonar-scanner-4.2.0.1873/bin:${PATH}"
RUN which java
ENV JAVA_HOME=/usr/bin
RUN echo $PATH
RUN java --version
RUN ls /sonar-scanner-4.2.0.1873/bin
RUN sonar-scanner --help

The logs

Step 15/20 : RUN which java
 ---> Running in 96b487f5b44f
/usr/bin/java
Removing intermediate container 96b487f5b44f
 ---> 5026de427a57
Step 16/20 : ENV JAVA_HOME=/usr/bin
 ---> Running in 4d7eee221e41
Removing intermediate container 4d7eee221e41
 ---> 76b9a8b9d803
Step 17/20 : RUN echo $PATH
 ---> Running in 4278142f7153
/sonar-scanner-4.2.0.1873/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
Removing intermediate container 4278142f7153
 ---> c3d528bdaa7e
Step 18/20 : RUN java --version
 ---> Running in 7df7e2d8a441
openjdk 11.0.5 2019-10-15
OpenJDK Runtime Environment (build 11.0.5+10-alpine-r0)
OpenJDK 64-Bit Server VM (build 11.0.5+10-alpine-r0, mixed mode)
Removing intermediate container 7df7e2d8a441
 ---> 0f7162dae25d
Step 19/20 : RUN ls /sonar-scanner-4.2.0.1873/bin
 ---> Running in b7efb86cd86e
sonar-scanner
sonar-scanner-debug
sonar-scanner-debug.bat
sonar-scanner.bat
Removing intermediate container b7efb86cd86e
 ---> ff640a70876f
Step 20/20 : RUN sonar-scanner --help
 ---> Running in 9164ac2e8369
Could not find 'java' executable in JAVA_HOME or PATH.
The command '/bin/sh -c sonar-scanner --help' returned a non-zero code: 1

Notice which java gives /usr/bin/java. So I set JAVA_HOME=/usr/bin. java can run. But sonar-scanner cannot appear to find java ... why is that?

like image 900
Jiew Meng Avatar asked Jun 06 '26 00:06

Jiew Meng


1 Answers

Add JAVA_HOME ENV according to version, as mentioned @cricket_007 the path in alpine for JDK is /usr/lib/jvm/{VERSION}

ENV JAVA_HOME=/usr/lib/jvm/java-11-openjdk
ENV PATH="$JAVA_HOME/bin:${PATH}"  
RUN echo $PATH

run docker

docker run --privileged -it --rm sonar-s sonar-scanner --version

output

INFO: Scanner configuration file: /sonar-scanner-4.2.0.1873/conf/sonar-scanner.properties
INFO: Project root configuration file: NONE
INFO: SonarQube Scanner 4.2.0.1873
INFO: Java 11.0.5 Alpine (64-bit)
INFO: Linux 4.14.70-67.55.amzn1.x86_64 amd64
like image 151
Adiii Avatar answered Jun 08 '26 12:06

Adiii



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!