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

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
    ...
时,效果很好

Docker文件
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个容器成为一个容器,并且搞砸了主机名的识别?我还是不太确定。帮助将不胜感激。谢谢!

最佳答案

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)手动更新

    您可以使用示例,例如:
  • k2works/aws-eb-docker-multi-container-sample **

  • b)使用micahhausler/container-transform更新它

    您可以尝试micahhausler/container-transform:



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

    $ 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:



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



    因此,由于所有容器都链接到相同的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"
            }
        ]
    }
    

    08-27 23:50
    查看更多