How to get Sonarqube host which running inside build stage of docker image

I’m using sonarqube-, deployed from unzipped directory with our custom sonar check settings.
I wrote Dockerfile for base image, where our custom Sonarqube is copying inside image:

FROM debian:bullseye AS deb
ENV DEBIAN_FRONTEND=noninteractive
RUN apt-get update && apt-get upgrade -y && apt-get clean && \
    ln -sf /usr/share/zoneinfo/Europe/Moscow /etc/localtime && dpkg-reconfigure --frontend noninteractive tzdata

FROM deb AS jre
RUN apt-get install -y openjdk-17-jre-headless
RUN apt-get update && apt-get install -y procps

FROM jre AS maven
RUN apt-get install -y maven

FROM maven AS sonar
RUN mkdir -p sonar
COPY ./sonarqube- /sonar
RUN groupadd sonarqube &&  \
    useradd -c "Sonar System User" -d /sonar -g sonarqube sonarqube &&  \
    chown -R sonarqube:sonarqube /sonar && \
    chmod -R 777 /sonar

And I wrote Dockerfile for my service:

FROM basewithlinter:latest AS debwithlinter
FROM base:latest AS deb

FROM debwithlinter AS builder
WORKDIR /sonar
USER sonarqube
RUN ./bin/linux-x86-64/ console  \
    -p 9000:9000  \
    -D &
USER root
WORKDIR /build
COPY . /build
RUN mvn dependency:go-offline

RUN mvn package -DskipTests \
    sonar:sonar -Dsonar.login=admin -Dsonar.password=<password> -Dsonar.qualitygate.wait=true

FROM deb AS runner
USER root
RUN mkdir -p app/tmp app/logs app/config
COPY --from=builder build/target/ratesdumper-1.0.0-rc1.jar /app/moex_exchange_rates_dumper.jar
ENTRYPOINT ["java", "-jar", "moex_exchange_rates_dumper.jar"]

But I’m getting error: [ERROR] SonarQube server [http://sonarqube:9000] can not be reached.
If I don’t specify the host “http://sonarqube:9000” I’m getting same error: localhost can’t be reached.
I checked: Sonarqube starts and is running, but I don’t know how to get it.


Welcome to the community!

We offer a supported Docker file. I suggest trying with that.