Create a docker file

Create a docker file

In the dotnet-docker directory, create a file named Dockerfile

# syntax=docker/dockerfile:1

FROM mcr.microsoft.com/dotnet/sdk:7.0 as build-env
WORKDIR /src
COPY src/*.csproj .
RUN dotnet restore
COPY src .
RUN dotnet publish -c Release -o /publish

FROM mcr.microsoft.com/dotnet/aspnet:7.0 as runtime
WORKDIR /publish
COPY --from=build-env /publish .
EXPOSE 80
ENTRYPOINT ["dotnet", "myWebApp.dll"]

Create a docker ignore file

To make your build context as small as possible, add a .dockerignore file to your dotnet-docker folder and copy the following into it.

Directory structure

Just to recap, we created a directory in our local machine called dotnet-docker and created a simple .NET application in the src folder. We also created a Dockerfile containing the commands to build an image as well as a .dockerignore file. The dotnet-docker directory structure should now look like:

Explanation for docker file

Last updated