You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

在AWS Elastic Beanstalk的Docker Compose中Django连PostgreSQL失败排查

Fixing PostgreSQL Hostname Resolution Error in Elastic Beanstalk Multi-Container Apps

Hey there, let's break down why you're hitting that could not translate host name "db" to address error when deploying to Elastic Beanstalk (EB), even though everything works locally with docker-compose up. The core issue boils down to how EB handles container networking vs. your local Docker setup, plus a few configuration gaps in your deployment setup. Here's what you need to fix:

1. You're Missing EB's Required Multi-Container Config: Dockerrun.aws.json

Local Docker Compose automatically creates a shared network where containers can resolve each other by service name (like "db"). But EB's multi-container environment doesn't use docker-compose.yml directly—you need a Dockerrun.aws.json file to define your services, networks, and environment variables for EB's ECS-backed runtime. This is almost certainly why your web container can't find the "db" host.

Example Dockerrun.aws.json for Your Setup

{
  "AWSEBDockerrunVersion": 2,
  "volumes": [
    {
      "name": "postgres_data",
      "host": {
        "sourcePath": "/var/app/current/postgres_data"
      }
    }
  ],
  "containerDefinitions": [
    {
      "name": "db",
      "image": "postgres:10.1",
      "environment": [
        {
          "name": "POSTGRES_USER",
          "value": "rdeng"
        },
        {
          "name": "POSTGRES_PASSWORD",
          "value": "walterfedy"
        },
        {
          "name": "POSTGRES_DB",
          "value": "db1"
        }
      ],
      "portMappings": [
        {
          "hostPort": 5432,
          "containerPort": 5432
        }
      ],
      "mountPoints": [
        {
          "sourceVolume": "postgres_data",
          "containerPath": "/var/lib/postgresql/data/"
        }
      ]
    },
    {
      "name": "web",
      "build": ".",
      "environment": [
        {
          "name": "RDS_DB_NAME",
          "value": "db1"
        },
        {
          "name": "RDS_USERNAME",
          "value": "rdeng"
        },
        {
          "name": "RDS_HOSTNAME",
          "value": "db"
        },
        {
          "name": "RDS_PASSWORD",
          "value": "walterfedy"
        },
        {
          "name": "RDS_PORT",
          "value": "5432"
        }
      ],
      "portMappings": [
        {
          "hostPort": 8000,
          "containerPort": 8000
        }
      ],
      "dependsOn": [
        "db"
      ],
      "essential": true
    }
  ]
}

This file tells EB to:

  • Create a shared volume for PostgreSQL data persistence
  • Launch both the db and web containers in the same default network (so service name resolution works)
  • Pass the exact environment variables your Django app expects

2. Double-Check Environment Variables in EB

Your settings.py uses the RDS_DB_NAME env var to switch between local and EB-compatible DB configs. Make sure these variables are actually available to your web container in EB:

  • If you use the Dockerrun.aws.json above, the variables are already defined
  • For better security (avoiding hardcoding passwords in files), you can add these variables via the EB Console: Configuration > Software > Environment Properties

3. Fix the Typo in Your Docker Compose Command

I noticed a small but critical typo in your web service's command:

bash -c "python /code/manage.py makemigrations && python /code/manage.py migrate && python /code/manage.py runserver 0.0$"

The 0.0$ should be 0.0.0.0:8000—otherwise Django will only bind to the container's localhost, making it unreachable from outside. Corrected command:

bash -c "python /code/manage.py makemigrations && python /code/manage.py migrate && python /code/manage.py runserver 0.0.0.0:8000"

The depends_on flag ensures the db container starts first, but not that PostgreSQL is actually ready to accept connections. To avoid race conditions, add a wait script to your web container's startup command:

First, install the PostgreSQL client in your Dockerfile:

FROM python:3.6
ENV PYTHONUNBUFFERED 1
ENV POSTGRES_DB db1
ENV POSTGRES_USER rdeng
ENV POSTGRES_PASSWORD walterfedy
COPY . /code/
WORKDIR /code/
RUN apt-get update && apt-get install -y postgresql-client  # Add this line
RUN pip install -r /code/requirements.txt
COPY ./back.sql /code/docker-entrypoint-initdb.d/back.sql
EXPOSE 8000

Then update the command to wait for PostgreSQL:

bash -c "until pg_isready -h db -p 5432 -U rdeng; do sleep 2; done && python /code/manage.py makemigrations && python /code/manage.py migrate && python /code/manage.py runserver 0.0.0.0:8000"

Step-by-Step Fix Checklist

  1. Create the Dockerrun.aws.json file and add it to your project root
  2. Fix the typo in your docker-compose.yml's web service command
  3. (Optional) Add the PostgreSQL client and wait logic to your Dockerfile/startup command
  4. Redeploy your app to Elastic Beanstalk

内容的提问来源于stack exchange,提问作者reddd__1

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 07:02:57