Barış Kısır

Software Developer

 » Home
 » Contact
 » RSS

How to Dockerize a .NET Core Application and Share on Docker Hub

19 Jul 2020 » docker, csharp, linux


How to Dockerize a .NET Core Application?

We will start with the installation of Docker. Docker runs in both Windows and Linux but we will do it in the Linux to be less problematic.

I’m using an Ubuntu server 20.04 at gcloud. Lets install the docker on it.

apt-get update && apt-get install -y
# don't mind the -y parameter, its just for saying "OK I will accept whatever you ask during the installation"
docker -v
# Docker version 19.03.8, build afacb8b7f0

Creating a simple net core application

dotnet new console --name SimpleNetCoreApp
// Program.cs
namespace SimpleNetCoreApp
    class Program
        static void Main(string[] args)

Creating Dockerfile and .dockerignore

Dockerfile is a text document that contains all the commands a user could call on the command line to assemble an image.

Dockerfile and dockerignore files can be generated with both Visual Studio Code and Visual Studio. Let’s move with Visual Studio Code. Install Docker plugin then select Docker: Add Docker files to Workspace


## Dockerfile
FROM AS base

FROM AS build
COPY ["SimpleNetCoreApp.csproj", "./"]
RUN dotnet restore "./SimpleNetCoreApp.csproj"
COPY . .
WORKDIR "/src/."
RUN dotnet build "SimpleNetCoreApp.csproj" -c Release -o /app/build

FROM build AS publish
RUN dotnet publish "SimpleNetCoreApp.csproj" -c Release -o /app/publish

FROM base AS final
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "SimpleNetCoreApp.dll"]

Building image

Since we have Dockerfile we can build an image.

docker build -t simplenetcoreappimage:v1 .
#Successfully built a0143404f51a
#Successfully tagged simplenetcoreappimage:v1

Let’s list the images we have

docker image list
#REPOSITORY                              TAG                 IMAGE ID            CREATED             SIZE
#simplenetcoreappimage                   v1                  a0143404f51a        45 seconds ago      190MB       3.1                 cf60f383a8d1        4 days ago          705MB   3.1                 939432e86f07        4 days ago          190MB

Yup, we have it.

docker run simplenetcoreappimage:v1
#07/19/2020 09:27:29

Pushing to the remote repository

Docker has a service called “Docker Hub” which allows us to share images with people. Free plan allows us to share 1 private and unlimited public repositories.

After we create a free account, we will need to login that account through the terminal.

docker login
#Login with your Docker ID to push and pull images from Docker Hub. If you don't have a Docker ID, head over to to create one.
Username: bariskisir
Password: #####
#Login Succeeded

Naming in docker images controls the remote repository we’re pushing to. An image id can be targeted from more than one name.

In order to use our repository, let’s set our username as prefix to our image.

docker tag simplenetcoreappimage:v1 bariskisir/simplenetcoreappimage:v1
docker image list
#REPOSITORY                              TAG                 IMAGE ID            CREATED             SIZE
#bariskisir/simplenetcoreappimage        v1                  a0143404f51a        10 minutes ago      190MB
#simplenetcoreappimage                   v1                  a0143404f51a        10 minutes ago      190MB       3.1                 cf60f383a8d1        4 days ago          705MB   3.1                 939432e86f07        4 days ago          190MB

Now, we have two names are targeting to same image id and we’re good to go.

docker push bariskisir/simplenetcoreappimage:v1
#The push refers to repository []

It is pushed and can be reached at

Pulling images from the remote repository

docker pull bariskisir/simplenetcoreappimage:v1
docker run bariskisir/simplenetcoreappimage:v1
#07/19/2020 09:46:50

That’s it.