🐳 Docker: The (Almost) Ultimate Guide for the TLDR Developer

🐳 Docker: The (Almost) Ultimate Guide for the TLDR Developer

Stop Fighting with "It Works on My Machine" Forever

By Adit Ishraq, Full Stack Developer

Docker changed everything. No more "it runs fine on my computer" problems. No more "I'll read this later" bookmark collections. This is Docker TLDR—digestible chunks for busy brains.

Ready? Let's dive in.

🎯 What is Docker? (In 30 Seconds)

Think of Docker like a lunch box. Your app goes inside with everything it needs to run. Same lunch box works at home, at work, or anywhere else.

That's Docker. Your app + its dependencies = one portable container.

The Big Win: Write once, run anywhere. Really.

🏗️ Docker Basics: The Building Blocks

Docker Images: Your App's Blueprint

Images are like recipes. They tell Docker how to make your app.

docker pull mcr.microsoft.com/dotnet/aspnet:8.0        

This gets Microsoft's .NET recipe from the internet.

Docker Containers: Your Running App

Containers are what you get when you follow the recipe. Your app is now running inside its own little world.

docker run -p 8080:8080 myapp        

Now your app runs on port 8080. Simple.

Dockerfile: Writing Your Own Recipe

Want to make your own image? Write a Dockerfile:

FROM mcr.microsoft.com/dotnet/aspnet:8.0
COPY . /app
WORKDIR /app
CMD ["dotnet", "MyApp.dll"]        

Four lines. That's it.

🔍 Docker Inspect: See What's Really Happening

Sometimes things break. Docker inspect shows you everything:

docker inspect mycontainer        

What you get:

  • How much memory it's using
  • What ports are open
  • Where files are stored
  • What went wrong

It's like looking under the hood of your car.

🏷️ Docker Tags: Version Control for Images

Tags are like version numbers for your apps:

docker tag myapp:latest myapp:v1.0        

