Skip to content

Instantly share code, notes, and snippets.

@ceduliocezar
Created August 1, 2017 18:58
Show Gist options
  • Save ceduliocezar/b3bf93125024482b5f2f479696842046 to your computer and use it in GitHub Desktop.
Save ceduliocezar/b3bf93125024482b5f2f479696842046 to your computer and use it in GitHub Desktop.
How easily run Sonar Qube and PostgresSQL with Docker Containers

TL;DR

After installing Docker, follow three steps:
Step 1:
Run: docker network create mynet

Step 2:
Run: docker run --name sonar-postgres -e POSTGRES_USER=sonar -e POSTGRES_PASSWORD=sonar -d -p 5432:5432 --net mynet postgres

Step 3:
Run: docker run --name sonarqube -p 9000:9000 -e SONARQUBE_JDBC_USERNAME=sonar -e SONARQUBE_JDBC_PASSWORD=sonar -e SONARQUBE_JDBC_URL=jdbc:postgresql://sonar-postgres:5432/sonar -d --net mynet sonarqube:5.6

Step 4:
Access http://localhost:9000/

How easily run Sonar Qube and PostgresSQL with Docker Containers

First install Docker on your system, it is available for Windows, Mac and Linux. Click here to download Docker

After installing run: docker version

Creating a Docker Network

In order to stablish a communication between Sonar and Postgres containers, we need to create a Docker Network. The following command will create a network called mynet.
docker network create mynet

Creating a PostgreSQL Docker Container

Sonar Qube depends on a database to works correctly, in this example we choose PostgreSQL. The command below creates and runs an instance of PostgreSQL in background with username sonar, password sonar, bounding host port 5432 with container port 5432 inside mynet Docker network.
docker run --name sonar-postgres -e POSTGRES_USER=sonar -e POSTGRES_PASSWORD=sonar -d -p 5432:5432 --net mynet postgres

Creating a Sonar Qube Docker Container

Creates and runs an instance of Sonar Qube 5.6 with database user, pass and JDBC connection by parameter. Bounding the host port 9000 to container port 9000 inside mynet Docker Network.
docker run --name sonarqube -p 9000:9000 -e SONARQUBE_JDBC_USERNAME=sonar -e SONARQUBE_JDBC_PASSWORD=sonar -e SONARQUBE_JDBC_URL=jdbc:postgresql://sonar-postgres:5432/sonar -d --net mynet sonarqube:5.6

It works!

At this point you should have Sonar working fine, access http://localhost:9000/ and see if everything works as expected.

How to start and stop previously created Docker Container

How to start a Docker Container.

Find the CONTAINER ID using the command:
docker ps --all

Start a container with the specified CONTAINER ID.
docker start YOUR_CONTAINER_ID

How to stop a Docker Container.

Find the CONTAINER ID using the command:
docker ps --all

Stop a container with the specified CONTAINER ID
docker stop YOUR_CONTAINER_ID

Undoing everything

Following are commands in case you messed up and want to start again.

Stoping all Docker Containers

Stops all containers that are running, if you run this command more than once in a row it will return a message about parameters, this is normal since the second part of the command won't anything after the first time.
docker stop $(docker ps -a -q)

Deleting all Docker Containers

Removes all containers, make sure you stopped all containers before run this command, otherwise it won't complete correctly.
docker rm $(docker ps -a -q)

Deleting all Docker Images

Removes all images downloaded, just take care with it since you will need to download all the dependencies again.
docker rmi $(docker images -q)

@ksummersill2
Copy link

I am having the same issue. The sonarqube container keeps stopping.

@shashankimanage
Copy link

@ksummersill2 i have the same issue sonarqube container just exits after 19seconds

@isildurmac
Copy link

the same issue, any idea?

@johnathafelix
Copy link

@ceduliocezar I just want to say thank you for providing this solution. It worked like a charm and saved me a lot of time and effort!

@Ya2e
Copy link

Ya2e commented Aug 12, 2019

@ksummersill2 @shashankimanage i think that you need more ressources at your vm :
sysctl -w vm_max_map_count=350000 and another cmd
check with docker logs [id] to look the logs

@ceduliocezar
Copy link
Author

ceduliocezar commented Aug 13, 2019

