Not Able To Send Email using Nodemailer within Docker container due to EAI_AGAIN

I’ve setup Nodemailer in a docker container, but I haven’t been able to get emails to be sent. I ran the same code outside of docker to test whether it worked and it sends emails. So it has to be something to do with my docker container setup. The docker-compose and docker-file I’m using for development are not fancy, just the essentials, and I’m using the docker-toolbox since I don’t have Windows 10 pro.

I found similar questions like Nodemailer not sending mails after deploying it on docker container that is over a year old and contains no example code or answers. Also, found https://stackoverflow.com/a/25270794/1148107, and tried changing the port to 465 with TLS on and off, but still doesn’t send mail.

Error

An EAI_AGAIN error is thrown, which I looked up and is a DNS lookup timeout issue so could be a network connectivity error or proxy related error, but I’m not a docker (or Devops) guru so I’m having issues figuring out how to resolve it. Can anyone explain how to get Nodemailer to work in a Docker container?

Error: getaddrinfo EAI_AGAIN smtp.mailtrap.io:2525
    at Object._errnoException (util.js:1021:11)
    at errnoException (dns.js:58:15)
    at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:95:26)
    code: 'ECONNECTION',
    errno: 'EAI_AGAIN',
    syscall: 'getaddrinfo',
    hostname: 'smtp.mailtrap.io',
    host: 'smtp.mailtrap.io',
    port: 2525,
    command: 'CONN'

Docker Compose

services:
  server:
    container_name: server
    build:
      context: .
      dockerfile: .docker/node.${APP_ENV}.dockerfile
    ports:
      - "3000:3000"
    volumes:
      - ${NODE_SRC_PATH}:${NODE_CONTAINER_PATH}
    working_dir: ${NODE_WORKING_DIR}
    env_file:
      - ./.docker/env/node.${APP_ENV}.env
    depends_on:
      - db
    networks:
      - app-network

    // ... for brevity removed mysql, adminer, and network services

Docker File

# Pull in image
FROM node:latest

# Expose the default Express port
EXPOSE 3000

# Main entry point
ENTRYPOINT ["npm", "run", "start"]