skip to Main Content

I have a problem about network in docker. In the docker-compose.yml includes 2 instance below

  • webserver (frontend + backend)
  • database

But i tried to bridge network and default but not working at all.The backend cannot connect to database show error "connection refuse". then i tried to docker exec -t .. into webserver and then ping to database it show "timeout".

I cannot connect database with ip address (i got a database ip address from docker exec and then hostname -i) but i connected success using "localhost"

this my docker-compose.yml

version: '3.8'
services:
  postgres_server:
    container_name: postgres14-4_container
    image: postgres:14.4
    command: postgres -c 'max_connections=200'    
    restart: always
    environment:
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=postgres
    ports:
      - '5222:5432'
    volumes: 
      - db:/var/lib/postgresql14/data
    networks:
      - web_network
  webserver:
    container_name: frontend_backend_container
    image: webserver
    ports:
      - '9090:80'
      - '8081:8081'
    env_file:
      - backend_env
    depends_on:
      - postgres_server
    restart: always
    networks:
      - web_network

volumes:
  db:
    driver: local
networks:
  web_network:
    driver: bridge

2

Answers


  1. where is your backend_env file?

    I guess you have there the host + port to connect to the db.

    You don’t need to define anything special (like the bridge).

    The webserver container should be able to access the postgres_server via postgres_server:5432 (not localhost and not 5222).

    Login or Signup to reply.
  2. To configure remote connections to postgres, you have to adjust pg_hba.conf. For example add:

    # Remote access
    host    all         all         0.0.0.0/0             trust
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search