Plausible self-hosted with Docker and Nomad

Guide on how to host Plausible with Docker on Hashi Nomad & Consul infrastructure.

Plausible self-hosted with Docker and Nomad
11 Apr 2023
|
1 min read

This guide will show you how to host Plausible on Nomad & Consul. Please see the Plausible self-hosting Docs for more information.

Requirements

Prequisites

  1. Note that this templates uses the Nomad Server client.disable_file_sandbox option se we can mount the environment config file as env values

  2. Create a folder on your host machine. In the example we will use /mnt/plausible

  3. Update the values in plausible-conf.env

Folder tree

1mnt/
2├─ plausible/
3│  ├─ db-data/
4│  ├─ event-data/
5│  ├─ plausible-conf.env

plausible-conf.env

1BASE_URL=...
2SECRET_KEY_BASE=...

Nomad Job Template

  1job "plausible" {
  2  datacenters = ["dc1"]
  3  type        = "service"
  4
  5  group "database" {
  6    count = 1
  7
  8    network {
  9      mode = "bridge"
 10
 11      port "db" {
 12        to = 5432
 13      }
 14    }
 15
 16    service {
 17      name = "plausible-database"
 18      port = 5432
 19
 20      connect {
 21        sidecar_service {}
 22      }
 23    }
 24
 25    task "postgres" {
 26      driver = "docker"
 27
 28      config {
 29        image = "postgres:14-alpine"
 30        ports = ["db"]
 31
 32        mount {
 33          type     = "bind"
 34          target   = "/var/lib/postgresql/data"
 35          source   = "/mnt/plausible/db-data/"
 36          readonly = false
 37          bind_options {
 38            propagation = "rshared"
 39          }
 40        }
 41      }
 42
 43      env {
 44        POSTGRES_PASSWORD = "postgres"
 45      }
 46    }
 47  }
 48
 49  group "events" {
 50    count = 1
 51
 52    network {
 53      mode = "bridge"
 54
 55      port "events" {
 56        to = 8123
 57      }
 58    }
 59
 60    service {
 61      name = "plausible-events"
 62      port = 8123
 63
 64      connect {
 65        sidecar_service {}
 66      }
 67    }
 68
 69    task "clickhouse" {
 70      driver = "docker"
 71
 72      config {
 73        image = "clickhouse/clickhouse-server:22.6-alpine"
 74        ports = ["events"]
 75
 76        mount {
 77          type     = "bind"
 78          target   = "/var/lib/clickhouse"
 79          source   = "/mnt/plausible/event-data/"
 80          readonly = false
 81          bind_options {
 82            propagation = "rshared"
 83          }
 84        }
 85
 86        mount {
 87          type     = "bind"
 88          target   = "/etc/clickhouse-server/config.d/logging.xml"
 89          source   = "local/clickhouse-config.xml"
 90          readonly = true
 91          bind_options {
 92            propagation = "rshared"
 93          }
 94        }
 95
 96        mount {
 97          type     = "bind"
 98          target   = "/etc/clickhouse-server/users.d/logging.xml"
 99          source   = "local/clickhouse-user-config.xml"
100          readonly = true
101          bind_options {
102            propagation = "rshared"
103          }
104        }
105
106        ulimit {
107          nofile = "262144:262144"
108        }
109      }
110
111      template {
112        data        = <<EOF
113        <clickhouse>
114            <logger>
115                <level>warning</level>
116                <console>true</console>
117            </logger>
118
119            <!-- Stop all the unnecessary logging -->
120            <query_thread_log remove="remove"/>
121            <query_log remove="remove"/>
122            <text_log remove="remove"/>
123            <trace_log remove="remove"/>
124            <metric_log remove="remove"/>
125            <asynchronous_metric_log remove="remove"/>
126            <session_log remove="remove"/>
127            <part_log remove="remove"/>
128        </clickhouse>
129        EOF
130        destination = "local/clickhouse-config.xml"
131      }
132
133      template {
134        data        = <<EOF
135        <clickhouse>
136            <profiles>
137                <default>
138                    <log_queries>0</log_queries>
139                    <log_query_threads>0</log_query_threads>
140                </default>
141            </profiles>
142        </clickhouse>
143        EOF
144        destination = "local/clickhouse-user-config.xml"
145      }
146
147      resources {
148        cpu    = 100
149        memory = 512
150      }
151    }
152  }
153  
154  group "plausible" {
155    count = 1
156
157    network {
158      mode = "bridge"
159
160      port "http" {
161        to = 8000
162      }
163    }
164
165    service {
166      name = "plausible-web"
167      port = "http"
168
169      connect {
170        sidecar_service {
171          proxy {
172            upstreams {
173              destination_name = "plausible-database"
174              local_bind_port  = 5432
175            }
176            upstreams {
177              destination_name = "plausible-events"
178              local_bind_port  = 8123
179            }
180          }
181        }
182      }
183    }
184
185    task "plausible" {
186      driver = "docker"
187
188      config {
189        image   = "plausible/analytics:latest"
190        ports   = ["http"]
191        command = "sh"
192        args    = ["/start.sh"]
193
194        mount {
195          type     = "bind"
196          target   = "/start.sh"
197          source   = "local/start.sh"
198          readonly = true
199          bind_options {
200            propagation = "rshared"
201          }
202        }
203      }
204
205      template {
206        data        = <<EOF
207        #!/bin/sh
208        sleep 10
209        sh /entrypoint.sh db createdb
210        sh /entrypoint.sh db migrate
211        sh /entrypoint.sh run
212        EOF
213        destination = "local/start.sh"
214      }
215
216      template {
217        source      = "/mnt/plausible/plausible-conf.env"
218        destination = "local/plausible-conf.env"
219        env         = true
220      }
221
222      env {
223        DATABASE_URL            = "postgres://postgres:postgres@${NOMAD_UPSTREAM_ADDR_plausible_database}/plausible_db"
224        CLICKHOUSE_DATABASE_URL = "http://${NOMAD_UPSTREAM_ADDR_plausible_events}/plausible_events_db"
225      }
226    }
227  }
228}


Read more...