Can't access SQL Server from my dockerized .NET Core app

I'm using docker to deploy my web application in my VPS (Ubuntu 18.04).

Here is my Dockerfile

FROM microsoft/dotnet:latest
COPY . /app
WORKDIR /app

RUN dotnet restore
RUN dotnet build

EXPOSE 5000/tcp
ENV ASPNETCORE_URLS http://*:5000
ENV ASPNETCORE_ENVIRONMENT docker

ENTRYPOINT [ "dotnet", "watch", "run", "--no-restore", "--urls", "http://*:5000"]

I also use Nginx and it works perfectly (http://www.onserassure.com)

It's already running with SQLite but I wanted to use SQL Server. So I found this tutorial to use SQL Server on Mac/Linux : https://www.phillipsj.net/posts/working-with-sql-server-on-linux-for-dotnet-development And it worked fine. I ran my SQL Server container with :

 docker run -d --name sql_server_demo -e 'ACCEPT_EULA=Y' -e 'SA_PASSWORD=XXXXX' -p 1433:1433 microsoft/mssql-server-linux

And used this connection string :

"Server=localhost;Database=Movies;User Id=sa;Password=XXXXXXX;"

So it worked fine with a simple dotnet run in local

But when I put my app in a container (even in local), it can't access to my database...

I don't really understand why. I'm testing on my Macbook and my VPS is on Linux

1 answer

  • answered 2019-06-12 06:52 Alex

    So it looks like you have .net core app in a container, and then you run your database in a separate container. If that is so then your connection string won't work in the .net core app container. Localhost inside a container is not the same as localhost of the host machine.

    You can try to use host networking to see if this is the issue probably. I remeber the host networking option has some funky behavior with Docker Desktop on mac and windows though. Should be ok if you're on linux.

    I would recommend you use something like docker-compose instead though.