Search code examples
dockersonarqubesonar-runner

Can't run sonar-scanner inside docker container: Permission denied


I'm trying to build a custom docker image with CI purposes (bitbucket pipelines). After build and test my code, I wish I could run some analysis with sonarqube.

On my custom image I tried to install sonar-scanner, so when in the container it will just be executed. However, inside the container (either on bitbucket or my local machine) it fails with this error:

/sonar-scanner-2.8/bin/sonar-scanner: 108: exec: : Permission denied

I already tried many different ways to set permissions and ownership on the scanner directory, but nothing has worked.

More surprisingly, even when running the container with the flash --privileged=true I still get the same error.

What am I missing on docker basics?

This is my last version of the Dockerfile:

# Pull base image.
FROM node:6

LABEL maintainer "Gabriel Araujo <[email protected]>"

ENV SONAR_SCANNER_VERSION 2.8
ENV SONAR_SCANNER_HOME /home/sonar-scanner-${SONAR_SCANNER_VERSION}
ENV SONAR_SCANNER_PACKAGE sonar-scanner-${SONAR_SCANNER_VERSION}.zip
ENV SONAR_RUNNER_HOME ${SONAR_SCANNER_HOME}
ENV PATH $PATH:${SONAR_SCANNER_HOME}/bin
ENV WORKDIR /home/workspace

# Define working directory.
WORKDIR ${WORKDIR}

# Install dependencies
RUN apt-get -yqq update && \
    apt-get -yqq --no-install-recommends install git bzip2 curl unzip && \
    npm install -g gulp bower && \
    npm cache clean && \
    apt-get -yqq autoremove && \
    apt-get -yqq clean && \
    rm -rf /var/lib/apt/lists/* /var/cache/* /tmp/* /var/tmp/*

# Allow root for bower
RUN echo '{ "allow_root": true }' > /root/.bowerrc

# Download sonar
RUN curl --insecure -OL https://sonarsource.bintray.com/Distribution/sonar-scanner-cli/${SONAR_SCANNER_PACKAGE} && \
  unzip ${SONAR_SCANNER_PACKAGE} -d /home && \
  rm ${SONAR_SCANNER_PACKAGE}

RUN addgroup sonar && \
  useradd -s /usr/sbin/nologin -d ${SONAR_SCANNER_HOME} -g sonar sonar && \
  chown -R sonar:sonar ${SONAR_SCANNER_HOME} && \
  chown -R sonar:sonar ${WORKDIR}

USER sonar

Solution

  • Java 8 Needs to be installed first, since sonar scanner requires it. I have added it to your Dockerfile. Needs to be installed from jessie backports.

    You should probably merge my additions to your existing Install dependencies section

    # Pull base image.
    FROM node:6
    
    LABEL maintainer "Gabriel Araujo <[email protected]>"
    
    ENV SONAR_SCANNER_VERSION 2.8
    ENV SONAR_SCANNER_HOME /home/sonar-scanner-${SONAR_SCANNER_VERSION}
    ENV SONAR_SCANNER_PACKAGE sonar-scanner-${SONAR_SCANNER_VERSION}.zip
    ENV SONAR_RUNNER_HOME ${SONAR_SCANNER_HOME}
    ENV PATH $PATH:${SONAR_SCANNER_HOME}/bin
    ENV WORKDIR /home/workspace
    
    # Define working directory.
    WORKDIR ${WORKDIR}
    
    # Install OpenJDK 8
    RUN echo 'deb http://deb.debian.org/debian jessie-backports main' > /etc/apt/sources.list.d/jessie-backports.list && \
         apt-get update && \
         apt-get install -y -t jessie-backports openjdk-8-jre-headless ca-certificates-java
    
    # Install dependencies
    RUN apt-get -yqq update && \
        apt-get -yqq --no-install-recommends install git bzip2 curl unzip && \
        npm install -g gulp bower && \
        npm cache clean && \
        apt-get -yqq autoremove && \
        apt-get -yqq clean && \
        rm -rf /var/lib/apt/lists/* /var/cache/* /tmp/* /var/tmp/*
    
    # Allow root for bower
    RUN echo '{ "allow_root": true }' > /root/.bowerrc
    
    # Download sonar
    RUN curl --insecure -OL https://sonarsource.bintray.com/Distribution/sonar-scanner-cli/${SONAR_SCANNER_PACKAGE} && \
      unzip ${SONAR_SCANNER_PACKAGE} -d /home && \
      rm ${SONAR_SCANNER_PACKAGE}
    
    RUN addgroup sonar && \
      useradd -s /usr/sbin/nologin -d ${SONAR_SCANNER_HOME} -g sonar sonar && \
      chown -R sonar:sonar ${SONAR_SCANNER_HOME} && \
      chown -R sonar:sonar ${WORKDIR}
    
    USER sonar
    

    Now it should work:

    docker build -t sonar-test .
    docker run -it --rm sonar-test /home/sonar-scanner-2.8/bin/sonar-scanner --help
    INFO: 
    INFO: usage: sonar-scanner [options]
    INFO: 
    INFO: Options:
    INFO:  -D,--define <arg>     Define property
    INFO:  -h,--help             Display help information
    INFO:  -v,--version          Display version information
    INFO:  -X,--debug            Produce execution debug output
    INFO:  -i,--interactive      Run interactively