0

I have below error connecting to postgresql database hosted in docker.

myapp_Container | WARNING - ConnectionBus: Database connections: 0 active, 0 idle.
myapp_Container | ERROR - ConnectionBus: Opening JDBC connection to Some(db:54325) failed with SQLState: 08001 Error code: 0 Message: The connection attempt failed., giving up...(4/4)
myapp_Container | WARNING - ConnectionBus: Cannot create database connection.

I have two docker-compose files as below

APP Docker-Compose file

version: "3"
services:
  app:
    image: myapp
    container_name: myapp_Container
    ports:
      - "8081:8081"
    network_mode: postgres_net
    environment:
      - ADMIN_PASSWORD=Password1!
      - DATABASE_ENDPOINT=postgres://user:passwd@db:54325/mydb
    external_links:
      - db:localpostgres_1

DB Docker-Compose file

version: "3"

services:
  postgres:
    image: postgres
    container_name: localpostgres_1
    restart: always
    environment:
      - POSTGRES_USER=user
      - POSTGRES_PASSWORD=passwd
      - TZ=Europe/Amsterdam
    ports:
      - 54325:5432
    networks:
      - postgres_net

networks:
  postgres_net:
    external: true

I created postgres_net via docker network create -d bridge postgres_net

Why am I doing this? I want to create apps with same docker-compose structure without creating a postgresql db for each, if I put them together like below it will work.

version: "3"
services:
  app:
    image: myapp
    container_name: myapp_Container
    ports:
      - "8081:8081"
    environment:
      - ADMIN_PASSWORD=Password1!
      - DATABASE_ENDPOINT=postgres://user:passwd@db/mydb
    links:
      - db

  db:
    image: postgres
    container_name: postgres_x
    ports:
      - "54320:5432"
    environment:
      - TZ=Europe/Amsterdam
      - POSTGRES_USER=user
      - POSTGRES_PASSWORD=passwd
    restart: always

I can connect to the Db via pgadmin, but can't understand why myapp cannot connect, please help

pg_hba.conf


local   all             all                                     trust
# IPv4 local connections:
host    all             all             127.0.0.1/32            trust
# IPv6 local connections:
host    all             all             ::1/128                 trust
# Allow replication connections from localhost, by a user with the
# replication privilege.
local   replication     all                                     trust
host    replication     all             127.0.0.1/32            trust  

host    replication     all             ::1/128                 trust

host all all all md5

2
  • In your first docker-compose.yml file, do you also declare the external network? Why not have a separate database server per Compose file? (That would be a pretty typical microservice setup.) Commented Jun 12, 2020 at 10:13
  • @DavidMaze , microservice setup is also ok, but our databases are so small and its completely waste of resources to setup each app a separate DBMS, besides I am focused on solving this technical issue, design choices are out of picture now. Commented Jun 12, 2020 at 13:11

2 Answers 2

1

You have not properly defined network in First compose file(App compose). You have specified postgres_net as network_mode: postgres_net which is invalid. You need to specify it properly like in the second compose file.

Corrected compose file will be:

APP Docker-Compose

version: "3"
services:
  app:
    image: myapp
    container_name: myapp_Container
    ports:
      - "8081:8081"
    environment:
      - ADMIN_PASSWORD=Password1!
      - DATABASE_ENDPOINT=postgres://user:passwd@db:54325/mydb
    networks:   # declare network here
      - postgres_net

networks: #define network
  postgres_net:
    external: true

DB Docker-Compose

version: "3"

services:
  postgres:
    image: postgres
    container_name: localpostgres_1
    restart: always
    environment:
      - POSTGRES_USER=user
      - POSTGRES_PASSWORD=passwd
      - TZ=Europe/Amsterdam
    ports:
      - 54325:5432
    networks:
      - postgres_net

networks:
  postgres_net:
    external: true
Sign up to request clarification or add additional context in comments.

1 Comment

since you are using container name you need to use localpostgres_1 to connect to db.
1

this config worked perfectly

app docker-compose file

version: "3"
services:
  mendix:
    image: myapp
    container_name: myapp_Container
    ports:
      - "8081:8081"
    network_mode: postgres_net
    environment:
      - ADMIN_PASSWORD=Password1!
      - DATABASE_ENDPOINT=postgres://user:passwd@localpostgres_1/myapp
    external_links:
      - localpostgres_1

networks:
  postgres_net:
    external: true

db docker-compose

version: "3"

services:
  postgres:
    image: postgres
    container_name: localpostgres_1
    restart: always
    environment:
      - POSTGRES_USER=user
      - POSTGRES_PASSWORD=passwd
      - TZ=Europe/Amsterdam
    ports:
      - 54325:5432
    networks:
      - postgres_net

networks:
  postgres_net:
    external: true

as probably people may know, one may run the docker-compose files in a different folder per application, because docker-compose cares about the path and overwrites the last container you created in a given folder.

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.