Docker learning notes — net core project container connecting to MSSQL container (environment: centos7)


The previous section shows how to install MSSQL in dockerAnd (7) connect with Navi SQL Server

This section demonstrates the interconnection between the. Net core project container and the MSSQL container!

1. Create a bridge in bridge mode and connect the bridge to the MSSQL container

Net, and create a new. Net bridge container and connect to the core database

3. Change the nginx reverse proxy configuration file, connect to the bridge bridge, and restart nginx


Step 1: create a bridge bridge and connect the bridge to the MSSQL container

Use docker network ls to view the container default support network

docker network ls

Use docker network create XXX to create a custom bridge network. The default is bridge mode

docker network create test_network

Connect this bridge to the SQL server container

docker network connect test_network mssqlserver

Step 2: change the. NETCORE project database connection profile and create the container

In the. Net core project, change the data source address in the database connection string to the container name of MSSQL, for example: MSSqlServer in this example

Then regenerate the image file

docker build -t myapidemo:1.0 .

Next, create the container, pay attention! Attention!

When creating a container, be sure to specify the bridge--network=test_ Network means to connect the container to test_ Network bridge

docker run --name=myapidemo -d -p 9020:80 --network=test_network myapidemo:1.0

As can be seen from the above figure, the container runs successfully! But how to verify that the application project container is linked to the MSSqlServer container?

Very simple, insert a piece of data into the database through the application interface. If the insertion is successful, it will prove that the link is successful!

Look, the data has been successfully inserted into the database!

The above can be used as a test, but the official project still uses nginx to reverse proxy!

Step 3: change the nginx reverse proxy configuration file, connect to the bridge bridge, and restart nginx

The previous section has demonstrated how to configure nginx reverse proxy, so I won’t repeat it here!

reference resources:Docker learning notes 1 – deploy the. Net core 3.1 project to the docker container and use nginx reverse proxy (centos7) (2)

Because all the containers above use the bridge bridge connection, you will find that it is invalid to continue to use IP mapping in the original nginx configuration file, so you need to change the original IP to the container name!

Here, you can directly modify the default.conf file

Set proxy_ The IP address after pass is changed to the container name of the. Net core project, as follows: http://myapidemo 80

server {
    listen 80;
    charset utf-8;
    server_ name;   #If there is a domain name, please fill in here : external port, you can access! No domain name, fill in localhost

    location / {
       proxy_pass http://myapidemo:80;
       proxy_redirect default;

       # root   /usr/share/nginx/html;
       # index  index.html index.htm;

After modification, save and exit! And restart the nginx container

docker restart nginxapi

Enter the container and reload the configuration,

Docker exec - it nginx Bash

Nginx - T & & nginx - s reload -- reload configuration

Then add a piece of data using the nginx proxy’s port 4030!

Data added successfully!


You can also visit successfully by using domain name!

Other related commands:

Docker network LS -- View network information of container

Docker network inspect network ID -- see which containers are linked to the network
Docker network connect bridge name container name -- connect the container to a custom bridge
Nginx - T & & nginx - s reload -- enter the nginx container and reload the configuration