@ceduliocezar I just want to say thank you for providing this solution. It worked like a charm and saved me a lot of time and effort!

@johnathafelix My pleasure man. Glad that it helped.

@AndyMurgin
Copy link

AndyMurgin commented Jan 31, 2020

@ceduliocezar Thank you man!
This md was posted about 3 years ago but it still work for LTS SonarQube!
I have no idea why instructions with '-e sonar.jdbc.username=sonar' don't work. If I try to do everything which is described here https://hub.docker.com/_/sonarqube/ then I just receive "Unable to start database" error. Looks like sonar.properties is just ignored in the container. Or something like that.
Again - thank you!

@Gershon-A
Copy link

@ceduliocezar Thank you so much!
This is saving my day.
I think this is should be a reference in official
https://hub.docker.com/_/sonarqube

@hb237
Copy link

hb237 commented Jun 9, 2020

With some adjustments this solution works perfect.

For testing purposes I used a Ubuntu 18.04 on VirtualBox. I used the image for postgres 12 and the one for sonarcube 8.3.1 Do not forget about the requirements and prerequisites: https://docs.sonarqube.org/latest/requirements/requirements/

Here the commands I executed:
Edit the config as mentioned in the platform notes for linux:
nano /etc/sysctl.conf

In this file add:
vm.max_map_count=262144
fs.file-max=65536

Then update the config:
sysctl -p

As before, I just explicitly stated version 12:
docker run --name sonar-postgres -e POSTGRES_USER=sonar -e POSTGRES_PASSWORD=sonar -d -p 5432:5432 --net mynet postgres:12

Also as before, here I updated the version:
docker run --name sonarqube -p 9000:9000 -e SONARQUBE_JDBC_USERNAME=sonar -e SONARQUBE_JDBC_PASSWORD=sonar -e SONARQUBE_JDBC_URL=jdbc:postgresql://sonar-postgres:5432/sonar -d --net mynet sonarqube:8.3.1-community

If there any error it might be helpful to check the logs:
docker logs sonarqube
docker logs sonqr-postgres

@suhastridot
Copy link

The same commands working on my local laptop but not working in ec2 instance. The sonarqube just stops after a few seconds

@ceduliocezar
Copy link
Author

ceduliocezar commented Jun 9, 2020

The same commands working on my local laptop but not working in ec2 instance. The sonarqube just stops after a few seconds

@suhastridot I never made it run in a ec2 instance, some guys mentioned in comments stuff about the vm and file system options. Maybe that would put you in the direction.

@ceduliocezar
Copy link
Author

@ksummersill2 @shashankimanage i think that you need more ressources at your vm :
sysctl -w vm_max_map_count=350000 and another cmd
check with docker logs [id] to look the logs

@ksummersill2 @shashankimanage Have this helped you? If it helped maybe we can mention it in the git so if people got the same problem they can easily fix it.

@hb237
Copy link

hb237 commented Jun 9, 2020

The same commands working on my local laptop but not working in ec2 instance. The sonarqube just stops after a few seconds

@suhastridot Check out the logs of the container. It might be that the aws instance does not fulfil the requirements to run sonarqube yet: https://docs.sonarqube.org/latest/requirements/requirements/#header-5

@isuruamantha
Copy link

Good tutorial.

@lanerfy182
Copy link

@ksummersill2 @shashankimanage i think that you need more ressources at your vm :
sysctl -w vm_max_map_count=350000 and another cmd
check with docker logs [id] to look the logs

@ksummersill2 @shashankimanage Have this helped you? If it helped maybe we can mention it in the git so if people got the same problem they can easily fix it.

@ceduliocezar thank you for this!

I also encountered the issue of sonarqube container stopping after a few seconds

the command that solved the issue for me is:
sysctl -w vm.max_map_count=262144

@acidiney
Copy link

Good article, a point that need attetion is PostgreSQL container isn't mapped for a volume... so if stops, you maybe lose all your data.

@ceduliocezar
Copy link
Author

ceduliocezar commented Dec 16, 2020

Good article, a point that need attetion is PostgreSQL container isn't mapped for a volume... so if stops, you maybe lose all your data.

@acidiney 🥇 can you share your solution to this problem? I never mapped a volume.

My use case was to run local instance to briefly check some points, not intended to keep the information there for longer periods.

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