Skip to content

The Infrastructure

suisoh edited this page Jan 18, 2023 · 28 revisions

The ProgPhil Discord Bot Infrastructure

The Infrastructure Overview

Containerization with Docker

Docker is a popular software used in creating and building containerized applications. Why do we need to containerize our application? Docker allows us to package our app with its required libs and dependencies inside the docker container to make our deployment into the server more efficient, taking up less space and time. It will be easier for the ops team to deliver changes in the codebase and dependencies to the serving by rebuilding the image of the new app and restarting the running container deployed in the server.

flowchart TB;
    A[/New App changes/] --> | on push:main | B(Build new docker image)
    B --> | uploads to | C[(DockerHub Repo)]
    C --> | connect to server via SSH | D(Pull latest image)
    D --> E(Build docker image and restart container)
    F[/Discord Token/] --> | pass via env | E
    E --> G[/Updated running Discord Bot/]
Loading

Current Docker workflow


Getting Started

You need to install Docker Desktop first. Check the link given below on how to get Docker into your desktop. You must sign in in the Docker Hub in order for you to use Docker.

Download Links

Linux installation

Mac Installation

Windows Installation

⚠️ You must have WSL2 installed in your desktop in order to run Docker in Windows.

For more information on setting up your docker and docker desktop please check the documentation here


The Docker Repository

The ProgPhil Bot image can be found in this repo. You can pull this image by running the command docker pull ssuish/progphil-bot:latest.

⚠️ The image doesn't contain .env file for running the Discord Bot in the container.


Dockerfile, Dockerignore and Why not Docker Compose?


Building Docker Image and pushing it to the docker repository

ℹ️ This is process is already automated in CI/CD

Assuming you already made changes in the codebase or dependencies and wanted to update the bot.

  1. First, pull the image using docker pull <username>/<repo-name>[:tag-name].
  2. Build the new image by running this command: docker build -t <username>/<repo-name> .
  3. Re-tagging the image with latest tag using docker tag <image-name> <username>/<repo-name>[:latest]
  4. Pushing the image to the docker repository docker push <username>/<repo-name>:<tag>.

⚠️ Docker must be running in the background in order to run docker commands.

⚠️ It is recommended to login first at once in the docker cli before running commands.

⚠️ Don't forget '.' after the image name.


Building and running the Docker Container

ℹ️ This is process is already automated in CI/CD

After you built the image, assuming you don't have any containers made, we are gonna create one and run it.

Optional: You can try to build and run containers in our remote testing server via SSH. You can look at the documentation about our remote server here.

Building and running container using the image

We are creating the container from the image we built before. This container can be easily deployed and run in our server with less memory and space used. You can access the app inside the container while running but if we want to change something inside our container it will be easy if we just make changes in our repo, build the image, delete the previous container, build and run the new container. This process can be done in seconds so there's no need to worry.

In this context we are building the container, pass the discord token, and run it. This command will do both the building of the container and running it.

docker run -d --name <container-name> -env token=discord-token <username>/<repo-name>:<tag>

Flags description:

-d - Run the container in the background.

--name - Naming the container.

-env - Required field for the discord bot in the container to run. token=discord-token will be pass as .env in the container.

⚠️ Discord Token in the CI/CD will be pass via Github Secrets through Github Actions, don't write the token anywhere in the codebase.

You can see all running containers using this command: docker ps.

Alternatively, if you want to see all containers exist in your desktop/remote server: docker ps -a.

If there's any changes in the repo or the dependencies we must restart the container by removing the previously running and replacing it with the new container built with new image base on the new codebase.

To do this we will run set of commands:

docker stop <container-name> 
docker rm <container-name>

Alternatively, you can do this on single command by forcing it using -f flag:

docker rm -f <container-name>

After stopping and removing the container, we have to run the new container with latest image:

docker run -d --name <container-name> -env token=discord-token <username>/<repo-name>:<tag>

The CI/CD Pipeline

Clone this wiki locally