-
Notifications
You must be signed in to change notification settings - Fork 0
Deploy your own typst bot
This guide outlines the steps to build and deploy the typst-bot Docker container on a Google Cloud Platform (GCP) e2 virtual machine (VM). This approach uses Docker and GCR to efficiently manage the application without needing to rebuild slowly on the less powerful remote VM.
- Your Discord app token. If you don't have one, you may want to see this YouTube tutorial on how to get Discord app token.
- A GCP Project with billing enabled.
- An active GCP e2 VM instance. If you don't have one, you may want to see this YouTube tutorial on how to create one.
- The Google Cloud CLI installed on your local machine (from which you access your remote VM, and for docker upload configurations).
-
Docker installed
- on your local machine (for local Docker image build & upload to Google Container Registry (GCR)), and
- on your GCP e2 VM (which pulls the Docker image from GCR and runs it).
First, clone the typst-bot repository and build the Docker image on your local, more powerful machine. This avoids the slow compilation on the e2 VM.
# Clone the repository
cd ~
git clone https://github.com/mattfbacon/typst-bot.git
cd typst-bot
# Build the Docker image
# This will run the slow Rust compilation on your local machine
docker build -t typst-bot .Next, you will push the built image to GCR. This makes the image available to your GCP VM and other services.
-
Authenticate Docker: Run this command on your local machine to configure Docker to use your
gcloudcredentials.gcloud auth configure-docker
-
Tag the Image for GCR: Tag your local image with the GCR path. Replace
[PROJECT_ID]with your GCP Project ID.docker tag typst-bot gcr.io/[PROJECT_ID]/typst-bot:latest
Note: the
[PROJECT_ID]can be found on the GCP console.
-
Push the Image: Push the tagged image to your GCR repository.
docker push gcr.io/[PROJECT_ID]/typst-bot:latest
Now that the image is in GCR, you will connect to your remote VM, pull the image, and run it.
-
Add SSH keys to your terminal
To avoid getting prompted for passphrase for the SSH key before each connection to remote VM.
eval "$(ssh-agent -s)" ssh-add ~/.ssh/google_compute_engine
-
Connect to your Remote VM: Use
gcloudto SSH into your remote VM instance.gcloud compute ssh [USERNAME]@[VM_INSTANCE_NAME] --zone=[VM_ZONE]
-
Authenticate Docker on the Remote VM: From within the remote VM's terminal, configure Docker to use the remote VM's service account credentials. This gives it permission to pull the private image.
gcloud auth configure-docker
-
Pull the Image from GCR: Pull the image you just pushed.
docker pull gcr.io/[PROJECT_ID]/typst-bot:latest
-
Remote Setup for Docker Compose Create the folder
typst-botunder the user directory (to match host machine's path).cd ~ mkdir typst-bot
Then switch to this directory, and create a file named
discord_token.txtwith the Discord token. If you're usingcatfor saving a text file, after pasting the token, tap Enter and Ctrl-d to finish.cd typst-bot cat > discord_token.txt
Logout from your remote VM. Copy your local
docker-compose.ymlto remote VM.gcloud compute scp docker-compose.yml [USERNAME]@[VM_INSTANCE_NAME]:/home/[USERNAME]/typst-bot/ --zone=[VM_ZONE]
Then adjust the docker compose file on remote VM for final deployment.
- Log in to your remote VM again.
- Change directory to
~/typst-bot. - In
docker-compose.yml, replacebuild: .withimage: gcr.io/[PROJECT_ID]/typst-bot:latest(because we are running from the image that we've just pushed to GCR, instead of building the image).
-
Run the Container: Finally, run the container in detached mode.
docker compose up -d
The typst-bot should now be running in a container on your remote VM. You can check its status and logs using the following commands:
# Check if the container is running
docker ps
# View the container logs
docker logs [CONTAINER][CONTAINER] is the name of the running container.