docker - 如何从Docker容器连接到远程服务

标签 docker .net-core docker-compose docker-networking

我需要在docker容器中运行的dotnet核心应用程序通过其IP连接到某些外部服务,其中之一是在google云托管的远程服务器上单独运行的sql数据库。当不与Docker一起运行时,该应用程序运行没有问题,但是与Docker一起运行时失败

An error occurred using the connection to database 'PartnersDb' on server '30.xx.xx.xx,39876'.
fail: Microsoft.EntityFrameworkCore.Update[10000]
      An exception occurred in the database while saving changes for context type 'Partners.Api.Infrastructure.Persistence.MoneyTransferDbContext'.
      System.InvalidOperationException: Timeout expired.  The timeout period elapsed prior to obtaining a connection from the pool.  This may have occurred because all pooled conn
ections were in use and max pool size was reached.

我的Docker撰写文件如下所示
version: "3.5"
networks:
  my-network:
    name: my_network

services:
  partners:
    image: partners.api:latest
    container_name: partners
    build:
      context: ./
    restart: always
    ports:
      - "8081:80"
    environment:
      - ASPNETCORE_ENVIRONMENT=Docker
      - ConnectionStrings:DefaultConnection=Server=30.xx.xx.xx,39876;Database=PartnersDb;User Id=don;Password=Passwor123$$
    networks:
      - my-network
    volumes:
      - /Users/mine/Desktop/logs⁩:/logs

我有
  • bin掉进了正在运行的容器中,我可以执行ping命令来
    远程sql数据库服务器
  • 我也可以远程登录到
    数据库端口
  • 上的远程sql数据库服务器

    但是,当我做docker-compose up时出现问题,然后出现上述错误。
    Docker版本19.03.5,内部版本633a0ea在MacOS Mojave 10.14.6上运行

    我真的不知道在这个阶段该做什么。

    最佳答案

    有两个独立的网络。 docker 内部是一个单独的网络。在docker之外,在主机上,这是一个不同的网络。如果要通过localhost或IP地址访问它,它将无法正常运行。

    docker network ls
    
    将显示类似以下的输出:
    NETWORK ID          NAME                DRIVER              SCOPE
    58a4dd9893e9        133_default         bridge              local
    424817227b42        bridge              bridge              local
    739297b8107e        host                host                local
    b9c4fb3ed4ba        none                null                local
    
    您需要在本地添加Java服务的主机。尝试像下面的命令一样运行:
    服务:
    docker run --add-host remoteservice:<ip address of java service> <your image>
    
    希望这会解决。
    更多内容:https://docs.docker.com/engine/reference/run/#managing-etchosts
    对于PartnersDb数据库:
    如果PartnersDb是SQL数据库,则必须将SQL Server配置为侦听特定端口。通过“SQL Server配置管理器”>“SQL Server网络配置”>“TCP / IP属性”。
    更多内容:https://docs.microsoft.com/en-us/sql/database-engine/configure-windows/configure-a-server-to-listen-on-a-specific-tcp-port?view=sql-server-ver15
    MySQL也有类似的设置。
    之后,您必须运行add-host开关
    docker run --add-host PartnersDb:<ip address of PartnersDb database> <your image>
    
    您也可以使用这些设置来更新hosts文件。我更喜欢通过命令行。

    关于docker - 如何从Docker容器连接到远程服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60859183/

    相关文章:

    linux - 如何在 docker 中成功启用 udev 同步?

    基于ubuntu的docker容器中的python argparse

    web-services - 在.Net Core Web应用程序中获取移动地理位置

    docker - 如何通过rancher compose命令将环境变量传递给docker-compose

    docker - 在 docker 卷中保留 apache nifi flow.xml.gz 文件

    docker - Docker Dind:无法连接到Docker守护程序

    .net - 无法从其他容器 : Connection refused - 1/1 brokers are down 连接到 Kafka Docker 容器

    c# - 如何在运行时加载 Blazor 页面?

    c# - 无法让控制台日志记录在 .net core 2.0 中工作

    angular - 为什么 Docker 中的 Angular 应用程序的 Live Reload 很慢?