C
C#5d ago
Keyvan

Dockerizing dotnet

Can anyone help me with dockerizing .NET backend? I have used Golang for backend before and dockerizing that was very simple, but it seems very complicated in .NET....
17 Replies
Salman
Salman5d ago
Have you tried something yet ? or are you facing any specific problem ? Visual Studio can generate the Dockerfile and compose for you if needed it's simple
Keyvan
KeyvanOP5d ago
yes I have a specific question When i run docker compose up i get the server running in the container and the volume mounting works. But when i try to send a curl request to the server i get the following: curl: (56) Recv failure: Connection reset by peer I have tried to troubleshoot this but i cant find the problem... Here is my docker file:
FROM mcr.microsoft.com/dotnet/sdk:9.0 AS build

WORKDIR /app

COPY *.csproj /app
RUN dotnet restore

EXPOSE 5001
FROM mcr.microsoft.com/dotnet/sdk:9.0 AS build

WORKDIR /app

COPY *.csproj /app
RUN dotnet restore

EXPOSE 5001
Here is my docker compose file:
version: '3'

services:
frontend:
build:
context: ./client
dockerfile: Dockerfile
volumes:
- ./client/:/app/
- ./scripts/client/:/scripts/
ports:
- "3000:3000"
entrypoint:
- /scripts/entrypoint.sh
environment:
- CHOKIDAR_USEPOLLING=True # Enable polling for file changes
- WATCHPACK_POLLING=True
- NODE_ENV=development

backend:
build:
context: ./server
dockerfile: Dockerfile
volumes:
- ./server/:/app/
- ./scripts/server/:/scripts/
ports:
- "5001:5001"
entrypoint:
- /scripts/entrypoint.sh
env_file:
- ./server/.env
version: '3'

services:
frontend:
build:
context: ./client
dockerfile: Dockerfile
volumes:
- ./client/:/app/
- ./scripts/client/:/scripts/
ports:
- "3000:3000"
entrypoint:
- /scripts/entrypoint.sh
environment:
- CHOKIDAR_USEPOLLING=True # Enable polling for file changes
- WATCHPACK_POLLING=True
- NODE_ENV=development

backend:
build:
context: ./server
dockerfile: Dockerfile
volumes:
- ./server/:/app/
- ./scripts/server/:/scripts/
ports:
- "5001:5001"
entrypoint:
- /scripts/entrypoint.sh
env_file:
- ./server/.env
Kauan
Kauan5d ago
after restoring the packages, you need to build the project and publish it. Here's an example
FROM mcr.microsoft.com/dotnet/sdk:9.0 AS build
WORKDIR /src
COPY ["src/WebApi/WebApi.csproj", "WebApi/"]
COPY ["src/Application/Application.csproj", "Application/"]
COPY ["src/Domain/Domain.csproj", "Domain/"]
COPY ["src/Contracts/Contracts.csproj", "Contracts/"]
COPY ["src/Infrastructure/Infrastructure.csproj", "Infrastructure/"]
COPY ["Directory.Packages.props", "./"]
COPY ["Directory.Build.props", "./"]
RUN dotnet restore "WebApi/WebApi.csproj"
COPY . ../
WORKDIR /src/WebApi
RUN dotnet build "WebApi.csproj" -c Release -o /app/build

FROM build AS publish
RUN dotnet publish --no-restore -c Release -o /app/publish

FROM mcr.microsoft.com/dotnet/aspnet:9.0
ENV ASPNETCORE_HTTP_PORTS=5001
EXPOSE 5001
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "WebApi.dll"]
FROM mcr.microsoft.com/dotnet/sdk:9.0 AS build
WORKDIR /src
COPY ["src/WebApi/WebApi.csproj", "WebApi/"]
COPY ["src/Application/Application.csproj", "Application/"]
COPY ["src/Domain/Domain.csproj", "Domain/"]
COPY ["src/Contracts/Contracts.csproj", "Contracts/"]
COPY ["src/Infrastructure/Infrastructure.csproj", "Infrastructure/"]
COPY ["Directory.Packages.props", "./"]
COPY ["Directory.Build.props", "./"]
RUN dotnet restore "WebApi/WebApi.csproj"
COPY . ../
WORKDIR /src/WebApi
RUN dotnet build "WebApi.csproj" -c Release -o /app/build

FROM build AS publish
RUN dotnet publish --no-restore -c Release -o /app/publish

FROM mcr.microsoft.com/dotnet/aspnet:9.0
ENV ASPNETCORE_HTTP_PORTS=5001
EXPOSE 5001
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "WebApi.dll"]
Kauan
Kauan5d ago
Amichai Mantinband
YouTube
Dockerize Your .NET Application in 5 Minutes!
All videos in this playlist: https://www.youtube.com/playlist?list=PLzYkqgWkHPKCw7zPNV3xnN1JjtglUzB2W In today's video we'll see how simple it is to dockerize a .NET application. We'll create a Dockerfile with 3 stages for building, publishing, and running our web application in just a few minutes. Join us on discord:  https://www.patreon.com/...
Amichai Mantinband
YouTube
Getting started with Docker Compose
All videos in this playlist: https://www.youtube.com/playlist?list=PLzYkqgWkHPKCw7zPNV3xnN1JjtglUzB2W In today's video, we'll see how simple it is to set up Docker Compose and use it to configure both our .NET application and a postgres database simply by running "docker compose up --build" Join us on discord:  https://www.patreon.com/amantinb...
Salman
Salman5d ago
yup that's not a complete dockerfile, look at what kauan posted
Keyvan
KeyvanOP5d ago
Its a development dockerfile Not prod
Salman
Salman5d ago
still that's not complete
Keyvan
KeyvanOP5d ago
Also i run dotnet run in entrypoint.sh
Anton
Anton5d ago
if the goal is to run the application, it's not happening in your case you just pulled the dependencies
Keyvan
KeyvanOP5d ago
Yes but in the docker compose there is defined entrypoint which executes a shell script dotnet run .
Anton
Anton5d ago
sure but it doesn't have any source code
Keyvan
KeyvanOP5d ago
yes it does because of the volume mounting in the docker compose
Anton
Anton5d ago
i see
Keyvan
KeyvanOP5d ago
yeah The thing is: i can send curl request from within the docker shell but not form outside of the docker shell So the code works fine, the backend is launched and runnung but i cannot send requests to the backend from outside of the container...
Anton
Anton5d ago
alr i think we had a similar problem it was something to do with the apache config because a proxy was used I remember there was also something to do with container networks or whatever we could send requests to a docker container directly but not via port mapping I wasn't the one solving it so I don't know the solution or the exact problem
Keyvan
KeyvanOP5d ago
hmmm, then i suspect port mapping as the problem. I will try to send http rq to the backend from the frontend. They are in the same container in development So if it is any mapping or CORS i should get an error I got it to work!!! @everyone The problem was that in docker i cant use localhost(127.0.0.0) i switched to 0.0.0.0 and now its working Thanks for the effort
wasabi
wasabi5d ago
Ya'll made this very difficult. Building container images is built into the .NET Publishing SDK. In VS you can right click and hit publish container. From the CLI you can use the PublishContainer target. No Dockerfiles or Docker required.

Did you find this page helpful?