Docker Beginners Guide: From Zero to Container
Complete beginner's guide to Docker containers, covering installation, basic commands, and your first containerized application. Learn Docker fundamentals with practical examples and best practices.
Docker Beginners Guide: From Zero to Container
Docker has revolutionized the way we develop, deploy, and manage applications. This comprehensive beginner's guide will take you from zero knowledge to running your first containerized application.
What is Docker?
Docker is a containerization platform that allows you to package applications and their dependencies into lightweight, portable containers. Think of containers as standardized units of software that include everything needed to run an application.
Key Benefits:
- Consistency: Same environment across development, testing, and production
- Isolation: Applications run in isolated environments
- Portability: Containers run anywhere Docker is installed
- Efficiency: Lightweight compared to virtual machines
Understanding Containers vs Virtual Machines
Virtual Machines
- Each VM includes a full operating system
- Heavy resource usage
- Slower startup times
- Complete isolation
Containers
- Share the host OS kernel
- Lightweight and fast
- Quick startup times
- Process-level isolation
Docker Installation
Windows Installation
Download Docker Desktop
- Visit Docker Desktop for Windows
- Click "Download for Windows"
System Requirements
- Windows 10 64-bit: Pro, Enterprise, or Education (Build 15063 or later)
- WSL 2 feature enabled
- Virtualization enabled in BIOS
Installation Steps
bash# Run the installer as administrator # Follow the installation wizard # Restart your computer when promptedVerify Installation
bash# Check Docker version docker --version # Check Docker Compose version docker-compose --version # Run hello-world container docker run hello-world
macOS Installation
Download Docker Desktop
- Visit Docker Desktop for Mac
- Choose the version for your Mac (Intel or Apple Silicon)
Installation Steps
- Download the
.dmgfile - Drag Docker to Applications folder
- Launch Docker Desktop
- Follow the setup wizard
- Download the
Verify Installation
bash# Check Docker version docker --version # Run hello-world container docker run hello-world
Linux Installation (Ubuntu/Debian)
# Update package index
sudo apt update
# Install required packages
sudo apt install apt-transport-https ca-certificates curl gnupg lsb-release
# Add Docker's official GPG key
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
# Add Docker repository
echo "deb [arch=amd64 signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
# Install Docker Engine
sudo apt update
sudo apt install docker-ce docker-ce-cli containerd.io docker-compose-plugin
# Add your user to docker group
sudo usermod -aG docker $USER
Essential Docker Concepts
Images
Docker images are read-only templates used to create containers. They contain:
- Application code
- Runtime environment
- System libraries
- Configuration files
Containers
Containers are running instances of Docker images. They are:
- Isolated from each other
- Lightweight and portable
- Ephemeral by default
Dockerfile
A Dockerfile is a text file containing instructions to build a Docker image:
- Base image specification
- Dependencies installation
- Application code copying
- Runtime configuration
Your First Docker Container
Let's create and run your first container:
1. Pull and Run a Simple Container
# Pull the latest Ubuntu image
docker pull ubuntu:latest
# Run an interactive Ubuntu container
docker run -it ubuntu:latest /bin/bash
# Inside the container, try:
ls -la
cat /etc/os-release
exit
2. Create a Simple Web Application
Create a file called app.py:
from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello():
return 'Hello, Docker World!'
if __name__ == '__main__':
app.run(host='0.0.0.0', port=5000)
Create a Dockerfile:
FROM python:3.9-slim
WORKDIR /app
COPY requirements.txt .
RUN pip install -r requirements.txt
COPY app.py .
EXPOSE 5000
CMD ["python", "app.py"]
Create requirements.txt:
Flask==2.0.1
Build and run your container:
# Build the image
docker build -t my-flask-app .
# Run the container
docker run -p 5000:5000 my-flask-app
# Visit http://localhost:5000 in your browser
Essential Docker Commands
Image Management
# List all images
docker images
# Pull an image
docker pull nginx:latest
# Remove an image
docker rmi nginx:latest
# Remove all unused images
docker image prune -a
Container Management
# List running containers
docker ps
# List all containers (including stopped)
docker ps -a
# Stop a container
docker stop <container_id>
# Remove a container
docker rm <container_id>
# Remove all stopped containers
docker container prune
Container Operations
# Run a container in detached mode
docker run -d nginx
# Execute commands in a running container
docker exec -it <container_id> /bin/bash
# View container logs
docker logs <container_id>
# Copy files to/from container
docker cp file.txt <container_id>:/path/to/destination
Docker Compose Basics
Docker Compose allows you to define multi-container applications:
Create docker-compose.yml:
version: '3.8'
services:
web:
build: .
ports:
- "5000:5000"
environment:
- FLASK_ENV=development
volumes:
- .:/app
database:
image: postgres:13
environment:
POSTGRES_DB: myapp
POSTGRES_USER: user
POSTGRES_PASSWORD: password
volumes:
- postgres_data:/var/lib/postgresql/data
volumes:
postgres_data:
Run with Docker Compose:
# Start services
docker-compose up
# Start in detached mode
docker-compose up -d
# Stop services
docker-compose down
# View logs
docker-compose logs
Best Practices for Beginners
1. Use .dockerignore
Create a .dockerignore file:
node_modules
npm-debug.log
.git
.gitignore
README.md
.env
.nyc_output
coverage
2. Optimize Dockerfile
FROM python:3.9-slim
# Use non-root user
RUN addgroup -g 1001 -S nodejs
RUN adduser -S nextjs -u 1001
# Copy package files first for better caching
COPY package*.json ./
RUN npm ci --only=production
# Copy source code
COPY . .
# Use non-root user
USER nextjs
# Expose port
EXPOSE 3000
# Use exec form for CMD
CMD ["npm", "start"]
3. Resource Management
# Limit memory usage
docker run -m 512m nginx
# Limit CPU usage
docker run --cpus="1.5" nginx
# Set restart policy
docker run --restart=unless-stopped nginx
Troubleshooting Common Issues
1. Permission Denied (Linux)
# Add user to docker group
sudo usermod -aG docker $USER
# Log out and log back in
# Or use newgrp docker
2. Port Already in Use
# Find process using port
sudo lsof -i :5000
# Kill the process
sudo kill -9 <pid>
# Or use different port
docker run -p 5001:5000 my-app
3. Out of Disk Space
# Clean up unused resources
docker system prune -a
# Remove specific volumes
docker volume prune
# Check disk usage
docker system df
Next Steps
Now that you have Docker installed and running, you're ready to:
- Learn Docker Compose for multi-container applications
- Explore Docker Hub for pre-built images
- Set up CI/CD pipelines with Docker
- Learn container orchestration with Kubernetes
- Implement monitoring and logging for containers
Additional Resources
- Docker Official Documentation
- Docker Hub
- Docker Compose Documentation
- Best Practices for Writing Dockerfiles
Conclusion
Docker is a powerful tool that simplifies application deployment and management. With this guide, you should be able to install Docker on any platform and start containerizing your applications. Remember to practice with simple examples before moving to complex multi-container setups.
Happy containerizing! 🐳