A Beginner’s Guide to Learning Docker: Step-by-Step Tutorial
Docker has revolutionized software development by simplifying containerization—a lightweight alternative to traditional virtual machines. Whether you're a developer, DevOps engineer, or just curious about modern deployment practices, learning Docker is a valuable skill.
This guide will take you from Docker basics to advanced concepts, with hands-on examples and best practices.
Table of Contents
- What is Docker?
- Why Use Docker?
- Installing Docker
- Basic Docker Commands
- Working with Dockerfiles
- Managing Data in Docker
- Docker Networking
- Multi-Container Apps with Docker Compose
- Advanced Docker Concepts
- Practice Projects
- Conclusion
1. What is Docker?
Docker is an open-source platform that allows you to package applications and their dependencies into containers. These containers are lightweight, portable, and run consistently across different environments.
Key Docker Components
- Images: Blueprint for containers (e.g.,
nginx,ubuntu). - Containers: Running instances of images.
- Docker Hub: Public registry for Docker images.
- Dockerfile: Script to build custom images.
2. Why Use Docker?
✅ Consistency: Works the same on any machine.
✅ Isolation: Apps run in separate environments.
✅ Efficiency: Containers share the host OS kernel (unlike VMs).
✅ Scalability: Easily deploy multiple instances.
3. Installing Docker
Windows/macOS
- Download Docker Desktop.
- Follow the installation wizard.
Linux (Ubuntu Example)
sudo apt update && sudo apt install docker.io
sudo systemctl start docker
sudo systemctl enable docker
Verify Installation
docker --version
docker run hello-world # Test a sample container
4. Basic Docker Commands
| Command | Description |
|---|---|
docker pull <image> |
Download an image (e.g., docker pull nginx) |
docker images |
List downloaded images |
docker run <image> |
Start a container |
docker ps |
List running containers |
docker stop <container> |
Stop a container |
docker rm <container> |
Remove a container |
docker rmi <image> |
Remove an image |
Example: Running an Ubuntu Container
docker run -it ubuntu bash # Interactive shell
exit # Leave the container
5. Working with Dockerfiles
A Dockerfile defines how to build a custom image.
Example: Dockerfile for a Python App
# Use an official Python image
FROM python:3.9-slim
# Set working directory
WORKDIR /app
# Copy requirements and install
COPY requirements.txt .
RUN pip install -r requirements.txt
# Copy the app code
COPY . .
# Run the app
CMD ["python", "app.py"]
Build and Run
docker build -t my-python-app . # Build the image
docker run -p 5000:5000 my-python-app # Run with port mapping
6. Managing Data in Docker
Volumes (Persistent Storage)
docker volume create my_volume
docker run -v my_volume:/data nginx
Bind Mounts (For Development)
docker run -v $(pwd):/app nginx # Sync host ↔ container
7. Docker Networking
Expose a Port
docker run -p 8080:80 nginx # Access at http://localhost:8080
Create a Custom Network
docker network create my_network
docker run --network my_network my-app
8. Multi-Container Apps with Docker Compose
Define services in docker-compose.yml:
version: '3'
services:
web:
build: .
ports: ["5000:5000"]
redis:
image: "redis"
Run the Stack
docker-compose up # Start all services
docker-compose down # Stop and remove
9. Advanced Docker Concepts
- Multi-Stage Builds: Reduce image size.
- Docker Swarm/Kubernetes: Orchestration for scaling.
- Security Best Practices:
- Avoid running containers as
root. - Scan images for vulnerabilities.
- Avoid running containers as
10. Practice Projects
- Containerize a Python/Node.js app.
- Set up WordPress with MySQL using Docker.
- Deploy a microservice with Docker Compose.
11. Conclusion
Docker simplifies development and deployment by providing a consistent environment. Start with basic commands, move to Dockerfiles, then explore Compose and orchestration tools.
Next Steps
- Explore Docker’s official docs.
- Try Docker Labs for hands-on practice.
Happy containerizing! 🐳
Projects to explore next
Source-code projects from the KritiMyantra catalogue. Check each project's details before buying.
Flask Authentication System (Free Version) - Kritim Yantra
🛠 Features✔ Google & GitHub Authentication✔ User Registration & Login✔ Profile/Dashboard Access✔ Secure & Mi...
Flask Blog CMS with Admin Dashboard — Python Source Code
A Flask-based blogging website with a built-in admin dashboard. Manage articles, images, categories, tags and featured p...
Laravel 12 + ReactJS Starter Kit: Add Multi-Language Support (Arabic, English, Spanish) with Blog CRUD
🚀 Installation & Setup Download & Extract Get the ZIP from kritimyantra.com and extract it. Includes preconfigu...
Comments
No comments yet. Be the first to comment.
Sign in to join the discussion.