简体   繁体   中英

How to connect docker images of ASP.NET Core (code first) and SQL server

I'm working with a project on nopCommerce, and trying to run it using docker images. There are two images, one is for nopCommerce and the second one is for MSSQL server. Here are the step I have followed,

1) Build docker image and run on a port 8080

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

The project runs on the port 8080

2) Pulled out MSSQL(Express) Linux image from the Docker Hub

3) Ran it using command

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) Created DB

7) Tried to connect MSSQL image using VSCode extension and it works

在此处输入图片说明

8) Passed the same connection string to the nopCommerce install page

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

But it gives an error

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)

I've tried using IP instead of localhost, adding port 1433 and almost everything from different forums though the error remains the same.

Edit: Here is network inspect

$ 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": {}
    }
]

This is usually because Docker creates its own internal network for the containers and thus the two containers do not see each other by default. So you have two options:

  1. Create a docker network and attach the two containers to that network

Create a new network:

docker network create <networkName>

To connect a container to a particular network either:

– Connect to the network when you start the container with docker run and the —network flag. Something like this (note the network flag)

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

– Connect already created container to the network using

docker network connect <networkName> <containerName>
  1. Use docker-compose to setup both your images (this is my preferred option). Docker compose automatically creates the network and sets up the containers to run in that network

Once you have set the network you should be able to reach from container to container using containerName:PortNumber

If the containers still cannot talk to each other:

– Check that they both actually in the same network: by running docker network inspect

– Check if the other machine can be reached (from within the container)

ping <dockerContainerName>
ping <dockerContainerIPaddress>

– Check if the appropriate port is open

nmap -p <portnumber> <dockerContainerIPaddress>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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