0

I have a service that runs inside a docker container. This service need to accesses the database that is run in another cluster (I use kubectl port-forward and after it I access database in localhost:5432)

Here is my docker-compose file

version: '3'

services:
  api:
    build: .
    ports:
      - "8000:8000"
      - "5432:5432"
    environment:
      - APP_PORT=8000 
      - DB_SERVER=localhost
      - DB_PORT=5432
      - DB_USER=postgres
      - DB_PASSWORD=mypassword 

It seems that redirection of port 5432 does not work as expected because connection to database fails inside the container. Could you help me to understand what's going wrong?

1 Answer 1

2

In current context, localhost is api container address. You have to set DB_SERVER for correct address. Your service and database has to be in the same docker network and then you can use internal docker address resolver. After that, you should set DB_SERVER=postgres. Another way, you can set DB_SERVER to your host address.

This is how you can define network in docker-compose: https://docs.docker.com/compose/networking/#use-a-pre-existing-network

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.