Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nodejs app not connecting to redis in amazon ecs task definition. getaddrinfo ENOTFOUND redis redis:6379

I am trying to create a task definition for a nodejs app connected to redis. I have tried several different task definitions but when I run them in my cluster they all fail, because my app cannot connect to redis. My task definition, log outputs, docker-compose.yml that I am basing my task def off of and my api dockerfile are listed below.

task def

{
  "ipcMode": null,
  "executionRoleArn": "arn:aws:iam::210939474461:role/ecsTaskExecutionRole",
  "containerDefinitions": [
    {
      "dnsSearchDomains": null,
      "logConfiguration": {
        "logDriver": "awslogs",
        "secretOptions": null,
        "options": {
          "awslogs-group": "/ecs/rest-api-explorer",
          "awslogs-region": "us-west-2",
          "awslogs-stream-prefix": "ecs"
        }
      },
      "entryPoint": null,
      "portMappings": [
        {
          "hostPort": 0,
          "protocol": "tcp",
          "containerPort": 3000
        },
        {
          "hostPort": 0,
          "protocol": "tcp",
          "containerPort": 6379
        }
      ],
      "command": null,
      "linuxParameters": null,
      "cpu": 0,
      "environment": [
        {
          "name": "redis_server_addr",
          "value": "redis"
        }
      ],
      "resourceRequirements": null,
      "ulimits": null,
      "dnsServers": null,
      "mountPoints": [],
      "workingDirectory": null,
      "secrets": null,
      "dockerSecurityOptions": null,
      "memory": null,
      "memoryReservation": 128,
      "volumesFrom": [],
      "stopTimeout": null,
      "image": "210939474461.dkr.ecr.us-west-2.amazonaws.com/rest-api-explorer",
      "startTimeout": null,
      "dependsOn": [
        {
          "containerName": "redis",
          "condition": "START"
        }
      ],
      "disableNetworking": null,
      "interactive": null,
      "healthCheck": null,
      "essential": true,
      "links": null,
      "hostname": null,
      "extraHosts": null,
      "pseudoTerminal": null,
      "user": null,
      "readonlyRootFilesystem": null,
      "dockerLabels": null,
      "systemControls": null,
      "privileged": null,
      "name": "api-rest-explorer"
    },
    {
      "dnsSearchDomains": null,
      "logConfiguration": {
        "logDriver": "awslogs",
        "secretOptions": null,
        "options": {
          "awslogs-group": "/ecs/rest-api-explorer",
          "awslogs-region": "us-west-2",
          "awslogs-stream-prefix": "ecs"
        }
      },
      "entryPoint": null,
      "portMappings": [
        {
          "hostPort": 0,
          "protocol": "tcp",
          "containerPort": 6379
        }
      ],
      "command": null,
      "linuxParameters": null,
      "cpu": 0,
      "environment": [],
      "resourceRequirements": null,
      "ulimits": null,
      "dnsServers": null,
      "mountPoints": [],
      "workingDirectory": null,
      "secrets": null,
      "dockerSecurityOptions": null,
      "memory": null,
      "memoryReservation": null,
      "volumesFrom": [],
      "stopTimeout": null,
      "image": "redis",
      "startTimeout": null,
      "dependsOn": null,
      "disableNetworking": null,
      "interactive": null,
      "healthCheck": null,
      "essential": true,
      "links": null,
      "hostname": null,
      "extraHosts": null,
      "pseudoTerminal": null,
      "user": null,
      "readonlyRootFilesystem": null,
      "dockerLabels": null,
      "systemControls": null,
      "privileged": null,
      "name": "redis"
    }
  ],
  "placementConstraints": [],
  "memory": "1024",
  "taskRoleArn": "arn:aws:iam::210939474461:role/ecsTaskExecutionRole",
  "compatibilities": [
    "EC2"
  ],
  "taskDefinitionArn": "arn:aws:ecs:us-west-2:210939474461:task-definition/rest-api-explorer:8",
  "family": "rest-api-explorer",
  "requiresAttributes": [
    {
      "targetId": null,
      "targetType": null,
      "value": null,
      "name": "ecs.capability.container-ordering"
    },
    {
      "targetId": null,
      "targetType": null,
      "value": null,
      "name": "ecs.capability.execution-role-ecr-pull"
    },
    {
      "targetId": null,
      "targetType": null,
      "value": null,
      "name": "com.amazonaws.ecs.capability.ecr-auth"
    },
    {
      "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-awslogs"
    },
    {
      "targetId": null,
      "targetType": null,
      "value": null,
      "name": "com.amazonaws.ecs.capability.logging-driver.awslogs"
    },
    {
      "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.docker-remote-api.1.19"
    }
  ],
  "pidMode": null,
  "requiresCompatibilities": [
    "EC2"
  ],
  "networkMode": "bridge",
  "cpu": "128",
  "revision": 8,
  "status": "ACTIVE",
  "proxyConfiguration": null,
  "volumes": []
}

app container log

2019-06-12 16:01:06 events.js:183
2019-06-12 16:01:06 throw er; // Unhandled 'error' event
2019-06-12 16:01:06 ^
2019-06-12 16:01:06 Error: Redis connection to redis:6379 failed - getaddrinfo ENOTFOUND redis redis:6379
2019-06-12 16:01:06 at RedisClient.on_error (/var/api-console/node_modules/redis/index.js:148:24)
2019-06-12 16:01:06 at Socket.<anonymous> (/var/api-console/node_modules/redis/index.js:83:14)
2019-06-12 16:01:06 at emitOne (events.js:116:13)
2019-06-12 16:01:06 at Socket.emit (events.js:211:7)
2019-06-12 16:01:06 at emitErrorNT (internal/streams/destroy.js:66:8)
2019-06-12 16:01:06 at _combinedTickCallback (internal/process/next_tick.js:139:11)
2019-06-12 16:01:06 at process._tickCallback (internal/process/next_tick.js:181:9)

