skip to Main Content

i have a project with 3 services
1-backend for golang project with groom db manager
2-db for postgres sql
3-redis for session manager

i have this dockercompose file

services:
   db:
    image: postgres:15.7
    restart: always
    network_mode: bridge
    volumes:
    - ./db/table.sql:/docker-entrypoint-initdb.d/init.sql
    ports:
    - '5433:5432'
    environment:
        POSTGRES_DB: postgres
        POSTGRES_USER: postgres
        POSTGRES_PASSWORD: 123
    
   redis:
    image: redis:7.0.12
    network_mode: bridge
    ports:
      - '6380:6379'
   
   backend:
   
    build: ./backend
    network_mode: bridge
   
    
    ports:
       - '8000:8089'  
    depends_on:
      - db

  

when i run docker compose build
and i run docker compose up i get this error on backend service

[error] **failed to initialize database, got error failed to connect to `host=127.0.0.1 user=postgres database=postgres`: dial error (dial tcp 127.0.0.1:5433: connect: connection refused)**

my connection string for postgres sql is

connection is :=>postgres://postgres:[email protected]:5433/postgres?sslmode=disable

and one more thing is 2 other services works properly

and when i run maing.go localy without docker it can connect to db and redis server

i tried change –network=host but not works and stil get that error

2

Answers


  1. Try changing it to the connection string to

    postgres://postgres:123@db:5432/postgres?sslmode=disable
    

    I have replaced 127.0.0.1 with db

    the reason is the two containers, you backend container and your database container, are in different universes(imagine each running on different computers) they have different networks config they know nothing about each other so when you tell the backend service that it needs to connect to 127.0.0.1 it thinks I got to connect to port 5432 on my localhost (not your machine localhost), its if each run on a seperate computer

    so backend tries to connect to port 5432 on its localhost but it found nothing because the database it running on another container(seperate computer) on port 5432

    docker compose solves that by giving you a way to make them communicate even though they are not in the same container(computer), by making you just use the name

    in this connection string we are telling docker compose that there is a service you have that I named "db" I want to connect to it, and docker compose dose everything in the background to make sure this happen

    version: "3.8"
    services:
      db:
        image: postgres
        restart: always
        networks:
          - main
        volumes:
          - ./db/table.sql:/docker-entrypoint-initdb.d/init.sql
        ports:
          - "5433:5432"
        environment:
          POSTGRES_DB: postgres
          POSTGRES_USER: postgres
          POSTGRES_PASSWORD: 123
    
      redis:
        image: redis
        ports:
          - "6380:6379"
        networks:
          - main
      backend:
        networks:
          - main
        build: ./backend
    
        ports:
          - "8000:8089"
        depends_on:
          - db
    
    networks:
      main:
    
    

    here its good formatted version of your compose file I also added a network main and made them all part of it

    see if this work for you 🙂

    Login or Signup to reply.
  2. Your db is not running on 127.0.0.1, it’s running on it’s own IP address, which you can find out by running docker inspect <container_name>.

    First of all, use container_name in docker-compose.yml. Otherwise your db container will have a random name assigned. I said "random" — usually it has some logic behind it, but you can’t rely on the name to be constant.

    services:
      db:
        container_name: my_postgres
        ...
      ...
    

    Next, use the container name (my_postgres in my example) in the connection string: postgres://postgres:123@my_postgres:5432/postgres?sslmode=disable

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search