Cannot connect to docker container on outside VM

I have Kafka and PostgreSQL database running on a remote VM as docker containers. I have written my test cases by making a connection to these containers. My tests are working locally. But when I run these tests in circleci env it throws an error and my test cases fail.
The error is : dial tcp x.x.x.x:9092: i/o timeout for Kafka and dial tcp x.x.x.x:5432: connect: connection timed out for PostgreSQL. Could you please tell me what is the error?

The config.yaml is as follows:

# Golang CircleCI 2.0 configuration file
version: 2
jobs:
  build:
    docker:
       # specify the version
        - image: circleci/golang:1.15
        
    steps:
      - checkout

      # Download and cache dependencies
      - restore_cache: # restores saved cache if no changes are detected since last run
          keys:
            - go-mod-v4-{{ checksum "go.sum" }}

      - run: 
          name: Run unit test
          command: go test -v ./...

      - save_cache:
          key: go-mod-v4-{{ checksum "go.sum" }}
          paths:
            - "/go/pkg/mod"

      - persist_to_workspace:
          root: ~/project
          paths:
            - target    
                      
workflows:
  version: 2
  build&publish:
    jobs:
      - build

Given that it works locally and not in CCI I’d imagine that your firewall settings are blocking connections from CCI. The recently announced Whitelisting IPs feature may be of interest to you. Or you may apply any of the known workarounds for this situation.