├── Dockerfile ├── README.md └── litestream ├── etc ├── litestream.primary.yml └── litestream.replica.yml └── start.sh /Dockerfile: -------------------------------------------------------------------------------- 1 | FROM litestream/litestream:pr-349 AS litestream 2 | 3 | # Use the Go image to build our application. 4 | FROM debian:stable-slim 5 | 6 | # Some utils for local dev 7 | RUN apt update && apt install -y procps sqlite3 8 | 9 | # Copy the present working directory to our source directory in Docker. 10 | # Change the current directory in Docker to our source directory. 11 | COPY litestream /litestream 12 | WORKDIR /litestream 13 | # Download the static build of Litestream directly into the path & make it executable. 14 | # This is done in the builder and copied as the chmod doubles the size. 15 | # ADD https://github.com/benbjohnson/litestream/releases/download/v0.3.8/litestream-v0.3.8-linux-amd64-static.tar.gz /tmp/litestream.tar.gz 16 | # RUN mkdir -p /litestream/bin && tar -C /litestream/bin -xzf /tmp/litestream.tar.gz 17 | 18 | COPY --from=litestream /usr/local/bin/litestream /litestream/bin/litestream 19 | 20 | # Create data directory (although this will likely be mounted too) 21 | RUN mkdir -p /data 22 | 23 | ENV DATABASE_URL=file:/data/sqlite.db 24 | 25 | EXPOSE 9090 26 | CMD [ "/litestream/start.sh" ] 27 | 28 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Litestream Base 2 | 3 | Add this to an existing Dockerfile as a stage: 4 | 5 | 6 | ```Dockerfile 7 | FROM flyio/litestream-base as litestream 8 | ``` 9 | 10 | Then, add these lines to the final stage: 11 | 12 | ```Dockerfile 13 | ## near the top of your dockerfile 14 | COPY --from=litestream /litestream /litestream 15 | 16 | 17 | # Replace your CMD with the litestream wrapper command 18 | 19 | CMD [ "/litestream/start.sh" , ""] 20 | ``` -------------------------------------------------------------------------------- /litestream/etc/litestream.primary.yml: -------------------------------------------------------------------------------- 1 | 2 | addr: ":9090" 3 | 4 | dbs: 5 | - path: ${LITESTREAM_DB_URL} 6 | -------------------------------------------------------------------------------- /litestream/etc/litestream.replica.yml: -------------------------------------------------------------------------------- 1 | addr: ":9091" 2 | 3 | dbs: 4 | - path: ${LITESTREAM_DB_URL} 5 | upstream: 6 | path: ${LITESTREAM_DB_URL} 7 | url: http://localhost:9090 -------------------------------------------------------------------------------- /litestream/start.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | set -e 3 | 4 | path_prefix="file:" 5 | export LITESTREAM_DB_URL=${DATABASE_URL#"$path_prefix"} 6 | echo "Primary: $FLY_PRIMARY_REGION, Current: $FLY_REGION, DB URL: ${LITESTREAM_DB_URL}" 7 | # Determine which configuration file to use based on region. 8 | if [ "$FLY_REGION" == "$FLY_PRIMARY_REGION" ] 9 | then 10 | echo "Configuring Litestream primary" 11 | cp -f /litestream/etc/litestream.primary.yml /etc/litestream.yml 12 | else 13 | echo "Configuring Litestream replica" 14 | cp -f /litestream/etc/litestream.replica.yml /etc/litestream.yml 15 | fi 16 | 17 | if [ $# -eq 0 ] 18 | then 19 | exec /litestream/bin/litestream replicate 20 | else 21 | # Run litestream with your app as the subprocess. 22 | exec /litestream/bin/litestream replicate --exec "$@" 23 | fi 24 | 25 | --------------------------------------------------------------------------------