Docker Tutorial for Beginners (Step by Step Guide)
What is Docker?
Docker is a platform for developing, shipping, and running applications in containers.
What is Container - A lightweight, standalone package that includes everything an app needs to run (code, libraries, runtime, OS-level dependencies).
Key benefits
- Consistent environment across machines
- Isolation from other apps
- Easy deployment in cloud services
Real-world analogy:
Think of Docker as packing your app into a “self-sufficient box”. No matter where you take the box, the app works the same.
Why Use Docker?
- Consistency - Works the same on all environments (dev, test, production).
- Portability - Runs on any system with Docker installed.
- Isolation - Avoids conflicts with other apps or libraries.
- Simplified deployment - Works perfectly with CI/CD pipelines.
- Scalability - Easy to scale apps in containers using tools like Kubernetes.
User Case
You develop an ASP.NET API on your laptop. Without Docker, it may fail on the server because the .NET version differs. With Docker, your app runs exactly the same anywhere.
Installing Docker
Windows / macOS
- Download Docker Desktop: https://www.docker.com/products/docker-desktop
- Install and start the application.
Linux (Ubuntu Example)
sudo apt update
sudo apt install docker.io
sudo systemctl start docker
sudo systemctl enable docker
sudo usermod -aG docker $USER
Check installation:
docker --version
docker run hello-world
Basic Docker Concepts
| Concept | Description |
|---|---|
| Image | Blueprint of your app (like a template). |
| Container | Running instance of an image. |
| Dockerfile | Instructions to build an image. |
| Docker Hub | Online repository for sharing images. |
| Volume | Persistent storage for containers. |
| Network | Connect multiple containers. |
Create Your First Dockerized ASP.NET App
Step 1: Create a simple ASP.NET Core Web API
dotnet new webapi -n BookStoreApp
cd BookStoreApp
User Case:
A REST API for a book store that interacts with a MySQL database.
Step 2 - Add MySQL Support
dotnet add package Pomelo.EntityFrameworkCore.MySql
dotnet add package Microsoft.EntityFrameworkCore.Design
Update appsettings.json:
{
"ConnectionStrings": {
"DefaultConnection": "Server=db;Database=BookStoreDB;User=root;Password=1234;"
},
"AllowedHosts": "*"
}
Step 3 - Create Dockerfile
Dockerfile defines how to build your Docker image:
# Build stage
FROM mcr.microsoft.com/dotnet/sdk:7.0 AS build
WORKDIR /app
COPY *.csproj .
RUN dotnet restore
COPY . .
RUN dotnet publish -c Release -o out
# Runtime stage
FROM mcr.microsoft.com/dotnet/aspnet:7.0
WORKDIR /app
COPY --from=build /app/out .
EXPOSE 80
ENTRYPOINT ["dotnet", "BookStoreApp.dll"]
Explanation:
- Multi-stage build - Reduces final image size.
- Build stage - Compiles the app.
- Runtime stage - Runs the compiled app.
User Case
You don’t need .NET SDK on the server—only the runtime to run the app.
Step 4 - Run MySQL Container
docker run -d \
--name bookstore-db \
-e MYSQL_ROOT_PASSWORD=1234 \
-e MYSQL_DATABASE=BookStoreDB \
-p 3306:3306 \
mysql:8
Explanation
-d→ run in background--name→ container name-e→ environment variables-p→ map ports
User Case
Your app can now connect to MySQL at host=db;port=3306.
Step 5 - Build & Run the App in Docker
docker build -t bookstoreapp .
docker run -d --name bookstoreapp --link bookstore-db:db -p 5000:80 bookstoreapp
--link bookstore-db:db→ connects app container to MySQL container- App runs at
http://localhost:5000
Step 6 - Docker Compose (Optional, Recommended)
docker-compose.yml
version: '3.8'
services:
app:
build: .
ports:
- "5000:80"
environment:
- ConnectionStrings__DefaultConnection=server=db;database=BookStoreDB;user=root;password=1234;
depends_on:
- db
db:
image: mysql:8
environment:
MYSQL_ROOT_PASSWORD: 1234
MYSQL_DATABASE: BookStoreDB
ports:
- "3306:3306"
Run everything:
docker-compose up --build
User Case
With one command, you run both API and MySQL, ready for testing locally.
Push Docker Image to Docker Hub
- Login
docker login
- Tag and push
docker tag bookstoreapp yourusername/bookstoreapp:latest
docker push yourusername/bookstoreapp:latest
User Case
Now your image is available in the cloud. Anyone (or Railway) can pull and run it.
Deploy Docker App to Railway
- Create a Railway project.
- Add MySQL plugin.
- Deploy via CLI:
railway login
railway up
User Case:
Your Dockerized ASP.NET app is live online with MySQL running on Railway.
Summary - Why Docker Here?
- Environment consistency: Runs same locally & in Railway.
- Simplified deployment: Only one Docker command.
- Isolation: App doesn’t interfere with other software.
- CI/CD friendly: Works seamlessly with GitHub Actions.
Next Steps for Readers
- Add GitHub Actions for automated build & deployment.
- Learn Docker volumes for persistent MySQL data.
- Explore Kubernetes to scale Docker containers in production.
Conclusion
Docker allows developers to package apps with all dependencies and deploy anywhere without worrying about environment differences. Combined with CI/CD (GitHub Actions) and cloud (Railway), it makes software delivery fast, reliable, and automated.
Comments
Post a Comment