Search code examples
node.jsdocker

Timeout Error When Running Discord Bot in Docker Container


I've been working on a simple Discord bot that performs simple tasks like Message.React() and Message.Respond(). The bot works as expected outside of Docker, but I'm encountering issues when trying to run it within a Docker container.

Here are the details:

  1. Objective: My bot logs in successfully and triggers when a message is sent to the chat.
  2. Problem: When the bot attempts to communicate back using Message.react or Message.reply, I receive a TIMEOUT error.
  3. Docker Configuration: I'm using the host network, so there shouldn't be any port-related issues.
  4. Assumption: As a Docker newbie, I suspect it might be an authentication issue.
  5. Next Steps: I plan to investigate the difference between the client "connecting" and the client "responding."

If anyone has encountered similar issues or has ideas on how to troubleshoot this, please share your insights. I'd appreciate any guidance before I lose more sleep over this!

Code

client.on('messageCreate', async (msg: Message) => {
  console.log('Message received');

  try {
    if (msg.content === 'ping') {
      msg.reply('Pong!');
    }

    if (msg.content === '!fitness') {
      FitnessChannelId = msg.channelId;
      msg.reply(`Set FitnessChannelId to ` + msg.channelId);
    }

    if (msg.content === imp?.emoji) {
      console.log('Imp emoji found!');
      msg.react(imp.emoji).catch((e) => console.log(e.message));
    }
FROM node:20.8.0
ENV NODE_ENV=production
ENV DISCORD_TOKEN=???
ENV DISCORD_CLIENT_ID=???
RUN apt-get update && \
    apt-get install --yes --no-install-recommends wget build-essential libcurl4 && \
    wget https://curl.se/download/curl-7.74.0.tar.gz && \
    tar -xvf curl-7.74.0.tar.gz && cd curl-7.74.0 && \
    ./configure && make && make install
WORKDIR /usr/src/app
COPY ["package.json", "package-lock.json*", "npm-shrinkwrap.json*", "./"]
RUN npm install && mv node_modules ../
COPY . .
EXPOSE 3000
EXPOSE 80 
EXPOSE 443
RUN chown -R node /usr/src/app
USER node
RUN npm run build
CMD ["npm", "start"]

I build the Docker image with this docker build --network=host -t foo . && docker run --network=host -p 80:80 -p 443:443 -it foo

Error

Message received
getResults
ConnectTimeoutError: Connect Timeout Error
    at onConnectTimeout (/usr/src/node_modules/undici/lib/core/connect.js:186:24)
    at /usr/src/node_modules/undici/lib/core/connect.js:133:46
    at Immediate._onImmediate (/usr/src/node_modules/undici/lib/core/connect.js:174:9)
    at process.processImmediate (node:internal/timers:478:21) {
  code: 'UND_ERR_CONNECT_TIMEOUT'
}
Connect Timeout Error

Thanks in advance!

I was expecting the program to preform the same in docker but instead I was given a timeout error

Attempts to fix

  1. I tried resetting my token
  2. Setting network to host
  3. Opening http and https ports
  4. Curl request https discord.com from within the docker container

Solution

  • I found the issue.

    I assumed since the build time was quick that my M2 mac would have no issue with the network. Turn out that Docker on Mac has a speed issue, and it just happens that the API endpoint that I was using had a time-out that just missed it.

    So my solution was to just do nothing. I just needed the docker file to easily add it to a Linux server, which it was able to do because the container in the Linux server did not have the same network issue.

    So just developing normally with node on my Mac, along with creating a good Dockerfile was all that was needed to get it running on my server, even though I couldn't test the docker container on my Mac