Back to DevOps

Docker Tutorial

Master Docker fundamentals with our comprehensive tutorial. Learn containerization, image management, and orchestration through practical examples.

Video Tutorial

Introduction to Docker

Docker is a platform that enables developers to package applications into containers—standardized executable components combining application source code with the operating system (OS) libraries and dependencies required to run that code in any environment.

Examples:

docker --version

Check your Docker installation version

docker run hello-world

Run your first Docker container to verify installation

Docker Images and Containers

Docker images are read-only templates used to create containers. Containers are running instances of Docker images.

Examples:

docker pull nginx:latest

Pull the latest nginx image from Docker Hub

docker images

List all Docker images on your system

docker run -d -p 8080:80 --name my-nginx nginx

Run nginx container in detached mode -d: Detached mode -p: Port mapping (host:container) --name: Container name

docker ps

List all running containers

docker ps -a

List all containers (including stopped ones)

Dockerfile Basics

A Dockerfile is a text document that contains all the commands to assemble a Docker image. It automates the image creation process.

Examples:

FROM node:18-alpine
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
EXPOSE 3000
CMD ["npm", "start"]

A basic Dockerfile for a Node.js application FROM: Base image WORKDIR: Set working directory COPY: Copy files RUN: Execute commands EXPOSE: Document port CMD: Default command

docker build -t my-app:1.0 .

Build a Docker image from Dockerfile -t: Tag the image .: Build context (current directory)

Docker Container Management

Learn essential commands to manage Docker containers throughout their lifecycle.

Examples:

docker start my-nginx

Start a stopped container

docker stop my-nginx

Stop a running container gracefully

docker restart my-nginx

Restart a container

docker rm my-nginx

Remove a stopped container

docker rm -f my-nginx

Force remove a running container

docker logs my-nginx

View container logs

docker exec -it my-nginx /bin/bash

Execute interactive bash shell inside container -it: Interactive terminal

Docker Volumes

Docker volumes are the preferred mechanism for persisting data generated by and used by Docker containers.

Examples:

docker volume create my-volume

Create a named volume

docker run -d -v my-volume:/data --name my-container nginx

Mount a volume to a container -v: Volume mapping (volume:container-path)

docker volume ls

List all volumes

docker volume inspect my-volume

View detailed information about a volume

Docker Compose

Docker Compose is a tool for defining and running multi-container Docker applications using a YAML file.

Examples:

version: '3.8'
services:
  web:
    image: nginx:latest
    ports:
      - "8080:80"
    volumes:
      - ./html:/usr/share/nginx/html
  
  db:
    image: postgres:15
    environment:
      POSTGRES_PASSWORD: example
    volumes:
      - db-data:/var/lib/postgresql/data

volumes:
  db-data:

A docker-compose.yml file defining a web server and database services: Define containers ports: Port mappings volumes: Volume mounts environment: Environment variables

docker-compose up -d

Start all services defined in docker-compose.yml in detached mode

docker-compose down

Stop and remove all containers, networks defined in compose file

docker-compose logs -f

View logs from all services -f: Follow log output

Docker Networking

Docker networking allows containers to communicate with each other and the outside world.

Examples:

docker network create my-network

Create a custom network

docker network ls

List all networks

docker run -d --network my-network --name app1 nginx

Run a container on a specific network

docker network inspect my-network

View detailed information about a network

Docker Cleanup Commands

Keep your Docker environment clean by removing unused containers, images, and volumes.

Examples:

docker system prune

Remove all stopped containers, unused networks, dangling images

docker system prune -a

Remove all unused images, not just dangling ones -a: All unused images

docker image prune

Remove unused images

docker volume prune

Remove unused volumes

docker container prune

Remove all stopped containers

Quick Reference

Essential Commands

  • docker ps - List running containers
  • docker images - List images
  • docker pull - Download image
  • docker build - Build image

Best Practices

  • ✓ Use official base images
  • ✓ Minimize layer count
  • ✓ Use .dockerignore files
  • ✓ Don't run as root user