Docker container port binding issue in GitHub Actions CI/CD

I’m facing a problem with port connectivity when running Docker containers in GitHub Actions. My configuration works flawlessly on local machines but encounters issues during the CI pipeline execution.

The error message I’m receiving is:

requests.exceptions.ConnectionError: HTTPConnectionPool(host='localhost', port=6789): Max retries exceeded with url: /api/v1/users (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x7f3d28e3d810>: Failed to establish a new connection: [Errno 111] Connection refused'))

This is my Docker Compose setup:

workflow_service:
  image: docker.n8n.io/n8nio/n8n
  ports:
    - "6789:5678"
  environment:
    - N8N_HOST=workflow_service:5678
    - N8N_PORT=5678
    - N8N_PROTOCOL=http
    - NODE_ENV=production
    - DB_TYPE=postgresdb
    - DB_TABLE_PREFIX=workflow_
    - DB_POSTGRESDB_DATABASE=workflow_db
  volumes:
    - ./config/workflow/data:/home/node/.n8n
    - ./config/workflow/files:/files

I’m trying to reach this service from a Django application using Python’s requests library. The configuration is consistent between local and CI environments.

I’ve verified with minimal curl commands that while other services on different ports are functioning correctly, this particular service consistently fails with connection refused errors in GitHub Actions.

I encountered a similar issue with GitHub Actions where the Docker container would start, but the service was not fully initialized when my tests tried to access it. To resolve this, I implemented a retry mechanism with a delay, allowing the service to become ready before making requests. Additionally, ensure your Django application and the workflow service are within the same Docker network; otherwise, using ‘localhost’ won’t be effective. Lastly, verify that the necessary ports are not blocked by the runner or occupied by other processes.

GitHub Actions runners have networking quirks that cause this exact issue. The container reports as “running” but the service inside hasn’t bound to the port yet - I’ve hit this before. Add a wait condition before making requests. Either throw in a sleep 30 or better yet, poll until you get a 200 status back. Also check if your workflow has network restrictions blocking container-to-container communication.

Check if your Django app’s using the right hostname - in Docker Compose you need to connect to workflow_service:5678, not localhost:6789. Inside a container, localhost points to the container itself, not your host machine. Also try adding a healthcheck to your compose file so Django waits until n8n’s actually ready before trying to connect.