Getting ETIMEDOUT while running Telegraf bot in docker container with network mode host

49 Views Asked by At
  • Telegraf.js Version: Final version
  • Node.js Version: v21
  • Operating System: Ubuntu:latest

Minimal Example Code Reproducing the Issue

const { Telegraf } = require('telegraf')
const bot = new Telegraf('TELEGRAM_TOKEN')

bot.telegram.setMyCommands([
    {command: 'start', description: 'Starts the system'},
    {command: 'restart', description: 'Restarts the system'},
    {command: 'help', description: 'Show commands help'},
])
bot.start((ctx) => {
    console.log(new Date(), 'started:', ctx.from)
    return ctx.reply('Hello Boss')
})

bot.launch()
FROM ubuntu:latest

WORKDIR /usr/src/app

RUN apt-get update && apt-get install curl build-essential libpangocairo-1.0-0 libx11-xcb1 libxcomposite1 libxcursor1 libxdamage1 libxi6 libxtst6 libnss3 libcups2 libxss1 libxrandr2 libasound2 libatk1.0-0 libgtk-3-0 libxshmfence1 libgbm-dev openssl tesseract-ocr git libc++-dev chromium-browser -y

RUN curl -fsSL https://deb.nodesource.com/setup_21.x | bash -
RUN apt-get install -y nodejs

COPY package.json ./package.json

RUN npm install
RUN npm install pm2 -g

COPY . .

CMD ["pm2-runtime", "--error", "./log/err.log", "--output", "./log/out.log" , "index.js"]

If I run docker container with network mode on host, with this command:

docker run --privileged -itd --network host --name ttelegraf hardc0der/ttelegraf:latest

I'm getting this error instantly:

FetchError: request to https://api.telegram.org/bot6411340281:[REDACTED]/setMyCommands failed, reason:
    at ClientRequest.<anonymous> (/usr/src/app/node_modules/node-fetch/lib/index.js:1501:11)
    at ClientRequest.emit (node:events:519:28)
    at TLSSocket.socketErrorListener (node:_http_client:492:9)
    at TLSSocket.emit (node:events:519:28)
    at emitErrorNT (node:internal/streams/destroy:169:8)
    at emitErrorCloseNT (node:internal/streams/destroy:128:3)
    at process.processTicksAndRejections (node:internal/process/task_queues:82:21) {
  type: 'system',
  errno: 'ETIMEDOUT',
  code: 'ETIMEDOUT'
}

I'm getting 200 response to my request when I logged in to container and make a CURL request to api.telegram.org

If I remove --network host from the running command, everything is running smoothly

What can be the reason to get this error while running in host mode network?

0

There are 0 best solutions below