🐳 Docker Basics
Package your app so it runs the same everywhere. A complete beginner guide with jokes, install steps, and a real project.
Why did the developer quit his job? Because he didn't get arrays. But then he found Docker and said: "Now my code works on everyone's machine. Even my grandma's!"
1 📖 What is Docker? (Simple Words)
Docker is a tool that puts your app and everything it needs into a container.
A container is like a small lunchbox. Inside the box:
- Your app
- The right version of .NET
- All the settings and libraries
Because everything is inside the box, the app works the same on your laptop, on the test server, and in the cloud.
Imagine you move to a new house. Instead of packing everything separately, you pack one box with everything you need. Wherever you go, the box works the same. Docker is that box for your app.
2 ⚖️ Container vs Virtual Machine
Many people confuse containers with virtual machines. They are very different.
| Feature | Virtual Machine (VM) | Docker Container |
|---|---|---|
| Size | Gigabytes (GB) | Megabytes (MB) |
| Startup time | Minutes | Seconds |
| Operating System | Full OS inside | Shares host OS |
| Resource use | Heavy | Light |
| Speed | Slow 🐢 | Fast 🐇 |
Simple rule: Use Docker for quick and cheap packaging. Use VMs when you need full separation.
A developer says: "It works on my machine!"
A DevOps engineer replies: "Then we'll ship your machine to the customer."
Docker: "Hold my container." 🐳
3 🤔 Why Use Docker?
Works Everywhere
Laptop, server, cloud — same result.No "Works On My Machine"
Goodbye, version problems.Fast
Starts in seconds, not minutes.Small
Less disk, less RAM, more apps per server.Easy to Share
Send a Dockerfile — anyone can run it.Isolated
One crash does not kill the others.4 🔑 Key Docker Words
| Word | Simple Meaning |
|---|---|
| Image | A recipe. Instructions to create a container. |
| Container | A running instance of an image. Like a running app. |
| Dockerfile | A text file with instructions to build an image. |
| Volume | A folder shared between your PC and the container. Data survives. |
| Port | A door. Users reach the app through a port. |
| Registry | A place to store images (like Docker Hub). |
| Docker Hub | A public registry where people share images. |
5 🌍 Real Client Example: The Restaurant Menu
Client: A family restaurant in Hamilton, Ontario. They want an online menu so customers can see dishes and prices.
Problem: The developer built the app on their laptop — works perfectly. But on the server, it crashes. Server has .NET 6, laptop has .NET 8. Same code, different result. The classic "works on my machine" problem.
"Use .NET 8. Copy the app. Build. Run." Now we have a recipe.
Run docker build -t menu-app .
Run docker run -p 8080:80 menu-app — starts in seconds.
- Runs the same on laptop, test server, and cloud.
- No more version problems.
- Menu updates in seconds.
- Deployment: 1 minute (was 1 hour).
6 💻 How to Install Docker (Step by Step)
Follow these steps exactly. Do not skip any.
🪟 Windows
- Go to docker.com/products/docker-desktop.
- Click Download for Windows.
- Run the installer (
Docker Desktop Installer.exe). - During setup, keep "Use WSL 2" checked (recommended).
- Click Ok and wait for install to finish.
- Restart your computer when asked.
- Open Docker Desktop from the Start Menu.
- Accept the terms. Wait for the whale icon 🐳 in the taskbar.
- Done. Docker is running.
🍎 macOS
- Download Docker Desktop for Mac.
- Choose Apple Silicon or Intel based on your Mac.
- Open the
.dmgfile. - Drag the Docker icon into Applications.
- Open Docker from Applications.
- Accept the terms and give permission.
- Wait for the whale icon 🐳 in the menu bar.
🐧 Linux (Ubuntu)
- Open a terminal.
- Run:
sudo apt update - Run:
sudo apt install docker.io -y - Run:
sudo systemctl start docker - Run:
sudo systemctl enable docker - To run docker without sudo:
sudo usermod -aG docker $USER - Log out and log back in.
Open a terminal and run:
docker --versionYou should see something like
Docker version 24.0.0.
If you do, you are ready!
Docker walked into a bar and asked for a drink.
The bartender said: "We don't serve containers here."
Docker replied: "That's fine, I'm isolated anyway." 🐳
7 ✍️ Your First Dockerfile (Line by Line)
A Dockerfile is a text file with instructions. Here is one for a .NET app:
# Use the official .NET SDK image to build the app
FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build
WORKDIR /app
# Copy the project file and restore dependencies
COPY *.csproj ./
RUN dotnet restore
# Copy the rest of the code and build it
COPY . ./
RUN dotnet publish -c Release -o out
# Use a smaller runtime image for the final container
FROM mcr.microsoft.com/dotnet/aspnet:8.0
WORKDIR /app
COPY --from=build /app/out .
# Tell Docker what command to run
ENTRYPOINT ["dotnet", "MyApp.dll"]
📘 What Each Line Does
| Line | What It Does |
|---|---|
FROM ... AS build | Starts from the SDK image. Names this stage "build". |
WORKDIR /app | Sets the working folder inside the container. |
COPY *.csproj ./ | Copies the project file into the container. |
RUN dotnet restore | Downloads NuGet packages. |
COPY . ./ | Copies your source code. |
RUN dotnet publish -c Release -o out | Builds the app in Release mode. |
FROM ...aspnet:8.0 | New, smaller image for runtime. |
COPY --from=build /app/out . | Copies only the built files. |
ENTRYPOINT ["dotnet", "MyApp.dll"] | Command to run when the container starts. |
"build" is big (~1 GB). "runtime" is small (~200 MB). This is a multi-stage build — smaller, faster, safer.
8 ⌨️ Basic Docker Commands
| Command | What It Does |
|---|---|
docker build -t myapp . | Builds an image named "myapp". |
docker run -p 8080:80 myapp | Runs and maps port 8080 → 80. |
docker ps | Shows running containers. |
docker ps -a | Shows all containers. |
docker stop <id> | Stops a container. |
docker rm <id> | Deletes a stopped container. |
docker images | Lists all images. |
docker rmi <id> | Deletes an image. |
docker logs <id> | Shows logs (great for debugging). |
docker exec -it <id> bash | Opens a terminal inside a container. |
9 🛠️ Hands-On Practice (Do This Now)
Run docker --version. Expect Docker version 24.0.0 or similar.
Run docker run hello-world. Docker downloads a tiny image and prints a message.
Run docker run -d -p 8080:80 nginx. Open http://localhost:8080 in a browser. Stop it with docker stop <id>.
Create a Dockerfile (from Section 7). Put your .NET project next to it. Run docker build -t my-first-app ..
Run docker run -d -p 8080:80 my-first-app. Open the browser. Your app is live!
How many DevOps engineers does it take to change a light bulb?
None. They automate the light bulb replacement and add a monitoring dashboard. 💡📊
10 🔧 Common Problems and Fixes
| Problem | Cause | Fix |
|---|---|---|
| Port is in use | Another app uses it. | Change the port: -p 8081:80 |
| Container stops instantly | App crashed on start. | Check logs: docker logs <id> |
| "docker: command not found" | Not installed. | Install Docker Desktop. |
| Build is slow | Rebuilding too much. | Add a .dockerignore. |
| Data disappears | Container data is temporary. | Use a volume: -v |
11 🏗️ Build a Real Docker Project (End-to-End)
Build, containerize, run, and share a real ASP.NET Core app. Follow this exactly and you will have a working Docker project.
🎯 What You Will Build
A simple "Hello World" ASP.NET Core web app, packaged in a Docker container, running on your computer.
📁 Project Folder
Create a folder called HelloDocker anywhere on your PC.
🛠️ Step 1: Create the App
Open a terminal in that folder and run:
dotnet new web -n HelloDocker
cd HelloDocker
🛠️ Step 2: Change the Program.cs
Open Program.cs. Replace everything with:
var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();
app.MapGet("/", () => "Hello from Docker! 🐳 My first container is alive!");
app.Run();
🛠️ Step 3: Add a Dockerfile
In the HelloDocker folder, create a new file called Dockerfile (no extension). Paste:
FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build
WORKDIR /app
COPY *.csproj ./
RUN dotnet restore
COPY . ./
RUN dotnet publish -c Release -o out
FROM mcr.microsoft.com/dotnet/aspnet:8.0
WORKDIR /app
COPY --from=build /app/out .
ENTRYPOINT ["dotnet", "HelloDocker.dll"]
🛠️ Step 4: Add .dockerignore
Create a file called .dockerignore with:
bin/
obj/
*.user
*.suo
This keeps the image small and build fast.
🛠️ Step 5: Build the Image
In the same folder, run:
docker build -t hello-docker .
Wait for it to finish. The first time may take 1–2 minutes.
🛠️ Step 6: Run the Container
docker run -d -p 8080:80 --name my-hello-app hello-docker
This runs the container in the background.
🛠️ Step 7: Test It
Open your browser and go to:
http://localhost:8080
You should see: "Hello from Docker! 🐳 My first container is alive!"
🛠️ Step 8: Manage the Container
docker ps # see it running
docker logs my-hello-app # see logs
docker stop my-hello-app # stop it
docker rm my-hello-app # delete it
🛠️ Step 9: Share It (Bonus)
To share your image with others, push it to Docker Hub:
docker login
docker tag hello-docker YOUR_DOCKERHUB_USERNAME/hello-docker
docker push YOUR_DOCKERHUB_USERNAME/hello-docker
Anyone in the world can now run:
docker run -p 8080:80 YOUR_DOCKERHUB_USERNAME/hello-docker
You just built a real Docker project. You:
- Wrote a Dockerfile
- Built a Docker image
- Ran the container
- Tested it in the browser
- Shared it with the world
12 ✅ What You Learned
- Docker puts your app into a container.
- Containers run the same everywhere.
- Containers are smaller and faster than VMs.
- A Dockerfile tells Docker how to build an image.
- You use
docker build,docker run,docker ps. - Multi-stage builds keep your image small.
- Volumes keep data safe.
- You built a real Docker project from scratch!
13 🧠 Test Yourself
- What is a Docker container?
- How is a container different from a VM?
- What does a Dockerfile do?
- Difference between an image and a container?
- What does
docker psdo? - Why use volumes?
- A small box with your app and everything it needs.
- Containers are smaller, faster, share the host OS. VMs are bigger, slower, own OS.
- A text file with instructions to build an image.
- Image = recipe (static). Container = running app (dynamic).
- Lists running containers.
- Volumes keep data safe after the container stops.
🚀 What's Next?
- CI/CD Pipelines — Automate build and deploy.
- Kubernetes — Manage many containers at once.
- Docker Compose — Run an app + database together.