Soysal Tan

Docker for Beginners: Complete Guide to Containerization

Tutorial Aug 14, 2025

Docker has revolutionized how we develop, ship, and run applications. This comprehensive guide will take you from Docker novice to confident container user, covering everything you need to know to get started with containerization.

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 that include everything needed to run an application: code, runtime, system tools, libraries, and settings.

Why Use Docker?

  • Consistency: "It works on my machine" becomes a thing of the past
  • Portability: Run anywhere - development, testing, production
  • Efficiency: Lightweight compared to virtual machines
  • Scalability: Easy to scale applications up or down
  • Isolation: Applications run in isolated environments

Docker vs Virtual Machines

Understanding the difference between Docker containers and virtual machines is crucial:

Virtual Machines:

  • Include full operating system
  • Resource-heavy (GBs of storage)
  • Slower startup times
  • Hardware-level virtualization

Docker Containers:

  • Share host OS kernel
  • Lightweight (MBs of storage)
  • Fast startup times (seconds)
  • OS-level virtualization

Installing Docker

Docker Desktop (Recommended for Beginners)

Docker Desktop provides an easy-to-use interface and includes Docker Engine, Docker CLI, and Docker Compose.

Installation Steps:

  1. Visit Docker Desktop
  2. Download for your operating system (Windows, macOS, Linux)
  3. Run the installer and follow the setup wizard
  4. Restart your computer if required
  5. Launch Docker Desktop

Verify Installation

Open your terminal and run:

docker --version
docker run hello-world

If successful, you'll see version information and a welcome message from the hello-world container.

Docker Core Concepts

1. Images

Docker images are read-only templates used to create containers. They contain the application code, runtime, libraries, and dependencies.

2. Containers

Containers are running instances of Docker images. They're lightweight, portable, and isolated from other containers and the host system.

3. Dockerfile

A Dockerfile is a text file containing instructions to build a Docker image. It defines the base image, dependencies, and configuration.

4. Docker Hub

Docker Hub is a cloud-based registry where you can find and share Docker images. It's like GitHub for Docker images.

Essential Docker Commands

Image Management

# Pull an image from Docker Hub
docker pull nginx

# List local images
docker images

# Remove an image
docker rmi nginx

# Build image from Dockerfile
docker build -t myapp .

Container Management

# Run a container
docker run nginx

# Run container in background (detached)
docker run -d nginx

# Run with port mapping
docker run -p 8080:80 nginx

# 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

# Execute command in running container
docker exec -it container_id bash

System Management

# View Docker system information
docker info

# Clean up unused resources
docker system prune

# View container logs
docker logs container_id

Creating Your First Dockerfile

Let's create a simple web application using Node.js and containerize it.

Step 1: Create the Application

Create a new directory and add these files:

package.json:

{
  "name": "docker-tutorial-app",
  "version": "1.0.0",
  "description": "Simple Node.js app for Docker tutorial",
  "main": "server.js",
  "scripts": {
    "start": "node server.js"
  },
  "dependencies": {
    "express": "^4.18.0"
  }
}

server.js:

const express = require('express');
const app = express();
const PORT = process.env.PORT || 3000;

app.get('/', (req, res) => {
  res.send(`
    

Hello from Docker!

This Node.js app is running in a Docker container.

Container ID: ${require('os').hostname()}

`); }); app.listen(PORT, () => { console.log(`Server running on port ${PORT}`); });

Step 2: Create the Dockerfile

Dockerfile:

# Use official Node.js runtime as base image
FROM node:18-alpine

# Set working directory in container
WORKDIR /usr/src/app

# Copy package.json and package-lock.json
COPY package*.json ./

# Install dependencies
RUN npm install

# Copy application code
COPY . .

# Expose port 3000
EXPOSE 3000

# Define command to run the application
CMD ["npm", "start"]

Step 3: Build and Run

# Build the image
docker build -t my-node-app .

# Run the container
docker run -p 3000:3000 my-node-app

Visit http://localhost:3000 to see your containerized application!

Dockerfile Best Practices

1. Use Official Base Images

# Good
FROM node:18-alpine

# Avoid
FROM ubuntu
RUN apt-get update && apt-get install -y nodejs npm

2. Minimize Layers

# Good - Single RUN instruction
RUN apt-get update && \
    apt-get install -y curl && \
    apt-get clean

# Avoid - Multiple RUN instructions
RUN apt-get update
RUN apt-get install -y curl
RUN apt-get clean

3. Use .dockerignore

Create a .dockerignore file to exclude unnecessary files:

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

4. Don't Run as Root

# Create non-root user
RUN addgroup -g 1001 -S nodejs
RUN adduser -S nextjs -u 1001

