Skip to content

Using Docker Compose to Deploy a Telegram Bot

Rafael O. Vega Rodriguez edited this page Dec 2, 2024 · 1 revision

Overview

This article explains how to use Docker Compose to deploy a containerized Telegram bot built with Telegraf. Docker Compose simplifies running multi-container Docker applications and helps manage environment variables, networking, and dependencies.


Setup

1. Required Files

Ensure your project directory contains the following files:

  1. app/app.js: Your Telegram bot application.
  2. Dockerfile: Defines the image for your bot.
  3. .env: Stores environment variables securely.
  4. docker-compose.yml: Configuration file for Docker Compose.

2. Dockerfile

Your Dockerfile should define how to build the container for your bot.

# Base image
FROM node:18-alpine

# Set working directory
WORKDIR /app

# Copy package.json and package-lock.json
COPY package*.json ./

# Install dependencies
RUN npm install

# Copy the rest of the application
COPY . .

# Expose an optional port (if needed)
EXPOSE 3000

# Start the bot
CMD ["node", "app/app.js"]

3. Environment Variables

Create a .env file to store sensitive information like API keys.

BOT_API_KEY=your-telegram-bot-api-key
WEB_APP_URL=https://your-web-app-url

Ensure .env is included in .gitignore to avoid committing sensitive information to version control.


4. docker-compose.yml

Create a docker-compose.yml file to define the services and configuration for your Telegram bot.

version: '3.8'
services:
  bot:
    build: .
    container_name: mango-bot
    restart: unless-stopped
    env_file: 
      - .env
    ports:
      - "3000:3000" # Optional, if the bot requires HTTP access

Usage

1. Build and Start the Application

Run the following command to build the Docker image and start the container:

docker-compose up -d
  • up: Builds and starts the services.
  • -d: Runs the services in detached mode.

2. Verify the Deployment

Check Running Containers

List all running containers to verify that your bot is running:

docker ps

Check Logs

To see the container logs:

docker-compose logs mango-bot

Test the Bot

Interact with your bot on Telegram by sending a message or using the /start command.


3. Stop the Application

To stop the bot, run:

docker-compose down

Clone this wiki locally