Skip to content

Commit

Permalink
(feat) add watchtower for cheap container updates
Browse files Browse the repository at this point in the history
  • Loading branch information
DudeCalledBro committed Dec 28, 2024
1 parent f06eb6a commit a3b1aae
Show file tree
Hide file tree
Showing 7 changed files with 90 additions and 1 deletion.
16 changes: 15 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

[![CI](https://github.com/DudeCalledBro/jellyfin/actions/workflows/ci.yml/badge.svg)](https://github.com/DudeCalledBro/jellyfin/actions/workflows/ci.yml)

This repository contains my ansible and docker deployment for Jellyfin - an open-source media server.
This repository contains the Ansible code for deploying Jellyfin using Docker.

## Prerequisites

Expand All @@ -23,6 +23,20 @@ This repository contains my ansible and docker deployment for Jellyfin - an open

> You may have to enter a password to SSH into the target system, so you need to add `-k` after the `ansible-playbook` command.

## Watchtower

Watchtower is a Docker container application that automates the process of updating other Docker containers. It monitors running containers and checks for changes to their base images. When Watchtower detects that a new version of an image has been pushed to a Docker registry, it performs the following actions:

- Pulls the updated image
- Gracefully shuts down the existing container
- Restarts the container using the new image with the same runtime options

> **Side Note!** I use Watchtower for effortless updates on my systems, as I often overlook Jellyfin updates. The installation is optional.

```bash
ansible-playbook play-watchtower.yml
```

## License

Copyright © 2024 Niclas Spreng
Expand Down
3 changes: 3 additions & 0 deletions example.config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,6 @@ jellyfin_docker_volumes:
# jellyfin proxy tls certificates
jellyfin_proxy_docker_crt: ""
jellyfin_proxy_docker_key: ""

# watchtower configuration
watchtower_docker_command: "--debug --cleanup --schedule '0 30 4 * * *'"
8 changes: 8 additions & 0 deletions play-watchtower.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
- name: Setup Watchtower
hosts: all
pre_tasks:
- name: Include Variables
ansible.builtin.include_vars: config.yml

roles:
- watchtower
10 changes: 10 additions & 0 deletions roles/watchtower/defaults/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Specifies the watchtower docker image to be used.
watchtower_docker_image: "containrrr/watchtower:latest"

# Specifies the watchtower path, owner and group.
watchtower_docker_path: /opt/watchtower
watchtower_docker_owner: root
watchtower_docker_group: root

# Specifies the watchtower command
watchtower_docker_command: "--debug"
7 changes: 7 additions & 0 deletions roles/watchtower/handlers/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
- name: Restart watchtower container
ansible.builtin.command: >-
docker compose --ansi never --progress plain restart
args:
chdir: "{{ watchtower_docker_path }}"
changed_when: true
listen: restart watchtower
31 changes: 31 additions & 0 deletions roles/watchtower/tasks/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
- name: Create watchtower directory
ansible.builtin.file:
path: "{{ watchtower_docker_path }}"
owner: "{{ watchtower_docker_owner }}"
group: "{{ watchtower_docker_group }}"
state: directory
mode: 0755

- name: Create watchtower docker compose deployment
ansible.builtin.template:
src: docker-compose.yml.j2
dest: "{{ watchtower_docker_path }}/docker-compose.yml"
owner: "{{ watchtower_docker_owner }}"
group: "{{ watchtower_docker_group }}"
trim_blocks: false
mode: 0644
notify: restart watchtower

- name: Validate watchtower docker compose deployment
ansible.builtin.command: docker compose config --quiet
args:
chdir: "{{ watchtower_docker_path }}"
changed_when: false

- name: Startup watchtower docker compose deployment
ansible.builtin.command: >-
docker compose --ansi never --progress plain up --detach
args:
chdir: "{{ watchtower_docker_path }}"
register: watchtower_container_startup
changed_when: "'Started' in watchtower_container_startup.stdout"
16 changes: 16 additions & 0 deletions roles/watchtower/templates/docker-compose.yml.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{{ ansible_managed | comment }}
---
services:
watchtower:
image: "{{ watchtower_docker_image }}"
container_name: watchtower
restart: unless-stopped
logging:
driver: json-file
options:
max-size: "100m"
max-file: "10"
command: "{{ watchtower_docker_command }}"
volumes:
- "/etc/localtime:/etc/localtime:ro"
- "/var/run/docker.sock:/var/run/docker.sock:ro"

0 comments on commit a3b1aae

Please sign in to comment.