Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
ne0c0de opened this issue Feb 29, 2024 · 14 comments

Comments

@ne0c0de
Copy link

ne0c0de commented Feb 29, 2024

  • 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 --mount type=bind,source=/ctb/log,target=/usr/src/app/log --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?

@neko0xff
Copy link

neko0xff commented Mar 3, 2024

I get the same problem when I run it on a physical machine.

# user @ Host-02 in ~/文件/GitHub/2023_schoolResearch_Server-HW/src/Server/mqtt_bot_node on git:main x [13:02:30] C:1
$ npm start

> [email protected] start
> node index.js

[2024/3/3 下午1:03:10] Service is Starting.....
(node:111283) [DEP0040] DeprecationWarning: The `punycode` module is deprecated. Please use a userland alternative instead.
(Use `node --trace-deprecation ...` to show where the warning was created)

/home/user/文件/GitHub/2023_schoolResearch_Server-HW/src/Server/mqtt_bot_node/node_modules/node-fetch/lib/index.js:1501
                        reject(new FetchError(`request to ${request.url} failed, reason: ${err.message}`, 'system', err));
                               ^
FetchError: request to https://api.telegram.org/bot7063183971:[REDACTED]/getMe failed, reason: 
    at ClientRequest.<anonymous> (/home/user/文件/GitHub/2023_schoolResearch_Server-HW/src/Server/mqtt_bot_node/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'
}

Node.js v21.6.2

@MKRhere
Copy link
Member

MKRhere commented Mar 3, 2024

It's likely your network itself that's blocking the request. Try over a VPN?

@ne0c0de
Copy link
Author

ne0c0de commented Mar 3, 2024

It's likely your network itself that's blocking the request. Try over a VPN?

As I mentioned on the issue, the problem is not related with network/firewall because I can able to send a GET request inside the docker machine, only telegraf bot getting this error when it's try to access to same URL

@MKRhere
Copy link
Member

MKRhere commented Mar 3, 2024

while running in host mode network

I have no idea why this would affect it. Someone more experienced with Docker should try to help. I'd start by investigating whether it works outside the container, and does it consistently work without --network host (to rule out flaky network).

However @neko0xff's issue is very likely network blocking requests to Telegram domains.

@neko0xff
Copy link

neko0xff commented Mar 3, 2024

I use curl & Postman test, is can work!
(I not use VPN)
圖片

@ne0c0de
Copy link
Author

ne0c0de commented Mar 4, 2024

while running in host mode network

I have no idea why this would affect it. Someone more experienced with Docker should try to help. I'd start by investigating whether it works outside the container, and does it consistently work without --network host (to rule out flaky network).

However @neko0xff's issue is very likely network blocking requests to Telegram domains.

docker without host network is working without any problem, also same code works on host machine without docker but when I put it in a docker container with configures as host network then it takes that error

@MKRhere
Copy link
Member

MKRhere commented Mar 21, 2024

I recently discovered that Node.js choosing IPv6 by default could be the root of this problem. If you still suffer from this issue, consider testing the following fix:

import { setDefaultResultOrder } from "node:dns";
setDefaultResultOrder("ipv4first");

Or if you use legacy require:

const { setDefaultResultOrder } = require("node:dns");
setDefaultResultOrder("ipv4first");

This has to be done before calling bot.launch.

@ne0c0de
Copy link
Author

ne0c0de commented Mar 21, 2024

I will try this, thanl you so much

@MKRhere
Copy link
Member

MKRhere commented Mar 31, 2024

Did that solve this issue?

@impuLssse
Copy link

impuLssse commented Mar 31, 2024

I have same error
Please tell me what could be the problem
image
image

@impuLssse
Copy link

Network remote docker settings in dev container
image

@dongitran
Copy link

I had a similar issue and fixed it by downgrading the node version to node:16-alpine

hopefully it will help you too.

@PororoManon
Copy link

fixed it by downgrading the node version to node:16-alpine

this work on me, but I don't use docker. I use native NodeJS on machine. I got error on node v20.12.2 LTS: Iron. then I downgrade it to node v16.20.2 (npm v8.19.4) LTS: gallium

@jrandiny
Copy link

jrandiny commented May 3, 2024

Downgrading to node 18 (in docker) also fix this issue for me. Compared to node 16, using node 18 is better because it still has security support

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants