How to host a Discord bot with Docker
Updated
A Discord bot only works while its process runs. On your laptop that means until you close the lid. This
guide moves a discord.py bot into a Docker container that keeps running without you.
Add a health endpoint
APPISH checks that a container answers HTTP before it marks the deployment healthy. A bot normally opens no
port, so give it a tiny web server. discord.py already depends on aiohttp, so there
is nothing new to install:
# bot.py
import os
import discord
from aiohttp import web
intents = discord.Intents.default()
client = discord.Client(intents=intents)
async def health(_request):
return web.Response(text="ok")
@client.event
async def setup_hook():
app = web.Application()
app.router.add_get("/", health)
runner = web.AppRunner(app)
await runner.setup()
await web.TCPSite(runner, "0.0.0.0", 8080).start()
@client.event
async def on_message(message):
if message.content == "!ping":
await message.channel.send("pong")
client.run(os.environ["DISCORD_TOKEN"])
Using discord.js? Add a few lines of Node's built-in http module listening on
0.0.0.0:8080; the rest is the same.
The Dockerfile
FROM python:3.12-slim
WORKDIR /app
RUN pip install --no-cache-dir discord.py
COPY bot.py .
EXPOSE 8080
CMD ["python", "bot.py"]
Pass the token, then push
Never commit the bot token or bake it into the image. Pass it as an environment variable tag; base64url-encode
it with an env64_ tag so any character is safe (the docs have the one-liner):
docker buildx build --platform linux/amd64 \
-t push.to.appi.sh/<your-push-id>/app:port_8080 \
-t push.to.appi.sh/<your-push-id>/app:env64_DISCORD_TOKEN___<base64url-token> \
--push .
The push address is private to your account and shown on your Dashboard. Once the slot turns Healthy, the bot is online in your server.
Keeping it online
A free slot is ideal for testing a bot: it runs for 4 hours per deployment and the Dashboard extends it with one click. For a bot your community uses every day, a paid slot keeps it running without the 4-hour clock. If the bot stores data, keep it in an external database: the container's disk is reset on every deploy.