Il arrive que des gens publient un code sur GitHub avec un Dockerfile mais sans package. Le container est donc à construire soi-même, localement.
Ça peut se faire directement depuis un compose
services:
applicationABC:
build:
context: https://github.com/user/applicationABC.git
dockerfile: Dockerfile
container_name: applicationABC
restart: always
ports:
- 8080:8080
Mais dans ce cas la mise à jour automatisée via WatchTower ne fonctionne pas puisqu’il n’y a pas d’image à aller chercher.
labels:
- com.centurylinklabs.watchtower.enable=true
Du coup voici une solution de contournement, simple et surtout qui n’implique pas d’outil tiers ou de cloner un dépôt GitHub et faire/tenir à jour un package moi-même.
Ce bout de code va checker les commits d’un dépôt GitHub à intervalles réguliers et, au besoin, construire un container à jour localement et relancer le tout.
Avec notification Discord, parce que j’aime ça.
applicationABC-autoupdate:
image: alpine:latest
container_name: applicationABC-autoupdate
restart: always
environment:
GITHUB_REPO: https://github.com/user/applicationABC.git
DISCORD_WEBHOOK: https://canary.discord.com/api/webhooks/xxx/xxx
POLL_INTERVAL: 172800 # secondes
SERVICE_NAME: applicationABC
volumes:
- /var/run/docker.sock:/var/run/docker.sock
- /tmp:/repo
command: >
sh -c "
apk add --no-cache git bash curl docker-cli jq &&
mkdir -p /repo &&
cd /repo &&
git clone --depth 1 \$GITHUB_REPO . || true &&
REPO_NAME=\$(basename -s .git \$GITHUB_REPO) &&
DEFAULT_BRANCH=\$(curl -s https://api.github.com/repos/\$(echo \$GITHUB_REPO | sed 's|.*/||;s|.git||') | jq -r .default_branch) &&
git fetch origin \$DEFAULT_BRANCH &&
git checkout \$DEFAULT_BRANCH &&
LAST_COMMIT=\$(git rev-parse HEAD) &&
while true; do
git fetch origin \$DEFAULT_BRANCH &&
NEW_COMMIT=\$(git rev-parse origin/\$DEFAULT_BRANCH) &&
if [ \"\$NEW_COMMIT\" != \"\$LAST_COMMIT\" ]; then
echo \"[$(date)] Nouveau commit détecté sur \$DEFAULT_BRANCH, rebuild...\"
git reset --hard origin/\$DEFAULT_BRANCH &&
docker compose -f /repo/docker-compose.yml build \$SERVICE_NAME &&
docker compose -f /repo/docker-compose.yml up -d \$SERVICE_NAME &&
REPO_LINK=\$GITHUB_REPO &&
COMMIT_LINK=\"\$GITHUB_REPO/commit/\$NEW_COMMIT\" &&
curl -H 'Content-Type: application/json' -X POST -d '{\"content\": \"\$REPO_NAME mis à jour automatiquement !\nBranche : \$DEFAULT_BRANCH\nCommit : \$NEW_COMMIT\nDépôt : <\$REPO_LINK|\$REPO_NAME>\nLien du commit : <\$COMMIT_LINK|voir commit>\"}' \$DISCORD_WEBHOOK &&
LAST_COMMIT=\$NEW_COMMIT
else
echo \"[$(date)] Aucun changement sur \$DEFAULT_BRANCH.\"
fi
sleep \$POLL_INTERVAL
done
"
Le travail est effectué dans le dossier temporaire.
Il suffit d’éditer les variables voire le nom du container, histoire de faire propre
applicationABC-autoupdate:
image: alpine:latest
container_name: applicationABC-autoupdate
restart: always
environment:
GITHUB_REPO: https://github.com/user/applicationABC.git
DISCORD_WEBHOOK: https://canary.discord.com/api/webhooks/xxx/xxx
POLL_INTERVAL: 172800 # secondes
SERVICE_NAME: applicationABC
Attention, la variable SERVICE_NAME doit être le nom exact du service à reconstruire/relancer
services:
applicationABC:
build:
context: https://github.com/user/applicationABC.git
dockerfile: Dockerfile
container_name: applicationABC
restart: always
ports:
- 8080:8080
![]()

