Tổng quan Docker Compose DNS lỗi: Service name vs localhost

Debug Docker DNS trong Compose: service name, resolver và retry

Trả lời nhanh: Muốn xử lý Docker Compose DNS lỗi, hãy ghi triệu chứng và tái hiện từng lớp bằng lệnh có exit code trước khi sửa. Bài này tách giả thuyết, expected/error signature và bước khoanh vùng để tránh retry mù hoặc rollback nhầm dependency.

Phạm vi: Bài áp dụng cho Service name vs localhost trong môi trường staging/standalone. Kiểm tra Nginx/Docker/OS/database và phiên bản CLI thực tế trước khi chạy; không đưa credential thật vào lệnh, log hay artifact.

Tổng quan Docker Compose DNS lỗi: Service name vs localhost
Tổng quan biên tập cho chủ đề: Docker Compose DNS lỗi.

Tín hiệu cần đối chiếu: Service name vs localhost; network inspect; `/etc/resolv.conf`; triệu chứng; lệnh tái hiện; chữ ký lỗi; kiểm tra hồi quy

Service name vs localhost — thao tác và bằng chứng

Inside a Compose network, `localhost` is the current container; the service name (for example `db`) resolves through Docker's embedded DNS. Reproduce from the calling container with `docker compose exec api getent hosts db` and then `nc -vz -w3 db 5432`. Expected evidence is an IP returned by `getent` followed by a TCP result; `NXDOMAIN` means the name/network is wrong, while a timeout after resolution is a readiness or firewall problem. Docker—Networking

Record the Compose project, network name, container IDs and engine version. Do not change `/etc/hosts` or hard-code a container IP: a recreate legitimately changes that address. If only one service cannot resolve, compare its network attachments with `docker network inspect app_net` before restarting the entire stack. (debug-docker-dns-compose-service-name-resolver · outline 1)

Với Service name vs localhost, ghi lại input và phiên bản CLI trước khi chạy; sau đó đối chiếu từng tín hiệu triệu chứng, lệnh tái hiện, chữ ký lỗi, kiểm tra hồi quy, Service name vs localhost, network inspect, `/etc/resolv.conf` trong stdout, stderr hoặc log audit. Kết quả đạt phải chỉ ra trạng thái và phạm vi tài nguyên; kết quả lỗi phải có chuỗi nhận diện để người trực ca biết bước khoanh vùng kế tiếp. Gắn transcript với debug-docker-dns-compose-service-name-resolver, commit/config hash, owner và timestamp. Không dùng exit code đơn độc làm bằng chứng. Che token, private key, cookie và PII trước khi lưu artifact; giữ bản raw trong kho có quyền hạn nếu incident yêu cầu.

network inspect — thao tác và bằng chứng

`docker network inspect app_net` should show the API and database under `Containers`, with aliases matching the Compose service key. Check that both containers are on the same user-defined network; the default bridge does not provide the same service-name discovery. Expected output includes `Internal`/subnet and member names. A missing member is an attachment error, not an application DNS cache bug. Docker—Networking

Capture the inspect JSON before reconnecting anything. If an external network is marked with the wrong project or name, fix the Compose network declaration and recreate only the affected service. Keep the old container/network long enough to compare DNS and readiness logs; rollback is reconnecting to the known-good network, not deleting evidence. (debug-docker-dns-compose-service-name-resolver · outline 2)

Một lần kiểm tra network inspect chỉ có giá trị khi lặp lại được. Chốt workload, hostname, digest hoặc schema, rồi chạy lệnh tương ứng với debug-docker-dns-compose-service-name-resolver. Đối chiếu các tín hiệu bắt buộc (triệu chứng, lệnh tái hiện, chữ ký lỗi, kiểm tra hồi quy, Service name vs localhost, network inspect, `/etc/resolv.conf`) với expected output và error signature; nếu thiếu dữ liệu, ghi unknown. So sánh baseline trước/sau trong cùng cửa sổ và nêu rõ điều gì không được suy ra từ phép đo. Artifact thay đổi cần ghi config diff đã redact, người chịu trách nhiệm và thời điểm hết hạn exception trước khi promote.

