-
Notifications
You must be signed in to change notification settings - Fork 0
Using Docker Compose to Deploy a Telegram Bot
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.
Ensure your project directory contains the following files:
-
app/app.js: Your Telegram bot application. -
Dockerfile: Defines the image for your bot. -
.env: Stores environment variables securely. -
docker-compose.yml: Configuration file for Docker Compose.
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"]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.
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 accessRun 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.
List all running containers to verify that your bot is running:
docker psTo see the container logs:
docker-compose logs mango-botInteract with your bot on Telegram by sending a message or using the /start command.
To stop the bot, run:
docker-compose down