How to Fix Docker Timeout Error on Ubuntu 22.04


As a Senior DevOps Engineer, addressing Docker timeout errors is critical for maintaining robust CI/CD pipelines and reliable container deployments. On Ubuntu 22.04, this issue frequently arises due to specific interactions with the host system’s DNS resolution.


Fixing “Docker Timeout Error” on Ubuntu 22.04

1. The Root Cause

Docker timeout errors on Ubuntu 22.04 often stem from an interplay between Docker’s internal networking and the host’s systemd-resolved DNS service. This can lead to delays in DNS resolution for external resources, causing pulls, builds, or network operations within containers to time out.

2. Quick Fix (CLI)

The most direct approach involves explicitly configuring Docker to use reliable public DNS servers, bypassing potential issues with systemd-resolved.

# Create the Docker configuration directory if it doesn't exist
sudo mkdir -p /etc/docker

# Create or overwrite daemon.json to specify Google's public DNS servers
echo '{ "dns": ["8.8.8.8", "8.8.4.4"] }' | sudo tee /etc/docker/daemon.json > /dev/null

# Reload systemd and restart the Docker service to apply changes
sudo systemctl daemon-reload
sudo systemctl restart docker

3. Configuration Check

To ensure the fix is persistent and to merge with any existing Docker daemon configurations, manually verify or edit the /etc/docker/daemon.json file.

  1. Open the daemon.json file using your preferred text editor:

    sudo nano /etc/docker/daemon.json
  2. Ensure the file contains the dns key with an array of reliable DNS server IP addresses. If other configurations exist (e.g., registry-mirrors, insecure-registries), ensure the dns entry is correctly added or updated within the existing JSON structure.

    {
      "dns": ["8.8.8.8", "8.8.4.4"]
      // Add or merge any other existing configurations here
      // "registry-mirrors": ["https://my.registry.com"],
      // "experimental": true
    }
  3. Save the file and exit the editor.

  4. After any manual changes to daemon.json, restart Docker to apply them:

    sudo systemctl daemon-reload
    sudo systemctl restart docker

4. Verification

Confirm the Docker daemon is now using the configured DNS servers and that the timeout issue is resolved.

# Check Docker daemon information for the configured DNS servers
sudo docker info | grep -i "dns server"

# Output should show:
#  DNS Server: 8.8.8.8
#  DNS Server: 8.8.4.4

# Attempt to pull an image to confirm the fix
docker pull alpine:latest

# (Optional) Run a temporary container and verify its internal resolv.conf
docker run --rm alpine:latest cat /etc/resolv.conf
# This output should also reflect the configured DNS servers.