Unknown Host Exception with Example Docker Compose (or anything I've tried)

I have been trying to get sonarqube and postgres up, per the example docker compose on github, but no matter what, I get “Caused by: java.net.UnknownHostException: db”. It does not like SONAR_JDBC_URL: jdbc:postgresql://db:5432/sonar at all - how do I fix this so it can resolve or communicate with postgres? I’ve tried localhost, etc…im flailing. I appreciate any walkthrough in understanding what is happening. Thanks!

Compose.yml:

version: "3"
services:
  sonarqube:
    image: sonarqube:community
    hostname: sonarqube
    container_name: sonarqube
    depends_on:
      - db
    environment:
      SONAR_JDBC_URL: jdbc:postgresql://db:5432/sonar
      SONAR_JDBC_USERNAME: sonar
      SONAR_JDBC_PASSWORD: sonar
    volumes:
      - sonarqube_data:/opt/sonarqube/data
      - sonarqube_extensions:/opt/sonarqube/extensions
      - sonarqube_logs:/opt/sonarqube/logs
    ports:
      - "9000:9000"
  db:
    image: postgres:13
    hostname: postgresql
    container_name: postgresql
    environment:
      POSTGRES_USER: sonar
      POSTGRES_PASSWORD: sonar
      POSTGRES_DB: sonar
    volumes:
      - postgresql:/var/lib/postgresql
      - postgresql_data:/var/lib/postgresql/data

volumes:
  sonarqube_data:
  sonarqube_extensions:
  sonarqube_logs:
  postgresql:
  postgresql_data:

Last few lines of compose logs:

e   | Caused by: java.net.UnknownHostException: db
sonarqube   |   at java.base/sun.nio.ch.NioSocketImpl.connect(NioSocketImpl.java:567)
sonarqube   |   at java.base/java.net.SocksSocketImpl.connect(SocksSocketImpl.java:327)
sonarqube   |   at java.base/java.net.Socket.connect(Socket.java:633)
sonarqube   |   at org.postgresql.core.PGStream.createSocket(PGStream.java:241)
sonarqube   |   at org.postgresql.core.PGStream.<init>(PGStream.java:98)
sonarqube   |   at org.postgresql.core.v3.ConnectionFactoryImpl.tryConnect(ConnectionFactoryImpl.java:109)
sonarqube   |   at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:235)
sonarqube   |   ... 62 common frames omitted
sonarqube   | 2023.01.07 00:55:22 INFO  web[][o.s.s.a.EmbeddedTomcat] HTTP connector enabled on port 9000
sonarqube   | 2023.01.07 00:55:22 INFO  web[][o.s.p.ProcessEntryPoint] Hard stopping process
sonarqube   | 2023.01.07 00:55:22 INFO  app[][o.s.a.SchedulerImpl] Process[Web Server] is stopped
sonarqube   | 2023.01.07 00:55:22 WARN  app[][o.s.a.p.AbstractManagedProcess] Process exited with exit value [ElasticSearch]: 143
sonarqube   | 2023.01.07 00:55:22 INFO  app[][o.s.a.SchedulerImpl] Process[ElasticSearch] is stopped
sonarqube   | 2023.01.07 00:55:22 INFO  app[][o.s.a.SchedulerImpl] SonarQube is stopped

Bump…

Hello @Kazulin ,

Could you try with the following SONAR_JDBC_URL environment variable?
SONAR_JDBC_URL: jdbc:postgresql://postgresql:5432/sonar

I try your compose.yml and everything works fine on my side, with both db and postgresql hostname in the JDBC URL.

I suspect that for some reason, your services are not in the same network, so to investigate further and after running the docker compose:

  • Check the networks that are created in docker docker network ls
  • Determine to which network the container are attached: do a docker network inspect {name} and in the section Containers, look for sonarqube and postgres, they should be on the same network