AWS Elastic Beanstalk 提供“无法翻译主机名"db"解决"错误 [英] AWS Elastic Beanstalk gives "could not translate host name "db" to address" error

查看:21
本文介绍了AWS Elastic Beanstalk 提供“无法翻译主机名"db"解决"错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试部署由 Django、Postgresql 和 Nginx 组成的 docker.当我执行

sudo docker-compose up

时它工作正常但是当在 AWS EB 上部署它时,它给了我

无法将主机名db"转换为地址:名称或服务未知

我所做的是使用

sudo docker build -t myname/dockername -f Dockerfile 将我的 docker 推送到 docker 集线器.

我只是做

eb deploy

文件结构

我的项目我的项目设置.py网址.py...文件Dockerrun.aws.json管理文件要求.txt...

Dockerfile

FROM python:3环境 PYTHONUNBUFFERED 1运行 mkdir/code工作目录/代码复制需求.txt/code/运行 pip install -r requirements.txt复制 ./代码/曝光 8000CMD ["sh", "on-container-start.sh"]

Dockerrun.aws.json

<代码>{"AWSEBDockerrunVersion": "1",图片": {"Name": "我的名字/dockername:latest",更新":真实"},端口":[{ContainerPort":8000"}]}

docker-compose.yml

版本:'3'服务:D b:图片:postgres主机名:db网络:- some_network网络:重启:总是建造: .卷:- .:/代码主机名:网络暴露:- 8000"依赖于取决于:- D b链接:- 数据库:数据库网络:- some_networknginx:图片:nginx主机名:nginx端口:- 8000:8000"卷:- ./config/nginx:/etc/nginx/conf.d依赖于取决于:- 网络网络:- some_network网络:some_network:

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

我认为这是因为我从使用这些文件构建的 docker hub 获取图像,并且以某种方式导致这 3 个容器合二为一,并且无法识别主机名?我还不确定.帮助将不胜感激.谢谢!

解决方案

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<中声明/p>

修复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

<代码>{容器定义":[{基本":真实,"image": "postgres",名称":数据库"},{基本":真实,图像":nginx",挂载点":[{"containerPath": "/etc/nginx/conf.d","sourceVolume": "_ConfigNginx"}],"name": "nginx",端口映射":[{容器端口":8000,主机端口":8000}]},{基本":真实,链接":[数据库:数据库"],挂载点":[{"containerPath": "/code",源卷":_"}],名称":网络"}],家庭": "",卷":[{主持人": {源路径":."},名称": "_"},{主持人": {"sourcePath": "./config/nginx"},"name": "_ConfigNginx"}]}

注意::当然,您应该为dbnginx 容器修复缺失的设置,例如memory.

你可以完全省略networks

根据 Compose 中的网络 |Docker 文档:

<块引用>

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

docker-compose.yml

版本:3"服务:网络:建造: .端口:- 8000:8000"D b:图片:postgres端口:- 8001:5432"

<块引用>

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

  1. 创建了一个名为 myapp_default 的网络.
  2. 容器是使用 web 的配置创建的.它以 web 的名义加入网络 myapp_default.
  3. 容器是使用 db 的配置创建的.它以 db 的名义加入网络 myapp_default.

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

docker-compose.yml

版本:'3'服务:D b:图片:postgres主机名:db网络:重启:总是建造: .卷:- .:/代码主机名:网络暴露:- 8000"依赖于取决于:- D b链接:- 数据库:数据库nginx:图片:nginx主机名:nginx端口:- 8000:8000"卷:- ./config/nginx:/etc/nginx/conf.d依赖于取决于:- 网络

$ container-transform docker-compose.yml >Dockerrun.aws.json 将产生:

Dockerrun.aws.json

<代码>{容器定义":[{基本":真实,"image": "postgres",名称":数据库"},{基本":真实,图像":nginx",挂载点":[{"containerPath": "/etc/nginx/conf.d","sourceVolume": "_ConfigNginx"}],"name": "nginx",端口映射":[{容器端口":8000,主机端口":8000}]},{基本":真实,链接":[数据库:数据库"],挂载点":[{"containerPath": "/code",源卷":_"}],名称":网络"}],家庭": "",卷":[{主持人": {源路径":."},名称": "_"},{主持人": {"sourcePath": "./config/nginx"},"name": "_ConfigNginx"}]}

I've been trying to deploy my docker consisted of Django, Postgresql and Nginx. It works fine when I do

sudo docker-compose up

However when deploy it on AWS EB, it gives me

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

What I've done is I pushed my docker to docker hub using

sudo docker build -t myname/dockername -f Dockerfile .

and I simply do

eb deploy

File Structure

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:

One thing I realize is that when I use docker-compose up on my machine, I get 3 different containers running. However on EB, I see only one container running.

I think it's because I'm fetching the image from docker hub that I built with those files and that somehow caused these 3 containers to be one and it's messing up with recognizing host names? I am quite not sure still. Help will be greatly appreciated. Thanks!

解决方案

Dockerrun.aws.json should correlate with docker-compose.yml

The reason of issue that host name "db" could not be translated to address is that the docker-compose.yml and Dockerrun.aws.json files describe a different architecture:

  • There are 3 containers in docker-compose.yml
  • There is only 1 container in Dockerrun.aws.json

Therefore, the application tries to resolve the db hostname and cannot find it, because db not declared in Dockerrun.aws.json

Fix Dockerrun.aws.json

So, update your Dockerrun.aws.json. You can do it either manually or using convenient tool micahhausler/container-transform:

a) update it manually

You can use samples, such as:

b) update it using micahhausler/container-transform

You can try micahhausler/container-transform:

Transforms docker-compose, ECS, and Marathon configurations Transforms docker-compose, ECS, and Marathon configurations

Here is what it outputs for your case:

$ 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"
        }
    ]
}

Note:: Of course, you should fix missing settings such as memory for db and nginx containers.

You can omit networks at all

According to Networking in Compose | Docker Documentation:

For example, suppose your app is in a directory called myapp, and your docker-compose.yml looks like this:

docker-compose.yml

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

When you run docker-compose up, the following happens:

  1. A network called myapp_default is created.
  2. A container is created using web’s configuration. It joins the network myapp_default under the name web.
  3. A container is created using db’s configuration. It joins the network myapp_default under the name db.

So, since all your containers linked to the same some_network, you can omit it.

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

And $ container-transform docker-compose.yml > Dockerrun.aws.json will produce:

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

这篇关于AWS Elastic Beanstalk 提供“无法翻译主机名"db"解决"错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