Docker-compose not able to bind network on both container (postgres & django test server)

So this may sound pretty usual for many of us, however, I have been banging my head against it for almost a week now.

This is what I’m trying to achieve with a docker-compose.yaml file

  version: '3.6'

  networks:
    network:
      driver: bridge

  services:
    webapp:
      build: .
      container_name: djangoapp
      hostname: djangoapp
      networks:
        - network
      volumes:
        - "${pwd}/code:/djangoapp/code"
      ports:
        - 8000:8000
      depends_on:
        - db

    db:
      container_name: psqldb
      hostname: psqldb
      networks:
        - network
      image: postgres:latest
      restart: always
      environment:
        POSTGRES_DB: djangoapp_web
        POSTGRES_USER: django
        POSTGRES_PASSWORD: django_password
      healthcheck:
        test: ['CMD-SHELL', 'pg_isready -U postgres']
        interval: 10s
        timeout: 5s
        retries: 5

Also, I do have a Dockerfile, which seems to be doing installation and configuration file with ubuntu:latest just fine.

My Django app is configured to use database from 5432 as shown below (pardon my illustration)

postgresql/5432 → django test server/8000

Problem is coming when Django app tries to contact database on other container, which is going on different network and not accessible.

(narender)-(~/code) → docker inspect 5e3480b46fb7 bf31406f1ecc -f “{{json .NetworkSettings.Networks }}”
{“bridge”:{“IPAMConfig”:null,“Links”:null,“Aliases”:null,“NetworkID”:“6cb7688e01d3d44503d1ac72b082dcca233fe5459d5bf7c4baafbb6455688af0”,“EndpointID”:“05a033e46c68023c3cf01a4f877857932248c4cacdde4b1b3d4be4c1c07e6711”,“Gateway”:“172.17.0.1”,“IPAddress”:“172.17.0.2”,“IPPrefixLen”:16,“IPv6Gateway”:“”,“GlobalIPv6Address”:“”,“GlobalIPv6PrefixLen”:0,“MacAddress”:“02:42:ac:11:00:02”,“DriverOpts”:null}}
{“phai_network”:{“IPAMConfig”:null,“Links”:null,“Aliases”:[“db”,“bf31406f1ecc”],“NetworkID”:“512c49f24c97049809891ec0edfacae7c85e37edde1540b4aa57dc39b48d8dbd”,“EndpointID”:“6886d6d62644d85c19504e0a39e84c3c0b0b52a5e87b7d7b182f4bc59032bc57”,“Gateway”:“172.18.0.1”,“IPAddress”:“172.18.0.2”,“IPPrefixLen”:16,“IPv6Gateway”:“”,“GlobalIPv6Address”:“”,“GlobalIPv6PrefixLen”:0,“MacAddress”:“02:42:ac:12:00:02”,“DriverOpts”:null}}

Notice that the both the networks are different (highlighted in bold).

I have no clue, why its happening. Any guidance would be highly appreciated.

Thanks.