nginx部署到ECS时无法解析容器名称,在本地工作

问题描述 投票:0回答:2

我有一个正在启动的 ECS 配置。然而,nginx

frontend
容器失败了:

nginx: [emerg] host not found in upstream “backend”

这在本地工作正常,但 ECS 似乎无法解析 docker 容器名称。

我正在使用

ecs-cli
进行部署。启动类型为
EC2
。网络模式是
bridge

命令:

ecs-cli compose \
--cluster mycluster \
--file docker-compose.yml \
--ecs-params ecs-params.yml service up \
--deployment-min-healthy-percent=50 --force-deployment \
--target-groups targetGroupArn=<load-balancer>,containerName=frontend,containerPort=80 \
--health-check-grace-period 60 \
--role <my-role> \
--timeout 30

ecs-params.yml

task_definition:
  task_role_arn: <my-arn>
  task_execution_role: <my-exec-role>
  services:
    backend:
      essential: true
      mem_reservation: 1024m
    frontend:
      essential: true
      mem_reservation: 1024m

nginx.conf

events {
  worker_connections 1024;
}

http {
  server_tokens off;
  upstream backend_server {
        server backend:8001;
    }

  server {
    listen 80;
    listen [::]:80;

    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
        try_files $uri $uri/ /index.html;
    }

    location /api {
      proxy_pass      http://backend_server/api;
    }

  }
}

docker-compose

version: '3'

services:
  backend:
    image: <backend-image>
    ports:
      - 8001:8001

  frontend:
    image: <frontend-image, built locally with the nginx conf>
    ports:
      - 80:80

任务定义:

{
  "ipcMode": null,
  "executionRoleArn": <exec-role>,
  "containerDefinitions": [
    {
      "dnsSearchDomains": [],
      "environmentFiles": null,
      "logConfiguration": {
        "logDriver": "awslogs",
        "secretOptions": null,
        "options": { <log-options>
        }
      },
      "entryPoint": [],
      "portMappings": [
        {
          "hostPort": 8001,
          "protocol": "tcp",
          "containerPort": 8001
        }
      ],
      "command": [],
      "linuxParameters": {
        "capabilities": {
          "add": null,
          "drop": null
        },
        "sharedMemorySize": null,
        "tmpfs": null,
        "devices": [],
        "maxSwap": null,
        "swappiness": null,
        "initProcessEnabled": null
      },
      "cpu": 0,
      "environment": [
      ],
      "resourceRequirements": null,
      "ulimits": null,
      "dnsServers": [],
      "mountPoints": [],
      "workingDirectory": null,
      "secrets": null,
      "dockerSecurityOptions": [],
      "memory": null,
      "memoryReservation": 1024,
      "volumesFrom": [],
      "stopTimeout": null,
      "image": <backend-image>,
      "startTimeout": null,
      "firelensConfiguration": null,
      "dependsOn": null,
      "disableNetworking": null,
      "interactive": null,
      "healthCheck": null,
      "essential": true,
      "links": [],
      "hostname": null,
      "extraHosts": [],
      "pseudoTerminal": false,
      "user": null,
      "readonlyRootFilesystem": false,
      "dockerLabels": null,
      "systemControls": null,
      "privileged": false,
      "name": "backend"
    },
    {
      "dnsSearchDomains": [],
      "environmentFiles": null,
      "logConfiguration": {
        "logDriver": "awslogs",
        "secretOptions": null,
        "options": { <log-options>
        }
      },
      "entryPoint": [],
      "portMappings": [
        {
          "hostPort": 80,
          "protocol": "tcp",
          "containerPort": 80
        }
      ],
      "command": [],
      "linuxParameters": {
        "capabilities": {
          "add": null,
          "drop": null
        },
        "sharedMemorySize": null,
        "tmpfs": null,
        "devices": [],
        "maxSwap": null,
        "swappiness": null,
        "initProcessEnabled": null
      },
      "cpu": 0,
      "environment": [],
      "resourceRequirements": null,
      "ulimits": null,
      "dnsServers": [],
      "mountPoints": [],
      "workingDirectory": null,
      "secrets": null,
      "dockerSecurityOptions": [],
      "memory": null,
      "memoryReservation": 1024,
      "volumesFrom": [],
      "stopTimeout": null,
      "image": <frontend-image>,
      "startTimeout": null,
      "firelensConfiguration": null,
      "dependsOn": null,
      "disableNetworking": null,
      "interactive": null,
      "healthCheck": null,
      "essential": true,
      "links": [],
      "hostname": null,
      "extraHosts": [],
      "pseudoTerminal": false,
      "user": null,
      "readonlyRootFilesystem": false,
      "dockerLabels": null,
      "systemControls": null,
      "privileged": false,
      "name": "frontend"
    }
  ],
  "placementConstraints": [],
  "memory": null,
  "taskRoleArn": <task-role-arn>,
  "compatibilities": [
    "EXTERNAL",
    "EC2"
  ],
  "taskDefinitionArn": <definition>,
  "family": "<my-family>",
  "requiresAttributes": [
    {
      "targetId": null,
      "targetType": null,
      "value": null,
      "name": "com.amazonaws.ecs.capability.logging-driver.awslogs"
    },
    {
      "targetId": null,
      "targetType": null,
      "value": null,
      "name": "ecs.capability.execution-role-awslogs"
    },
    {
      "targetId": null,
      "targetType": null,
      "value": null,
      "name": "com.amazonaws.ecs.capability.ecr-auth"
    },
    {
      "targetId": null,
      "targetType": null,
      "value": null,
      "name": "com.amazonaws.ecs.capability.docker-remote-api.1.19"
    },
    {
      "targetId": null,
      "targetType": null,
      "value": null,
      "name": "com.amazonaws.ecs.capability.docker-remote-api.1.17"
    },
    {
      "targetId": null,
      "targetType": null,
      "value": null,
      "name": "com.amazonaws.ecs.capability.docker-remote-api.1.21"
    },
    {
      "targetId": null,
      "targetType": null,
      "value": null,
      "name": "com.amazonaws.ecs.capability.task-iam-role"
    },
    {
      "targetId": null,
      "targetType": null,
      "value": null,
      "name": "ecs.capability.execution-role-ecr-pull"
    }
  ],
  "pidMode": null,
  "requiresCompatibilities": [],
  "networkMode": null,
  "runtimePlatform": null,
  "cpu": null,
  "revision": 75,
  "status": "ACTIVE",
  "inferenceAccelerators": null,
  "proxyConfiguration": null,
  "volumes": []
}
amazon-web-services docker nginx docker-compose amazon-ecs
2个回答
1
投票

@MarkB 是正确的。我需要添加链接。做到这一点的方法是通过

docker-compose.yml
:

version: '3'

services:
  backend:
    image: <backend-image>
    ports:
      - 8001:8001

  frontend:
    image: <frontend-image, built locally with the nginx conf>
    links: ["backend"]
    ports:
      - 80:80

我还向

ecs-params.yml
添加了运行状况检查,以确保后端在前端之前启动。

出现这种情况是因为ECS网桥是默认网桥。这不允许容器通过其名称进行解析(因此有链接)。我原以为这是一个用户定义的桥。更多信息可以在Dockers文档中找到。


0
投票

我没明白,你在 docker compose 中放置了链接,但是如何在任务定义中做到这一点?我也面临着同样的问题。

© www.soinside.com 2019 - 2024. All rights reserved.