Skip to content

Instantly share code, notes, and snippets.

@ThabetAmer
Last active May 28, 2024 00:01
Show Gist options
  • Save ThabetAmer/b818a262c71467c7403fd75bc526b8f9 to your computer and use it in GitHub Desktop.
Save ThabetAmer/b818a262c71467c7403fd75bc526b8f9 to your computer and use it in GitHub Desktop.
Run SonarQube and SonarScanner in Docker-compose
#
# Based on https://hub.docker.com/_/sonarqube
#
version: "3.7"
services:
sonarqube:
container_name: sonarqube
image: sonarqube:7.9.2-community
ports:
- "9000:9000"
environment:
- SONARQUBE_JDBC_URL=jdbc:postgresql://db:5432/sonar
- SONARQUBE_JDBC_USERNAME=sonar
- SONARQUBE_JDBC_PASSWORD=sonar
networks:
- sonarnet
volumes:
- sonarqube_conf:/opt/sonarqube/conf
- sonarqube_data:/opt/sonarqube/data
- sonarqube_logs:/opt/sonarqube/logs
- sonarqube_extensions:/opt/sonarqube/extensions
- sonarqube_bundled-plugins:/opt/sonarqube/lib/bundled-plugins
db:
container_name: sonardb
image: postgres
networks:
- sonarnet
environment:
- POSTGRES_USER=sonar
- POSTGRES_PASSWORD=sonar
volumes:
- postgresql:/var/lib/postgresql
- postgresql_data:/var/lib/postgresql/data
sonarscanner:
container_name: sonarscanner
image: newtmitch/sonar-scanner
networks:
- sonarnet
volumes:
- ./:/usr/src
networks:
sonarnet:
volumes:
sonarqube_conf:
sonarqube_data:
sonarqube_logs:
sonarqube_extensions:
sonarqube_bundled-plugins:
postgresql:
postgresql_data:
sonar-project.properties
# must be unique in a given SonarQube instance
sonar.projectKey=sample1
# --- optional properties ---
# defaults to project key
#sonar.projectName=My project
# defaults to 'not provided'
#sonar.projectVersion=1.0
# Path is relative to the sonar-project.properties file. Defaults to .
sonar.sources=.
# Encoding of the source code. Default is default system encoding
sonar.sourceEncoding=UTF-8
#!/bin/bash
# per https://hub.docker.com/_/sonarqube
# to be executed before launching the app
sysctl -w vm.max_map_count=262144
sysctl -w fs.file-max=65536
ulimit -n 65536
ulimit -u 4096
@titobundy
Copy link

Hi, I borrowed some of your settings
Is it possible to add this docker-compose file in a dev container and access sonar scanner from the remote container in vscode ?

I tried some like this:

version: '3.8'

services:
  app:
    container_name: dev
    image: mcr.microsoft.com/devcontainers/java:8
    volumes:
      - ../..:/workspaces:cached
    command: sleep infinity
    network_mode: service:sonarqube

  sonarqube:
    image: sonarqube:lts-community
    ports:
      - "9000:9000"
    networks:
      - sonarnet
    environment:
      - SONARQUBE_JDBC_URL=jdbc:postgresql://db:5432/sonar
      - SONARQUBE_JDBC_USERNAME=sonar
      - SONARQUBE_JDBC_PASSWORD=sonar
    volumes:
      - sonarqube_conf:/opt/sonarqube/conf
      - sonarqube_data:/opt/sonarqube/data
      - sonarqube_extensions:/opt/sonarqube/extensions
      - sonarqube_bundled-plugins:/opt/sonarqube/lib/bundled-plugins

  sonarscanner:
    platform: linux/amd64
    image: sonarsource/sonar-scanner-cli
    volumes:
      - ../..:/workspaces:cached
    command: sleep infinity
    networks: 
      - sonarnet
      
  db:
    image: postgres
    networks:
      - sonarnet
    environment:
      - POSTGRES_USER=sonar
      - POSTGRES_PASSWORD=sonar
    volumes:
      - postgresql:/var/lib/postgresql
      - postgresql_data:/var/lib/postgresql/data
      
networks:
  sonarnet:
    driver: bridge
 
volumes:
  sonarqube_conf:
  sonarqube_data:
  sonarqube_extensions:
  sonarqube_bundled-plugins:
  postgresql:
  postgresql_data:

and in my .devcontainer file:

{
	"name": "Testing SAST tools for Java",
	// Or use a Dockerfile or Docker Compose file. More info: https://containers.dev/guide/dockerfile
	"dockerComposeFile": "docker-compose.yml",
	"service": "app",
	"workspaceFolder": "/workspaces/${localWorkspaceFolderBasename}",
	// Features to add to the dev container. More info: https://containers.dev/features.
	"features": {
		"ghcr.io/devcontainers/features/java:1": {},
		"ghcr.io/devcontainers-contrib/features/maven-sdkman:2": {},
		"ghcr.io/devcontainers-contrib/features/snyk-cli:1": {},
		"ghcr.io/devcontainers/features/docker-in-docker:2": {}
	},
	// Use 'forwardPorts' to make a list of ports inside the container available locally.
	"forwardPorts": [
		9000
	],
	"customizations": {
		"vscode": {
			"extensions": [

				"GitHub.copilot",
				"vscjava.vscode-gradle",
			]
		}
	},
	"hostRequirements": {
		"cpus": 4,
		"memory": "4gb"
	}
}

When I "Reopen In container" on my vscode I'd like to execute something like this:

docker run \
   --network=host \
   --rm -v "$(pwd):/usr/src" \
   sonarsource/sonar-scanner-cli \
  -Dsonar.projectKey=my-project \
  -Dsonar.projectName=my-project \
  -Dsonar.language=java \
  -Dsonar.java.binaries=target/classes \
  -Dsonar.sources=src/main/java \
  -Dsonar.host.url=http://localhost:9000 \
  -Dsonar.login=$JAVA_SONARQUBE_TOKEN

but inside devcontainer I can't view the sonar scanner images

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment