AWS Elastic Beanstalk给出“无法将主机名“ db”转换为地址”错误

时间:2019-08-28 04:06:30

标签: django amazon-web-services docker docker-compose amazon-elastic-beanstalk

我一直在尝试部署由Django,Postgresql和Nginx组成的docker。

sudo docker-compose up
可以很好地工作,但是在AWS EB上部署它时,它会给我

could not translate host name "db" to address: Name or service not known

我所做的是我使用

sudo docker build -t myname/dockername -f Dockerfile .
将docker推送到docker hub,我只是做了
eb deploy

文件结构

myproject
    myproject
        settings.py
        urls.py
        ...
    Dockerfile
    Dockerrun.aws.json
    manage.py
    requirements.txt
    ...

Dockerfile

FROM python:3
ENV PYTHONUNBUFFERED 1
RUN mkdir /code
WORKDIR /code
COPY requirements.txt /code/
RUN pip install -r requirements.txt
COPY . /code/
EXPOSE 8000
CMD ["sh", "on-container-start.sh"]

Dockerrun.aws.json

{
 "AWSEBDockerrunVersion": "1",
 "Image": {
   "Name": "myname/dockername:latest",
   "Update": "true"
 },
 "Ports": [
   {
     "ContainerPort": "8000"
   }
 ]
}

docker-compose.yml

version: '3'

services:
  db:
    image: postgres
    hostname: db
    networks:
     - some_network
  web:
    restart: always
    build: .
    volumes:
      - .:/code
    hostname: web
    expose:
      - "8000"
    depends_on:
      - db
    links:
      - db:db
    networks:
      - some_network
  nginx:
    image: nginx
    hostname: nginx
    ports:
      - "8000:8000"
    volumes:
      - ./config/nginx:/etc/nginx/conf.d
    depends_on:
      - web
    networks:
      - some_network
networks:
  some_network:

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

我认为这是因为我正在从使用这些文件构建的docker hub中获取图像,并且以某种方式导致这3个容器成为一个容器,并且搞乱了主机名的识别?我还是不太确定。帮助将不胜感激。谢谢!

1 个答案:

答案 0 :(得分:4)

Dockerrun.aws.json应该与docker-compose.yml

相关

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

  • docker-compose.yml中有3个容器
  • Dockerrun.aws.json中只有1个容器

因此,应用程序尝试解析db主机名,但找不到它,因为db中未声明Dockerrun.aws.json

修复Dockerrun.aws.json

因此,更新您的Dockerrun.aws.json。您可以手动执行此操作,也可以使用便捷的工具micahhausler/container-transform

a)手动更新

您可以使用示例,例如:

b)使用micahhausler/container-transform

更新

您可以尝试micahhausler/container-transform

  

转换docker-compose,ECS和Marathon配置   转换docker-compose,ECS和Marathon配置

这是您的案例输出的结果:

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

Dockerrun.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"
        }
    ]
}

注意::当然,您应该修复memorydb容器缺少的设置,例如nginx

您可以完全省略networks

根据Networking in Compose | Docker Documentation

  

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

docker-compose.yml

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

  

运行docker-compose up时,会发生以下情况:

     
      
  1. 创建了一个名为myapp_default的网络。
  2.   
  3. 使用web的配置创建了一个容器。它以名称myapp_default加入网络web
  4.   
  5. 使用db的配置创建了一个容器。它以名称myapp_default加入网络db
  6.   

因此,由于所有容器都链接到相同的some_network,因此您可以省略它。

docker-compose.yml

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 > Dockerrun.aws.json将产生:

Dockerrun.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"
        }
    ]
}