0
votes

an small problem with docker.

An net core container is not able to connect to an Postgres container with docker compose.

A connection from host to the Postgres container works well with "Host= localhost;Port=5432;.. .."

But an netcore Container together with Postgres did not work, database is not reachable:

In the Net core app "Host=psql;Port=5432;.. .."

Dockerfile:

version: '3.1'

services:

  psql:
    build: 
      context: .
      dockerfile: DockerfileDb
    restart: always
    environment:
      POSTGRES_PASSWORD: Start!123
    ports: 
      - "5432:5432"
    container_name: psql
   
  
  app:
    build: 
      context: .
      dockerfile: DockerfileApp
    ports: 
      - "80:80"
     # - "443:443"
    container_name: watchdog
    links: 
      - psql
    depends_on:
      - psql
    restart: always
    

Also with an configured network :


version: '3.1'

services:

  psql:
    build: 
      context: .
      dockerfile: DockerfileDb
    restart: always
    environment:
      POSTGRES_PASSWORD: Start!123
    ports: 
      - "5432:5432"
    container_name: psql
    networks:
      - local

    
  app:
    build: 
      context: .
      dockerfile: DockerfileApp
    ports: 
      - "80:80"
     # - "443:443"
    container_name: watchdog
    links: 
      - psql
    depends_on:
      - psql
    restart: always
    networks:
      - local

networks:
  local:
    driver: bridge    

1

1 Answers

0
votes

As far as I can tell, it looks like it should be fine.

Have you tried using defined networks and removing the 'links'?

According to this it has been deprecated and I suspect it might be causing issues.

Give this a try:

version: '3.1'

services:

  psql:
    build: 
      context: .
      dockerfile: DockerfileDb
    restart: always
    environment:
      POSTGRES_PASSWORD: Start!123
    ports: 
      - "5432:5432"
    container_name: psql
    networks:
      - local

  app:
    build: 
      context: .
      dockerfile: DockerfileApp
    ports: 
      - "80:80"
     # - "443:443"
    container_name: watchdog
    depends_on:
      - psql
    restart: always
    networks:
      - local

networks:
  local:
    name: local
    driver: bridge