Docker容器连接到不在Docker中的Postgres [英] Docker container to connect to Postgres not in docker

查看:109
本文介绍了Docker容器连接到不在Docker中的Postgres的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用docker和postgres在Windows 10计算机上运行的.NET应用程序。当我使用

I have a .NET application running on a windows 10 computer using docker and postgres. When I run using the

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

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

Connection string = "Server=localhost;Port=5433;Database=xxx;Uid=yyy;Pwd=zzz;",

选项,我可以毫无问题地连接到我的postgres数据库。

option in VS2019 I can connect to my postgres database without problems.

当我尝试运行

 "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;",

无论我尝试使用什么连接字符串,都会拒绝CONNECTION或超时错误。

No matter what I try for a connection string I get CONNECTION WAS REFUSED or TIMEOUT errors.

有人可以给我指教一下尝试什么的方法吗?我已经检查了两天的留言板,并尝试为com.docker.backend添加WINDOWS DEFENDER,我尝试了不同的连接字符串,尝试了许多其他方法来解决此问题,但都空白了。

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.

最终目标是让docker运行我的REST API,并在同一台计算机上安装postgres,但不在docker中运行。

The end goal is to have docker run my REST API and have postgres installed on the same computer but not running in docker.

新编辑: 7月6日上午1:45 MST

NEW July 6 1:45 AM MST

在容器内,我运行了以下命令:

inside the container I ran the following:

C:\app> ipconfig

C:\app>ipconfig

Windows IP配置

Windows IP Configuration

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 host.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

注意:如果我尝试在自己的环境中运行自己的应用程序,则该程序实际上可以与10.0.0.47一起使用(即使在主机上也可以) .docker.internal)...它只是从容器内部而不建立Postgres连接。

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.

**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

新编辑: 7月6日2:00 MST
如果我使用其他网络IP的IP地址我的主机只要将它们放入PG_HBA.CONF文件(在dockertest选项中),它们都可以工作。

NEW 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)

似乎没有什么可以从conta过渡当我使用DOCKER选项运行时,主机托管。即使当我将10.0.0.47添加到文件中并使用HOST.DOCKER.INTERNAL(转换为10.0.0.47)运行时,它仍然不起作用

Nothing seems to make the transition from container to host when I run with the DOCKER option. 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

**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

在搜寻互联网的过程中,我看到了一个桥梁或其他网络。我需要向docker添加网络以进行此转换吗?

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?

新编辑: MST 7月6日11:00

NEW July 6 11:00 AM MST

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

我不能打开容器中的主机文件(c:\windows\system32\drivers\etc)以查看其内容。

I cannot open the hosts file (c:\windows\system32\drivers\etc) in the container to see what it contains.

推荐答案

您是否尝试过使用hos ip作为PostgreSQL主机。我刚刚在Windows 10 docker Linux单独的容器中使用asp.net core 3.1和PostgreSQL进行了测试。

Have you tried with hos ip as your PostgreSQL host. I have just tested with asp.net core 3.1 and PostgreSQL both in windows 10 docker Linux separate container.

它运行没有问题。

我的连接字符串是

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

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

这篇关于Docker容器连接到不在Docker中的Postgres的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