redis container log

2019-06-12 16:01:071:M 12 Jun 2019 23:01:07.216 * DB saved on disk
2019-06-12 16:01:071:M 12 Jun 2019 23:01:07.216 # Redis is now ready to exit, bye bye...
2019-06-12 16:01:071:M 12 Jun 2019 23:01:07.213 # User requested shutdown...
2019-06-12 16:01:071:M 12 Jun 2019 23:01:07.213 * Saving the final RDB snapshot before exiting.
2019-06-12 16:01:071:signal-handler (1560380467) Received SIGTERM scheduling shutdown...
2019-06-12 16:01:041:M 12 Jun 2019 23:01:04.400 * Running mode=standalone, port=6379.
2019-06-12 16:01:041:M 12 Jun 2019 23:01:04.400 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
2019-06-12 16:01:041:M 12 Jun 2019 23:01:04.400 # Server initialized
2019-06-12 16:01:041:M 12 Jun 2019 23:01:04.400 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
2019-06-12 16:01:041:M 12 Jun 2019 23:01:04.400 # WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled.
2019-06-12 16:01:041:M 12 Jun 2019 23:01:04.400 * Ready to accept connections

docker-compose.yml

version: '3'
services:
  redis:
    image: redis
    expose:
      - "6379"
    command:
      redis-server
    networks:
      - webnet
  app:
    build: ./
    image: rest_api_console
    ports:
      - 3000:3000
    depends_on:
      - redis
    environment:
      redis_server_addr: redis 
    networks:
      - webnet
networks:
  webnet:

nodejs dockerfile

# Use an official Python runtime as a parent image
FROM node:8-jessie

# Set the working directory to /var/api-console
WORKDIR /var/api-console

# Copy api-consol.tar, unpack, and install dependencies
COPY api-console.tar ./
RUN tar -xvf api-console.tar && \
    npm install && \
    apt-get update && \
    apt-get install python

# Modify config.js to replace host:'localhost' with host:'redis'
RUN sed -i -r 's/localhost/redis/g' config.json

# Make port 3000 available to the world outside this container
EXPOSE 3000

# Run app.js when the container launches
CMD ["node", "app.js"]

like image 950
Evan Dhillon Avatar asked Oct 15 '22 14:10

Evan Dhillon


1 Answers

AWS support linking in bridge network mode but your task definition seems link is null.

"links": null,

Your both redis and nodejs server should be in the same task definition.

First, create redis container in the same task definition

redis container

Then define linking in nodejs server

enter image description here

enter image description here

{
"ipcMode": null,
"executionRoleArn": null,
"containerDefinitions": [
    {
        "dnsSearchDomains": null,
        "logConfiguration": null,
        "entryPoint": null,
        "portMappings": [
            {
                "hostPort": 8700,
                "protocol": "tcp",
                "containerPort": 8700
            }
        ],
        "command": null,
        "linuxParameters": null,
        "cpu": 0,
        "environment": [],
        "resourceRequirements": null,
        "ulimits": null,
        "dnsServers": null,
        "workingDirectory": null,
        "secrets": null,
        "dockerSecurityOptions": null,
        "memory": 1000,
        "memoryReservation": 300,
        "volumesFrom": [],
        "stopTimeout": null,
        "image": "****.dkr.ecr.us-west-2.amazonaws.com/stage/nodejs:latest",
        "startTimeout": null,
        "dependsOn": null,
        "disableNetworking": null,
        "interactive": null,
        "healthCheck": null,
        "essential": true,
        "links": [
            "redis"
        ],
        "hostname": null,
        "extraHosts": null,
        "pseudoTerminal": null,
        "user": null,
        "readonlyRootFilesystem": null,
        "dockerLabels": {
            "Name": "nodejs"
        },
        "systemControls": null,
        "privileged": null,
        "name": "nodejs"
    },
    {
        "dnsSearchDomains": null,
        "logConfiguration": null,
        "entryPoint": null,
        "portMappings": [
            {
                "hostPort": 6379,
                "protocol": "tcp",
                "containerPort": 6379
            }
        ],
        "command": null,
        "linuxParameters": null,
        "cpu": 0,
        "environment": [],
        "resourceRequirements": null,
        "ulimits": null,
        "dnsServers": null,
        "mountPoints": [],
        "workingDirectory": null,
        "secrets": null,
        "dockerSecurityOptions": null,
        "memory": 600,
        "memoryReservation": 200,
        "volumesFrom": [],
        "stopTimeout": null,
        "image": "****.dkr.ecr.us-west-2.amazonaws.com/redis:latest",
        "startTimeout": null,
        "dependsOn": null,
        "disableNetworking": null,
        "interactive": null,
        "healthCheck": null,
        "essential": true,
        "links": null,
        "hostname": null,
        "extraHosts": null,
        "pseudoTerminal": null,
        "user": null,
        "readonlyRootFilesystem": null,
        "dockerLabels": null,
        "systemControls": null,
        "privileged": null,
        "name": "redis"
    }
],
"memory": null,
"family": "staging-nodejs-redis",
"pidMode": null,
"requiresCompatibilities": [],
"networkMode": null,
"cpu": null,
"proxyConfiguration": null,


   "placementConstraints": []
}
like image 119
Adiii Avatar answered Nov 03 '22 01:11

Adiii