Docker Community Forums

Share and learn in the Docker community.

Different compose services cant communicate to each other through network

I have two separate compose files; one for my postgres server which I use for several things in my own network and another compose for teamcity server/agent.

Both of these compose projects are on the same server machine.

I created two networks using docker network create called dbnet and teamcity:

  • dbnet is for teamcity-server to communicate with the database
  • teamcity is for the teamcity server and agent to communicate

All three of the services have hostname defined so that I can easily reference them through the networks via hostname and not IP’s.

I run up on both compose projects and they both start up correctly and those supposed to be in the dbnet network are there, checking with docker network inspect dbnet.

All seems good at this point.

I go to the teamcity server web page by using the host machines ip (the server running docker) and port. I am able to access the web page.

Going through teamcity’s setup, I need to now configure the tc server to use the postgres database (which I am also able to connect to with pgadmin using the server’s ip and port setup for postgres.

The configuration asks for the host and port of the postgres server, which is postgresql:1111 as seen in the attached image. I input the credentials for the pg server and submit. I get an error from the tc server saying:

Could not connect to PostgreSQL server.
the connection attempt failed.: org.postgresql.util.PSQLException: The connection attempt failed. Caused by: java.net.NoRouteToHostException: No route to host (Host unreachable)

I found this odd so I ran this command on the teamcity server container:
docker exec -it teamcity-server curl postgresql:1111

From this I got roughly the same error from tc:

curl: (7) Failed to connect to postgresql port 1111: No route to host

Based on my knowledge, this sort of setup should work. Is there some other consideration I have not taken into account or am I completely wrong on this? Below are the two different compose files and the output of the network inspect command

version: '3'

services:
    teamcity-server:
        image: jetbrains/teamcity-server
        container_name: teamcity-server
        hostname: teamcity-server
        restart: always
        networks:
            - dbnet
            - teamcity
        ports:
            - 8011:8111
        volumes:
            - tc-data-dir:/data/teamcity_server/datadir
            - tc-logs-dir:/opt/teamcity/logs
    teamcity-agent:
        image: jetbrains/teamcity-agent
        container_name: teamcity-agent
        hostname: teamcity-agent
        networks:
            - teamcity
        depends_on:
            - teamcity-server
        environment:
            - SERVER_URL=http://teamcity-server:8011
            - AGENT_NAME=agent-007

volumes:
    tc-data-dir:
    tc-logs-dir:

networks:
    teamcity:
        external:
            name: teamcity
    dbnet:
        external:
            name: dbnet
version: '3'
services:
    postgresql-sa:
        image: postgres:13.1
        container_name: postgresql
        hostname: postgresql
        restart: always
        ports:
            - 1111:5432
        environment:
            POSTGRES_PASSWORD: ${PG_SA_PW}
            POSTGRES_USER: ${PG_SA_U}
            POSTGRES_DB: public
            PGDATA: /var/lib/postgresql/data/pgdata
        volumes:
            - pgsa-data:/var/lib/postgresql/data
        networks:
            - dbnet
volumes:
    pgsa-data:
        driver: local

networks:
  dbnet:
    external:
      name: dbnet
[
    {
        "Name": "dbnet",
        "Id": "311d11d0c0349b6b76b66ce42267f960b7f7b49f241e177e9734b67ccdc2b4cf",
        "Created": "2021-04-23T10:46:45.444152793-07:00",
        "Scope": "local",
        "Driver": "bridge",
        "EnableIPv6": false,
        "IPAM": {
            "Driver": "default",
            "Options": {},
            "Config": [
                {
                    "Subnet": "192.168.32.0/20",
                    "Gateway": "192.168.32.1"
                }
            ]
        },
        "Internal": false,
        "Attachable": false,
        "Ingress": false,
        "ConfigFrom": {
            "Network": ""
        },
        "ConfigOnly": false,
        "Containers": {
            "20b27f072c3848c816b2decaa7b6c5a46c6ad982f948ef1fc44e01ddeb5bbe9a": {
                "Name": "postgresql",
                "EndpointID": "e15a3bc754409e1de9118fb958c743150b2b1c01296094dd73753b2339c3ffb1",
                "MacAddress": "02:42:c0:a8:20:02",
                "IPv4Address": "192.168.32.2/20",
                "IPv6Address": ""
            },
            "b45be7d6807af19cdeb88a5157d5526ac27691b4a94e58163c3c45ab9eccf26b": {
                "Name": "teamcity-server",
                "EndpointID": "c28b8430c6cc0f00404eb6616962c0a2b594f6a108665ec9fe51647a80098e4d",
                "MacAddress": "02:42:c0:a8:20:03",
                "IPv4Address": "192.168.32.3/20",
                "IPv6Address": ""
            }
        },
        "Options": {},
        "Labels": {}
    }
]

Bumping, still can’t figure out this issue.

The way to do this is today Docker Networking, which another answer briefly mentioned.

The short version is that you can run docker network ls to get a listing of your networks. By default, you should have one called bridge. You can either create a new one or use this one by passing --net=bridge when creating your container. From there, containers launched with the same network can communicate with each other over exposed ports.

If you use Docker Compose as has been mentioned, it will create a bridge network for you when you run docker-compose up with a name matching the folder of your project and _default appended. Each image defined in the Compose file will get launched in this network automatically.

With all that said, I’m guessing your frontend is a webserver that just serves up the HTML/JS/CSS and those pages access the backend service. If that’s accurate, you don’t really need container-to-container communication in this case anyway… both need to be exposed to the host since connections originate from the client system.

What you have described is exactly I am doing. If you review the docker-compose file, you will see that I am creating networks and mutual services that need to communicate are within those networks. So what you suggested is what I am already doing and is not helpful unfortunately.
The issue is that despite both services being in the same docker network, they still cannot communicate with each other.