diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..6a2a193 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,4 @@ +.git +.env +data +pocketkv diff --git a/.env.example b/.env.example index 0544624..f7d652a 100644 --- a/.env.example +++ b/.env.example @@ -1,7 +1,11 @@ -# Dashboard login (HTTP Basic Auth). Both are required. +# Dashboard login (HTTP Basic Auth). Both are required. Don't use quotes. DASHBOARD_USER=admin DASHBOARD_PASSWORD=change-me -# Optional -ADDR=:8080 -DATA_FILE=data/pocketkv.json +# Docker (deploy.sh): host port to publish on, or ip:port. Default 8080. +# Use 127.0.0.1:8080 to only allow access through a local reverse proxy. +# PORT=8080 + +# Only for `go run` (the container sets these itself): +# ADDR=:8080 +# DATA_FILE=data/pocketkv.json diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..9fe80e7 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,15 @@ +FROM golang:1.25-alpine AS build +WORKDIR /src +COPY . . +RUN CGO_ENABLED=0 go build -trimpath -ldflags="-s -w" -o /pocketkv ./cmd/pocketkv + +FROM alpine:3.22 +RUN adduser -D -H -u 10001 pocketkv && mkdir /data && chown pocketkv:pocketkv /data +COPY --from=build /pocketkv /usr/local/bin/pocketkv +# Fixed inside the container: listen on 8080, keep data on the /data volume. +ENV ADDR=:8080 DATA_FILE=/data/pocketkv.json +USER pocketkv +VOLUME /data +EXPOSE 8080 +HEALTHCHECK --interval=30s --timeout=3s CMD wget -qO- http://127.0.0.1:8080/healthz || exit 1 +ENTRYPOINT ["pocketkv"] diff --git a/README.md b/README.md index 5a65e14..cdaf3ed 100644 --- a/README.md +++ b/README.md @@ -6,7 +6,7 @@ Simple minimal self hostable kv datastore you can use to serve dynamic read-only - **API** (read-only, CORS `*`): read one value or all values of a store with an API key. Keys are safe to embed in static sites. - Go standard library only. All data lives in one JSON file. -## Run +## Run locally ```sh cp .env.example .env # set DASHBOARD_USER / DASHBOARD_PASSWORD @@ -21,11 +21,47 @@ Open http://localhost:8080. | `DASHBOARD_PASSWORD` | required | dashboard password | | `ADDR` | `:8080` | listen address | | `DATA_FILE` | `data/pocketkv.json` | where everything is stored | +| `PORT` | `8080` | host port, Docker only (`deploy.sh`) | Real environment variables take precedence over `.env`. Back up by copying `DATA_FILE`. Basic Auth sends the password with every request, so put the dashboard behind HTTPS (e.g. a reverse proxy) if it is reachable from outside your network. +## Deploy with Docker + +On any Linux server with [Docker](https://docs.docker.com/engine/install/) installed: + +```sh +git clone https://github.com/katistix/pocketkv.git +cd pocketkv +cp .env.example .env +nano .env # set DASHBOARD_USER and DASHBOARD_PASSWORD +./deploy.sh +``` + +`deploy.sh` builds the image and starts a container named `pocketkv` in the background. It restarts automatically if it crashes or the server reboots. The dashboard is at `http://:8080`; set `PORT` in `.env` to use another port. + +Your data is kept in the Docker volume `pocketkv-data`, so it survives restarts, redeploys and updates. Inside the container, the listen address and data path are fixed, so `ADDR` and `DATA_FILE` in `.env` are ignored. + +```sh +docker logs -f pocketkv # follow the logs +./deploy.sh # apply .env changes +git pull && ./deploy.sh # update to the latest version +docker cp pocketkv:/data/pocketkv.json ./backup.json # back up all data +``` + +### HTTPS (recommended outside your LAN) + +To reach it from the internet, put a reverse proxy in front. With [Caddy](https://caddyserver.com), which gets a certificate automatically, the whole `/etc/caddy/Caddyfile` is: + +``` +kv.example.com { + reverse_proxy localhost:8080 +} +``` + +Then set `PORT=127.0.0.1:8080` in `.env` and run `./deploy.sh` again, so pocketkv is only reachable through Caddy. + ## API The OpenAPI spec is served at `/openapi.yaml` (source: [`internal/api/openapi.yaml`](internal/api/openapi.yaml)). @@ -50,6 +86,7 @@ internal/config/ .env + environment loading internal/store/ stores, values, API keys; persisted to one JSON file internal/api/ public read-only API + openapi.yaml internal/web/ dashboard handlers + html/template pages +Dockerfile image used by deploy.sh ``` The JSON editor is [Ace](https://ace.c9.io), loaded from jsDelivr. If the CDN can't be reached, the editor falls back to a plain textarea. diff --git a/deploy.sh b/deploy.sh new file mode 100755 index 0000000..eda2019 --- /dev/null +++ b/deploy.sh @@ -0,0 +1,32 @@ +#!/usr/bin/env bash +# Build the pocketkv image and (re)start it as a background container. +# Data lives in the Docker volume "pocketkv-data", so it survives redeploys. +set -euo pipefail +cd "$(dirname "$0")" + +NAME=pocketkv +VOLUME=pocketkv-data + +if [[ ! -f .env ]]; then + echo "Missing .env: run 'cp .env.example .env' and set DASHBOARD_USER / DASHBOARD_PASSWORD." >&2 + exit 1 +fi +command -v docker >/dev/null || { echo "Docker is not installed: https://docs.docker.com/engine/install/" >&2; exit 1; } + +# Host port (or ip:port) to publish on; read from .env, default 8080. +PORT=$(sed -n 's/^PORT=//p' .env | tail -n 1) +PORT=${PORT:-8080} + +docker build -t "$NAME" . +docker rm -f "$NAME" >/dev/null 2>&1 || true +docker run -d \ + --name "$NAME" \ + --restart unless-stopped \ + --env-file .env \ + -e ADDR=:8080 \ + -e DATA_FILE=/data/pocketkv.json \ + -p "$PORT:8080" \ + -v "$VOLUME:/data" \ + "$NAME" >/dev/null + +echo "pocketkv is running on port $PORT (logs: docker logs -f $NAME)"