Django AWS弹性豆茎给出;无法翻译主机名“;db";解决;错误

Django AWS弹性豆茎给出;无法翻译主机名“;db";解决;错误,django,amazon-web-services,docker,docker-compose,amazon-elastic-beanstalk,Django,Amazon Web Services,Docker,Docker Compose,Amazon Elastic Beanstalk,我一直在尝试部署由Django、Postgresql和Nginx组成的docker。当我这样做的时候,它工作得很好 sudo docker-compose up Dockerfile dockrun.aws.json docker-compose.yml 我意识到的一件事是,当我在我的机器上使用docker compose时,我会运行3个不同的容器。但是在EB上,我只看到一个容器在运行 我想这是因为我从docker hub获取了我用这些文件构建的图像,不知何故导致了这3个容器成为一个,并且它搞乱

我一直在尝试部署由Django、Postgresql和Nginx组成的docker。当我这样做的时候,它工作得很好 sudo docker-compose up Dockerfile

dockrun.aws.json

docker-compose.yml

我意识到的一件事是,当我在我的机器上使用docker compose时,我会运行3个不同的容器。但是在EB上,我只看到一个容器在运行

我想这是因为我从docker hub获取了我用这些文件构建的图像,不知何故导致了这3个容器成为一个,并且它搞乱了主机名的识别?我还是不太确定。非常感谢您的帮助。谢谢

dockrun.aws.json
应与
docker compose.yml
主机名“db”无法转换为地址的原因是
docker compose.yml
dockrun.aws.json
文件描述了不同的体系结构:

  • docker compose.yml中有3个容器
  • dockrun.aws.json中只有1个容器
因此,应用程序尝试解析
db
主机名,但找不到它,因为
db
未在
dockrun.aws.json

修复
dockrun.aws.json
因此,请更新您的
dockrun.aws.json
。您可以手动或使用方便的工具:

a) 手动更新 您可以使用示例,例如:

  • **
b) 使用micahausler/container转换更新它 您可以尝试:

转换docker compose、ECS和Marathon配置 转换docker compose、ECS和Marathon配置

以下是它为您的案例输出的内容:

$container transform docker-compose.yml>dockrun.aws.json
dockrun.aws.json

{
    "containerDefinitions": [
        {
            "essential": true,
            "image": "postgres",
            "name": "db"
        },
        {
            "essential": true,
            "image": "nginx",
            "mountPoints": [
                {
                    "containerPath": "/etc/nginx/conf.d",
                    "sourceVolume": "_ConfigNginx"
                }
            ],
            "name": "nginx",
            "portMappings": [
                {
                    "containerPort": 8000,
                    "hostPort": 8000
                }
            ]
        },
        {
            "essential": true,
            "links": [
                "db:db"
            ],
            "mountPoints": [
                {
                    "containerPath": "/code",
                    "sourceVolume": "_"
                }
            ],
            "name": "web"
        }
    ],
    "family": "",
    "volumes": [
        {
            "host": {
                "sourcePath": "."
            },
            "name": "_"
        },
        {
            "host": {
                "sourcePath": "./config/nginx"
            },
            "name": "_ConfigNginx"
        }
    ]
}
{
    "containerDefinitions": [
        {
            "essential": true,
            "image": "postgres",
            "name": "db"
        },
        {
            "essential": true,
            "image": "nginx",
            "mountPoints": [
                {
                    "containerPath": "/etc/nginx/conf.d",
                    "sourceVolume": "_ConfigNginx"
                }
            ],
            "name": "nginx",
            "portMappings": [
                {
                    "containerPort": 8000,
                    "hostPort": 8000
                }
            ]
        },
        {
            "essential": true,
            "links": [
                "db:db"
            ],
            "mountPoints": [
                {
                    "containerPath": "/code",
                    "sourceVolume": "_"
                }
            ],
            "name": "web"
        }
    ],
    "family": "",
    "volumes": [
        {
            "host": {
                "sourcePath": "."
            },
            "name": "_"
        },
        {
            "host": {
                "sourcePath": "./config/nginx"
            },
            "name": "_ConfigNginx"
        }
    ]
}
注意::当然,您应该修复缺少的设置,例如
db
nginx
容器的
memory

您可以完全忽略
网络
根据:

例如,假设您的应用程序位于名为
myapp
的目录中,并且您的
docker compose.yml
如下所示:

docker compose.yml

version: "3"
services:
  web:
    build: .
    ports:
      - "8000:8000"
  db:
    image: postgres
    ports:
      - "8001:5432"

version: '3'

services:
  db:
    image: postgres
    hostname: db
  web:
    restart: always
    build: .
    volumes:
      - .:/code
    hostname: web
    expose:
      - "8000"
    depends_on:
      - db
    links:
      - db:db
  nginx:
    image: nginx
    hostname: nginx
    ports:
      - "8000:8000"
    volumes:
      - ./config/nginx:/etc/nginx/conf.d
    depends_on:
      - web
运行
docker compose up
时,会发生以下情况:

  • 创建一个名为
    myapp\u default
    的网络
  • 容器是使用
    web
    的配置创建的。它以
    web
    的名称加入网络
    myapp\u default
  • 容器是使用
    db
    的配置创建的。它以
    db
    的名称加入网络
    myapp\u default
  • 因此,由于所有容器都链接到同一个
    网络
    ,因此可以忽略它

    docker compose.yml

    version: "3"
    services:
      web:
        build: .
        ports:
          - "8000:8000"
      db:
        image: postgres
        ports:
          - "8001:5432"
    
    
    version: '3'
    
    services:
      db:
        image: postgres
        hostname: db
      web:
        restart: always
        build: .
        volumes:
          - .:/code
        hostname: web
        expose:
          - "8000"
        depends_on:
          - db
        links:
          - db:db
      nginx:
        image: nginx
        hostname: nginx
        ports:
          - "8000:8000"
        volumes:
          - ./config/nginx:/etc/nginx/conf.d
        depends_on:
          - web
    
    $container transform docker-compose.yml>dockrun.aws.json
    将生成:

    dockrun.aws.json

    {
        "containerDefinitions": [
            {
                "essential": true,
                "image": "postgres",
                "name": "db"
            },
            {
                "essential": true,
                "image": "nginx",
                "mountPoints": [
                    {
                        "containerPath": "/etc/nginx/conf.d",
                        "sourceVolume": "_ConfigNginx"
                    }
                ],
                "name": "nginx",
                "portMappings": [
                    {
                        "containerPort": 8000,
                        "hostPort": 8000
                    }
                ]
            },
            {
                "essential": true,
                "links": [
                    "db:db"
                ],
                "mountPoints": [
                    {
                        "containerPath": "/code",
                        "sourceVolume": "_"
                    }
                ],
                "name": "web"
            }
        ],
        "family": "",
        "volumes": [
            {
                "host": {
                    "sourcePath": "."
                },
                "name": "_"
            },
            {
                "host": {
                    "sourcePath": "./config/nginx"
                },
                "name": "_ConfigNginx"
            }
        ]
    }
    
    {
        "containerDefinitions": [
            {
                "essential": true,
                "image": "postgres",
                "name": "db"
            },
            {
                "essential": true,
                "image": "nginx",
                "mountPoints": [
                    {
                        "containerPath": "/etc/nginx/conf.d",
                        "sourceVolume": "_ConfigNginx"
                    }
                ],
                "name": "nginx",
                "portMappings": [
                    {
                        "containerPort": 8000,
                        "hostPort": 8000
                    }
                ]
            },
            {
                "essential": true,
                "links": [
                    "db:db"
                ],
                "mountPoints": [
                    {
                        "containerPath": "/code",
                        "sourceVolume": "_"
                    }
                ],
                "name": "web"
            }
        ],
        "family": "",
        "volumes": [
            {
                "host": {
                    "sourcePath": "."
                },
                "name": "_"
            },
            {
                "host": {
                    "sourcePath": "./config/nginx"
                },
                "name": "_ConfigNginx"
            }
        ]
    }
    

    您在这两种情况下使用的是同一个docker compose文件吗?非常感谢您的帮助!但是现在我面临着由于以下原因而停止ECS任务的
    错误:任务启动失败。(nginx:CannotCreateContainerError:daemon的错误响应:create./config/nginx:“./config/nginx”包含本地卷名的无效字符,仅“[a-zA-Z0-9]允许使用[a-zA-Z0-9.-]。如果您打算传递主机目录,请使用aws端的绝对路径db:web:)
    。这是nginx.conf问题吗?再次感谢您!!@Jason,像往常一样,nginx config存储在类似
    /etc/nginx/conf
    的地方。对于您的情况,请尝试绝对路径而不是
    /config/nginx
    version: "3"
    services:
      web:
        build: .
        ports:
          - "8000:8000"
      db:
        image: postgres
        ports:
          - "8001:5432"
    
    
    version: '3'
    
    services:
      db:
        image: postgres
        hostname: db
      web:
        restart: always
        build: .
        volumes:
          - .:/code
        hostname: web
        expose:
          - "8000"
        depends_on:
          - db
        links:
          - db:db
      nginx:
        image: nginx
        hostname: nginx
        ports:
          - "8000:8000"
        volumes:
          - ./config/nginx:/etc/nginx/conf.d
        depends_on:
          - web
    
    {
        "containerDefinitions": [
            {
                "essential": true,
                "image": "postgres",
                "name": "db"
            },
            {
                "essential": true,
                "image": "nginx",
                "mountPoints": [
                    {
                        "containerPath": "/etc/nginx/conf.d",
                        "sourceVolume": "_ConfigNginx"
                    }
                ],
                "name": "nginx",
                "portMappings": [
                    {
                        "containerPort": 8000,
                        "hostPort": 8000
                    }
                ]
            },
            {
                "essential": true,
                "links": [
                    "db:db"
                ],
                "mountPoints": [
                    {
                        "containerPath": "/code",
                        "sourceVolume": "_"
                    }
                ],
                "name": "web"
            }
        ],
        "family": "",
        "volumes": [
            {
                "host": {
                    "sourcePath": "."
                },
                "name": "_"
            },
            {
                "host": {
                    "sourcePath": "./config/nginx"
                },
                "name": "_ConfigNginx"
            }
        ]
    }