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.
Dependency Hell: machine had its own setup, and “It works on my machine” was a common excuse.
Environment Inconsistency: Dev, testing, and production environments were never the same.
Heavy Virtual Machines (VMs): Each environment needed its own VM, slow and resource intense.
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
Dockerfile: The blueprint for building an image.
Image: An immutable file that contains everything needed to run the app.
Container: A running instance of the image.
Here’s how i containerised my Project: 🛠️
My app has three main parts:
API: The brain that handles requests.
UI: The face that users interact with.
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
Use Multi-Stage Builds:
- Keeps the final Docker image small by discarding unnecessary files.
Environment Variables for Configuration:
- Store sensitive settings (e.g., API keys) in environment variables, not in code.
Docker Compose for Multi-Container Apps:
- Use Docker Compose to manage multiple containers (e.g., API, UI, database) together.
Keep Images Lightweight:
- Use small base images (e.g.,
mcr.microsoft.com/dotnet/aspnet:8.0
) to reduce image size.
- Use small base images (e.g.,
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! 🐳