Docker 🐳

Docker 🐳

Docker Guide

What is Docker?

Docker is like a magic box for your applications. It packages your app and all its dependencies (code, libraries, tools) into a single, lightweight container. This container runs the same way everywhere—on your laptop, your friend’s computer, or even in the cloud. ☁️

Before Docker

Developing and deploying applications was a nightmare.

  1. Dependency Hell: machine had its own setup, and “It works on my machine” was a common excuse.

  2. Environment Inconsistency: Dev, testing, and production environments were never the same.

  3. Heavy Virtual Machines (VMs): Each environment needed its own VM, slow and resource intense.

  4. Scaling Problems:Running multiple instances of an app required multiple VMs. Scaling horizontally was expensive and time-consuming.


Docker 🐳

  • Then came Docker, and everything changed. Here’s how:

  • Consistency: No more “it works on my machine” problems.

  • Isolation: Each app runs in its own little world, so no dependency conflicts.

  • Portability: Move your app anywhere with ease.

  • Scalability: Run multiple copies of your app effortlessly.


How Docker Works🪄:

The Docker Workflow

  1. Dockerfile: The blueprint for building an image.

  2. Image: An immutable file that contains everything needed to run the app.

  3. Container: A running instance of the image.


Here’s how i containerised my Project: 🛠️

My app has three main parts:

  1. API: The brain that handles requests.

  2. UI: The face that users interact with.

  3. Shared Library: The glue that connects the API and UI.


How I Containerised My App 🐳

Step 1: Create a Dockerfile

# Use the official ASP.NET Core runtime as a base image
FROM mcr.microsoft.com/dotnet/aspnet:8.0 AS base
WORKDIR /app
EXPOSE 80

# Use the SDK image to build the project
FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build
WORKDIR /src
COPY ["API/API.csproj", "API/"]
COPY ["Shared/Shared.csproj", "Shared/"]
RUN dotnet restore "API/API.csproj"
COPY . .
WORKDIR "/src/API"
RUN dotnet build "API.csproj" -c Release -o /app/build

# Publish the project
FROM build AS publish
RUN dotnet publish "API.csproj" -c Release -o /app/publish /p:UseAppHost=false

# Final stage: copy the build output to the runtime image
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "API.dll"]

Step 2: Build the Docker Image

I built the Docker image using this command:

docker build -t my-api -f API/Dockerfile .

Step 3: Run the Container

After building the image, I ran the container:

docker run -d -p 5001:80 --name my-api-container my-api

This maps port 5001 on my machine to port 80 inside the container. Now, my API is live at http://localhost:5001! 🌐


Best Practices

  1. Use Multi-Stage Builds:

    • Keeps the final Docker image small by discarding unnecessary files.
  2. Environment Variables for Configuration:

    • Store sensitive settings (e.g., API keys) in environment variables, not in code.
  3. Docker Compose for Multi-Container Apps:

    • Use Docker Compose to manage multiple containers (e.g., API, UI, database) together.
  4. Keep Images Lightweight:

    • Use small base images (e.g., mcr.microsoft.com/dotnet/aspnet:8.0) to reduce image size.

Docker is a game-changer for developers. It simplifies development, ensures consistency, and makes deployment a breeze. Whether you’re building a small app or a large system, Docker can help you focus on what really matters: creating awesome software.


Next Steps

Ready to dive into Docker? Here are some resources to get started:

Happy containerising! 🐳