-
Notifications
You must be signed in to change notification settings - Fork 5
The Infrastructure
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/]
Current Docker workflow
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
For more information on setting up your docker and docker desktop please check the documentation here
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.
.env file for running the Discord Bot in the container.
ℹ️ 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.
- First, pull the image using
docker pull <username>/<repo-name>[:tag-name]. - Build the new image by running this command:
docker build -t <username>/<repo-name> . - Re-tagging the image with latest tag using
docker tag <image-name> <username>/<repo-name>[:latest] - Pushing the image to the docker repository
docker push <username>/<repo-name>:<tag>.
ℹ️ 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.
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>