.NET Tutorial - Your First Microservice

Add Docker metadata

To run with a Docker image, you need a Dockerfile — a text file that contains instructions for how to build your app as a Docker image. A Docker image contains everything needed to run your app as a Docker container.

Return to app directory

Since you opened a new command prompt in the previous step, you'll need to return to the directory you created your service in.

Since you opened a new terminal in the previous step, you'll need to return to the directory you created your service in.

Command prompt
cd MyMicroservice

Add a DockerFile

Create a file called Dockerfile with this command:

Command prompt
touch Dockerfile
Command prompt
fsutil file createnew Dockerfile 0

You can then open it in your favorite text editor.

You can then open it in your favorite text editor manually or with this command:

Command prompt
open Dockerfile
Command prompt
start Dockerfile

Replace the content of the Dockerfile to the following in the text editor:

Dockerfile
FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build
WORKDIR /src
COPY MyMicroservice.csproj .
RUN dotnet restore
COPY . .
RUN dotnet publish -c release -o /app

FROM mcr.microsoft.com/dotnet/aspnet:8.0
WORKDIR /app
COPY --from=build /app .
ENTRYPOINT ["dotnet", "MyMicroservice.dll"]

Note: Make sure to name the file as Dockerfile and not Dockerfile.txt or some other name.

Optional: Add a .dockerignore file

A .dockerignore file reduces the set of files that are used as part of `docker build`. Fewer files will result in faster builds.

Create a file called .dockerignore file (this is similar to a .gitignore file if you're familiar with those) with this command:

Command prompt
touch .dockerignore
Command prompt
fsutil file createnew .dockerignore 0

You can then open it in your favorite text editor.

You can then open it in your favorite text editor manually or with this command:

Command prompt
open .dockerignore
Command prompt
start .dockerignore

Replace the content of the .dockerignore to the following in the text editor:

.dockerignore
Dockerfile
[b|B]in
[O|o]bj
Continue