From 23b32f55c54db11e28e463921bc5f286d153faa3 Mon Sep 17 00:00:00 2001 From: pajlada Date: Sun, 11 Dec 2022 12:30:09 +0100 Subject: [PATCH] Add docker-compose file (#395) --- CHANGELOG.md | 1 + README.md | 3 ++- docker-compose.yml | 48 ++++++++++++++++++++++++++++++++++++++ docker/README.md | 27 +++++++++++++++++++++ docker/chatterino2-api.env | 13 +++++++++++ docker/env | 11 +++++++++ docker/prometheus.yml | 16 +++++++++++++ 7 files changed, 118 insertions(+), 1 deletion(-) create mode 100644 docker-compose.yml create mode 100644 docker/README.md create mode 100644 docker/chatterino2-api.env create mode 100644 docker/env create mode 100644 docker/prometheus.yml diff --git a/CHANGELOG.md b/CHANGELOG.md index 24ea8c11..6d08e11f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,7 @@ - Fix: JSON responses now always return the proper content type. (#334) - Dev: Improve BetterTTV emote tests. (#282) - Minor: BetterTTV cache key changed from plural to singular form. (#282) +- Dev: Add docker-compose support. (#395) - Dev: Improve Twitch.tv clip tests. (#283) - Dev: Improve YouTube tests. (#284) - Dev: Resolver Check now returns a context. (#287) diff --git a/README.md b/README.md index 5fba82be..6f024c58 100644 --- a/README.md +++ b/README.md @@ -85,4 +85,5 @@ Uptime: 928h5m7.937821282s - Memory: Alloc=510 MiB, TotalAlloc=17419213 MiB, Sys If you host your own version of this API, you can modify which url Chatterino2 uses to resolve links and to resolve twitch emote sets. [Change link resolver](https://wiki.chatterino.com/Environment%20Variables/#chatterino2_link_resolver_url) [Change Twitch emote resolver](https://wiki.chatterino.com/Environment%20Variables/#chatterino2_twitch_emote_set_resolver_url) -[How to build and host](docs/build.md) +[How to build and host](docs/build.md) +[Running in Docker](docker/README.md) diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 00000000..07c8db26 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,48 @@ +version: "3.9" +services: + api: + restart: always + image: ghcr.io/chatterino/api:latest + environment: + - CHATTERINO_API_DSN=postgres://${PG_USER:-api}:${PG_PASS:-api}@db:5432/${PG_USER:-api} + - CHATTERINO_API_PROMETHEUS_BIND_ADDRESS=0.0.0.0:9182 + env_file: docker/chatterino2-api.env + ports: + - "${API_PORT:-1234}:1234" + depends_on: + - "db" + db: + image: postgres:15-alpine + restart: always + env_file: docker/env + environment: + - POSTGRES_USER=${PG_USER:-api} + - POSTGRES_PASSWORD=${PG_PASS:-api} + volumes: + - db:/var/lib/postgresql/data + prometheus: + restart: always + image: prom/prometheus:latest + env_file: docker/env + volumes: + - $PWD/docker/prometheus.yml:/etc/prometheus/prometheus.yml + - prometheus_data:/prometheus + ports: + - "${PROM_PORT:-9090}:9090" + command: + - '--config.file=/etc/prometheus/prometheus.yml' + - '--storage.tsdb.path=/prometheus' + pg_exporter: + restart: always + image: quay.io/prometheuscommunity/postgres-exporter:latest + env_file: docker/env + environment: + - DATA_SOURCE_URI=db?sslmode=disable + - DATA_SOURCE_USER=${PG_USER:-api} + - DATA_SOURCE_PASS=${PG_PASS:-api} + - PG_EXPORTER_EXCLUDE_DATABASES=template0,template1 + depends_on: + - "db" +volumes: + db: + prometheus_data: diff --git a/docker/README.md b/docker/README.md new file mode 100644 index 00000000..89a1398f --- /dev/null +++ b/docker/README.md @@ -0,0 +1,27 @@ +# Running in Docker + +In the root directory there's a `docker-compose.yml` file which defines the Chatterino API, a PostgreSQL database, and Prometheus + +You can configure those with the `env` and `chatterino2-api.env` files in this directory. + +If you're going to make changes to the env files directly in the repo, you will want to ignore them locally: +`git update-index --assume-unchanged env chatterino2-api.env` + +## Forwarding prometheus + +If you want to forward prometheus at a different route, you'll need to add a `--web.route-prefix` to the command in the `docker-compose.yml` file. For example: + +```diff + command: + - '--config.file=/etc/prometheus/prometheus.yml' + - '--storage.tsdb.path=/prometheus' ++ - '--web.route-prefix=/prometheus' +``` + +## Changing the base URL + +If you host this service under a subpath, you'll need to add and modify the `CHATTERINO_API_BASE_URL` environment variable to the `chatterino2-api.env` file. For example: + +```env +CHATTERINO_API_BASE_URL=https://braize.pajlada.com/chatterino +``` diff --git a/docker/chatterino2-api.env b/docker/chatterino2-api.env new file mode 100644 index 00000000..deaa44ea --- /dev/null +++ b/docker/chatterino2-api.env @@ -0,0 +1,13 @@ +CHATTERINO_API_LOG_LEVEL=info +CHATTERINO_API_ENABLE_ANIMATED_THUMBNAILS=true + +CHATTERINO_API_DISCORD_TOKEN= + +CHATTERINO_API_IMGUR_CLIENT_ID= + +CHATTERINO_API_TWITCH_CLIENT_ID= +CHATTERINO_API_TWITCH_CLIENT_SECRET= + +CHATTERINO_API_YOUTUBE_API_KEY= + +CHATTERINO_API_TWITTER_BEARER_TOKEN= diff --git a/docker/env b/docker/env new file mode 100644 index 00000000..30865fa8 --- /dev/null +++ b/docker/env @@ -0,0 +1,11 @@ +## .env file for docker-compose + +# Postgres credentials - Will default to "api" if not set +PG_USER= +PG_PASS= + +# Port to expose the API from. Use 127.0.0.1:1234 to expose on localhost only +API_PORT=127.0.0.1:1234 + +# Port to expose the Prometheus server from. Use 127.0.0.1:9090 to expose on localhost only +PROM_PORT=127.0.0.1:9090 diff --git a/docker/prometheus.yml b/docker/prometheus.yml new file mode 100644 index 00000000..191e6f6a --- /dev/null +++ b/docker/prometheus.yml @@ -0,0 +1,16 @@ +global: + scrape_interval: 15s + +scrape_configs: + - job_name: 'prometheus' + metrics_path: '/prometheus/metrics' + static_configs: + - targets: ['127.0.0.1:9090'] + + - job_name: 'postgres' + static_configs: + - targets: ['pg_exporter:9187'] + + - job_name: 'chatterino2_api' + static_configs: + - targets: ['api:9182']