Docker-compose service(sonarqube) can't access other services(postgres)

I’ve the following sonarqube+postgres docker-compose file. It works fine on my local machines on both Docker for Windows and Docker for Mac.

But when I deploy it on our live server (Ubuntu 18.04), sonarqube can not access postgres and I get the following Error in sonarqube container logs:

Any idea why this happens on the live server?

These are Docker and Docker-Compose versions:

Local Machine

Docker version 18.09.2, build 6247962

Docker-compose version 1.23.2, build 1110ad01

Server

Docker version 18.09.6, build 481bc77

Docker-compose version 1.24.0, build 0aa5906

version: "3.5"
services:
  sonarqube:
    image: sonarqube:7.7-community    
    ports:
    - "9000:9000"
    depends_on:
    - db
    networks:
    - sonarnet
    environment:
    - SONARQUBE_JDBC_URL=jdbc:postgresql://db:5432/sonar
    - SONARQUBE_JDBC_USERNAME=sonar
    - SONARQUBE_JDBC_PASSWORD=sonar
    - sonar.forceAuthentication=true
    volumes:
    - sonarqubeConf:/opt/sonarqube/conf
    - sonarqubeData:/opt/sonarqube/data
    - sonarqubeBundledPlugins:/opt/sonarqube/lib/bundled-plugins
    - sonarqubeLogs:/opt/sonarqube/logs
  db:
    image: postgres
    networks:
    - sonarnet
    environment:
    - POSTGRES_USER=sonar
    - POSTGRES_PASSWORD=sonar
    volumes:
    - postgresql:/var/lib/postgresql
    - postgresqlData:/var/lib/postgresql/data      
networks:
  sonarnet:
    driver: bridge
volumes:
  sonarqubeConf:
    name: sonarqubeConf
    driver: local
  sonarqubeData:
    name: sonarqubeData
    driver: local 
  sonarqubeBundledPlugins:
    name: sonarqubeBundled
    driver: local
  sonarqubeLogs:
    name: sonarqubeLogs
    driver: local
  postgresql:
    name: postgresql
    driver: local
  postgresqlData:
    name: postgresqlData
    driver: local  

I get the following error in sonarqube container logs:

SonarQube Server / 7.7.0.23042 / 1dcac8b8de36b377a1810cc8f1c4c31744e12729
sonarqube_1  | 2019.06.13 09:16:13 INFO  web[][o.sonar.db.Database] Create JDBC data source for jdbc:postgresql://db:5432/sonar
sonarqube_1  | 2019.06.13 09:16:23 ERROR web[][o.s.s.p.Platform] Web server startup failed
sonarqube_1  | java.lang.IllegalStateException: Can not connect to database. Please check connectivity and settings (see the properties prefixed by 'sonar.jdbc.').

postgres container logs

db_1         | 2019-06-13 11:18:36.548 UTC [1] LOG:  listening on IPv4 address "0.0.0.0", port 5432
db_1         | 2019-06-13 11:18:36.548 UTC [1] LOG:  listening on IPv6 address "::", port 5432
db_1         | 2019-06-13 11:18:36.561 UTC [1] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
db_1         | 2019-06-13 11:18:36.579 UTC [24] LOG:  database system was shut down at 2019-06-13 11:18:35 UTC
db_1         | 2019-06-13 11:18:36.585 UTC [1] LOG:  database system is ready to accept connections

These log file have different times. Are they really related to the same start command?
sonarqube depends on db, but there is no healthcheck. Did you give Postgres enough time at the first start to create the databases?

Yes, they are related to the same containers but I got them on different time intervals.
I added healthcheck for postgres but we still have the same issue.

I added another service(jenkins) into the compose file and it’s up and running BUT I can’t access db service from jenkins service! for instance ping db does not work!!

When I get the network status of their bridge network, I can see that postgres and jenkins are both added but I even can’t ping db service by its IP address (ping 172.22.0.3) from inside jenkins service!
I suspect it’s sth related to the network between docker compose services.
Any Idea why it happens?

[
    {
        "Name": "pipeline_sonarnet",
        "Id": "754f1d4c5240fc34f0b34faffad61513b95c7c44f89fe15bd66efb7b79c4f53f",
        "Created": "2019-06-13T22:33:58.907722824+01:00",
        "Scope": "local",
        "Driver": "bridge",
        "EnableIPv6": false,
        "IPAM": {
            "Driver": "default",
            "Options": null,
            "Config": [
                {
                    "Subnet": "172.22.0.0/16",
                    "Gateway": "172.22.0.1"
                }
            ]
        },
        "Internal": false,
        "Attachable": true,
        "Ingress": false,
        "ConfigFrom": {
            "Network": ""
        },
        "ConfigOnly": false,
        "Containers": {
            "10e84761051c6e6044be3d976ee51dfd70c41c89dc38aab2d3ebcbeb9c2d6129": {
                "Name": "pipeline_db_1",
                "EndpointID": "d8e3b21ffa536c7c437479b5dbaf9a5946177ef87f73cf8e024610ebc8d3c1db",
                "MacAddress": "02:42:ac:16:00:03",
                "IPv4Address": "172.22.0.3/16",
                "IPv6Address": ""
            },
            "c1d042c77b3a0a6f519e11a5a812e38b95ac229244a5d6f28669c4132a104f30": {
                "Name": "pipeline_jenkins_1",
                "EndpointID": "e7592e5de8001ebf4df854c83670552bda78dad51191e215885fd7d58339d98b",
                "MacAddress": "02:42:ac:16:00:02",
                "IPv4Address": "172.22.0.2/16",
                "IPv6Address": ""
            }
        },
        "Options": {},
        "Labels": {
            "com.docker.compose.network": "sonarnet",
            "com.docker.compose.project": "pipeline",
            "com.docker.compose.version": "1.24.0"
        }
    }
]

Don’t know if this helps (because of that I didn’t ask it before), but why don’t you let Docker manage the networking? The same for the volumes.
Do you see the db container running or does it exit after some time? Can you exec bash in the db container and connect to the database from there?

Yes I can access db container by exec bash. It’s up and running.
I suspect the machine that runs the Docker has tight security settings. I asked the Admin to double check it

Did you try what happens when you remove the network stuff from the compose file?