C
C#4w ago
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.
No description
5 Replies
✿ Scarlet ✿
Dont the ports appear when you expose a container in docker desktop?
XZetkaX
XZetkaXOP4w ago
I see then on pc this is screen from laptop but they are viable in dockerfile I pasted
✿ Scarlet ✿
Can you access the db through dbms?
Jimmacle
Jimmacle4w ago
the ports would appear for published ports, but that shouldn't be done unless you want the db to be accessible outside the container network
XZetkaX
XZetkaXOP4w ago
yes, I found out that i have to use Server=docker_sqlserver, 1433; inside container Server=localhost, 1433; when i run locally
Want results from more Discord servers?
Add your server