user3365017
user3365017

Reputation: 511

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?

Upvotes: 0

Views: 970

Answers (1)

Alexandre Juma
Alexandre Juma

Reputation: 3303

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 transparentdriver 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

Upvotes: 1

Related Questions