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

Amazon Linux 2实例ECS任务中Docker容器Bash重定向失败

问题:ECS任务中Bash等待PostgreSQL就绪失败

我在运行包含PostgreSQL、Redis和私有ECR镜像的ECS任务时,为自定义镜像的容器定义添加了一段Bash命令,用于等待PostgreSQL就绪:

"command": [
    "/bin/bash",
    "-c",
    "while !</dev/tcp/postgres/5432; do echo \"Waiting for postgres database to start...\"; /bin/sleep 1; done; /bin/sh /app/start-server.sh;"
],

本地通过docker-compose运行完全正常,但在Amazon Linux 2的EC2实例上执行该while循环时,出现以下错误:

/bin/bash: line 1: postgres: Name or service not known
/bin/bash: line 1: /dev/tcp/postgres/5432: Invalid argument

PostgreSQL容器本身运行无报错,日志最后显示:

database system is ready to accept connections

我不确定这是Docker网络问题,还是Amazon Linux 2的Bash未编译--enable-net-redirections参数导致的。


任务定义

{
    "networkMode": "bridge",
    "containerDefinitions": [
        {
            "environment": [
                {
                    "name": "POSTGRES_DB",
                    "value": "metadeploy"
                },
                {
                    "name": "POSTGRES_USER",
                    "value": "<redacted>"
                },
                {
                    "name": "POSTGRES_PASSWORD",
                    "value": "<redacted>"
                }
            ],
            "essential": true,
            "image": "postgres:12.9",
            "mountPoints": [],
            "name": "postgres",
            "memory": 1024,
            "logConfiguration": {
                "logDriver": "awslogs",
                "options": {
                    "awslogs-group": "metadeploy-postgres",
                    "awslogs-region": "us-east-1",
                    "awslogs-create-group": "true",
                    "awslogs-stream-prefix": "mdp"
                }
            }
        },
        {
            "essential": true,
            "image": "redis:6.2",
            "name": "redis",
            "memory": 1024
        },
        {
        "command": [
            "/bin/bash",
            "-c",
            "while !</dev/tcp/postgres/5432; do echo \"Waiting for postgres database to start...\"; /bin/sleep 1; done; /bin/sh /app/start-server.sh;"
        ],
            "environment": [
                {
                    "name": "DJANGO_SETTINGS_MODULE",
                    "value": "config.settings.local"
                },
                {
                    "name": "DATABASE_URL",
                    "value": "<redacted-postgres-url>"
                },
                {
                    "name": "REDIS_URL",
                    "value": "redis://redis:6379"
                },
                {
                    "name": "REDIS_HOST",
                    "value": "redis"
                }
            ],
            "essential": true,
            "image": "the private ecr image uri built from here https://github.com/SFDO-Tooling/MetaDeploy",
            "links": [
                "redis"
            ],
            "mountPoints": [
                {
                    "containerPath": "/app/node_modules",
                    "sourceVolume": "AppNode_Modules"
                }
            ],
            "name": "web",
            "portMappings": [
                {
                    "containerPort": 8080,
                    "hostPort": 8080
                },
                {
                    "containerPort": 8000,
                    "hostPort": 8000
                },
                {
                    "containerPort": 6006,
                    "hostPort": 6006
                }
            ],
            "memory": 1024,
            "logConfiguration": {
                "logDriver": "awslogs",
                "options": {
                    "awslogs-group": "metadeploy-web",
                    "awslogs-region": "us-east-1",
                    "awslogs-create-group": "true",
                    "awslogs-stream-prefix": "mdw"
                }
            }
        }
    ],
    "family": "MetaDeploy",
    "volumes": [
        {
            "host": {
                "sourcePath": "/app/node_modules"
            },
            "name": "AppNode_Modules"
        }
    ]
}

docker-compose.yml

version: '3'

services:
  postgres:
    environment:
      POSTGRES_DB: metadeploy
      POSTGRES_USER: postgres
      POSTGRES_PASSWORD: sample_db_password
    volumes:
      - ./postgres:/var/lib/postgresql/data:delegated
    image: postgres:12.9
    restart: always

  redis:
    image: redis:6.2

  web:
    build:
      context: .
      dockerfile: Dockerfile
    command: |
      /bin/bash -c 'while !</dev/tcp/postgres/5432; do echo "Waiting for postgres database to start..."; /bin/sleep 1; done; \
      /bin/sh /app/start-server.sh;'
    ports:
      - '8080:8080'
      - '8000:8000'
      # Storybook server
      - '6006:6006'
    stdin_open: true
    tty: true
    depends_on:
      - postgres
      - redis
    links:
      - redis
    environment:
      DJANGO_SETTINGS_MODULE: config.settings.local
      DATABASE_URL: postgres://postgres:sample_db_password@postgres:5432/metadeploy
      REDIS_URL: redis://redis:6379
      REDIS_HOST: redis
    volumes:
      - .:/app:cached
      - /app/node_modules

请问我是否需要重新编译Bash以启用--enable-net-redirections?如果需要,该如何操作?

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.07 18:40:56