Super quick Sonar/Postgres setup with docker

Maybe I am easily impressed but wow! Here is how I used docker to setup sonar on my (Ubuntu) laptop superquick.

Postgres

First, set up a postgres container. The command below creates and starts a container called sonar-postgres, using the official docker postgres image.

docker run --name sonar-postgres -e POSTGRES_USER=sonar -e POSTGRES_PASSWORD=secret -d postgres

The container is created containing a sonar database and user with the supplied password. There are various options to the run command, for example to restart the container automatically. See docker run for more details. -d means detach from the container and run it in the background.

The command above does not publish any ports to the host, so we can’t psql to localhost port 5432 to see the database. However, the postgres container does “expose” port 5432 to linked containers. To have a quick peek inside, the following command creates a temporary container which executes the psql command. After entering the password you chose earlier, you are logged into the sonar database. When you exit, the container is gone.

docker run -it --link sonar-postgres:postgres --rm postgres sh -c 'exec psql -h "$POSTGRES_PORT_5432_TCP_ADDR" -p "$POSTGRES_PORT_5432_TCP_PORT" -U sonar'

An alternative would have been to modify the original run command to include -p 5432:5432 which would publish the ports to the host and allow direct access via psql. This was handy for me when getting started but obviously not ideal in an environment where you might have several postgres containers.

Sonar

The default command to start sonar uses the built in h2 database:

docker run -d --name sonarqube -p 9000:9000 -p 9092:9092 sonarqube:5.1

It’s not good practice to use the embedded database for continued use. To set up a container which uses the postgres container created above, use the following command:

docker run -d --name sonarqube --link sonar-postgres:pgsonar -p 9000:9000 -e SONARQUBE_JDBC_USERNAME=sonar -e SONARQUBE_JDBC_PASSWORD=secret -e SONARQUBE_JDBC_URL=jdbc:postgresql://pgsonar:5432/sonar sonarqube:5.1

In the above command, the –link option links the postgres container with a hostname of “pgsonar”, which is used again in the SONARQUBE_JDBC_URL setting to tell sonar where to find the postgres database.

Tip! If the container won’t start, run the command without the -d switch, so it remains in the foreground and you can see the log output.

Once the container is started, navigate to http://localhost:9000/ and you should see the familiar sonarqube dashboard. This process felt quicker than the usual sonar install done properly, and I can definitely see myself using docker more to supercharge my dev environment!

4 thoughts on “Super quick Sonar/Postgres setup with docker

  1. It has been pointed out to me that I could be using the variables POSTGRES_PORT_5432_TCP_ADDR and POSTGRES_PORT_5432_TCP_PORT provided by the postgres container to specify the database url for sonar, but I can’t quite work out how to do this yet. Will come back to it…

    • I’m just running locally, so if I’m using the h2 database, the url is jdbc:h2:tcp://localhost:9092/sonar (I’m exposing 9092). So could you could replace localhost with your ip address if running remotely.

      If you’re using postgres, I think you’ll need to expose the postgres port on the postgres container too, then use that port in the url.

      docker run --name sonar-postgres -p 5432:5432 -e POSTGRES_USER=sonar -e POSTGRES_PASSWORD=secret -d postgres
  2. Running docker image provided by sonarqube gave bunch of errors because i wanted to use a separate postgresdb other than the H2 db.
    I got this done with docker-compose set up.

    here is my working config:

    version: “3”

    services:
    sonarqube:
    image: “sonarqube:latest”
    ports:
    – “9000:9000”
    networks:
    – sonarnet
    depends_on:
    – db
    environment:
    – sonar.jdbc.url=jdbc:postgresql://db:5432/sonar
    – sonar.jdbc.username=sonar
    – sonar.jdbc.password=mysecretpassword
    volumes:
    – /data/docker/sonarqube/data:/opt/sonarqube/data
    – /data/docker/sonarqube/logs:/opt/sonarqube/logs
    – /data/docker/sonarqube/extensions:/opt/sonarqube/extensions

    db:
    image: “postgres:latest”
    networks:
    – sonarnet
    environment:
    – POSTGRES_USER=sonar
    – POSTGRES_PASSWORD=mysecretpassword
    volumes:
    – /data/docker/sonarqube/postgresql:/var/lib/postgresql
    – /data/docker/sonarqube/postgresql_data:/var/lib/postgresql/data

    networks:
    sonarnet:
    driver: bridge

Leave a Reply

Your email address will not be published. Required fields are marked *