从另一个Docker容器连接到在Docker容器中运行的SQL Server数据库

时间:2019-10-03 10:57:59

标签: sql-server docker

我正在通过以下方法在Docker容器中创建数据库

docker run -e "ACCEPT_EULA=Y" -e "SA_PASSWORD=YourStrong@Passw0rd" -p 1433:1433 --network MyAppNetwork --name MyAppDb -d mcr.microsoft.com/mssql/server:2017-latest-ubuntu

然后我正在运行ASP.NET Core 3.0应用程序

docker run -p 5000:80 -e "ASPNETCORE_ENVIRONMENT=Development" --network MyAppNetwork --name MyApp myimage

应用程序内部有一个连接字符串

Data Source=MyAppDb;Initial Catalog=MyAppDb;User ID=sa;Password=YourStrong@Passw0rd

我一直得到错误

  

建立与SQL Server的连接时发生与网络相关或特定于实例的错误。服务器未找到或无法访问。验证实例名称正确,并且已将SQL Server配置为允许远程连接。 (提供者:TCP Provider,错误:40-无法打开与SQL Server的连接)

我尝试过MyAppDb,1433 localhost localhost,1433 ...没有任何作用

Docker文档说,只要其他Docker容器位于同一网络上,您就应该能够通过它们的名称来引用它们,但是它的行为与我预期的不同。我没有尝试不指定网络,因此容器仅默认为网桥,但没有任何更改。

注意:我可以从我的主机上完全正常地连接到数据库,但是从另一个Docker容器中可以正常工作。

有什么想法吗?

-更新-

docker network inspect MyAppNetwork

[
    {
        "Name": "MyAppNetwork",
        "Id": "2f334b2df38c199ef423fdbe65c15e33b43d983e91a3398d75f0d988d5588c13",
        "Created": "2019-10-03T10:40:40.3165785Z",
        "Scope": "local",
        "Driver": "bridge",
        "EnableIPv6": false,
        "IPAM": {
            "Driver": "default",
            "Options": {},
            "Config": [
                {
                    "Subnet": "172.19.0.0/16",
                    "Gateway": "172.19.0.1"
                }
            ]
        },
        "Internal": false,
        "Attachable": false,
        "Ingress": false,
        "ConfigFrom": {
            "Network": ""
        },
        "ConfigOnly": false,
        "Containers": {
"f3106bc710fe760e257dd05aace7e76827d953600e5520556b040706f10246fb": {
                "Name": "MyAppDb",
                "EndpointID": "c4c58df89b4117674a0db57dfe276086eb48dda36fcc6a0989425b65fc130c41",
                "MacAddress": "02:42:ac:13:00:02",
                "IPv4Address": "172.19.0.2/16",
                "IPv6Address": ""
            }
        },
        "Options": {},
        "Labels": {}
]

2 个答案:

答案 0 :(得分:1)

我花了很多时间试图“正确地”做到这一点,但最终我放弃了,回到了在docker-compose文件中使用基于弃用链接功能的解决方案

https://docs.docker.com/network/links/

立即工作,没有任何问题。当下一个Docker的重大更新发布并删除链接功能时,我将重新讨论。

docker-compose.yml

version: "3.5"

networks:
  myappnetwork:
    name: myappnetwork

services:
  myapp:
    build: .
    restart: always
    container_name: myapp
    depends_on: [myappdb]
    environment:
      ASPNETCORE_ENVIRONMENT: Development
    networks:
      - myappnetwork
    ports:
      - 5000:80
    links:
      - myappdb
  myappdb:
    image: mcr.microsoft.com/mssql/server:2017-latest-ubuntu
    restart: always
    container_name: myappdb
    environment:
      ACCEPT_EULA: Y
      SA_PASSWORD: YourStrong@Passw0rd
    networks:
      - myappnetwork
    ports:
      - 1433:1433

dockerfile

FROM mcr.microsoft.com/dotnet/core/sdk:3.0 AS build
WORKDIR /app

# copy csproj and restore as distinct layers
COPY *.sln .
COPY MyApp/*.csproj ./MyApp/
RUN dotnet restore

# copy everything else and build app
COPY MyApp/. ./MyApp/
WORKDIR /app/MyApp
RUN dotnet publish -c Release -o out

FROM mcr.microsoft.com/dotnet/core/aspnet:3.0 AS runtime
WORKDIR /app
COPY --from=build /app/MyApp/out ./
ENTRYPOINT ["dotnet", "MyApp.dll"]

appsettings.json

{
  "ConnectionStrings": {
    "DefaultConnection": "Data Source=myappdb,1433;Initial Catalog=myappdb;User ID=sa;Password=YourStrong@Passw0rd"
  }
}

答案 1 :(得分:0)

您可以尝试以下操作:

开始运行SQL容器后,通过

获取容器的IP。

docker inspect MyAppDb | grep IPAddress

如果这是Linux服务器,则应该能够通过IP地址连接另一个容器。或者,您可以使用Docker容器运行的主机服务器的IP。

此外,您可能需要添加:

Trusted_Connection=false;属性指定给您的连接字符串。