zlaayaa
zlaayaa

Reputation: 615

Connection string for postgresql in docker-compose.yml file

I am running NetCore project with docker support. My docker-compose.override.yml:

version: '3.4'

services:
  traefik:
    ports:
      - '80:80'
      - '443:443'
      - '8080:8080'

  myapi:
    environment:
      - 'ASPNETCORE_ENVIRONMENT=Development'
      - 'ConnectionStrings:DbConnection=postgresql://postgres'
      - 'Services:Authorization=http://10.0.75.1'
      - 'Authorization:Url=http://10.0.75.1'

  postgres:
    environment:
      POSTGRES_USER: "user"
      POSTGRES_PASSWORD: "pass"
      POSTGRES_DB: testdb
    ports:
      - 5432:5432

  pgadmin4:
    environment:
      - [email protected]
      - PGADMIN_DEFAULT_PASSWORD=SuperSecret
    ports:
     - 19000:80

And docker-compose.yml:

version: '3.4'

services:
  traefik:
    image: traefik
    command: --web --docker --docker.domain=docker --logLevel=DEBUG
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock
      - /develop/traefik/acme:/etc/traefik/acme

  postgres:
    image: postgres
    labels: { "traefik.enable":"false" }

  myapi:
    image: myapi
    build:
      context: .
      dockerfile: MyApi/Dockerfile
    links:
      - postgres
    labels: { "traefik.backend":"myapi", "traefik.frontend.rule":"PathPrefixStrip:/" }

  pgadmin4:
    image: dpage/pgadmin4
    links:
      - postgres

I can't bind connection string from myapi service to postgres docker container. I have also tried with postgresql://postgres:postgres@db:5432, and postgresql://, and just postgres, no luck.

I am getting log:

System.ArgumentException: Format of the initialization string does not conform to specification starting at index 0.

So obviously i cannot reach the db server.

My original connection string, when I run app locally, works great, in form:

User ID = user;Password=pass;Server=localhost;Port=5432;Database=testdb;Integrated Security=true;Pooling=true

Any ideas how to map my service with postgres in docker here? Thx!

Upvotes: 5

Views: 19677

Answers (2)

CodingYourLife
CodingYourLife

Reputation: 8586

This worked for me in my docker environment (Linux Containers with docker-compose) with multiple docker containers:

Connection string:

host=host.docker.internal;port=5432;database=db1;username=userxyz;password=password123

docker-compose.yml:

version: '3.5'

networks:
  proxynet:
    driver: bridge
services:
  db1:
    image: postgres:11.4
    restart: always
    environment:
      - POSTGRES_USER=userxyz
      - POSTGRES_PASSWORD=password123
      - POSTGRES_DB=mydb1
    ports:
      - "5434:5432"
    networks:
      - proxynet
  auth_db:
    image: postgres:11.4
    restart: always
    environment:
      - POSTGRES_USER=userxyz
      - POSTGRES_PASSWORD=password123
      - POSTGRES_DB=mydb2
    ports:
      - "5435:5432"
    networks:
      - proxynet
  myprojectapi:
    image: ${DOCKER_REGISTRY-}myprojectapi
    environment:
      - ASPNETCORE_ENVIRONMENT=Development
      - ASPNETCORE_URLS=https://+:443;http://+:80
    ports:
      - "443:443"
      - "80:80"
    build:
      context: .
      dockerfile: myprojectapi/Dockerfile
    depends_on:
      - db1
      - auth_db
    networks:
      - proxynet

This starts 2 postgresql database containers with 2 databases and a .NET Core project (like a .NET Core Web API) that can talk to those databases.

I could generate the basis for this via right click > add > docker support and right click > add > Container Orchestrator Support on the project you want to dockerize and that should use the 2 databases. See also the Microsoft Documentation

Upvotes: 3

Soorena
Soorena

Reputation: 4452

I think you should use this:

User ID = user;Password=pass;Server=postgres;Port=5432;Database=testdb;Integrated Security=true;Pooling=true

Upvotes: 4

Related Questions