Deploy .NET Core APIs Locally with Docker | Step-by-Step Guide

Deploy .NET Core APIs Locally with Docker | Step-by-Step Guide

Deploying .NET Core APIs using Docker allows developers to create lightweight, portable, and scalable applications. Docker provides a consistent runtime environment, ensuring that your .NET API runs the same way across different machines. This article will guide you through installing Docker, writing a Dockerfile, and running a containerized .NET Core API locally.

Prerequisites

Before we start, ensure you have the following installed on your system:

  • .NET SDK (Latest Version) - Download Here
  • Docker Desktop - Download Here
  • Docker Compose (bundled with Docker Desktop)
  • Basic knowledge of .NET Core and Docker

Step 1: Install Docker and Docker Compose

Docker is a platform that allows you to develop, ship, and run applications inside containers. Docker Compose is a tool used to define and manage multi-container applications.

Installing Docker

  1. Windows and Mac:

    • Download Docker Desktop from the official website and install it.
    • Ensure virtualization is enabled in your BIOS settings.
    • Restart your machine if required.
  2. Linux:

    • Use the following commands to install Docker:
      sudo apt update
      sudo apt install docker.io -y
      sudo systemctl start docker
      sudo systemctl enable docker
      
    • Verify installation:
      docker --version
      

Installing Docker Compose

Docker Compose is bundled with Docker Desktop, but if you are using Linux, install it manually:

sudo curl -L "https://github.com/docker/compose/releases/latest/download/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
sudo chmod +x /usr/local/bin/docker-compose

Verify installation:

docker-compose --version

Step 2: Write a Dockerfile for .NET Core API

A Dockerfile is a script that contains a set of instructions to build a Docker image.

Create a .NET Core API

If you don’t have a .NET Core API, create one using:

dotnet new webapi -n DockerDemoAPI
cd DockerDemoAPI

Write the Dockerfile

Inside the project folder, create a file named Dockerfile and add the following content:

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

# Copy the project files and restore dependencies
COPY . .
RUN dotnet restore
RUN dotnet publish -c Release -o out

# Use the runtime image
FROM mcr.microsoft.com/dotnet/aspnet:8.0 AS runtime
WORKDIR /app
COPY --from=build /app/out .

# Expose the application on port 5000
EXPOSE 5000

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

Create a .dockerignore File

Create a .dockerignore file in the project directory to exclude unnecessary files:

bin/
obj/
*.md
.vscode/

Step 3: Build and Run the Containerized .NET API Locally

Building the Docker Image

Run the following command inside the project directory:

docker build -t dockerdemoapi .

This command creates a Docker image named dockerdemoapi.

Running the Container

Execute the following command to run the container:

docker run -p 5000:5000 dockerdemoapi

Now, your API is running inside a container and can be accessed at http://localhost:5000.

Running in Detached Mode

If you want to run the container in the background:

docker run -d -p 5000:5000 dockerdemoapi

Step 4: Use Docker Compose for Multi-Container Deployment

Docker Compose allows managing multiple services. Create a docker-compose.yml file in the project directory:

version: '3.8'
services:
  api:
    build: .
    ports:
      - "5000:5000"
    environment:
      - ASPNETCORE_ENVIRONMENT=Development

Start the container using:

docker-compose up -d

To stop the container:

docker-compose down

Conclusion

Deploying .NET Core APIs with Docker simplifies dependency management and ensures consistency across environments. By following this guide, you have successfully containerized a .NET Core API and run it locally using Docker and Docker Compose. With this foundation, you can now explore cloud deployments and orchestration tools like Kubernetes.

Sandip Mhaske

I’m a software developer exploring the depths of .NET, AWS, Angular, React, and digital entrepreneurship. Here, I decode complex problems, share insightful solutions, and navigate the evolving landscape of tech and finance.

Post a Comment

Previous Post Next Post