Can’t connect to docker container from codebuild?

I have configured codebuild to run docker by setting PrivilegedMode: true. I have docker compose file like:

version: "3.3"

services:
  postgresql:
    image: postgres:11
    ports: 
      - '5432:5432'
    command: -c ssl=on -c ssl_cert_file=/workdir/tests/cert/server.crt -c ssl_key_file=/workdir/tests/cert/server.key
    volumes: 
      - ./:/workdir
    environment:
      POSTGRES_HOST_AUTH_METHOD: trust
      POSTGRES_USER: user
      POSTGRES_DB: test

  migration:
    image: flyway/flyway
    environment:
      DB_HOST: postgresql
      STAGE: dev
      SCHEMA_LOCATION: filesystem:/workdir/dbschema
    depends_on:
      - postgresql
    volumes:
      - ".:/workdir"
    entrypoint: /workdir/bin/flyway.sh
    links:
      - postgresql
  

I has two containers postgresql and flyway. Flyway is a tool to do db schema migration basically all it does is to connect to the postgresql and apply sql statement. In flyway.sh, I have below script code:

#!/bin/sh

sleep 20 

echo "Run flyway to migrate db schema"
flyway migrate -url=jdbc:postgresql://$HOST/$DBNAME -user=$USER_NAME -locations=$SCHEMA_LOCATION -password=$PASSWORD -schemas=$STAGE

It sleeps 20 seconds in order to wait the postgresql to be available. But I got this error when running it in codebuild:

migration_1   | Run flyway to migrate db schema
migration_1   | Flyway Community Edition 7.0.4 by Redgate
migration_1   | ERROR: 
migration_1   | Unable to obtain connection from database (jdbc:postgresql://postgresql/test) for user 'user': The connection attempt failed.

It seems that it fails to connect to the db instance. I also tried to connect to postgresql from codebuild container via 127.0.0.1:5432 but also failed. The problem seems to be codebuild doesn’t make the network available for these containers. Is there a way for me to fix that?

And there is no problem for me to run them from my local (MacOS).

Source: Docker Questions