Automation: Re-deploy docker container via Github workflow

Before explaining what is going on, this is my docker setup.

General setup:
Github action workflow -> Remote Server -> Build docker image if not present and run container.

Problem:
If the container goes down it can not come backup due to not having environment secrets which are kept at Github’s repository secrets.

Solution:
Bash script runs to check “docker ps” output. Filters the output and if conditions are met, triggers Github workflow dispatch and also emails admin about the outage. The workflow dispatch runs the starting cycle of my General setup listed above.

Example output of my “docker ps”

github@~ $ docker ps
CONTAINER ID   IMAGE                              COMMAND                   CREATED        STATUS                  PORTS                                             NAMES
b4bf4aa17f3e   btc2api:1.1.0                      "docker-entrypoint.s…"   21 hours ago   Up 21 hours (healthy)   0.0.0.0:4431->443/tcp, [::]:4431->443/tcp         btc2-api-110-api-1
5ab42117d482   ghcr.io/plankanban/planka:latest   "docker-entrypoint.s…"   10 days ago    Up 10 days (healthy)    0.0.0.0:3001->1337/tcp, [::]:3001->1337/tcp       planka_planka.1.pk0zp8xj90qqiazsowxm4aj4y
240cf6039329   mongo:8.0.9-noble                  "docker-entrypoint.s…"   10 days ago    Up 10 days (healthy)    0.0.0.0:27017->27017/tcp, [::]:27017->27017/tcp   mongodb_btc_mongodb.1.gvsz1v742huwxwbpzosm0adqf
1185a81b312e   postgres:16-alpine                 "docker-entrypoint.s…"   2 weeks ago    Up 2 weeks (healthy)    5432/tcp                                          planka_postgres.1.a81wcxtmfmnz4ajtqa072k92c

Bash script to check if btc2api is running at is in “(healthy)” state. I have Cron running this every minute.

#!/bin/bash

# Settings
IMAGE_KEYWORD="btc2api"
REPO="beetron/btc2_API"
TOKEN=""
WORKFLOW_FILE="deploy-v1.yml"

# Check if btc2api is running and healthy
if ! docker ps | grep "$IMAGE_KEYWORD" | grep -q "(healthy)"; then
  echo "btc2api was down at: $(date)" | s-nail -s "btc2api was down" admin@mail.com

  # Trigger GitHub Actions workflow_dispatch
  curl -X POST \
    -H "Accept: application/vnd.github+json" \
    -H "Authorization: Bearer $TOKEN" \
    "https://api.github.com/repos/$REPO/actions/workflows/$WORKFLOW_FILE/dispatches" \
    -d '{"ref":"main"}'

else
  echo "$IMAGE_KEYWORD is running and healthy"
fi

To get an idea of my deploy-v1.yml, you could check my api repo: https://github.com/beetron/btc2_API

mailx/s-nail super basic setting

I needed a quick email to go out from my server so I decided to install mailx.
Found out that s-nail is the new package name.
I’m the only one for this system so I went ahead with a global setting.

1.) dnf -y s-nail
2.) vi /etc/s-nail.rc

#v15 settings compatibility
set v15-compat

#smtp config
set mta=smtp://USERNAME:PASSWORD@SMTP_HOSTNAME \
 smtp-use-starttls
set from="mail@mail.com"

3.) Test sending an email out

echo "Body Here" | s-nail -s "Subject Here" Recipient@mail.com

Open firewall for Expo node project (windows)

If you are testing your mobile app from a real devicc and need to access your expo app locally, here’s an example of opening the ports for the necessary services. (Windows powershell (admin))

# Allow Node.js inbound
New-NetFirewallRule -DisplayName "Node.js" -Direction Inbound -Program "C:\Program Files\nodejs\node.exe" -Action Allow

# Allow specific Expo port
New-NetFirewallRule -DisplayName "Expo Dev Server" -Direction Inbound -Protocol TCP -LocalPort 8081 -Action Allow

# Allow Metro bundler port range
New-NetFirewallRule -DisplayName "Metro Bundler" -Direction Inbound -Protocol TCP -LocalPort 19000-19006 -Action Allow