This post will teach you how to dockerize your discord bot
What is docker?
Docker is a computer tool designed so it is easier to create release and run applications by using things they call “Containers” Containers allow developers to package their application with all the parts it needs to run such as the dependencies and then sends it out as one package.
The first thing you will need to do is to make sure you have docker downloaded which you can read about Here if you have not already.
You will then need to create a file in the bots main directory called “Dockerfile”, this will be the main file that docker needs in order to run.
In this file, you will need to add the following
I originally had it so it was node:latest but I noticed that the latest version is not compatible with some packages so I decided to use 8.15.0-jessie. This part of the file makes it so docker knows that this is a nodejs application and it needs to install the image node in order to run the bot
# Create the directory! RUN mkdir -p /usr/src/bot WORKDIR /usr/src/bot
This will tell docker to create a directory inside the container called bot and then will tell docker that the bot folder is the working directory where all the main code will be.
# Copy and Install our bot COPY package.json /usr/src/bot RUN npm install
This will tell docker to copy the package.json file over to the container and then run npm install which will run all of the packages the bot needs in order to run correctly
# Our precious bot COPY . /usr/src/bot
This part of the docker file is to tell docker to copy over all the code from our desktop into the container
# Start me! CMD ["node", "index.js"]
The last part of the dockerfile is telling docker how to run the bot which is by running node index.js
Please note if your main file is not index.js please change it so it is the main file
Running the image
So our dockerfile is now complete now we just need to build the bot by running
docker build -t my-bot . and then start the container by running
docker run -d my-bot and then hopefully your bot should start running :)
Thanks for reading this post on how to dockerize your discord bot using Docker