`/etc/resolv.conf` — thao tác và bằng chứng

Read `/etc/resolv.conf` in the failing container. A normal Compose container lists `127.0.0.11` as nameserver; host resolvers belong to the host, not to the service-name lookup path. Compare `docker compose exec api cat /etc/resolv.conf` with a healthy container. Expected error `server misbehaving` or a missing embedded nameserver narrows the issue to daemon/network configuration. Docker—Networking

Avoid editing the file in the running container because it disappears on recreate. Record daemon DNS settings, network ID and the exact query. If public names fail while `db` resolves, separate upstream DNS/egress from Compose discovery and test each path independently. (debug-docker-dns-compose-service-name-resolver · outline 3)

Đừng gộp `/etc/resolv.conf` vào một checklist chung. Tạo một phép thử nhỏ có điều kiện, ghi command, exit code, metric và log line riêng cho debug-docker-dns-compose-service-name-resolver. Các marker triệu chứng, lệnh tái hiện, chữ ký lỗi, kiểm tra hồi quy, Service name vs localhost, network inspect, `/etc/resolv.conf` phải xuất hiện ở đúng section để người đọc có thể truy từ claim tới evidence. Khi kết quả không khớp expected (Output của debug-docker-dns-compose-service-name-resolver phải có trạng thái success và error phân biệt bằng exit code/stderr; unknown flag, permission denied hoặc timeout dẫn tới nhánh khoanh vùng riêng.), dừng rollout/restore, giữ hiện trường và mở issue thay vì sửa nhiều biến cùng lúc. Artifact công khai chỉ chứa giá trị đã che và hash kiểm chứng.

Quy trình Docker Compose DNS lỗi: network inspect
Minh họa đặt cạnh phần hướng dẫn tương ứng.

startup race — thao tác và bằng chứng

A startup race occurs when API starts before the database is accepting connections. `depends_on` without a health condition only orders process creation. Add a DB healthcheck (`pg_isready -U app`) and gate the API on `condition: service_healthy`; still keep bounded application retry because a healthy socket does not prove migrations finished. Expected logs show retries followed by a single ready transition. Docker—Networking

Measure the time from container start to DB readiness and set retry/backoff from that baseline. A repeated `connection refused` during the first 30 seconds is different from a permanent `authentication failed`; route each signature to its own fix. Stop the rollout if retries become unbounded or queue depth grows. (debug-docker-dns-compose-service-name-resolver · outline 4)

Trong runbook của debug-docker-dns-compose-service-name-resolver, startup race là điểm quyết định: allow/deny, ready/not-ready, pass/fail hoặc baseline/regression. Định lượng bằng tín hiệu triệu chứng, lệnh tái hiện, chữ ký lỗi, kiểm tra hồi quy, Service name vs localhost, network inspect, `/etc/resolv.conf`, ghi phiên bản và phạm vi áp dụng, rồi nêu ngưỡng chuyển sang rollback. Một output xanh nhưng dependency hoặc health đỏ vẫn là fail. Sau khi sửa, chạy lại cùng lệnh và lưu kết quả cạnh config hash; revoke/rotate credential trước khi dọn mọi bản sao chứa secret.

retry/backoff — thao tác và bằng chứng

Use exponential backoff with a cap (for example 250ms, 500ms, 1s … up to 5s) and a deadline instead of a tight loop. Log attempt count, resolved address and error class, but never credentials. Test by delaying the DB container in staging and verify the API eventually serves `/ready`; expected output includes bounded attempts and a successful readiness timestamp. Docker—Networking

