Skip to main content

Docker Documentation

Updated 9 min readMarkdown

Table of Contents

  1. Introduction to Docker
  2. Installing Docker
  3. Docker Basics
  4. Docker Images and Containers
  5. Docker Networking
  6. Docker Volumes
  7. Docker Compose
  8. Dockerfile
  9. Advanced Docker Topics
  10. Docker Security
  11. Docker Orchestration
  12. Monitoring and Logging
  13. Best Practices
  14. Conclusion

1. Introduction to Docker

Docker is an open platform for developing, shipping, and running applications. It enables you to separate your applications from your infrastructure so you can deliver software quickly. With Docker, you can manage your infrastructure in the same ways you manage your applications.

Key Concepts

  • Containers: Lightweight, portable, and self-sufficient units that can run virtually anywhere.
  • Images: Immutable templates that you use to create containers.
  • Docker Engine: The runtime that allows you to build and run containers.
  • Docker Hub: A cloud-based registry service that allows you to link to code repositories, build your images, and test them.

2. Installing Docker

Prerequisites

  • Supported OS: Docker can be installed on various operating systems like Linux, Windows, and macOS.
  • Hardware requirements: Minimum of 4GB RAM.

Installation Steps

On Linux

  1. Update your existing list of packages:

    sudo apt-get update
    
  2. Install Docker using the convenience script:

    curl -fsSL https://get.docker.com -o get-docker.sh
    sudo sh get-docker.sh
    
  3. Verify the installation:

    sudo docker --version
    

On Windows and macOS

  1. Download Docker Desktop from the Docker website.

  2. Follow the installation instructions for your operating system.

  3. Verify the installation by running:

    docker --version
    

3. Docker Basics

Starting and Stopping Docker

  • Start Docker:

    sudo systemctl start docker
    
  • Stop Docker:

    sudo systemctl stop docker
    
  • Enable Docker to start at boot:

    sudo systemctl enable docker
    

Basic Commands

  • Run a container:

    docker run hello-world
    
  • List running containers:

    docker ps
    
  • List all containers:

    docker ps -a
    
  • Stop a container:

    docker stop <container_id>
    
  • Remove a container:

    docker rm <container_id>
    

4. Docker Images and Containers

Images

  • Pull an image:

    docker pull ubuntu
    
  • List images:

    docker images
    
  • Remove an image:

    docker rmi <image_id>
    

Containers

  • Create and run a container:

    docker run -it ubuntu
    
  • Run a container in the background:

    docker run -d ubuntu
    
  • Attach to a running container:

    docker attach <container_id>
    

5. Docker Networking

Network Drivers

  • Bridge: Default network driver.
  • Host: Removes network isolation between the container and the Docker host.
  • None: Disables all networking.
  • Overlay: Enables multi-host communication.
  • Macvlan: Allows you to assign a MAC address to a container.

Network Commands

  • List networks:

    docker network ls
    
  • Inspect a network:

    docker network inspect <network_name>
    
  • Create a network:

    docker network create <network_name>
    
  • Connect a container to a network:

    docker network connect <network_name> <container_id>
    
  • Disconnect a container from a network:

    docker network disconnect <network_name> <container_id>
    

6. Docker Volumes

Volume Management

  • Create a volume:

    docker volume create <volume_name>
    
  • List volumes:

    docker volume ls
    
  • Inspect a volume:

    docker volume inspect <volume_name>
    
  • Remove a volume:

    docker volume rm <volume_name>
    

Using Volumes with Containers

  • Mount a volume:

    docker run -d -v <volume_name>:/path/in/container <image_name>
    
  • Bind mount a host directory:

    docker run -d -v /host/path:/container/path <image_name>
    

Docker Volume Types: Detailed Explanation

Docker volumes are a mechanism to persist data generated and used by Docker containers. Volumes are stored outside the container’s filesystem and thus are not lost when a container is removed. Docker offers different types of volumes, each suited to different use cases.

1. Named Volumes

Named volumes are the most commonly used type of Docker volumes. They are created and managed by Docker and have a specific name.

  • Creation:

    docker volume create my_volume
    
  • Usage:

    docker run -d --name my_container -v my_volume:/app/data my_image
    
  • Pros:

    • Easy to manage and use.
    • Portable between containers.
    • Persist data even when the container is removed.
  • Cons:

    • Limited control over the exact location on the host filesystem.

2. Anonymous Volumes

Anonymous volumes are similar to named volumes but are not given explicit names. Docker manages them with generated names.

  • Creation and Usage:

    docker run -d --name my_container -v /app/data my_image
    
  • Pros:

    • Simple to use for temporary data storage.
  • Cons:

    • Hard to reference and manage outside the container lifecycle.
    • Not reusable across multiple containers easily.

3. Host Volumes (Bind Mounts)

Host volumes map a specific directory or file on the host machine to a container.

  • Creation and Usage:

    docker run -d --name my_container -v /host/data:/app/data my_image
    
  • Pros:

    • Provides fine-grained control over the data location on the host.
    • Useful for sharing data between the host and containers or between containers.
    • Immediate reflection of changes made on the host or in the container.
  • Cons:

    • Not portable across different hosts without ensuring the directory structure is identical.
    • Potential security risks if not managed carefully.