# Switch to non-root user
USER nextjs

Working with Volumes

Volumes allow you to persist data and share files between containers and the host system.

Types of Volumes

1. Bind Mounts

# Mount host directory to container
docker run -v /host/path:/container/path nginx

2. Named Volumes

# Create named volume
docker volume create myvolume

# Use named volume
docker run -v myvolume:/data nginx

3. Anonymous Volumes

# Docker manages the volume location
docker run -v /data nginx

Docker Networking

Docker provides several networking options for containers to communicate.

Network Types

  • Bridge: Default network for containers
  • Host: Container uses host's network
  • None: No network access
  • Custom: User-defined networks

Creating Custom Networks

# Create custom network
docker network create mynetwork

# Run containers on custom network
docker run --network mynetwork --name web nginx
docker run --network mynetwork --name db mysql

Multi-Container Applications with Docker Compose

Docker Compose allows you to define and run multi-container applications using a YAML file.

docker-compose.yml Example

version: '3.8'

services:
  web:
    build: .
    ports:
      - "3000:3000"
    environment:
      - NODE_ENV=production
    depends_on:
      - db
    volumes:
      - .:/usr/src/app
      - /usr/src/app/node_modules

  db:
    image: postgres:13
    environment:
      POSTGRES_DB: myapp
      POSTGRES_USER: user
      POSTGRES_PASSWORD: password
    volumes:
      - postgres_data:/var/lib/postgresql/data

volumes:
  postgres_data:

Docker Compose Commands

# Start services
docker-compose up

# Start in background
docker-compose up -d

# Stop services
docker-compose down

# View logs
docker-compose logs

# Scale services
docker-compose up --scale web=3

Debugging Docker Containers

Common Debugging Commands

# Inspect container details
docker inspect container_id

# View container logs
docker logs -f container_id

# Execute shell in running container
docker exec -it container_id /bin/bash

# View container processes
docker top container_id

# View container resource usage
docker stats container_id

Common Issues and Solutions

1. Port Already in Use

# Check what's using the port
lsof -i :3000

# Use different port
docker run -p 3001:3000 myapp

2. Permission Denied

# Fix file permissions
sudo chown -R $USER:$USER /path/to/files

# Or run with user flag
docker run --user $(id -u):$(id -g) myapp

3. Out of Disk Space

# Clean up unused resources
docker system prune -a

# Remove unused volumes
docker volume prune

Security Best Practices

Image Security

  • Use official, minimal base images
  • Regularly update base images
  • Scan images for vulnerabilities
  • Don't include secrets in images

Runtime Security

  • Run containers as non-root users
  • Use read-only filesystems when possible
  • Limit container resources
  • Use security profiles (AppArmor, SELinux)

Network Security

  • Use custom networks instead of default bridge
  • Implement network segmentation
  • Use TLS for communication
  • Regularly audit network configurations

Next Steps

Congratulations! You've learned the fundamentals of Docker. Here's what to explore next:

Advanced Topics

  • Kubernetes: Container orchestration at scale
  • Docker Swarm: Docker's native clustering solution
  • Multi-stage builds: Optimize image sizes
  • Health checks: Monitor container health
  • Secrets management: Secure handling of sensitive data

Development Workflow

  • Integrate Docker into CI/CD pipelines
  • Use Docker for local development environments
  • Implement blue-green deployments
  • Set up monitoring and logging

Conclusion

Docker has transformed how we build, ship, and run applications. By containerizing your applications, you gain consistency, portability, and scalability benefits that are essential in modern software development.

Start small by containerizing a simple application, then gradually adopt more advanced features like multi-container applications with Docker Compose. Remember that mastering Docker is a journey - practice regularly and don't hesitate to experiment with different configurations.

The containerization ecosystem is vast and constantly evolving. Stay curious, keep learning, and you'll soon find Docker to be an indispensable tool in your development toolkit.

🐳 Ready to Dive Deeper?

Practice makes perfect! Try containerizing your existing projects and experiment with different Docker features. Join the Docker community, read the official documentation, and don't be afraid to break things - that's how you learn!

#Docker #Containerization #Tutorial #Beginners #DevOps

Related Posts

DevOps Best Practices
DevOps 10 min read

DevOps Best Practices: A Complete Guide

Discover essential DevOps best practices that will transform your development workflow. Learn about CI/CD, infrastructure as code, and monitoring.

Read More
Building RESTful APIs with Laravel
Tutorial 8 min read

Building RESTful APIs with Laravel

Learn how to build robust and scalable RESTful APIs using Laravel. This comprehensive guide covers everything from setup to deployment.

Read More