version: "3.9"

volumes:
  sonarqube_data:
  sonarqube_logs:
  pg_db:
  pg_data:

networks:
  sonarnet:

services:
  sonarqube:
    image: sonarqube:9.9.2-community
    container_name: sonarqube
    hostname: sonarqube
    ports:
      - "9000:9000"
    environment:
      - sonar.jdbc.username=sonar
      - sonar.jdbc.password=sonar
      - sonar.search.javaAdditionalOpts=-Dbootstrap.system_call_filter=false
    volumes:
      - sonarqube_data:/opt/sonarqube/data
      - ./extensions/plugins:/opt/sonarqube/extensions/plugins
      - ./logs:/opt/sonarqube/logs
    networks:
      - sonarnet

  sonar_db:
    image: postgres:16.0
    container_name: sonar_db
    hostname: db
    environment:
      - POSTGRES_USER=sonar
      - POSTGRES_PASSWORD=sonar
    volumes:
      - pg_db:/var/lib/postgresql
      - pg_data:/var/lib/postgresql/data
    ulimits:
      nofile:
        soft: 65536
        hard: 65536
    networks:
      - sonarnet

  sonar_scanner:
    image: "sonar-scanner"
    container_name: sonar_scanner
    build:
      context: .
      dockerfile: Dockerfile
    restart: on-failure
    command: >
      bash -c "echo 'start sleeping 30 sec' && sleep 30
      && echo 'Changing default passwords on sonar webpage'
      && curl -u admin:admin -X POST 'http://sonarqube:9000/api/users/change_password?login=admin&previousPassword=admin&password=password'
      && echo 'start sleeping 3 sec' && sleep 3
      && sonar-scanner -Dsonar.projectBaseDir=/usr/src"
    depends_on:
      - sonarqube
      - sonar_db
    networks:
      - sonarnet
    volumes:
      - ./code:/usr/src
