Skip to main content

Installing and Configuring Docker

1. Installing Docker Engine

Operating System Installation Method Notes
Ubuntu 20.04+ sudo apt update && sudo apt install docker.io Available in official repositories
CentOS/RHEL 7+ sudo yum install docker Requires EPEL repository
Debian 10+ sudo apt update && sudo apt install docker.io Official repository support
macOS brew install docker Install via Homebrew or Docker Desktop
Windows Install Docker Desktop Available from Docker official site
Amazon Linux 2 sudo yum install docker Use Amazon ECR for container images

Example: Post-installation setup on Linux

sudo usermod -aG docker $USER
newgrp docker
Note: On Linux, you must either run commands with sudo or add your user to the docker group to avoid permission errors.
Warning: Adding a user to the docker group grants root-level privileges to that user.

2. Installing Docker Desktop

Operating System Installation Method Notes
Windows 10/11 Pro/Enterprise docker-desktop-installer.exe Download from Docker Hub. Requires WSL2 backend.
macOS (Intel) docker.dmg Download from Docker Hub. Requires macOS 10.15 or later.
macOS (Apple Silicon) docker.dmg Download from Docker Hub. Requires macOS 12 or later.
Linux (Ubuntu/Debian) apt install docker.io Use official Docker repository for latest stable version.
Linux (CentOS/RHEL) yum install docker Use Docker CE repository. Requires root privileges.

Example: Verify Installation

$ docker --version
Docker version 27.4.0, build abc123d
Note: Docker Desktop includes Docker Engine, Docker CLI, and Docker Compose. It is the recommended way to run Docker on Windows and macOS.
Warning: On Linux, ensure the docker user is added to the docker group to avoid needing sudo for every command.

Installation Summary

  • Windows: Use Docker Desktop installer with WSL2 backend.
  • macOS: Download and install the .dmg file from Docker Hub.
  • Linux: Install via package manager or official repository.

3. Configuring Docker Daemon

Configuration Option Description Default Value Notes
data-root Root directory for Docker's data storage /var/lib/docker Can be changed to use a different filesystem or mount point
log-driver Default logging driver for containers json-file Options include json-file, journald, syslog
log-opts Options for the logging driver null Example: max-size=10m max-file=3
live-restore Keep containers running when the daemon restarts false Useful for production environments to avoid container shutdowns
userland-proxy Use userland proxy for loopback traffic true Set to false for better performance in some cases
icc Allow inter-container communication true Set to false for enhanced security

Example: Docker Daemon Configuration File

{
  "data-root": "/mnt/docker",
  "log-driver": "json-file",
  "log-opts": {
    "max-size": "10m",
    "max-file": "3"
  },
  "live-restore": true,
  "userland-proxy": false
}
Note: The Docker daemon configuration file is typically located at /etc/docker/daemon.json. Changes to this file require a daemon restart to take effect.

4. Configuring Storage Drivers

Driver Description Use Case Notes
aufs Union File System, layer-based storage Older Docker versions (pre-1.10) DEPRECATED in favor of overlay2
overlay2 Modern union filesystem, supports multiple layers Recommended for most Linux distributions Default in Docker 17.06+
btrfs Btrfs filesystem-based storage driver Systems already using Btrfs Supports snapshots and compression
zfs ZFS-based storage driver Systems using ZFS filesystem High performance, snapshots supported
devicemapper Device Mapper thin provisioning driver Legacy systems or specific storage needs DEPRECATED in favor of overlay2

Example: Configure overlay2 storage driver

{
  "storage-driver": "overlay2",
  "data-root": "/var/lib/docker"
}
Note: The storage driver must be set at Docker daemon startup and cannot be changed without reinitializing the Docker data directory.

5. Configuring Logging Drivers

Driver Description Usage Example Notes
json-file Default logging driver. Stores logs in JSON format. "log-driver": "json-file" Logs are stored in /var/lib/docker/containers/<container-id>/<container-id>-json.log
syslog Sends logs to syslog daemon. "log-driver": "syslog" Requires syslog daemon to be running on host
journald Logs to the systemd journal. "log-driver": "journald" Best for systems using systemd
gelf Sends logs to a Graylog server using GELF format. "log-driver": "gelf" Requires a Graylog server to receive logs
fluentd Logs to a Fluentd server. "log-driver": "fluentd" Requires a Fluentd server to receive logs
awslogs Logs to Amazon CloudWatch Logs. "log-driver": "awslogs" Requires AWS credentials and CloudWatch Logs permissions
splunk Logs to Splunk Enterprise or Splunk Cloud. "log-driver": "splunk" Requires Splunk HEC endpoint and token
none Disables logging for the container. "log-driver": "none" Useful for containers that should not log anything

Example: Setting a logging driver for a container

docker run --log-driver=syslog --log-opt syslog-address=udp://127.0.0.1:514 nginx

Example: Setting a logging driver for a service (Docker Swarm)

docker service create --log-driver=gelf --log-opt gelf-address=udp://192.168.1.10:12201 --name myapp nginx
Note: The logging driver can be configured globally in /etc/docker/daemon.json or per-container using the --log-driver flag.
Warning: Some logging drivers like awslogs or splunk require additional configuration and permissions to function properly.

6. Configuring Docker Context

Context Description Commands
default Default Docker context for local Docker daemon docker context ls
named-context User-defined Docker context with custom configuration docker context create <name>

Example: Creating a Named Docker Context

docker context create --description "Production environment" prod-context
docker context use prod-context
Note: Docker contexts allow switching between different Docker environments (local, remote, Swarm) easily.
Warning: Ensure the Docker daemon is running before switching contexts.

7. Setting Up Proxy Configuration

Example: Docker Daemon Proxy Configuration

{
  "proxies": {
    "default": {
      "httpProxy": "http://proxy.company.com:8080",
      "httpsProxy": "http://proxy.company.com:8080",
      "noProxy": "*.test.example.com,.example.com,127.0.0.0/8"
    }
  }
}
Property Value Description
httpProxy http://proxy.company.com:8080 HTTP proxy server URL
httpsProxy http://proxy.company.com:8080 HTTPS proxy server URL
noProxy *.test.example.com,.example.com,127.0.0.0/8 Comma-separated list of domains/IPs to bypass proxy
Note: The proxy configuration is applied at the Docker daemon level and affects all containers created by that daemon.
Warning: Ensure proxy credentials are securely managed, especially in production environments. Avoid hardcoding credentials in configuration files.

8. Configuring Registry Mirrors

Note: Registry mirrors can improve pull speeds and reliability by routing requests through geographically closer servers.
Configuration File Path Description
/etc/docker/daemon.json /etc/docker/daemon.json JSON configuration file for Docker daemon

Example: Setting up a Registry Mirror

{
  "registry-mirrors": [
    "https://mirror.gcr.io",
    "https://docker.mirrors.ustc.edu.cn"
  ]
}

Example: Using Aliyun Mirror

{
  "registry-mirrors": [
    "https://docker.mirror.aliyuncs.com"
  ]
}

Steps to Configure Registry Mirrors

  • Edit /etc/docker/daemon.json to include the desired mirrors under registry-mirrors
  • Restart Docker daemon to apply changes: sudo systemctl restart docker
  • Verify configuration by pulling an image: docker pull hello-world
Note: Ensure that the mirror URLs are accessible and not blocked by firewalls or network policies.

9. Enabling BuildKit

BuildKit is a Docker feature that improves build performance and provides advanced capabilities like parallel builds, better caching, and multi-stage build optimizations.

Configuration Method Description Example
Environment Variable Set BUILDKIT to 1 BUILDKIT=1 docker build -t myapp .
Docker Daemon Config Modify /etc/docker/daemon.json
{
  "features": {
    "buildkit": true
  }
}
CLI Flag Use --buildkit flag in build command docker build --buildkit -t myapp .
Note: BuildKit is enabled by default in Docker Desktop and newer versions of Docker Engine.

Example: Using BuildKit with Dockerfile

FROM alpine:latest
RUN --mount=type=cache,target=/var/cache/apk \
    apk update && apk add curl
CMD ["curl", "https://example.com"]

10. Setting Up Experimental Features

Note: Experimental features in Docker are subject to change and may not be stable. Use them at your own risk.

Experimental features in Docker can be enabled through the Docker daemon configuration. These features are typically not available by default and must be explicitly enabled.

Feature Description Status Configuration
experimental Enables experimental features in the Docker daemon EXPERIMENTAL Set to true in daemon.json
features Enables specific experimental features (e.g., buildkit) EXPERIMENTAL Defined in daemon.json under features

Example: Enabling Experimental Features

{
  "experimental": true,
  "features": {
    "buildkit": true
  }
}

After enabling experimental features, the Docker daemon must be restarted for changes to take effect.

Workflow: Enabling Experimental Features

  1. Edit /etc/docker/daemon.json to include experimental settings
  2. Restart the Docker daemon using sudo systemctl restart docker
  3. Verify that experimental features are active using docker info

Summary of Experimental Features

  • BuildKit: Enhances build performance and caching
  • Build Secrets: Allows secure handling of secrets during builds
  • Multi-platform Builds: Enables building images for different architectures

11. Configuring Docker Root Directory

By default, Docker stores its data (images, containers, volumes, etc.) in the /var/lib/docker directory. You can configure this location using the data-root option.

Configuration Option Description Example Value
data-root Specifies the root directory for Docker data storage. /docker/data

Example: Setting Docker Root Directory

sudo mkdir -p /docker/data
sudo dockerd --data-root /docker/data &amp; # Start Docker with new root directory
Note: When changing the Docker root directory, ensure that the new location has sufficient disk space and proper permissions for the Docker daemon to read/write files.

12. Managing Docker User Groups

Docker daemon runs as the root user by default, which means that only root users or members of the docker group can control Docker. Adding a user to the docker group provides them with full access to Docker.

User Group Membership Access Level
root docker Full Docker control
regular_user docker Full Docker control
regular_user None No Docker access

Example: Adding a User to the Docker Group

sudo usermod -aG docker $USER
Note: After adding a user to the docker group, they must log out and back in for the changes to take effect.
Warning: Adding users to the docker group provides them with root-level access to the Docker daemon. This can pose a security risk if not managed carefully.

Was this article helpful?