Skip to content

SSL HTTPS Configuration

Cyril Rohr edited this page Jun 2, 2023 · 6 revisions

Example using the Caddy reverse-proxy with automatic Let's Encrypt certificate generation:

# if you want to test locally, this requires Compose v2.17+
services:
  # use Caddy for SSL termination
  proxy:
    image: caddy:2
    restart: unless-stopped
    command: "caddy reverse-proxy --from '${PULLPREVIEW_URL}' --to web:4567"
    depends_on:
      - web
    ports:
      - "80:80"
      - "443:443"
    volumes:
      - "/data"

  # simple ruby web server for demo purposes, which connects to a postgres DB
  web:
    restart: unless-stopped
    depends_on:
      - db
    build:
      context: .
      dockerfile_inline: |
        FROM ruby:3.2
        RUN gem install sinatra pg puma
        CMD ruby -rsinatra -rpg -rpuma \
          -e'get("/"){ "Hey PullPreview user, here is the postgres version: #{PG.connect(ENV["PGURI"]).exec("SELECT VERSION()").getvalue(0,0)}" }'
        EXPOSE 4567
    environment:
      PGURI: "postgres://postgres:p4ssw0rd@db/postgres"
      APP_ENV: production

  db:
    restart: unless-stopped
    image: postgres:13
    environment:
      POSTGRES_PASSWORD: p4ssw0rd

The PULLPREVIEW_URL is one of the environment variables automatically available to your Docker Compose files, and contains the full URL to your ephemeral server.

An up to date configuration can be found in the Rails demo project. It also includes an example of how to restrict access to your preview environments with a simple Basic Auth configuration.