A retry that masks `NXDOMAIN` makes an operator wait for a name that can never resolve. A retry that masks `permission denied` similarly wastes the budget. Keep the original stderr and network inspect output so a rollback can restore the previous hostname/attachment without guessing. (debug-docker-dns-compose-service-name-resolver · outline 5)

Với retry/backoff, ghi lại input và phiên bản CLI trước khi chạy; sau đó đối chiếu từng tín hiệu triệu chứng, lệnh tái hiện, chữ ký lỗi, kiểm tra hồi quy, Service name vs localhost, network inspect, `/etc/resolv.conf` trong stdout, stderr hoặc log audit. Kết quả đạt phải chỉ ra trạng thái và phạm vi tài nguyên; kết quả lỗi phải có chuỗi nhận diện để người trực ca biết bước khoanh vùng kế tiếp. Gắn transcript với debug-docker-dns-compose-service-name-resolver, commit/config hash, owner và timestamp. Không dùng exit code đơn độc làm bằng chứng. Che token, private key, cookie và PII trước khi lưu artifact; giữ bản raw trong kho có quyền hạn nếu incident yêu cầu.

IPv6 — thao tác và bằng chứng

IPv6 can produce a different path from IPv4. Query both address families (`getent ahosts db`) and test the application connection with an explicit family when diagnosing. If AAAA resolves to an unreachable subnet while A works, fix the network/advertised address or client preference; do not disable IPv6 globally as a first response. Expected evidence records A/AAAA results and connect latency. Docker—Networking

Compare resolver answers from two replicas and from the host. An IPv6-only failure with healthy IPv4 points to routing or firewall, not Docker DNS itself. Keep the working replica serving, change one resolver/network setting at a time and roll back if the healthy family also begins timing out. (debug-docker-dns-compose-service-name-resolver · outline 6)

Một lần kiểm tra IPv6 chỉ có giá trị khi lặp lại được. Chốt workload, hostname, digest hoặc schema, rồi chạy lệnh tương ứng với debug-docker-dns-compose-service-name-resolver. Đối chiếu các tín hiệu bắt buộc (triệu chứng, lệnh tái hiện, chữ ký lỗi, kiểm tra hồi quy, Service name vs localhost, network inspect, `/etc/resolv.conf`) với expected output và error signature; nếu thiếu dữ liệu, ghi unknown. So sánh baseline trước/sau trong cùng cửa sổ và nêu rõ điều gì không được suy ra từ phép đo. Artifact thay đổi cần ghi config diff đã redact, người chịu trách nhiệm và thời điểm hết hạn exception trước khi promote.

checklist — thao tác và bằng chứng

The final checklist should run in order: resolve (`getent`), connect (`nc`), authenticate (a redacted client error), request (`curl`/driver health) and dependency readiness. Save command, exit code, timestamp, network ID and one representative log line. This order prevents changing credentials when the packet never reached the DB. Link the runbook to the Compose commit and owner. Docker—Networking

A pass is a repeatable sequence, not one lucky retry: run it twice on the canary and once after a container recreate. If any step regresses, stop promotion, keep the network/log evidence and restore the prior Compose revision. Clean up only after the incident owner confirms no further packet capture or DNS comparison is needed. (debug-docker-dns-compose-service-name-resolver · outline 7)

Đừng gộp checklist vào một checklist chung. Tạo một phép thử nhỏ có điều kiện, ghi command, exit code, metric và log line riêng cho debug-docker-dns-compose-service-name-resolver. Các marker triệu chứng, lệnh tái hiện, chữ ký lỗi, kiểm tra hồi quy, Service name vs localhost, network inspect, `/etc/resolv.conf` phải xuất hiện ở đúng section để người đọc có thể truy từ claim tới evidence. Khi kết quả không khớp expected (Output của debug-docker-dns-compose-service-name-resolver phải có trạng thái success và error phân biệt bằng exit code/stderr; unknown flag, permission denied hoặc timeout dẫn tới nhánh khoanh vùng riêng.), dừng rollout/restore, giữ hiện trường và mở issue thay vì sửa nhiều biến cùng lúc. Artifact công khai chỉ chứa giá trị đã che và hash kiểm chứng.

