Docker-for-beginners-step-by-step

Docker for Beginners: Deploy Your First .NET Hello World App

Docker for Beginners: Let’s Understand Docker:

If you’re just starting your journey with containers, Docker for Beginners article is the perfect place to start. Docker has become one of the most popular tools in modern software development because it makes building, running, and deploying applications much easier and more consistent. Instead of worrying about different environments (“it works on my machine but not on the server”, LOL), Docker allows developers to package an app with everything it needs (code, runtime, and libraries) into a single unit called a container.

Think of containers like shipping containers in logistics. No matter what’s inside; a car, electronics, or clothes, the container looks and works the same way. Ships, trucks, and cranes know exactly how to handle it. Similarly, Docker containers standardize applications, so they can run anywhere, be it your laptop, a testing server, or the cloud without compatibility issues.

In this article, we’ll walk through deploying a simple .NET Hello World application inside a Docker container. By the end, you’ll not only understand the basics of Docker but also have a hands-on example that shows how containers simplify app deployment for students, IT professionals, and developers alike.

Setting Up Prerequisites for Docker

Before we start deploying our .NET Hello World app inside Docker, let’s make sure we have the right tools installed.

You’ll need the .NET SDK (Software Development Kit) to build and run your .NET applications. You can skip if you have already installed .NET SDK.

Step 1: Install .NET SDK

  • Download it from the official Microsoft site: Download .NET
  • Verify installation by running:
dotnet --version

If you see a version number, you’re good to go

Step 2: Install Docker Desktop

It will create and run the containers.

  • Download Docker Desktop from: Get Docker
  • Once installed, open a terminal/command prompt and check:
docker --version

If it shows a version, Docker is ready to use.

Now, it’s time to create your Helllo World .NET Application

Step 3: Create a .NET Hello World App

Open VS Code, go the terminal and run the following command

dotnet new console -o HelloWorldApp
cd HelloWorldApp

Open the project folder in your VS Code. Inside Program.cs, you’ll find something like this:

using System;

class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine("Hello World from .NET!");
    }
}

Run the application locally before moving to Docker, run the command in terminal

dotnet run

You will see the output string in the Console

Hello World from .NET!

Great! You now have a simple .NET application running locally. Next, we’ll move to write the Dockerfile.

Step 4: Write a Dockerfile to containerize this app

Now that we have a working .NET Hello World App, let’s package it into a Docker image so it can run anywhere.

Inside your HelloWorldApp folder, create a new file named Dockerfile without any extention and add the following code

# Use the official .NET SDK image to build the app
FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build
WORKDIR /app

# Copy project files and restore dependencies
COPY *.csproj ./
RUN dotnet restore

# Copy the rest of the source code and build the app
COPY . ./
RUN dotnet publish -c Release -o out

# Use the smaller runtime image for final container
FROM mcr.microsoft.com/dotnet/runtime:8.0
WORKDIR /app
COPY --from=build /app/out .

# Set the entry point
ENTRYPOINT ["dotnet", "HelloWorldApp.dll"]

Wondering what this code chunk does? Let’s see

  • FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build : uses the .NET SDK to build the app.
  • RUN dotnet restore : restores required dependencies.
  • RUN dotnet publish : builds and packages the app into a folder named out.
  • FROM mcr.microsoft.com/dotnet/runtime:8.0 : uses a lightweight runtime image (no SDK).
  • ENTRYPOINT [“dotnet”, “HelloWorldApp.dll”] : tells Docker to run your app when the container starts.

You now have a Dockerfile ready to build your app image. It’s suggested to verify the file structure now and it should look like this,

HelloWorldApp/
 ├── HelloWorldApp.csproj
 ├── Program.cs
 ├── Dockerfile

If everything is fine, we will proceed to build the Docker image to run it as a container

Open a terminal inside your HelloWorldApp folder and run:

docker build -t helloworldapp

By this command, Docker will download the required base images and build your .NET app into a container image.

Once the build is successful, run the container with:

docker run --rm helloworldapp

You should see the output in your terminal and this time the app is being run from Docker.

Conclusion:

Getting started with Docker for beginners doesn’t have to be complicated. By deploying a simple .NET Hello World app inside a Docker container, you’ve just learned the basics of how Docker works : building an image, running a container, and seeing your code execute in an isolated environment. Let me know if you have any query/suggestions.

Thanks for reading.

Leave a Comment

Your email address will not be published. Required fields are marked *