Monday, 22 September 2025

Docker

🟢 1️⃣ What is Docker?

  • Docker is a tool to package applications with all dependencies into a container.

  • Container = isolated environment that runs your app anywhere (Windows, Linux, cloud).

  • Benefit: “It works on my machine” problem is solved.

Analogy: Docker is like a shipping container for your app. It includes your app, .NET runtime, libraries, and everything needed.


🟢 2️⃣ Why Use Docker with .NET Core?

  1. Cross-platform: Run .NET Core apps on Linux, Windows, or cloud.

  2. Easy deployment: Package app + dependencies.

  3. Isolation: No conflicts between apps.

  4. Microservices: Each service can run in its own container.


🟢 3️⃣ Steps to Dockerize a .NET Core App

Step 1: Create a .NET Core App

dotnet new webapi -n MyWebApi cd MyWebApi dotnet build dotnet run
  • Your API runs locally on http://localhost:5000.


Step 2: Create a Dockerfile

  • In the root folder, create a file named Dockerfile (no extension):

# 1. Base image with .NET runtime FROM mcr.microsoft.com/dotnet/aspnet:6.0 AS base WORKDIR /app EXPOSE 80 # 2. Build image with SDK FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build WORKDIR /src COPY ["MyWebApi.csproj", "./"] RUN dotnet restore "./MyWebApi.csproj" COPY . . RUN dotnet publish "MyWebApi.csproj" -c Release -o /app/publish # 3. Final image FROM base AS final WORKDIR /app COPY --from=build /app/publish . ENTRYPOINT ["dotnet", "MyWebApi.dll"]

Explanation:

  • Stage 1 (base): Runtime image to run the app.

  • Stage 2 (build): SDK image to build and publish the app.

  • Stage 3 (final): Copy published files to runtime image and run.


Step 3: Build Docker Image

docker build -t mywebapi:1.0 .
  • mywebapi → image name, 1.0 → version tag.

  • Docker reads the Dockerfile and creates the image.


Step 4: Run the Container

docker run -d -p 8080:80 --name mywebapi_container mywebapi:1.0
  • -d → run in background

  • -p 8080:80 → map container port 80 to local port 8080

  • --name → container name

Now open http://localhost:8080 → your API is running inside a Docker container!


Step 5: (Optional) Docker Compose

  • Useful if you have multiple services (like API + database).

  • Create docker-compose.yml:

version: '3.8' services: webapi: image: mywebapi:1.0 ports: - "8080:80" db: image: mcr.microsoft.com/mssql/server:2019-latest environment: SA_PASSWORD: "Your_password123" ACCEPT_EULA: "Y" ports: - "1433:1433"
  • Run with: docker-compose up -d


🟢 4️⃣ Tips

  1. Always use multi-stage Dockerfile → keeps image size small.

  2. Expose only needed ports (EXPOSE 80).

  3. Use environment variables for secrets/configuration.

  4. Use .dockerignore to exclude unnecessary files (like bin, obj).


🟢 5️⃣ Summary

  • Docker packages your .NET Core app + dependencies into a container.

  • Steps:

    1. Create .NET Core app

    2. Write Dockerfile

    3. Build Docker image (docker build)

    4. Run container (docker run)

  • Optional: Use Docker Compose for multi-container apps.

No comments:

Post a Comment