首页 > 解决方案 > 如何连接 ASP.NET Core(代码优先)和 SQL server 的 docker 镜像

问题描述

我正在处理 nopCommerce 上的一个项目,并尝试使用 docker 图像运行它。有两张图片,一张用于 nopCommerce,第二张用于 MSSQL 服务器。这是我遵循的步骤,

1) 构建 docker 镜像并在 8080 端口上运行

C:\Users\Admin>docker run -d -p 8080:80 --name mystore nop420
ca626cc5ed4e3759a03e9645dcd374016a5d8f278ffede8e1345f851f9a82c7d

项目运行在8080端口

2) 从Docker Hub中提取 MSSQL(Express) Linux 镜像

3)使用命令运行它

docker run -e 'ACCEPT_EULA=Y' -e 'SA_PASSWORD=yourStrong(!)Password' -e 'MSSQL_PID=Express' -p 1433:1433 -d mcr.microsoft.com/mssql/server:2017-latest-ubuntu 

4)docker exec -it unruffled_tharp "bash"

5)/opt/mssql-tools/bin/sqlcmd -S localhost -U SA -P 'yourStrong(!)Password'

6) 创建数据库

7) 尝试使用 VSCode 扩展连接 MSSQL 图像,它可以工作

在此处输入图像描述

8) 将相同的连接字符串传递给 nopCommerce 安装页面

Data Source=localhost;Initial Catalog=nop420;User ID=sa;Password=yourStrong(!)Password

但它给出了一个错误

Setup failed: An error occurred while creating the database: A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: TCP Provider, error: 40 - Could not open a connection to SQL Server)

我尝试使用 IP 而不是 localhost,添加端口 1433 以及来自不同论坛的几乎所有内容,尽管错误仍然相同。

编辑:这是网络检查

$ docker network inspect 6cb
[
    {
        "Name": "mynetwork",
        "Id": "6cb7171b74ee08a55d4ab8c9a26518bfb0a21ee5d8894300a7151453925f550d",
        "Created": "2019-07-16T01:13:46.4791178Z",
        "Scope": "local",
        "Driver": "bridge",
        "EnableIPv6": false,
        "IPAM": {
            "Driver": "default",
            "Options": {},
            "Config": [
                {
                    "Subnet": "172.18.0.0/16",
                    "Gateway": "172.18.0.1"
                }
            ]
        },
        "Internal": false,
        "Attachable": false,
        "Ingress": false,
        "ConfigFrom": {
            "Network": ""
        },
        "ConfigOnly": false,
        "Containers": {
            "ca626cc5ed4e3759a03e9645dcd374016a5d8f278ffede8e1345f851f9a82c7d": {
                "Name": "mystore",
                "EndpointID": "dbe46ebc4208bbce5d29c55c40bc0a0809ff3196da5ce7360c6e1a6771fcb7be",
                "MacAddress": "02:42:ac:12:00:03",
                "IPv4Address": "172.18.0.3/16",
                "IPv6Address": ""
            },
            "ca7b383ff3d5118aa15183728084e355619eb28b47d6e60c885a9e5c5af795ba": {
                "Name": "nostalgic_northcutt",
                "EndpointID": "6ec4ae69ba2586ce432471e82dcecc94e598e9f9c18f59ef2b557a317adf4736",
                "MacAddress": "02:42:ac:12:00:02",
                "IPv4Address": "172.18.0.2/16",
                "IPv6Address": ""
            }
        },
        "Options": {},
        "Labels": {}
    }
]

标签: c#sql-serverdockerdocker-buildnopcommerce-4.2

解决方案


这通常是因为 Docker 为容器创建了自己的内部网络,因此默认情况下两个容器看不到彼此。所以你有两个选择:

  1. 创建一个 docker 网络并将两个容器附加到该网络

创建一个新网络:

docker network create <networkName>

要将容器连接到特定网络:

– 使用 docker run 和 --network 标志启动容器时连接到网络。像这样的东西(注意网络标志)

docker run -dit --name <containerName> --network <networkName> <imageName>

- 使用已创建的容器连接到网络

docker network connect <networkName> <containerName>
  1. 使用 docker-compose 设置两个图像(这是我的首选)。Docker compose 自动创建网络并设置容器以在该网络中运行

一旦你设置了网络,你应该能够使用从容器到容器的访问containerName:PortNumber

如果容器仍然无法相互通信:

– 检查它们是否实际上在同一个网络中:通过运行docker network inspect

– 检查是否可以到达另一台机器(从容器内)

ping <dockerContainerName>
ping <dockerContainerIPaddress>

– 检查相应的端口是否打开

nmap -p <portnumber> <dockerContainerIPaddress>

推荐阅读