Docker Beginners Guide: From Zero to Container - Complete beginner's guide to Docker containers, covering installation, basic commands, and your firs...
Tutorial

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.

TechDevDex Team
12/15/2024
20 min
#Docker#Containers#DevOps#Beginner#Containerization#Docker Tutorial#DevOps Tools#Software Development#Deployment#Microservices

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

  1. Download Docker Desktop

  2. System Requirements

    • Windows 10 64-bit: Pro, Enterprise, or Education (Build 15063 or later)
    • WSL 2 feature enabled
    • Virtualization enabled in BIOS
  3. Installation Steps

    bash
    # Run the installer as administrator
    # Follow the installation wizard
    # Restart your computer when prompted
  4. Verify Installation

    bash
    # Check Docker version
    docker --version
    
    # Check Docker Compose version
    docker-compose --version
    
    # Run hello-world container
    docker run hello-world

macOS Installation

  1. Download Docker Desktop

  2. Installation Steps

    • Download the .dmg file
    • Drag Docker to Applications folder
    • Launch Docker Desktop
    • Follow the setup wizard
  3. Verify Installation

    bash
    # Check Docker version
    docker --version
    
    # Run hello-world container
    docker run hello-world

Linux Installation (Ubuntu/Debian)

bash
# 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

bash
# 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:

python
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:

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:

text
Flask==2.0.1

Build and run your container:

bash
# 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

bash
# 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

bash
# 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

bash
# 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:

yaml
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:

bash
# 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:

text
node_modules
npm-debug.log
.git
.gitignore
README.md
.env
.nyc_output
coverage

2. Optimize Dockerfile

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

bash
# 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)

bash
# 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

bash
# 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

bash
# 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:

  1. Learn Docker Compose for multi-container applications
  2. Explore Docker Hub for pre-built images
  3. Set up CI/CD pipelines with Docker
  4. Learn container orchestration with Kubernetes
  5. Implement monitoring and logging for containers

Additional Resources

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! 🐳