Skip to content

Configuration Management

Docker Engine behavior can be customized through daemon configuration, client configuration, and environment variables. This guide covers all configuration options and best practices.

Daemon Configuration

The Docker daemon (dockerd) is configured using a JSON configuration file and/or command-line flags.

Configuration File Location

PlatformDefault Path
Linux/etc/docker/daemon.json
WindowsC:\ProgramData\docker\config\daemon.json
macOS (Docker Desktop)~/.docker/daemon.json or via Docker Desktop UI

Complete Configuration Example

json
{
  "debug": false,
  "tls": true,
  "tlscacert": "/etc/docker/certs/ca.pem",
  "tlscert": "/etc/docker/certs/server-cert.pem",
  "tlskey": "/etc/docker/certs/server-key.pem",
  "hosts": ["unix:///var/run/docker.sock", "tcp://0.0.0.0:2376"],
  "storage-driver": "overlay2",
  "storage-opts": ["overlay2.size=20G"],
  "data-root": "/var/lib/docker",
  "exec-root": "/var/run/docker",
  "log-driver": "json-file",
  "log-opts": {
    "max-size": "10m",
    "max-file": "5",
    "labels": "production_status",
    "env": "os,customer"
  },
  "default-address-pools": [
    {
      "base": "172.17.0.0/16",
      "size": 24
    }
  ],
  "dns": ["8.8.8.8", "8.8.4.4"],
  "dns-search": ["example.com"],
  "max-concurrent-downloads": 10,
  "max-concurrent-uploads": 5,
  "default-runtime": "runc",
  "runtimes": {
    "custom": {
      "path": "/usr/local/bin/custom-runtime"
    }
  },
  "default-ulimits": {
    "nofile": {
      "Name": "nofile",
      "Hard": 65536,
      "Soft": 32768
    }
  },
  "live-restore": true,
  "userland-proxy": false,
  "no-new-privileges": true,
  "registry-mirrors": ["https://mirror.example.com"],
  "insecure-registries": [],
  "features": {
    "buildkit": true
  }
}

Configuration Options Reference

Storage Configuration

OptionDescriptionDefault
storage-driverStorage driver to useoverlay2
storage-optsStorage driver options[]
data-rootRoot directory for Docker data/var/lib/docker
json
{
  "storage-driver": "overlay2",
  "storage-opts": ["overlay2.size=20G"],
  "data-root": "/mnt/docker-data"
}
bash
# Move Docker data to a new location
sudo systemctl stop docker
sudo mv /var/lib/docker /mnt/docker-data
# Update data-root in daemon.json
sudo systemctl start docker

Logging Configuration

OptionDescriptionDefault
log-driverDefault logging driverjson-file
log-optsLogging driver options{}

Available Log Drivers:

DriverDescription
json-fileDefault JSON file logging
localCustom log format, minimal disk usage
syslogWrite to syslog facility
journaldWrite to systemd journal
fluentdForward to Fluentd
awslogsSend to Amazon CloudWatch
gcplogsSend to Google Cloud Logging
splunkSend to Splunk
noneDisable logging
json
{
  "log-driver": "json-file",
  "log-opts": {
    "max-size": "10m",
    "max-file": "5",
    "compress": "true"
  }
}
bash
# Override log driver for a specific container
docker run -d --log-driver syslog --log-opt syslog-address=tcp://192.168.1.100:514 nginx

# View current log driver
docker info --format '{{.LoggingDriver}}'

Network Configuration

json
{
  "dns": ["8.8.8.8", "8.8.4.4"],
  "dns-search": ["example.com"],
  "dns-opts": ["ndots:5"],
  "bip": "172.17.0.1/16",
  "fixed-cidr": "172.17.0.0/24",
  "default-address-pools": [
    {
      "base": "10.10.0.0/16",
      "size": 24
    }
  ],
  "mtu": 1500,
  "iptables": true,
  "ip-forward": true,
  "ip-masq": true,
  "userland-proxy": false,
  "ipv6": false
}
OptionDescriptionDefault
dnsDNS servers for containersHost DNS
bipBridge IP address172.17.0.1/16
fixed-cidrSubnet for container IPsMatches bip
default-address-poolsPool for custom networksDocker default
mtuMaximum transmission unit1500
iptablesEnable iptables rulestrue
userland-proxyUse userland proxy for port forwardingtrue

Security Configuration

