DEV Community

Jose Rodriguez Marrero
Jose Rodriguez Marrero

Posted on

Taming Your First Docker Build in 5 Minutes

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
Enter fullscreen mode Exit fullscreen mode

📄 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"]

Enter fullscreen mode Exit fullscreen mode

Tip: Replace MyApp.dll with your project’s DLL name.


🏗 Building the Image

Run:

docker build -t infraforge-dev-hello .
Enter fullscreen mode Exit fullscreen mode
  • -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
Enter fullscreen mode Exit fullscreen mode
curl http://localhost:8080
Enter fullscreen mode Exit fullscreen mode

✨ Quick Wins & Optimizations

  • .dockerignore
bin/
obj/
.vs/
Enter fullscreen mode Exit fullscreen mode
  • 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 .
Enter fullscreen mode Exit fullscreen mode
  • Inspect broken images:
docker run --rm -it infraforge-dev-hello /bin/bash
Enter fullscreen mode Exit fullscreen mode
  • 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:

  1. Created: Container exists, but not running.
docker ps -a
# STATUS: Created
Enter fullscreen mode Exit fullscreen mode
  1. Running: Main process is executing.
docker ps
# STATUS: Up X seconds
Enter fullscreen mode Exit fullscreen mode
  1. Paused: Process temporarily frozen.
docker pause <container_id>
docker unpause <container_id>
Enter fullscreen mode Exit fullscreen mode
  1. Exited: Process finished or crashed.
docker ps -a
# STATUS: Exited (1) X seconds ago
Enter fullscreen mode Exit fullscreen mode
  1. Removed: Container deleted.
docker rm <container_id>
Enter fullscreen mode Exit fullscreen mode

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>
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode
  • 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
Enter fullscreen mode Exit fullscreen mode
  • Automate with GitHub Actions: integrate docker build & docker push on every PR.

Additional Resources:

  1. 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/

  2. 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/

  3. 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!

Top comments (0)