简体   繁体   English

Docker 容器连接到不在 docker 中的 Postgres

[英]Docker container to connect to Postgres not in docker

I have a .NET application running on a windows 10 computer using docker and postgres.我有一个使用 docker 和 postgres 在 windows 10 计算机上运行的 .NET 应用程序。 When I run using the当我使用

 "DockerTest": {
      "commandName": "Project",
      "launchBrowser": true,
      "launchUrl": "asset",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      },
      "applicationUrl": "https://localhost:5001;http://localhost:5000"
    },

Connection string = "Server=localhost;Port=5433;Database=xxx;Uid=yyy;Pwd=zzz;",连接字符串 = "Server=localhost;Port=5433;Database=xxx;Uid=yyy;Pwd=zzz;",

option in VS2019 I can connect to my postgres database without problems. VS2019 中的选项我可以毫无问题地连接到我的 postgres 数据库。

When I try to run the application using the当我尝试使用

 "Docker": {
      "commandName": "Docker",
      "launchBrowser": true,
      "launchUrl": "{Scheme}://{ServiceHost}:{ServicePort}/asset",
      "publishAllPorts": true,
      "useSSL": true
    },

option, I am getting errors connecting.选项,我在连接时遇到错误。 Here is my connection string.这是我的连接字符串。

 "Database1": "Server=host.docker.internal;Port=5433;Database=xxx;Uid=yyy;Pwd=zzz;",

No matter what I try for a connection string I get CONNECTION WAS REFUSED or TIMEOUT errors.无论我尝试什么连接字符串,我都会收到 CONNECTION WAS REFUSED 或 TIMEOUT 错误。

Can anyone give me pointer as to what to try?谁能告诉我要尝试什么? I have gone over the message boards for two days and tries WINDOWS DEFENDER additions for com.docker.backend, I have tried different connection strings, I have tried many many other ways to solve this and have come up blank.我已经浏览了两天的留言板并尝试了 WINDOWS DEFENDER 添加 com.docker.backend,我尝试了不同的连接字符串,我尝试了很多其他方法来解决这个空白和许多其他的方法。

The end goal is to have docker run my REST API and have postgres installed on the same computer but not running in docker.最终目标是让 docker 运行我的 REST API 并在同一台计算机上安装 postgres 但不在 Z2134AF6053B18A 中运行。

NEW EDIT: July 6 1:45 AM MST新编辑:美国东部标准时间 7 月 6 日凌晨 1:45

inside the container I ran the following:在容器内我运行了以下内容:

C:\app>ipconfig C:\app>ipconfig

Windows IP Configuration Windows IP 配置

Ethernet adapter Ethernet:

   Connection-specific DNS Suffix  . : allworx.activeis.ca
   Link-local IPv6 Address . . . . . : fe80::c06e:18b5:f9e0:48bb%4
   IPv4 Address. . . . . . . . . . . : 172.30.81.112
   Subnet Mask . . . . . . . . . . . : 255.255.240.0
   Default Gateway . . . . . . . . . : 172.30.80.1

C:\app>ping host.docker.internal C:\app>ping 主机。docker.internal

Pinging host.docker.internal [10.0.0.47] with 32 bytes of data:
Reply from 10.0.0.47: bytes=32 time<1ms TTL=127

NOTE: if I try to run my own app in the environment it actually works in with the 10.0.0.47 it works (even with host.docker.internal)... It is just from inside the container that it does not make the Postgres connection.注意:如果我尝试在环境中运行我自己的应用程序,它实际上可以在 10.0.0.47 中运行(即使使用 host.docker.internal)......它只是来自容器内部,它不会生成 Postgres联系。

**PG_HBA.CONF FILE :** 
host    replication     all             127.0.0.1/32            md5
host    replication     all             ::1/128                 md5
host    all     all     172.17.0.1/16       md5

NEW EDIT: July 6 2:00 AM MST If I use the IP address of my other network IPs on my host they all work as long as I put them into the PG_HBA.CONF FILE (in the dockertest option)新编辑: MST 7 月 6 日凌晨 2:00 如果我在主机上使用其他网络 IP 的 IP 地址,只要我将它们放入 PG_HBA.CONF 文件(在 dockertest 选项中),它们都可以工作

Nothing seems to make the transition from container to host when I run with the DOCKER option.当我使用 Z469A31FD9D773110F14057BAECCDDD25Z 选项运行时,似乎没有任何东西可以从容器过渡到主机。 Even when I add the 10.0.0.47 to the file and run with HOST.DOCKER.INTERNAL (which translates to 10.0.0.47) it still does not work即使我将 10.0.0.47 添加到文件并使用 HOST.Z469A31FD9D773110F14057BAECCDDD25Z.INTERNAL (转换为 10.0.0.47)运行,它仍然无法正常工作

**PG_HBA.CONF FILE :** 
host    replication     all             127.0.0.1/32            md5
host    replication     all             ::1/128                 md5
host    all     all     172.17.0.1/16       md5
host    all     all     10.0.0.47/16        md5
host    all     all     169.254.214.72/16   md5

C:\Users\Chris\source\repos\QuickTech.Com\QuickTechAPI>docker network ls
NETWORK ID          NAME                DRIVER              SCOPE
661118d67505        Default Switch      ics                 local
eae6a3536ef1        nat                 nat                 local
9d0b1f0209f6        none                null                local

I've seen a bridge or other network added in my hours of scouring the internet.在我上网的几个小时里,我看到了一个桥接器或其他网络。 Do I need to add a network to docker to make this transition?我是否需要向 docker 添加网络才能进行此转换?

NEW EDIT: July 6 11:00 AM MST新编辑:美国东部标准时间7 月 6 日上午 11:00

Host File in host computer
10.0.0.47 host.docker.internal
10.0.0.47 gateway.docker.internal
# To allow the same kube context to work on the host and the container:
127.0.0.1 kubernetes.docker.internal

I cannot open the hosts file (c:\windows\system32\drivers\etc) in the container to see what it contains.我无法打开容器中的主机文件 (c:\windows\system32\drivers\etc) 以查看其中包含的内容。

docker run --name=XXX -d -p 8081:8081 --add-host=computername:IP address name This was what finally make it work docker run --name=XXX -d -p 8081:8081 --add-host=computername:IP address name 这就是最终使它工作的原因

Have you tried with hos ip as your PostgreSQL host.您是否尝试过使用 hos ip 作为您的 PostgreSQL 主机。 I have just tested with asp.net core 3.1 and PostgreSQL both in windows 10 docker Linux separate container. I have just tested with asp.net core 3.1 and PostgreSQL both in windows 10 docker Linux separate container.

Its running without issue.它运行没有问题。

my connection string is我的连接字符串是

optionsBuilder.UseNpgsql("HOST=192.168.1.100;DataBase=TestIdentity;UserName=postgres;Password=*****;"); optionsBuilder.UseNpgsql("HOST=192.168.1.100;DataBase=TestIdentity;UserName=postgres;Password=*****;");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM