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 |
sudo or add your user to the docker group to avoid permission errors.
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. |
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
}
/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"
}
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
/etc/docker/daemon.json or per-container using the --log-driver flag.
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
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 |
8. Configuring Registry Mirrors
| 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"
]
}
Steps to Configure Registry Mirrors
- Edit
/etc/docker/daemon.jsonto include the desired mirrors underregistry-mirrors - Restart Docker daemon to apply changes:
sudo systemctl restart docker - Verify configuration by pulling an image:
docker pull hello-world
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 |
|
| CLI Flag | Use --buildkit flag in build command |
docker build --buildkit -t myapp . |
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
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 |
After enabling experimental features, the Docker daemon must be restarted for changes to take effect.
Workflow: Enabling Experimental Features
- Edit
/etc/docker/daemon.jsonto include experimental settings - Restart the Docker daemon using
sudo systemctl restart docker - 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 & # Start Docker with new root directory
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 |
docker group, they must log out and back in for the changes to take effect.
docker group provides them with root-level access to the Docker daemon. This can pose a security risk if not managed carefully.