4. tmpfs Volumes

tmpfs volumes store data in the host system’s memory. They are ephemeral and are cleared when the container stops.

  • Creation and Usage:

    docker run -d --name my_container --mount type=tmpfs,destination=/app/data my_image
    
  • Pros:

    • Very fast access since data is in memory.
    • Useful for sensitive data that should not be written to disk.
  • Cons:

    • Data is lost when the container stops.
    • Limited by the host system’s memory.

5. NFS (Network File System) Volumes

NFS volumes allow you to mount directories from a remote server into the container. This is useful for sharing data across multiple containers running on different hosts.

  • Usage:

    docker run -d --name my_container -v nfs_server:/nfs/data:/app/data my_image
    
  • Pros:

    • Enables sharing of data across different hosts.
    • Centralized storage for multi-host setups.
  • Cons:

    • Dependent on network reliability and performance.
    • More complex to set up and manage.

6. Third-Party Volume Plugins

Docker supports third-party volume plugins that integrate with various storage solutions (e.g., AWS EFS, Azure Files, GCE Persistent Disks).

  • Usage:
    • Install the plugin:

      docker plugin install rexray/efs
      
    • Create and use the volume:

      docker volume create --driver rexray/efs my_efs_volume
      docker run -d --name my_container -v my_efs_volume:/app/data my_image
      
  • Pros:
    • Integrates with cloud storage solutions for scalable and managed storage.
    • Offers advanced features such as snapshots, replication, and more.
  • Cons:
    • May require additional configuration and management.
    • Plugin availability and support can vary.

Summary

Each type of Docker volume has its own advantages and use cases:

  • Named Volumes: Best for portable and easy-to-manage persistent data storage.
  • Anonymous Volumes: Useful for temporary data that doesn't need to be referenced outside the container.
  • Host Volumes (Bind Mounts): Ideal for sharing data between host and container or ensuring data is stored at a specific location on the host.
  • tmpfs Volumes: Suitable for fast, ephemeral data storage in memory.
  • NFS Volumes: Great for sharing data across multiple hosts in a network.
  • Third-Party Plugins: Perfect for integrating with external storage solutions, especially in cloud environments.

Choosing the right type of volume depends on your specific needs, such as portability, performance, persistence, and the environment in which your containers are running.

7. Docker Compose

Introduction

Docker Compose is a tool for defining and running multi-container Docker applications. With Compose, you use a YAML file to configure your application's services.

Installing Docker Compose

sudo curl -L "https://github.com/docker/compose/releases/download/1.29.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
sudo chmod +x /usr/local/bin/docker-compose

Basic Commands

  • Start services:

    docker-compose up
    
  • Start services in the background:

    docker-compose up -d
    
  • Stop services:

    docker-compose down
    

Example docker-compose.yml

version: '3'
services:
  web:
    image: nginx
    ports:
      - "80:80"
  db:
    image: mysql
    environment:
      MYSQL_ROOT_PASSWORD: example

8. Dockerfile

Introduction

A Dockerfile is a script that contains a series of instructions on how to build a Docker image.

Basic Instructions

  • FROM: Specifies the base image.
  • RUN: Executes a command in the container.
  • COPY: Copies files from the host to the container.
  • CMD: Specifies the command to run when the container starts.

Example Dockerfile

# Use an official Python runtime as a parent image
FROM python:3.9

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

# Copy the current directory contents into the container at /usr/src/app
COPY . .

# Install any needed packages specified in requirements.txt
RUN pip install --no-cache-dir -r requirements.txt

# Make port 80 available to the world outside this container
EXPOSE 80

# Define environment variable
ENV NAME World

# Run app.py when the container launches
CMD ["python", "app.py"]

9. Advanced Docker Topics

Docker Swarm

Docker Swarm is a native clustering and orchestration tool for Docker. It turns a pool of Docker hosts into a single, virtual host.

  • Initialize a swarm:

    docker swarm init
    
  • Join a swarm:

    docker swarm join --token <token> <manager_ip>:<port>
    
  • Deploy a stack:

    docker stack deploy -c <compose_file> <stack_name>
    

Kubernetes

Kubernetes is an open-source system for automating the deployment, scaling, and management of containerized applications.

  • Minikube: A tool that sets up a single-node Kubernetes cluster on your local machine.
  • Kubectl: The command-line tool to interact with Kubernetes clusters.

Docker Secrets

Docker secrets allow you to securely manage sensitive data, such as passwords and API keys, in your Docker applications.

  • Create a secret:

    echo "my_secret" | docker secret create my_secret -
    
  • List secrets:

    docker secret ls
    
  • Remove a secret:

    docker secret rm my_secret
    

10. Docker Security

Best Practices

  • Run containers as non-root: Avoid running containers as the root user.
  • Use official images: Prefer official and verified images from Docker Hub.
  • Regularly scan images: Use tools like docker scan to check for vulnerabilities.

Tools

  • Docker Bench for Security: A script that checks for common best practices in deploying Docker containers in production.
  • Clair: An open-source project for the static analysis of vulnerabilities in application containers (currently including Docker and appc).

No comments yet. Be the first to comment.