mediumIntermediateSymptom-first#build#apt-get#dns#network#dockerfile

apt-get install fails during docker build — network or DNS issue in builder

docker build fails at RUN apt-get install with 'Could not resolve archive.ubuntu.com'

Likely source:connectivityconfiguration

Focused incident mode — strips away everything but this runbook.

Symptoms

  • docker build fails at RUN apt-get install with 'Could not resolve archive.ubuntu.com'
  • Err:1 http://archive.ubuntu.com/ubuntu ... Temporary failure in name resolution
  • docker build hangs on apt-get update then times out

docker build --network=host is a fast way to isolate DNS/network issues from Docker's bridge networking. If it works, the problem is Docker's DNS, not the upstream network.

Evidence to grab

Collect these artifacts before you change anything — they prove (or rule out) each cause.

  • Build error output

    docker build . 2>&1 | tail -30
  • Docker build DNS config

    docker build --network=host . 2>&1 | tail -5

Diagnostic flow

Run the steps in order, marking each claim confirmed or refuted. The evidence you gather feeds the ranked causes below.

Guided decision tree is ready.

Steps with commands, expected output, and pass/fail branches.

Ranked root causes

Evidence checklist

  • docker build fails with name resolution error
  • docker build --network=host works
  • daemon.json has no dns or wrong dns setting

Confirm before fixing

docker run --rm alpine nslookup archive.ubuntu.com
READ

Test DNS from a container on the default bridge

Expected: Address: 91.189.91.x

Fix

echo '{"dns": ["8.8.8.8", "8.8.4.4"]}' | sudo tee /etc/docker/daemon.json && sudo systemctl restart docker
CHANGE

Set DNS servers in Docker daemon config

Resolution runbook

Apply from top to bottom after the cause is confirmed. Review every command before running it — risk tags are shown on each.

1
docker build --network=host . 2>&1 | tail -20
READ

Test if host network fixes the build

2
cat /etc/docker/daemon.json 2>/dev/null || echo 'No daemon.json'
READ

Check daemon DNS config

3
echo '{"dns": ["8.8.8.8", "8.8.4.4"]}' | sudo tee /etc/docker/daemon.json
CHANGE

Set DNS servers globally for Docker

4
sudo systemctl restart docker
CHANGE

Restart Docker to pick up the new DNS config

Prevention

  • Configure DNS in /etc/docker/daemon.json: {"dns": ["8.8.8.8", "8.8.4.4"]}.
  • Use multi-stage builds to reduce the number of apt-get calls per layer.
  • Pin Ubuntu/Debian base images to avoid repo expiration.
  • Pass proxy env vars via --build-arg in CI/CD pipelines behind corporate proxies.

Commands marked READ CHANGE DESTRUCTIVE — never run DESTRUCTIVE commands against production data you cannot restore. Copy → verify → execute.