[FAQ] HW3 with act: the postgres service is "healthy" but the API dies with "connection refused" on localhost:5432
Maintainer thường phản hồi trong vòng 2 ngày
Đánh giá
- Độ khó
- 2/5
- Thời gian dự kiến
- 1-3 giờ
- Mức phù hợp với người mới
- 75/100
- Loại issue
- Tài liệu
- Độ rõ ràng
- Đặc tả rõ ràng
- Mức độ hoạt động
- Sôi nổi
- Công nghệ
- docker, postgresql, python
- Lĩnh vực
- ci-cd, devops, documentation
Hướng nghiên cứu
Issue này nói về việc cấu hình một workflow CI để chạy cục bộ với act. Câu trả lời giải thích sự khác biệt về mạng giữa GitHub Actions và act, đồng thời cung cấp một giải pháp có tham số hóa. Để xác minh, người ta nên kiểm tra tệp workflow trong kho lưu trữ khóa học, hiểu định nghĩa vùng chứa dịch vụ và kiểm tra bản sửa lỗi bằng cách chạy act với biến được cung cấp. Trạng thái 'done' đạt được khi API có thể kết nối với PostgreSQL cả cục bộ với act và trên GitHub Actions.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
Course
ai-dev-tools-zoomcamp
Question
In Homework 3 (module 03) the assignment tells you to run your CI workflow
locally with act. My workflow starts a postgres:16 service container
with a pg_isready healthcheck, and act prints container health … is healthy. But when the job boots the Agent Relay API with
RELAY_DATABASE_URL=postgresql+psycopg://relay:relay@localhost:5432/relay,
it crashes:
text
psycopg.OperationalError: connection failed: connection to server at
"127.0.0.1", port 5432 failed: Connection refused
The identical workflow passes on GitHub Actions. Why does localhost work
there but not under act, and what hostname should I use?
Answer
Because GitHub and act place service containers in different networks:
GitHub Actions publishes service ports on the job runner's loopback, so
localhost:5432 is correct there.
act runs each service as a separate container on a user-defined docker
network shared with the job container. Nothing is published on the job's
loopback; the service is reachable by its service name (the key under
services:), resolved by docker's embedded DNS. Under act the host is
postgres, not localhost. The giveaway is the error text itself: it
retries ::1 and 127.0.0.1 — loopback — while the healthy service sits
one hop away on the act network.
Fix that is correct in BOTH worlds — parameterise the host with a per-runner
default (vars is legal in step env and, unlike env, in job-level if):
YAML
jobs:
test:
services:
postgres:
image: postgres:16
env:
POSTGRES_USER: relay
POSTGRES_PASSWORD: relay
POSTGRES_DB: relay
options: >-
--health-cmd "pg_isready -U relay -d relay"
--health-interval 3s --health-timeout 3s --health-retries 10
steps:
- name: boot api against postgres
env:
RELAY_DATABASE_URL: >-
postgresql+psycopg://relay:relay@${{ vars.DB_HOST || 'localhost' }}:5432/relay
run: |
uv run uvicorn main:app --port 8005 &
...
Locally: act … --var DB_HOST=postgres. On GitHub vars.DB_HOST is unset, the
expression falls back to localhost, and the workflow is unchanged.
Sanity checks: docker network inspect <act-…-test-network> lists both
containers; getent hosts postgres resolves inside the job container; and a
service that is healthy in act's log yet "refused" from the job is always
this networking difference, never a bad healthcheck.
Checklist
- I have searched existing FAQs and this question is not already answered
- The answer provides accurate, helpful information
- I have included any relevant code examples or links
- Ngôn ngữ chính
- Python
- Star
- 7
- Fork
- 20
- Merge trung bình
- 7 ngày 9 giờ
- Pull request đã merge (30 ngày)
- 8
Chuẩn bị môi trường
- Không có Dockerfile hay tệp Docker Compose
- Không có mẫu pull request
- Đọc hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của DataTalksClub/faq
-
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 85/100
DataTalksClub/faq#518 ·
Maintainer thường phản hồi trong vòng 2 ngày
-
[FAQ] Commit ID for ProjectsCó thể đã có người làm @github-actions đã nhận 1 ngày trước. Đang mởfaq-proposal
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 84/100
DataTalksClub/faq#503 · 1 bình luận ·
Maintainer thường phản hồi trong vòng 2 ngày
-
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 82/100
DataTalksClub/faq#486 ·
Maintainer thường phản hồi trong vòng 2 ngày
-
[FAQ] Docker Compose: Why Aren't My Environment Variables Being Loaded?Có thể đã có người làm @github-actions đã nhận 1 ngày trước. Đang mởfaq-proposal
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 20/100
DataTalksClub/faq#516 · 1 bình luận ·
Maintainer thường phản hồi trong vòng 2 ngày
-
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 35/100
DataTalksClub/faq#515 ·
Maintainer thường phản hồi trong vòng 2 ngày
Tất cả issue của DataTalksClub/faq
Issue tương tự
-
parse names a key after a top-level multi() threshold Bitcoin Core v31.1 refuses as not validĐang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
btclib-org/btclib-wallet#587 · 1 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
-
documentation
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 68/100
Judgment-Pack/judgment-pack-spec#275 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
ai-generated
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 77/100
Maintainer thường phản hồi trong vòng 1 ngày
-
documentation good first issue priority: low size: XS
Độ khó 1/5 1-3 giờ Mức phù hợp với người mới 92/100
yeongseon/azure-functions-doctor-python#651 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 65/100
Changaco/python-libarchive-c#146 ·