简体   繁体   中英

How to communicate between two docker containers (mssql and .net core app)

I have the following issue: I have the following docker-compose file

version: "3"
services:
    web:
        build: .
        ports:
            - "8000:80"
        links:
            - my-special-db
        networks:
            - demo-net
    my-special-db:
        image: "microsoft/mssql-server-windows-developer"
        ports:
            - "1433:1433"
        environment:
            - ACCEPT_EULA=Y
            - sa_password=demo
        networks:
            - demo-net
networks:
        demo-net:
          driver: nat

In the appsettings.Docker.json I have the following connection string: "ConnectionStrings": { "DefaultConnection": "Server=my-special-db;Database=ContosoUniversity3;Trusted_Connection=True;MultipleActiveResultSets=true" }

I've tried passing also the password, it doesn't work. What I'm doing wrong?

You need to add both services in a common network.

Please rede this document: Docker Networks

In each of your services:

  networks:
   - sql-net

In the end of the compose file:

networks:
   sql-net:
     driver: bridge

Then you'll be able to connect to the database through its container name, in your case my-special-db

EDIT 1 - Network Drivers under windows

The error reported by the OP suggests that it's running Windows 10. There is an open bug for it here

There are reports that using transparent driver might work:

networks:
   sql-net:
     driver: transparent

EDIT 2 - Links Option

You can also try using the legacy Link feature . Add this to the web container:

web:
  links:
    - my-special-db

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