Ever wished your app “just worked” on every machine? Docker gives you consistent, isolated environments—no more “it works on my machine” drama. In this hands-on guide, you’ll go from zero to a running container in five minutes flat.
🛠 Prerequisites
- Docker Desktop (macOS/Windows) or Docker Engine (Linux) installed
- A simple sample app. We’ll use a minimal ASP.NET Core “Hello World” API, but you can swap in Node.js or static files.
# Check you have Docker:
docker --version
📄 Crafting Your Dockerfile
Create a file named Dockerfile in your project root:
# 1. Base runtime image
FROM mcr.microsoft.com/dotnet/aspnet:7.0 AS base
WORKDIR /app
EXPOSE 80
# 2. Build stage
FROM mcr.microsoft.com/dotnet/sdk:7.0 AS build
WORKDIR /src
COPY *.csproj .
RUN dotnet restore
COPY . .
RUN dotnet publish -c Release -o /app/publish
# 3. Final image
FROM base AS final
WORKDIR /app
COPY --from=build /app/publish .
ENTRYPOINT ["dotnet", "MyApp.dll"]
Tip: Replace MyApp.dll with your project’s DLL name.
🏗 Building the Image
Run:
docker build -t infraforge-dev-hello .
- -t infraforge-dev-hello tags your image
- . tells Docker to use the current folder as the build context
- You’ll see logs of each layer being built and cached
▶️ Running & Testing
Start your container and map port 80 → 8080:
docker run --rm -p 8080:80 infraforge-dev-hello
- --rm cleans up the container when you stop it
- Navigate to http://localhost:8080 or run:
curl http://localhost:8080
✨ Quick Wins & Optimizations
- .dockerignore
bin/
obj/
.vs/
- Layer caching: put infrequently changing instructions (e.g., RUN dotnet restore) early.
- Multi-stage builds: keeps final image slim—our example already uses one.
🐞 Troubleshooting Tips
- Verbose output:
docker build --progress=plain .
- Inspect broken images:
docker run --rm -it infraforge-dev-hello /bin/bash
- Common pitfalls: wrong file paths, missing dependencies, port mismatches.
🐳 Deep Dive: Understanding the Container Lifecycle
Docker containers transition through well-defined states. Knowing these helps you manage, debug, and automate effectively:
- Created: Container exists, but not running.
docker ps -a
# STATUS: Created
- Running: Main process is executing.
docker ps
# STATUS: Up X seconds
- Paused: Process temporarily frozen.
docker pause <container_id>
docker unpause <container_id>
- Exited: Process finished or crashed.
docker ps -a
# STATUS: Exited (1) X seconds ago
- Removed: Container deleted.
docker rm <container_id>
Common Lifecycle Commands
# List all containers (running + stopped)
docker ps -a
# Start a stopped container
docker start <container_id>
# Stop a running container (graceful shutdown)
docker stop <container_id>
# Force-restart
docker restart <container_id>
# Remove
docker rm <container_id>
Why it matters:
- CI/CD automation: detect “Exited” states to trigger alerts or restarts.
- Resource cleanup: proactively remove unused containers to free disk space.
- Debugging: start a crashed container interactively to inspect logs or shell in.
📚 Next Steps
- Scan for vulnerabilities:
docker scan infraforge-dev-hello
- Push to a registry:
docker tag SOURCE_IMAGE TARGET_IMAGE:TAG
# example: docker tag infraforge-dev-hello infraforge-dev/infraforge-dev-hello:v1.0
docker push infraforg-dev/infraforge-dev-hello:v1.0
- Automate with GitHub Actions: integrate docker build & docker push on every PR.
Additional Resources:
Docker Official Docs – Get Started & Dockerfile Best Practices
The definitive guide from Docker: an end-to-end “Get Started” tutorial plus the full Dockerfile reference and best-practices guide to optimize builds.
https://docs.docker.com/get-started/
https://docs.docker.com/engine/reference/builder/Docker Curriculum by Prakhar Srivastav
A free, community-driven, step-by-step walkthrough of Docker fundamentals, from images and containers to multi-stage builds and networking.
https://docker-curriculum.com/Play with Docker
An in-browser Docker playground—spin up real Docker hosts and containers without installing anything. Great for experimenting with Dockerfiles, commands, and networking.
https://labs.play-with-docker.com/
That’s it—five minutes to your first Docker build, plus a clear view of what happens under the hood. Drop a comment if you hit any snags or have tips of your own!
- Jose (@infraforge-dev)
Top comments (0)