docker-compose.yml文件中Postgresql的连接字符串

时间:2018-07-20 12:14:28

标签: postgresql docker docker-compose

我正在运行具有Docker支持的NetCore项目。我的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:
      - PGADMIN_DEFAULT_EMAIL=user@domain.com
      - PGADMIN_DEFAULT_PASSWORD=SuperSecret
    ports:
     - 19000:80

还有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

我无法将myapi服务的连接字符串绑定到postgres docker容器。我也尝试过postgresql:// postgres:postgres @ db:5432和postgresql://,只是postgres,没有运气。

我正在获取日志:

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

所以很明显我无法到达数据库服务器。

当我在本地运行应用程序时,我的原始连接字符串效果很好,格式如下:

用户ID =用户;密码=密码;服务器=本地主机;端口= 5432;数据库=测试数据库;集成安全性=真;存储=真

有什么想法如何在此处将我的服务与docker中的postgres映射? 谢谢!

2 个答案:

答案 0 :(得分:2)

我认为您应该使用此:

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

答案 1 :(得分:0)

这在具有多个Docker容器的docker环境(带有docker-compose的Linux容器)中为我工作:

连接字符串:

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

这将启动2个具有2个数据库的Postgresql数据库容器和一个可以与这些数据库进行通信的.NET Core项目(例如.NET Core Web API)。

我可以通过右键单击>添加>泊坞窗支持,然后右键单击>添加>您要泊坞化的项目上的Container Orchestrator支持来生成此基础,该项目应使用2个数据库。另请参见Microsoft Documentation