gramjs: Client not starting on Docker containerization

I have this simple code:

const client = new TelegramClient(new StoreSession("my_session"), parseInt(process.env.API_ID), process.env.API_HASH, {
    connectionRetries: 5,
});

(async () => {
    await client.start({
        phoneNumber: async () => await input.text("Please enter your number: "),
        password: async () => await input.text("Please enter your password: "),
        phoneCode: async () => await input.text("Please enter the code you received: "),
        onError: (err) => console.log(err),
    });

    console.log("You should now be connected.");

    await client.sendMessage("me", { message: "test" });
    console.log("Sent to me.");
})()

It perfectly works without containerization. I want to create a Docker container with this code but it looks like it stucks on client.start function. This is the container log:

2024-02-08 17:42:34 [2024-02-08T16:42:34.106] [INFO] - [Running gramJS version 2.19.9]
2024-02-08 17:42:34 [2024-02-08T16:42:34.119] [INFO] - [Connecting to 91.108.56.132:80/TCPFull...]
2024-02-08 17:42:34 [2024-02-08T16:42:34.125] [INFO] - [Connection to 91.108.56.132:80/TCPFull complete!]
2024-02-08 17:42:34 [2024-02-08T16:42:34.134] [INFO] - [Using LAYER 166 for initial connect] 

It doesn’t even print “You should now be connected”. I took care of copying into the image also “my_session” in order to avoid the need to login again.

This is the Dockerfile:

FROM node:18-alpine

WORKDIR /telegrambot

COPY ./bot.js ./package.json .
COPY ./my_session ./my_session

RUN npm install

CMD [ "node", "bot.js" ]

What does it could be the problem?

Thank you in advance!

About this issue

Most upvoted comments

@hamakh11 I recently encountered and resolved an issue with the Telegram client connection in a Docker environment, and I wanted to share my solution to help others who might face the same problem.

Problem When using the Telegram client in a Docker environment, I noticed that creating a new connection each time an API endpoint was hit caused the client to freeze or block. This happens because Telegram only allows one connection request per session. If multiple connections are attempted from the same account, it leads to issues, especially when handling multiple API requests. as Telegram authentication tokens (stringSession) do not expire on their own. They remain valid indefinitely unless one of the following conditions occurs:

  1. Account Deletion: The token becomes invalid if the associated Telegram account is deleted.
  2. Manual Logout: The token expires if the user logs out from the session.
  3. Inactivity: If the session is not used for 6 months, it will expire based on Telegram’s inactivity policy.

Solution The key to solving this problem is to create the Telegram client connection only once and keep it persistent, similar to how you would handle a database connection. What you should have to do is generate a sessionString, store this string as a credential, and then use this sessionString, apiHash, and apiId as credentials to connect to the client. Here’s a detailed explanation:

Instead of creating a new Telegram client connection for each API request, establish the connection once the server starts. Maintain this connection for all subsequent operations. This prevents multiple connection attempts and ensures smooth communication with the Telegram API.

As I said for me is the solution to make a first run of the docker in interactive mode:

docker run -it image_name -v docker_path:external_path -e ENV_VAR='somethin' comman_to_run_app

If you don’t use the volume mapping inside the container - you can sipt -v docker_path:external_path, the same - for environment variables… Most important thing is the comman_to_run_app, which has to be equal to CMD comman_to_run_app from Dockerfile. I.e. if in docker file it is CMD npm run start, and no any volumes and environments, please try for the first run next command:

docker run -it image_name npm run start