json
{
  "tls": true,
  "tlscacert": "/etc/docker/certs/ca.pem",
  "tlscert": "/etc/docker/certs/server-cert.pem",
  "tlskey": "/etc/docker/certs/server-key.pem",
  "no-new-privileges": true,
  "seccomp-profile": "/etc/docker/seccomp/default.json",
  "userns-remap": "default",
  "live-restore": true
}

Registry Configuration

json
{
  "registry-mirrors": [
    "https://mirror.example.com",
    "https://mirror2.example.com"
  ],
  "insecure-registries": [
    "registry.internal.example.com:5000"
  ],
  "allow-nondistributable-artifacts": [
    "registry.internal.example.com:5000"
  ]
}

Resource Defaults

json
{
  "default-ulimits": {
    "nofile": {
      "Name": "nofile",
      "Hard": 65536,
      "Soft": 32768
    },
    "nproc": {
      "Name": "nproc",
      "Hard": 4096,
      "Soft": 2048
    }
  },
  "default-shm-size": "64M",
  "cgroup-parent": "docker",
  "init": true
}

Client Configuration

Docker CLI Configuration

The Docker CLI stores configuration in ~/.docker/config.json:

json
{
  "auths": {
    "https://index.docker.io/v1/": {
      "auth": "base64-encoded-credentials"
    },
    "registry.example.com": {
      "auth": "base64-encoded-credentials"
    }
  },
  "credHelpers": {
    "gcr.io": "gcloud",
    "*.dkr.ecr.*.amazonaws.com": "ecr-login"
  },
  "credsStore": "desktop",
  "currentContext": "default",
  "plugins": {},
  "aliases": {
    "builder": "buildx"
  }
}

Docker Contexts

Contexts allow you to switch between different Docker environments:

bash
# List contexts
docker context ls

# Create a new context
docker context create production \
  --docker "host=tcp://prod-server:2376,ca=~/certs/ca.pem,cert=~/certs/cert.pem,key=~/certs/key.pem"

# Switch to a context
docker context use production

# Run a command with a specific context
docker --context production ps

# Remove a context
docker context rm production

Environment Variables

VariableDescription
DOCKER_HOSTDaemon socket URL
DOCKER_TLS_VERIFYEnable TLS verification
DOCKER_CERT_PATHPath to TLS certificates
DOCKER_CONFIGLocation of client config
DOCKER_CONTENT_TRUSTEnable image signing
DOCKER_BUILDKITEnable BuildKit
COMPOSE_FILECompose file path
COMPOSE_PROJECT_NAMECompose project name
bash
# Example environment setup
export DOCKER_HOST=tcp://192.168.1.10:2376
export DOCKER_TLS_VERIFY=1
export DOCKER_CERT_PATH=~/.docker/certs
export DOCKER_BUILDKIT=1

Applying Configuration Changes

bash
# After editing /etc/docker/daemon.json:

# Option 1: Restart Docker (causes container downtime)
sudo systemctl restart docker

# Option 2: Reload configuration (no downtime, limited options)
sudo systemctl reload docker
# or
sudo kill -SIGHUP $(pidof dockerd)

# Verify configuration
docker info

Reloadable vs Non-Reloadable Options

Reloadable (no restart)Non-Reloadable (restart required)
debugstorage-driver
labelsdata-root
max-concurrent-downloadstls / tlscacert / tlscert / tlskey
max-concurrent-uploadshosts
log-driver / log-optsbip
shutdown-timeoutuserns-remap
live-restoreiptables

Docker Desktop Configuration

Docker Desktop provides a GUI for configuration:

Resource Settings

SettingDescriptionRecommended
CPUsCPU cores allocated4+ for development
MemoryRAM allocated8 GB+ for development
SwapSwap space2 GB
Disk image sizeMax disk space60 GB+
File sharingShared directoriesProject directories

WSL 2 Configuration (Windows)

Create %USERPROFILE%\.wslconfig:

ini
[wsl2]
memory=8GB
processors=4
swap=2GB
localhostForwarding=true
nestedVirtualization=true

[experimental]
sparseVhd=true

Configuration Validation

bash
# Validate daemon.json syntax
python3 -c "import json; json.load(open('/etc/docker/daemon.json'))"

# Test Docker configuration
dockerd --validate

# Check current configuration
docker info

# Check specific settings
docker info --format '{{.Driver}}'
docker info --format '{{.LoggingDriver}}'
docker info --format '{{.CgroupDriver}}'

Next Steps

基于 MIT 许可发布