Docker containers requests return Error: read ECONNRESET

3.7k Views Asked by At

I am using docker-compose to start two containers: one with a micronaut service and one with postgres.

This is my docker-compose.yml

version: "3.8"

services:
  web:
    image: "time"
    container_name: "time"
    build:
      context: .
      network: bridge
    ports:
      - "8081:8080"
    depends_on:
      - db
    links:
      - "db"
    environment:
      - PGHOST=db
      - PGDATABASE=postgres
      - PGUSER=postgres

  db:
    image: "postgres"
    ports:
      - "5432:5432"
    environment:
      POSTGRES_USER: postgres
      POSTGRES_PASSWORD: postgres
      POSTGRES_DB: time

But when I try to send a request to one of my endpoints, nothing happens until eventually I get a Error: read ECONNRESET

Of course when I run my app in my PC outside of docker, I can send requests fine. Is there anything wrong with my set up?

This other set up works fine, but I'd like to know how to get the two containers to connect like in my first docker-compose AND being able to access them from my host.

version: "3.8"

services:
  web:
    image: "time"
    container_name: "time"
    build: .
    depends_on:
      - db
    environment:
      - PGHOST=db
      - PGDATABASE=postgres
      - PGUSER=postgres
    network_mode: host

  db:
    image: "postgres"
    environment:
      POSTGRES_USER: postgres
      POSTGRES_PASSWORD: postgres
      POSTGRES_DB: time
    network_mode: host
1

There are 1 best solutions below

1
On BEST ANSWER

I finally got it!

version: "3.8"

services:
  web:
    image: "time"
    container_name: "time"
    build: .
    ports:
      - "8080:8080"
    depends_on:
      - db
    links:
      - "db"
    environment:
      - PGHOST=db
      - PGDATABASE=postgres
      - PGUSER=postgres
    network_mode: bridge

  db:
    image: "postgres"
    ports:
      - "5432:5432"
    environment:
      POSTGRES_USER: postgres
      POSTGRES_PASSWORD: postgres
      POSTGRES_DB: time
    network_mode: bridge