DEV Community

Cover image for πŸš€ Dockerizing and Setting Up CI/CD for a .NET API with GitLab
YASH MAISURIYA
YASH MAISURIYA

Posted on

2

πŸš€ Dockerizing and Setting Up CI/CD for a .NET API with GitLab

In this guide, we'll walk through the steps to containerize a .NET API using Docker and set up a CI/CD pipeline with GitLab. This setup enables automatic building, testing, and deployment of your application, ensuring a smoother development lifecycle.

βœ… Prerequisites
Before getting started, make sure you have the following:

  • A .NET API project (.NET 8 or compatible)
  • A GitLab account with a repository
  • Docker installed on your local machine

🐳 Dockerizing the .NET API

  • Create a Dockerfile in the root of your project directory. This file defines how to build your Docker image.
# Base image for running the app
FROM mcr.microsoft.com/dotnet/aspnet:8.0 AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443

# Build image
FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build
WORKDIR /src
COPY ["blog.backend.api/blog.backend.api.csproj", "blog.backend.api/"]
RUN dotnet restore "blog.backend.api/blog.backend.api.csproj"
COPY . .
WORKDIR "/src/blog.backend.api"
RUN dotnet build "blog.backend.api.csproj" -c Release -o /app/build

# Publish image
FROM build AS publish
RUN dotnet publish "blog.backend.api.csproj" -c Release -o /app/publish /p:UseAppHost=false

# Final image
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "blog.backend.api.dll"]

Enter fullscreen mode Exit fullscreen mode

βš™οΈ Setting Up the GitLab CI/CD Pipeline

  • Create a .gitlab-ci.yml file at the root of your repository:
image: docker:latest

services:
  - docker:dind

stages:
  - build
  - test
  - deploy

variables:
  DOCKER_IMAGE: registry.gitlab.com/yash-project/blogApplicationAPI
  DOCKER_TAG: latest

build:
  stage: build
  script:
    - docker build -f Dockerfile -t $DOCKER_IMAGE:$DOCKER_TAG .
    - echo "$CI_REGISTRY_PASSWORD" | docker login -u "$CI_REGISTRY_USER" --password-stdin $CI_REGISTRY
    - docker push $DOCKER_IMAGE:$DOCKER_TAG

test:
  stage: test
  image: mcr.microsoft.com/dotnet/sdk:8.0
  script:
    - dotnet test

deploy:
  stage: deploy
  script:
    - echo "Deploying using Docker Compose..."
    - docker-compose up -d
  only:
    - main

Enter fullscreen mode Exit fullscreen mode

This pipeline includes three stages:

  • Build: Builds and pushes the Docker image to GitLab's container registry.
  • Test: Runs your unit/integration tests.
  • Deploy: Spins up the container using Docker Compose.

πŸ“¦ Docker Compose Deployment
Create a docker-compose.yml file:

version: "3.9"

services:
  blog-backend-api:
    image: registry.gitlab.com/yash-project/blogApplicationAPI:latest
    ports:
      - "80:80"

Enter fullscreen mode Exit fullscreen mode
  • This simple setup pulls the latest image and runs your API on port 80.

🎯 Conclusion
By combining Docker with GitLab CI/CD, you now have a robust and automated workflow for building, testing, and deploying your .NET API. This approach streamlines development and ensures consistency across environments.

Heroku

Built for developers, by developers.

Whether you're building a simple prototype or a business-critical product, Heroku's fully-managed platform gives you the simplest path to delivering apps quickly β€” using the tools and languages you already love!

Learn More

Top comments (0)

ACI image

ACI.dev: The Only MCP Server Your AI Agents Need

ACI.dev’s open-source tool-use platform and Unified MCP Server turns 600+ functions into two simple MCP tools on one serverβ€”search and execute. Comes with multi-tenant auth and natural-language permission scopes. 100% open-source under Apache 2.0.

Star our GitHub!

πŸ‘‹ Kindness is contagious

Explore a trove of insights in this engaging article, celebrated within our welcoming DEV Community. Developers from every background are invited to join and enhance our shared wisdom.

A genuine "thank you" can truly uplift someone’s day. Feel free to express your gratitude in the comments below!

On DEV, our collective exchange of knowledge lightens the road ahead and strengthens our community bonds. Found something valuable here? A small thank you to the author can make a big difference.

Okay