Skip to main content

Overview

Bot Planning can be deployed using Docker, which provides an isolated environment with all required dependencies. This method ensures consistency across different environments and simplifies deployment.

Prerequisites

  • Docker installed on your system
  • A Discord bot token from Discord Developer Portal
  • Google Sheets URL for your planning/schedule (exported as CSV)

Dockerfile Explanation

The project uses the following Dockerfile:
# Image de base Python
FROM python:3.11-slim

# Définir le répertoire de travail
WORKDIR /app

# Copier les fichiers requirements et code
COPY requirements.txt .
COPY . .

# Installer les dépendances
RUN pip install --no-cache-dir -r requirements.txt

# Définir la commande pour démarrer le bot
CMD ["python", "bot.py"]
What this Dockerfile does:
  1. Base Image: Uses python:3.11-slim for a lightweight Python environment
  2. Working Directory: Sets /app as the working directory inside the container
  3. Copy Files: Copies requirements.txt first (for layer caching), then all project files
  4. Install Dependencies: Installs Python packages:
    • discord.py - Discord bot framework
    • pandas - Data manipulation for CSV processing
    • matplotlib - Image generation for schedules
    • python-dotenv - Environment variable management
  5. Start Command: Launches the bot with python bot.py

Environment Configuration

Create a .env file in your project directory with the following variables:
DISCORD_TOKEN=your_discord_bot_token_here
EDT_PATH=https://docs.google.com/spreadsheets/d/YOUR_SHEET_ID/export?format=csv
Important: The EDT_PATH must end with /export?format=csv to properly export the Google Sheets data.

Deployment Steps

1

Create Discord Bot

Navigate to the Discord Developer Portal and create a new bot application. Copy your bot token.
2

Prepare Environment File

Create a .env file in your project directory with your DISCORD_TOKEN and EDT_PATH variables.
3

Build Docker Image

Build the Docker image from the Dockerfile:
docker build -t bot-planning .
This creates an image tagged as bot-planning with all dependencies installed.
4

Run the Container

Start the bot container with your environment configuration:
docker run -d --name bot-planning \
  --env-file .env \
  --restart always \
  bot-planning:latest
Command options explained:
  • -d - Run container in detached mode (background)
  • --name bot-planning - Assign a friendly name to the container
  • --env-file .env - Load environment variables from .env file
  • --restart always - Automatically restart container if it stops
  • bot-planning:latest - Use the image we just built
5

Verify Deployment

Check that the container is running:
docker ps
View bot logs:
docker logs bot-planning
Follow logs in real-time:
docker logs -f bot-planning
Restart Policy: The --restart always flag ensures your bot automatically restarts after system reboots or if the container crashes. This is crucial for maintaining uptime in production.

Managing the Container

Stop the Bot

docker stop bot-planning

Start the Bot

docker start bot-planning

Restart the Bot

docker restart bot-planning

Remove the Container

docker rm -f bot-planning

View Resource Usage

docker stats bot-planning

Updating the Bot

When you need to update the bot code:
1

Stop and Remove Old Container

docker stop bot-planning
docker rm bot-planning
2

Rebuild the Image

docker build -t bot-planning .
3

Start New Container

docker run -d --name bot-planning \
  --env-file .env \
  --restart always \
  bot-planning:latest

Troubleshooting

Container Exits Immediately

Check the logs for errors:
docker logs bot-planning
Common issues:
  • Invalid Discord token
  • Incorrect Google Sheets URL format
  • Missing environment variables

Environment Variables Not Loading

Verify your .env file path is correct and the file exists:
ls -la .env
Alternatively, pass environment variables directly:
docker run -d --name bot-planning \
  -e DISCORD_TOKEN="your_token" \
  -e EDT_PATH="your_sheets_url" \
  --restart always \
  bot-planning:latest

Image Build Fails

Ensure you’re in the correct directory containing the Dockerfile:
ls Dockerfile requirements.txt bot.py
Clear Docker cache and rebuild:
docker build --no-cache -t bot-planning .

Build docs developers (and LLMs) love