How to Set Up Docker for Your Discord Bot
Containerizing your bot ensures consistent, reliable deploys across machines and clouds. This guide gives you production‑grade Dockerfiles for Node (discord.js) and Python (discord.py), a solid .dockerignore, Docker Compose for dev and prod, and the nuances that matter for long‑lived Gateway connections (signals, restarts, voice libs).
Table of Contents
TL;DR: Quick Start
Production basics
- Multi‑stage builds, non‑root user, small base images.
- Use --init or tini to forward signals.
- Keep secrets in env vars; never bake .env into images.
Compose in prod
- restart: unless-stopped, init: true.
- Store token as DISCORD_TOKEN in .env or platform secrets.
Voice bots
- Add ffmpeg (and Opus libs) in your image.
- Prefer region‑near hosts to reduce audio latency.
Dockerfile (Node / discord.js)
Production‑ready multi‑stage build for a TypeScript bot compiled to dist/. Installs only production deps in the final image, runs as non‑root, and uses an init for clean shutdowns.
node:20-alpine
to reduce size, but Debian slim is simpler when you need FFmpeg.Dockerfile (Python / discord.py)
Slim, non‑root image with optional voice packages. Adjust requirements.txt as needed (e.g., discord.py
or discord.py[voice]
).
.dockerignore
Keep images lean and protect secrets. Place this in your project root:
.env
into the image; load it at runtime.Docker Compose (dev & prod)
compose.dev.yml
compose.prod.yml
Build & run: docker compose -f compose.prod.yml up -d --build
Signals, Restarts, and Health
Graceful shutdown
- Run with --init or include dumb-init/tini in the image.
- Ensure your bot listens for SIGTERM to disconnect and save state.
Health & restarts
Most bots don't expose HTTP. Prefer restart: unless-stopped and rely on your app's own reconnect logic for Discord Gateway. If you add a small status HTTP endpoint, you can wire a Docker HEALTHCHECK
.
Voice Dependencies
Node / discord.js voice
- Add
ffmpeg
in the image. Many setups also use@discordjs/voice
+@discordjs/opus
. - If native modules build from source, install build-essential temporarily in a build stage.
Python / discord.py voice
- Install
ffmpeg
andlibopus0
in the image. - Use
discord.py[voice]
to pull the right extras.
Deploy Targets & Practical Tips
Single VPS + Compose
- Great for small–medium bots. Use
docker compose up -d
andjournalctl/docker logs
for ops. - Back up volume data (if any) and keep images updated (
docker system prune
cautiously).
PaaS container hosts
Fly.io, Koyeb, Render, and Railway can run your image 24/7. Use their secrets UI for DISCORD_TOKEN, set restart policies, and ship via CI/CD.
Environment & secrets
- Put
DISCORD_TOKEN
and other keys in platform secrets or.env
(not committed). - Prefer OIDC or secret managers on cloud providers for CI/CD deploys.
Online 24/7? Grow it on Rank.top
List your bot for modern discovery, built‑in analytics, and passive vote revenue. Connect webhooks in minutes.