Fixing Docker MTU mismatches and 5-second Alpine DNS delays
How I diagnosed hanging HTTPS responses and 5-second DNS timeouts inside Docker containers caused by PMTU black holes and musl libc.
External HTTPS calls worked fine from the host VM, yet hung mid-TLS handshake or stalled for exactly 5 seconds inside Docker containers. Here’s how I traced both failures to a 1500 vs. 1420 MTU mismatch (PMTU black hole) and Alpine’s
musl libcDNS resolver, then fixed both in production.
1. The problem: host curl succeeds, container curl hangs
I was running a Spring Boot microservice that called an external payment gateway and a reporting API returning multi-kilobyte payloads. Running curl directly on the Linux host (EC2 / on-prem VM) succeeded every time. Inside the Docker container on the exact same host, requests failed in two distinct ways:
- Small payloads worked, large payloads hung: Health checks and tiny JSON responses (a few hundred bytes) succeeded, while multi-KB responses and TLS handshakes (during certificate exchange) froze until hitting
Read timed out. - Intermittent 5,000 ms latency spikes: Even requests that succeeded occasionally stalled for almost exactly 5 seconds before returning normally.
1
2
3
4
5
6
7
8
9
10
11
12
# On the host shell: instant 200 OK
$ curl -s -o /dev/null -w "%{http_code}\n" https://api.partner-service.com/v1/large-dataset
200
# Inside the container: freezes during TLS handshake or response body
$ docker exec -it api-server curl -v https://api.partner-service.com/v1/large-dataset
* Connected to api.partner-service.com (203.0.113.50) port 443 (#0)
* ALPN: offers h2,http/1.1
* TLSv1.3 (OUT), TLS handshake, Client hello (1):
* TLSv1.3 (IN), TLS handshake, Server hello (2):
* TLSv1.3 (IN), TLS handshake, Encrypted Extensions (8):
# ... stalls here until Connection timed out after 120s
Neither issue was an application bug. Two separate lower-layer behaviors—L3/L4 MTU mismatch and Alpine’s musl libc DNS resolver—were colliding at the same time.
2. Root cause 1: MTU mismatch and PMTU black holes
What MTU is and why tunnels shrink it
MTU (Maximum Transmission Unit) is the largest IP packet a network interface can send in one frame. Standard Ethernet defaults to 1500 bytes.
In cloud overlay networks (AWS Geneve, GCP, Azure) and VPN tunnels (WireGuard, IPsec, OpenVPN, VXLAN), encapsulation headers eat 20–80 bytes per packet. That forces the host’s outbound interface to use a smaller MTU such as 1420 or 1450:
1
2
3
4
5
6
# Check interface MTUs on the host
$ ip link show
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 ...
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 ...
3: wg0: <POINTOPOINT,NOARP,UP,LOWER_UP> mtu 1420 ... # VPN interface MTU is 1420
4: docker0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 ... # Docker bridge defaults to 1500
Why Docker creates the mismatch
Unless configured otherwise, dockerd creates docker0 and user-defined bridge networks with an MTU of 1500. That leaves a mismatch along the outbound path:
- Container
eth0: MTU 1500 - Host Docker bridge (
docker0/br-*): MTU 1500 - Host outbound tunnel (
wg0or cloud overlay): MTU 1420
The DF flag and PMTU black holes
Modern TCP/HTTPS stacks set the IP DF (Don’t Fragment) flag to 1 to avoid fragmentation overhead and security issues. When the container sends a 1500-byte packet through a 1420-byte tunnel, here’s what breaks:
sequenceDiagram
autonumber
participant C as Container (MTU 1500)
participant B as Docker Bridge (MTU 1500)
participant H as Host Tunnel (MTU 1420)
participant F as Firewall / External API
C->>B: Send 1500-byte packet (DF=1)
B->>H: Route packet to host tunnel
Note over H: Exceeds MTU 1420 and DF=1<br/>Cannot fragment -> Drop
H--xF: Packet dropped
H->>C: Send ICMP Type 3 Code 4<br/>(Fragmentation Needed, MTU 1420)
Note over B,C: Firewall or iptables drops ICMP!
Note over C: Never learns smaller MTU<br/>(PMTU Black Hole)
C->>B: Retransmits 1500-byte packet repeatedly...
Note over C,F: Connection / Read Timeout
Normally, the router or host sends back ICMP Type 3, Code 4 (Fragmentation Needed) so the sender shrinks its packet size via Path MTU Discovery (PMTUD). When a firewall or iptables rule drops ICMP packets, the container never gets the message and keeps retransmitting oversized packets until the socket times out.
That explains why TCP handshakes and tiny JSON payloads (under 1420 bytes) passed fine, while TLS certificate chains and larger HTTP responses froze solid.
3. Root cause 2: Alpine Linux (musl libc) 5-second DNS timeouts
After fixing MTU, the hangs disappeared, but the 5,000 ms latency spikes remained. The culprit was our lightweight base image: Alpine Linux, which uses musl libc instead of glibc.
How musl libc queries DNS differently from glibc
Ubuntu and Debian use glibc, which either queries A and AAAA records sequentially or separates UDP sockets cleanly. musl libc takes a simpler approach:
- On every hostname lookup,
muslfires IPv4 (A) and IPv6 (AAAA) queries in parallel from the exact same local UDP source port. - Some routers, legacy DNS forwarders, and cloud NAT gateways hit a conntrack race condition when two UDP packets leave the same source port simultaneously and silently drop one reply (usually the
AAAAresponse). musl’s default DNS query timeout is 5 seconds. Waiting for the missing reply blocks the resolver for 5 full seconds before it retries and resolves the name.
1
2
3
4
5
6
[musl libc DNS packet sequence]
00:00.000 [Socket 4521] -> DNS Query A api.partner-service.com
00:00.001 [Socket 4521] -> DNS Query AAAA api.partner-service.com (same UDP socket)
00:00.015 [Socket 4521] <- DNS Response A 203.0.113.50
00:00.016 (AAAA reply dropped -> musl resolver blocks...)
00:05.016 (5s timeout expires -> retry succeeds -> 5-second stall)
Every time that race hit on an outbound API call, the request paused for 5 seconds and then completed normally.
4. Fix 1: Match Docker’s MTU to the host path
1) Measure the real path MTU from the host
Use ping with DF enabled (-M do) to find the largest unfragmented payload. Remember that IP (20 bytes) + ICMP (8 bytes) add 28 bytes of header overhead, so testing MTU 1420 uses -s 1392:
1
2
3
4
5
6
7
8
9
# Test MTU 1420 (1392 payload + 28 header = 1420)
$ ping -M do -s 1392 -c 3 8.8.8.8
PING 8.8.8.8 (8.8.8.8) 1392(1420) bytes of data.
1400 bytes from 8.8.8.8: icmp_seq=1 ttl=115 time=14.2 ms
1400 bytes from 8.8.8.8: icmp_seq=2 ttl=115 time=14.1 ms
# One byte over (1421) fails immediately
$ ping -M do -s 1393 -c 1 8.8.8.8
ping: local error: message too long, mtu=1420
2) Set the global MTU in /etc/docker/daemon.json
Configure dockerd so docker0 and future default networks inherit 1420:
1
2
3
4
5
6
7
8
{
"log-driver": "json-file",
"log-opts": {
"max-size": "50m",
"max-file": "3"
},
"mtu": 1420
}
Reload and restart Docker:
1
2
$ sudo systemctl daemon-reload
$ sudo systemctl restart docker
Existing user-defined bridge networks do not update automatically.
Settingmtuindaemon.jsonapplies todocker0and newly created default networks, not to existing Compose or custom bridge networks. You must setdriver_optsexplicitly and recreate those networks.
3) Pin MTU explicitly in compose.yaml
For Docker Compose projects, declare the bridge MTU directly in compose.yaml:
1
2
3
4
5
6
7
8
9
10
11
12
13
services:
api-server:
image: my-service:latest
ports:
- "8080:8080"
networks:
- internal-net
networks:
internal-net:
driver: bridge
driver_opts:
com.docker.network.driver.mtu: "1420"
Recreate the network and verify the applied option:
1
2
3
4
5
6
7
8
9
# Tear down and recreate containers + network
$ docker compose down
$ docker compose up -d
# Confirm the bridge MTU
$ docker network inspect <project-name>_internal-net --format '{{ index .Options "com.docker.network.driver.mtu" }}'
1420
5. Fix 2: Eliminate the Alpine 5-second DNS stall
Option A: Tune dns_opt in Compose (for glibc / patched resolvers)
In environments where the resolver honors resolv.conf options (single-request-reopen works on glibc, though musl ignores single-request-reopen and only respects timeout and attempts), you can pass DNS options via Compose:
1
2
3
4
5
6
7
services:
api-server:
image: my-service:latest
dns_opt:
- single-request-reopen
- timeout:2
- attempts:2
Option B: Switch the base image to Debian/Ubuntu Slim (glibc) — recommended
Because musl ignores single-request-reopen, the cleanest permanent fix for production services is switching from Alpine to a glibc-based slim image (Debian Slim or Ubuntu Jammy/Noble).
| Comparison | Alpine Linux (musl libc) | Debian / Ubuntu Slim (glibc) |
|---|---|---|
| Base image size | ~5–8 MB | ~30–70 MB |
| C standard library | musl libc | GNU C Library (glibc) |
| DNS query behavior | Parallel A/AAAA on one socket (prone to 5s stalls) | Separate sockets / supports single-request-reopen |
| JVM compatibility | Occasional allocator and musl-specific edge cases | Officially recommended and battle-tested for JVM |
For Spring Boot, Go, or Node.js services in production, saving 30 MB of disk space isn’t worth debugging intermittent 5-second DNS timeouts.
1
2
3
4
5
6
7
8
# Before (Alpine / musl libc)
# FROM eclipse-temurin:21-jre-alpine
# After (Ubuntu Jammy / glibc — avoids musl DNS stalls)
FROM eclipse-temurin:21-jre-jammy
WORKDIR /app
COPY build/libs/api-service.jar app.jar
ENTRYPOINT ["java", "-jar", "app.jar"]
When to keep Alpine: If you’re running in a tightly constrained edge environment where image size truly matters, keep Alpine and add a local caching DNS forwarder (like dnsmasq) or pin static high-traffic endpoints—otherwise pick glibc slim images by default.
6. Verification and before/after results
After applying MTU 1420 and switching to eclipse-temurin:21-jre-jammy, I verified the container interface and ran repeated 10 MB downloads (for packet-level captures during similar issues, see debugging Docker networks with netshoot):
1
2
3
4
5
6
7
8
9
10
11
12
# 1. Confirm container eth0 MTU
$ docker exec -it api-server ip link show eth0
12: eth0@if13: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1420 qdisc noqueue state UP ...
# 2. Download 10 MB payload with timing breakdown
$ docker exec -it api-server curl -w "@curl-format.txt" -o /dev/null -s https://api.partner-service.com/v1/large-dataset
time_namelookup: 0.004120s
time_connect: 0.015234s
time_appconnect: 0.038192s
time_starttransfer: 0.052101s
----------
time_total: 0.245812s
| Metric | Before (MTU 1500 + Alpine) | After (MTU 1420 + glibc) | Result |
|---|---|---|---|
| Small API call latency | Intermittent 5,012 ms spikes | 18 ms average | 99.6% latency reduction on affected calls |
| Large payload / TLS handshake | Hung until 120s Read timed out | 245 ms steady | Hangs eliminated |
| TCP retransmission rate | 38.4% | < 0.02% | Stable throughput |
7. Troubleshooting checklist
When network calls work on the host but stall inside containers, check these three things first:
- Check host interface MTUs: Run
ip link showand look for VPN, VXLAN, or cloud overlay interfaces below1500. - Sync Docker’s MTU everywhere: Update
/etc/docker/daemon.jsonanddriver_opts.com.docker.network.driver.mtuincompose.yaml, then recreate existing bridge networks. - Audit your base image’s libc: If you see exact 5-second DNS delays, check whether you’re running Alpine (
musl libc) and switch to aglibcslim image.