Smart tagging:

  • v1.0, v1.1, v2.0 (version numbers)
  • dev, test, prod (environments)
  • monday, tuesday (if you're feeling creative)

Pro tip: Never use "latest" in production. Always use specific versions.

📤📥 Docker Push and Pull: Sharing is Caring

Pull = Download someone else's image Push = Upload your image for others

docker pull nginx  # Get nginx web server
docker push myregistry.com/myapp:v1.0  # Share your app        

It's like GitHub, but for Docker images.

🏃♂️ Docker Exec: Jump Inside Running Containers

Need to poke around inside a running container?

docker exec -it mycontainer /bin/bash        

Now you're inside. Look around, run commands, debug problems.

Warning: Be careful in there. You can break things.

📋 Docker Logs: See What Your App is Saying

Your app is trying to tell you something. Listen:

docker logs mycontainer
docker logs -f mycontainer  # Follow along live        

Pro tip: Make your .NET apps log properly. You'll thank yourself later.

🔧 Dockerfile Deep Dive: All the Important Bits

The Essential Commands

FROM - Start with this base image COPY - Put these files in the container RUN - Execute this command while building CMD - Run this when the container starts EXPOSE - This port will be used WORKDIR - Work from this folder

COPY vs ADD: What's the Difference?

COPY - Simple file copying

COPY appsettings.json /app/        

ADD - Fancier, but usually overkill

ADD https://example.com/file.json /app/  # Downloads from internet        

Rule: Use COPY unless you need ADD's magic tricks.

RUN: Building Your Environment

Wrong way (creates big images):

RUN apt-get update
RUN apt-get install curl
RUN apt-get clean        

Right way (smaller images):

RUN apt-get update && apt-get install curl && apt-get clean        

One command = one layer = smaller image.

ARG vs ENV: Build Time vs Run Time

ARG - Only available while building:

ARG VERSION=8.0
FROM mcr.microsoft.com/dotnet/aspnet:${VERSION}        

ENV - Available always:

ENV ASPNETCORE_ENVIRONMENT=Production        

USER: Don't Run as Root

RUN adduser --disabled-password appuser
USER appuser        

Running as root = security risk. Don't do it.

CMD vs ENTRYPOINT: Starting Your App

CMD - Default command (can be changed):

CMD ["dotnet", "MyApp.dll"]        

ENTRYPOINT - Always runs (can't be changed):

ENTRYPOINT ["dotnet", "MyApp.dll"]        

Best of both:

ENTRYPOINT ["dotnet"]
CMD ["MyApp.dll"]        

Multi-Stage Builds: Build Small, Run Fast

# Build stage (has all the tools)
FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build
COPY . /app
WORKDIR /app
RUN dotnet publish -o out

# Runtime stage (just what you need)
FROM mcr.microsoft.com/dotnet/aspnet:8.0
COPY --from=build /app/out /app
WORKDIR /app
CMD ["dotnet", "MyApp.dll"]        

Why this rocks: Production image is tiny. No build tools, no extra junk.

Multi-Platform Builds: Works Everywhere

docker buildx build --platform linux/amd64,linux/arm64 -t myapp .        

One command. Works on Intel, works on ARM. Magic.

🌐 Docker Networking: Containers Talking to Each Other

Containers need to chat. Docker networks make it happen.

docker network create mynetwork
docker run --network mynetwork --name app1 myapp
docker run --network mynetwork --name app2 myapp        

Now app1 and app2 can talk to each other by name.

Network types:

  • Bridge - Default, works fine
  • Host - Container uses your computer's network directly
  • None - No network (isolated)

💾 Docker Volumes: Keep Your Data Safe

Containers die. Data shouldn't.

Volume Types Made Simple

Named volumes (Docker manages them):

docker volume create mydata
docker run -v mydata:/app/data myapp        

Bind mounts (you manage them):

docker run -v /my/folder:/app/data myapp        

Temp storage (disappears when container stops):

docker run --tmpfs /tmp myapp        

Real Examples

Database that survives restarts:

database:
  image: mcr.microsoft.com/mssql/server:2022-latest
  volumes:
    - sql_data:/var/opt/mssql        

App logs you can read:

web:
  image: myapp
  volumes:
    - ./logs:/app/logs        

🎼 Docker Compose: Multiple Containers Made Easy

Real apps need multiple pieces. Database, web server, cache, etc.

Docker Compose handles this:

services:
  web:
    build: .
    ports:
      - "8080:8080"
    depends_on:
      - database
    environment:
      - ConnectionStrings__DefaultConnection=Server=database;Database=MyApp;User Id=sa;Password=Password123;

  database:
    image: mcr.microsoft.com/mssql/server:2022-latest
    environment:
      SA_PASSWORD: Password123
      ACCEPT_EULA: Y
    volumes:
      - sql_data:/var/opt/mssql

volumes:
  sql_data:        

What this does:

  • Starts a web app
  • Starts SQL Server
  • Connects them together
  • Saves database data

Run it:

docker-compose up        

Everything starts. Everything talks. Everything works.

Docker Compose Features

Health checks - Make sure services actually work:

healthcheck:
  test: ["CMD", "curl", "-f", "http://localhost:8080/health"]
  interval: 30s        

Restart policies - Automatically restart crashed containers:

restart: unless-stopped        

Secrets - Handle passwords safely:

secrets:
  - db_password        

Dependencies - Start things in the right order:

depends_on:
  - database
  - redis        

Multiple services - Scale your app:

docker-compose up --scale web=3        

🔐 Docker Security: Stay Safe

Use small base images:

FROM mcr.microsoft.com/dotnet/aspnet:8.0-alpine        

Alpine = smaller = fewer security holes.

Don't run as root:

USER appuser        

Scan for problems:

docker scout cves myapp:latest        

Keep secrets out of images: Use environment variables or Docker secrets. Never hardcode passwords.

📊 Debugging Docker: When Things Go Wrong

Container won't start?

docker logs mycontainer        

Container using too much memory?

docker stats        

Need to look inside?

docker exec -it mycontainer /bin/bash        

Container configuration messed up?

docker inspect mycontainer        

🚀 Docker Best Practices That Actually Matter

Build images efficiently:

  • Put changing code last
  • Use .dockerignore
  • Combine RUN commands

Tag images properly:

  • Use version numbers
  • Never rely on "latest" in production
  • Tag for different environments

Handle data correctly:

  • Use volumes for important data
  • Don't store data in containers
  • Back up your volumes

Security basics:

  • Run as non-root user
  • Use official base images
  • Keep images updated
  • Scan for vulnerabilities

💡 Why Docker Changes Everything

Before Docker:

  • "Works on my machine" problems
  • Complex environment setup
  • Inconsistent deployments
  • Hard to scale

After Docker:

  • Consistent everywhere
  • Easy environment setup
  • Predictable deployments
  • Simple scaling

Docker isn't just a tool. It's a new way of thinking about applications.

The bottom line: Docker makes deployment predictable. Your app works the same way everywhere. That's powerful.

What Docker topic confuses you most? Drop a comment and let's figure it out together!


Insightful Thanks for sharing

Like
Reply

To view or add a comment, sign in

Others also viewed

Explore content categories