Skip to content

Instantly share code, notes, and snippets.

@waja
Last active June 5, 2025 09:50
Show Gist options
  • Save waja/68853e25aa0f3b3ce46020f60ca2599c to your computer and use it in GitHub Desktop.
Save waja/68853e25aa0f3b3ce46020f60ca2599c to your computer and use it in GitHub Desktop.
Deploy watchtower container with label support
#!/bin/bash
DOCKER_BASE=/srv/docker
mkdir -p ${DOCKER_BASE}/watchtower/container.conf
[ $(grep -c "healthcheck:" ${DOCKER_BASE}/watchtower/container.conf/production.yml) -eq 0 ] && sed -i '/labels:/i\ healthcheck:\n\ test: ["CMD", "/watchtower", "--health-check"]\n\ interval: 30s\n\ timeout: 10s\n\ retries: 3\n\ start_period: 40s' ${DOCKER_BASE}/watchtower/container.conf/docker-compose.yml && cd ${DOCKER_BASE}/watchtower/ && docker-compose -f docker-compose.yml -f container.conf/production.yml config && systemctl restart watchtower
#!/bin/bash
DOCKER_BASE=/srv/docker
mkdir -p ${DOCKER_BASE}/watchtower/container.conf
cat > ${DOCKER_BASE}/watchtower/container.conf/docker-compose.yml <<EOF
version: '3.7'
services:
watchtower:
image: ghcr.io/nicholas-fedor/watchtower
healthcheck:
test:
- CMD
- /watchtower
- --health-check
timeout: 10s
interval: 30s
retries: 3
start_period: 40s
labels:
com.centurylinklabs.watchtower.enable: "true"
restart: on-failure
volumes:
- /var/run/docker.sock:/var/run/docker.sock
- /etc/localtime:/etc/localtime:ro
- /etc/timezone:/etc/timezone:ro
command: --interval 3600 --label-enable
EOF
ln -s container.conf/docker-compose.yml ${DOCKER_BASE}/watchtower/
cat > ${DOCKER_BASE}/watchtower/container.conf/production.yml <<EOF
version: '3.7'
EOF
cat > ${DOCKER_BASE}/watchtower/container.conf/watchtower.service <<EOF
[Unit]
Description=Watchtower Service
After=network.target docker.service
Requires=docker.service
[Service]
Type=oneshot
RemainAfterExit=yes
Environment="WORK_DIR=/srv/docker/watchtower/"
WorkingDirectory=/srv/docker/watchtower/
ExecStartPre=-/usr/local/bin/docker-compose -f "\${WORK_DIR}/docker-compose.yml" -f "\${WORK_DIR}/container.conf/production.yml" down
ExecStartPre=-/usr/local/bin/docker-compose -f "\${WORK_DIR}/docker-compose.yml" -f "\${WORK_DIR}/container.conf/production.yml" pull
ExecStart=/usr/local/bin/docker-compose -f "\${WORK_DIR}/docker-compose.yml" -f "\${WORK_DIR}/container.conf/production.yml" up -d
ExecStop=/usr/local/bin/docker-compose -f "\${WORK_DIR}/docker-compose.yml" -f "\${WORK_DIR}/container.conf/production.yml" down
[Install]
WantedBy=docker.service
EOF
ln -s ${DOCKER_BASE}/watchtower/container.conf/watchtower.service /etc/systemd/system/
systemctl daemon-reload && systemctl enable watchtower && systemctl start watchtower
#!/bin/bash
DOCKER_BASE=/srv/docker
WATCHTOWER_PATH=${DOCKER_BASE}/watchtower
DC_COMMAND="docker-compose -f docker-compose.yml"
cd ${WATCHTOWER_PATH} || exit
[ -f container.conf/production.yml ] && DC_COMMAND="${DC_COMMAND} -f container.conf/production.yml"
[ "$(${DC_COMMAND} config | grep "WATCHTOWER_NOTIFICATIONS" | awk '{print $2}')" != "email" ] && exit
cp container.conf/production.yml container.conf/production.yml.old
SMTP_HOST="$(${DC_COMMAND} config | grep "WATCHTOWER_NOTIFICATION_EMAIL_SERVER:" | awk '{print $2}')"
SMTP_FROM="$(${DC_COMMAND} config | grep "WATCHTOWER_NOTIFICATION_EMAIL_FROM:" | awk '{print $2}')"
SMTP_TO="$(${DC_COMMAND} config | grep "WATCHTOWER_NOTIFICATION_EMAIL_TO:" | awk '{print $2}')"
[ ! -e ${WATCHTOWER_PATH}/.env ] && ln -s container.conf/.env ${WATCHTOWER_PATH}/.env
touch ${WATCHTOWER_PATH}/container.conf/.env
sed -iE '/^SMTP_/d' ${WATCHTOWER_PATH}/container.conf/.env
cat >> ${WATCHTOWER_PATH}/container.conf/.env <<EOF
SMTP_HOST=${SMTP_HOST}
SMTP_FROM=${SMTP_FROM}
SMTP_TO=${SMTP_TO}
EOF
sed -i 's/WATCHTOWER_NOTIFICATIONS.*email/WATCHTOWER_NOTIFICATIONS=shoutrrr/' ${WATCHTOWER_PATH}/container.conf/production.yml
sed -i 's#WATCHTOWER_NOTIFICATION_EMAIL_FROM.*"#WATCHTOWER_NOTIFICATION_URL=smtp://:@\${SMTP_HOST}/?auth=None\&encryption=None\&fromaddress=\${SMTP_FROM}\&fromname=Watchtower\&starttls=No\&toaddresses=\${SMTP_TO}\&usehtml=No"#' ${WATCHTOWER_PATH}/container.conf/production.yml
sed -iE '/WATCHTOWER_NOTIFICATION_EMAIL_/d' ${WATCHTOWER_PATH}/container.conf/production.yml
#!/bin/bash
DOCKER_BASE=/srv/docker
mkdir -p ${DOCKER_BASE}/watchtower/container.conf
sed -i s/v2tec/containrrr/g ${DOCKER_BASE}/watchtower/container.conf/docker-compose.yml
systemctl restart watchtower && docker image rm v2tec/watchtower
#!/bin/bash
DOCKER_BASE=/srv/docker
mkdir -p ${DOCKER_BASE}/watchtower/container.conf
sed -i "s/image: containrrr\/watchtower/image: ghcr.io\/nicholas-fedor\/watchtower/" ${DOCKER_BASE}/watchtower/container.conf/docker-compose.yml && cd ${DOCKER_BASE}/watchtower && systemctl restart watchtower && docker-compose -f docker-compose.yml -f container.conf/production.yml logs -f; docker image rm containrrr/watchtower
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment