How to Run a Python Discord Bot on a Docker Application

Updated on September 1, 2021
How to Run a Python Discord Bot on a Docker Application header image


Discord is a video, voice, and text communication platform that allows bots to perform automated tasks based on user commands. This article explains how to run a Python-based Discord bot on a Docker application.

1. Create a Discord Application

  1. Go to the Discord Developer Portal and log in.
  2. Click the New Application button.
  3. Enter an application name and click Create.
  4. Select the Bot tab from the Settings menu.
  5. Click Add Bot then click Yes, do it!.
  6. Click Copy to copy your bot's token to the clipboard. Save a copy to use later in this guide.

2. Install Prerequisites

Install the Discord API wrapper for Python.

# pip install

3. Create the Discord Bot

  1. Switch to your home directory.

     # cd ~
  2. Create a project folder.

     # mkdir discord-bot
  3. Switch to the project folder.

     # cd discord-bot
  4. Create a python file named

     # nano
  5. Paste this sample code to your file. Replace <YOUR BOT TOKEN> with the token you saved in section 1.

     import discord
     client = discord.Client()
     async def on_ready():
         print('We have successfully loggged in as {0.user}'.format(client))
     async def on_message(message):
         if == client.user:
         if message.content.lower() == 'hello':
             await'Hello, {}!')
         if message.content.lower() == 'bye':
             await'See you later, {}!')

    The Discord bot should respond to commands such as hello, and bye.

  6. Save and exit the file.

  7. Create a Dockerfile.

     # nano Dockerfile
  8. Paste this to your Dockerfile.

     FROM python:3
     FROM gorialis/
     RUN mkdir -p /usr/src/bot
     WORKDIR /usr/src/bot
     COPY . .
     CMD [ "python3", "" ]
  9. Save and exit the Dockerfile.

4. Run the Discord Bot

  1. Change to the Discord bot project directory.

     # cd ~/discord-bot
  2. Build the docker container for the Discord bot.

     # docker build -t discord-bot .
  3. Run the docker container.

     # docker run -d discord-bot

    Running the bot with -d flag runs the container in detached mode (it runs in the background).

Common Docker Commands:

  • List all docker processes:

      # docker ps
  • Stop the docker container (discord bot):

      # docker stop <CONTAINER ID>
  • Restart the docker container:

      # docker restart <CONTAINER ID>

You can find the <CONTAINER ID> when you list all the Docker processes.

5. Invite the Bot to a Discord Server

  1. Go to the Discord Developer Portal and log in.
  2. Click the Application that you have created.
  3. Navigate to the OAuth2 Tab from the Settings menu.
  4. In the Scopes Section, select bot.
  5. In the Bot Permissions Section, select Administrator. This gives the bot an Administrator Role from the Discord Server.
  6. Click the Copy Button to copy the generated invite link.
  7. Open the invite link on your browser.
  8. Select the Discord server to add the bot to then click Continue.
  9. Authorize the bot and complete the CAPTCHA.

The Discord bot is now up and running.

More Information

To learn more about Discord bots, please see these resources: