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