© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•16mo ago•
5 replies
XZetkaX

Cannot connect my application to sql server on docker.

I tried to dockerise my asp.net application, but I get an error while applying migrations if I try to run locally. When I try doing it I have API turned off and SQL server turned on.

Docker-Compose
version: '3.4'

name: fitness

services:
  api:
    container_name: api
    build:
      context: .
      dockerfile: API/Dockerfile
    ports:
      - "5000:8080"
    depends_on:
      - docker_sqlserver

  docker_sqlserver:
    container_name: docker_sqlserver
    build:
      context: .
      dockerfile: API/db.Dockerfile
    ports:
      - "1433:1433"
version: '3.4'

name: fitness

services:
  api:
    container_name: api
    build:
      context: .
      dockerfile: API/Dockerfile
    ports:
      - "5000:8080"
    depends_on:
      - docker_sqlserver

  docker_sqlserver:
    container_name: docker_sqlserver
    build:
      context: .
      dockerfile: API/db.Dockerfile
    ports:
      - "1433:1433"


Dockerfile for API
FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build-env
WORKDIR /App

COPY API/API.csproj ./API/

RUN dotnet restore API/API.csproj

COPY API/ ./API/

RUN dotnet publish API/API.csproj -c Release -o out

FROM mcr.microsoft.com/dotnet/aspnet:8.0

WORKDIR /App
EXPOSE 8080
COPY --from=build-env /App/out ./
ENTRYPOINT ["dotnet", "API.dll"]
FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build-env
WORKDIR /App

COPY API/API.csproj ./API/

RUN dotnet restore API/API.csproj

COPY API/ ./API/

RUN dotnet publish API/API.csproj -c Release -o out

FROM mcr.microsoft.com/dotnet/aspnet:8.0

WORKDIR /App
EXPOSE 8080
COPY --from=build-env /App/out ./
ENTRYPOINT ["dotnet", "API.dll"]


Dockerfile for SQL
FROM mcr.microsoft.com/mssql/server:latest AS sqlserver
ENV ACCEPT_EULA=Y
ENV SA_PASSWORD=myPassword1!

ENV MSSQL_DBNAME=FitnessApp
FROM mcr.microsoft.com/mssql/server:latest AS sqlserver
ENV ACCEPT_EULA=Y
ENV SA_PASSWORD=myPassword1!

ENV MSSQL_DBNAME=FitnessApp


My connection sting is:
"Server=docker_sqlserver, 1433; Database=FitnessApp; User Id=sa; Password=myPassword1!; TrustServerCertificate=True;"

The are 2 exceptions

Inner Exception 1:
SqlException: 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: 0 - A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond.)

Inner Exception 2:
Win32Exception: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond.
image.png
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources
Was this page helpful?

Similar Threads

Recent Announcements

Similar Threads

✅ Docker Container Can't connect to remote SQL Server
C#CC# / help
10mo ago
how to connect to sql server?
C#CC# / help
4y ago
❔ Entity Framework + Docker sql server on ubuntu
C#CC# / help
3y ago
❔ ❔ Docker-Compose cannot find Dockerfile
C#CC# / help
4y ago