Recipe lệnh/config cho debug-docker-dns-compose-service-name-resolver

Chạy ở staging cho Docker Compose DNS lỗi với hostname, path và digest đã thay bằng giá trị không nhạy cảm. Lưu exit code cùng stdout/stderr đã redact và gắn artifact với debug-docker-dns-compose-service-name-resolver.

docker compose exec api cat /etc/resolv.conf
docker compose exec api getent hosts db
docker compose exec api sh -c 'nc -vz -w3 db 5432'
docker compose logs --since 5m api db

Expected output và error signature — debug-docker-dns-compose-service-name-resolver

Output của debug-docker-dns-compose-service-name-resolver phải có trạng thái success và error phân biệt bằng exit code/stderr; unknown flag, permission denied hoặc timeout dẫn tới nhánh khoanh vùng riêng. Đối chiếu thêm version, timestamp, health/readiness và log liên quan; output minh họa không phải kết quả production.

Quyết định Docker Compose DNS lỗi: `/etc/resolv.conf`
Bảng quyết định giúp chọn bước tiếp theo có điều kiện.

Rollback và stop boundary của debug-docker-dns-compose-service-name-resolver

Dừng debug-docker-dns-compose-service-name-resolver khi health mất, error signature đổi sang nhánh nặng hơn hoặc chưa tái hiện được trong staging. Giữ hiện trường, khôi phục revision trước và mở issue kèm transcript.

Failure mode cần tránh ở Service name vs localhost

  • Service name vs localhost: thay đổi nhiều biến cùng lúc nên không biết nguyên nhân.
  • Service name vs localhost: đọc exit code nhưng bỏ qua health, dependency hoặc error signature.
  • Service name vs localhost: lưu secret/PII trong log, image layer, backup hoặc ảnh chụp.
  • Service name vs localhost: rollback nhầm failure domain làm blast radius lớn hơn.

Đọc tiếp trong cùng cụm Linux/DevOps — debug-docker-dns-compose-service-name-resolver

docker compose startup order healthcheckcach xem log docker tren linux

Nguồn chính thức cho debug-docker-dns-compose-service-name-resolver

Docker—Networking; Docker—Compose networking

FAQ theo đúng chủ đề Docker Compose DNS lỗi

Service name vs localhost áp dụng cho phiên bản nào?

Kiểm tra version/tool và topology trước khi áp dụng Service name vs localhost. Chạy recipe ở trên trong staging; nếu expected/error signature khác, dừng và cập nhật runbook thay vì copy nguyên lệnh.

network inspect áp dụng cho phiên bản nào?

Kiểm tra version/tool và topology trước khi áp dụng network inspect. Chạy recipe ở trên trong staging; nếu expected/error signature khác, dừng và cập nhật runbook thay vì copy nguyên lệnh.

`/etc/resolv.conf` áp dụng cho phiên bản nào?

Kiểm tra version/tool và topology trước khi áp dụng `/etc/resolv.conf`. Chạy recipe ở trên trong staging; nếu expected/error signature khác, dừng và cập nhật runbook thay vì copy nguyên lệnh.

startup race áp dụng cho phiên bản nào?

Kiểm tra version/tool và topology trước khi áp dụng startup race. Chạy recipe ở trên trong staging; nếu expected/error signature khác, dừng và cập nhật runbook thay vì copy nguyên lệnh.

Cập nhật debug-docker-dns-compose-service-name-resolver: 08/09/2026. Nội dung là runbook tham khảo; phải kiểm tra phiên bản, nguồn chính thức và rollback trong môi trường thật trước khi áp dụng. Safety boundary: Không thay thế tư vấn chuyên môn; kiểm tra tình huống thực tế trước khi áp dụng.

Leave a Comment

Your email address will not be published. Required fields are marked *