Deploying Your Bot

How to run your Discord bot in production.


On a Linux Server

  1. SSH into your server
  2. Clone your bot’s repo
  3. Set up Python and dependencies
sh
python3 -m venv venv
source venv/bin/activate
python3 -m pip install -r requirements.txt
  1. Set your environment variables (.env or Doppler). Remember that .env is gitignored, so it will not be in the clone
  2. Run:
sh
./run.sh

Using a Process Manager

A bot needs to stay running and to come back after a reboot, so use systemd rather than a bare ./run.sh in a shell.

ini
Description=My Discord Bot
After=network-online.target

[Service]
Type=simple
User=bot
WorkingDirectory=/home/bot/my-bot
ExecStart=/home/bot/my-bot/run.sh
Restart=always
RestartSec=5
Environment=ENVIRONMENTS=production
Environment=LOG_LEVEL=INFO

[Install]
WantedBy=multi-user.target
sh
sudo systemctl enable --now my-bot
sudo systemctl status my-bot
journalctl -u my-bot -f

With Docker (Example)

dockerfile
FROM python:3.11
WORKDIR /app
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
COPY . .
ENV ENVIRONMENTS=production
CMD ["python3", "src/main.py"]
⚠️ WARNING
Don't bake .env into the image. Pass secrets at run time with docker run --env-file .env or your orchestrator's secret store.

Logs

Your bot writes to logs/bot.log and to stdout. Mount or ship LOG_DIR somewhere persistent if you want logs to survive a container restart, and set LOG_LEVEL=INFO or higher in production.


Next Steps