askrobots / dbbasic-object-server
Public
Runs live, versioned Python application objects on one VM, with a suite of apps: projects, notes, tasks, files and more.
Dockerfile · 7.0 KB · 129 lines
| 1 | # DBBASIC Object Server — container image. |
| 2 | # |
| 3 | # This is a second deployment path alongside the bare-VM systemd installer |
| 4 | # (scripts/install.sh, docs/single-vm-deployment.md). See |
| 5 | # docs/docker-deployment.md for the full story, especially what ships baked |
| 6 | # into this image versus what must live on a mounted volume. |
| 7 | FROM python:3.14-slim AS app |
| 8 | |
| 9 | # Non-root service user, mirroring the `dbbasic` service user that |
| 10 | # scripts/install.sh creates for the bare-VM systemd unit -- the process |
| 11 | # does not run as root in the container either. |
| 12 | RUN useradd --system --create-home --home-dir /home/dbbasic --shell /usr/sbin/nologin dbbasic \ |
| 13 | && useradd --system --user-group --no-create-home --home-dir /nonexistent \ |
| 14 | --shell /usr/sbin/nologin dbbasic-sandbox |
| 15 | |
| 16 | # User objects (u_<uid>_*) run as `dbbasic-sandbox`, a second user that owns |
| 17 | # nothing, started by object_sandbox_launcher.py -- the one process that runs |
| 18 | # as root, and only to switch to that user (docs/runtime-contract.md "User |
| 19 | # Object Isolation"). The entrypoint starts it, then drops to `dbbasic` for |
| 20 | # the server. No capabilities beyond Docker's defaults are needed. |
| 21 | |
| 22 | # The code is NOT pip-installed. The server, the daemon and the tests all run |
| 23 | # from the source tree (`uvicorn object_server:app` puts the working directory |
| 24 | # on sys.path; `python object_daemon.py` puts the script's directory there), |
| 25 | # and the pyproject's py-modules list is not even the full set of modules the |
| 26 | # server imports. An editable install added a finder to every interpreter |
| 27 | # start (about 8 ms each, paid again by every sandbox worker boot) for no |
| 28 | # import that the working directory did not already give. Only the |
| 29 | # dependencies are installed. |
| 30 | # |
| 31 | # Owned by the service user from the start: the server writes into the |
| 32 | # packages directory at runtime, and chown-ing a copied tree afterwards would |
| 33 | # store the whole tree a second time as a layer. |
| 34 | RUN install -d -o dbbasic -g dbbasic /opt/dbbasic-object-server |
| 35 | WORKDIR /opt/dbbasic-object-server |
| 36 | |
| 37 | # Install the runtime dependencies in their own layer, before the source is |
| 38 | # copied, so ordinary code changes do not re-download them on every rebuild: |
| 39 | # the `server` extra (uvicorn, websockets) plus the `scheduler` extra |
| 40 | # (croniter). The scheduler extra is here because this one image also runs |
| 41 | # the daemon (docker-compose.yml's object-daemon service), and every |
| 42 | # schedule the shipped packages declare is cron: without croniter the daemon |
| 43 | # can never compute a next run, and no scheduled pass runs. Keep these pins |
| 44 | # in sync with pyproject.toml's [project.optional-dependencies] `server` and |
| 45 | # `scheduler` lists; tests/test_docker_dependencies.py fails when they differ. |
| 46 | RUN python -m pip install --no-cache-dir --upgrade pip \ |
| 47 | && python -m pip install --no-cache-dir "uvicorn>=0.30.0" "websockets>=12.0" "croniter>=2.0" |
| 48 | |
| 49 | # .dockerignore keeps git history, tests, docs, caches, and any local |
| 50 | # data/objects/ runtime state out of this layer -- see its comments. |
| 51 | COPY --chown=dbbasic:dbbasic . . |
| 52 | # Root runs only these two root-owned copies (and the root-owned interpreter, |
| 53 | # with -I -S): the checkout above is owned by `dbbasic`, and a file the |
| 54 | # server can edit must never be something root executes. |
| 55 | COPY --chown=root:root --chmod=0755 scripts/docker-entrypoint.sh /usr/local/bin/dbbasic-entrypoint |
| 56 | COPY --chown=root:root --chmod=0644 object_sandbox_launcher.py /usr/local/lib/dbbasic/object_sandbox_launcher.py |
| 57 | |
| 58 | # The git commit this image was built from, shown by GET /admin/status and the |
| 59 | # startup line (object_server._build_commit). Passed in because the build |
| 60 | # context has no .git (.dockerignore, and a `git archive` tree never has one). |
| 61 | # Declared late so a new value rebuilds only the layers after it. Empty means |
| 62 | # unknown; the server ignores anything that is not 7 to 40 hex characters. |
| 63 | ARG BUILD_COMMIT="" |
| 64 | RUN printf '%s\n' "$BUILD_COMMIT" > BUILD_COMMIT \ |
| 65 | && chown dbbasic:dbbasic BUILD_COMMIT |
| 66 | |
| 67 | # Compile the bytecode now, as the service user (so the files are its own), |
| 68 | # so a cold start never compiles and a read-only root filesystem loses |
| 69 | # nothing. Timestamp-checked: an edited source file is still picked up. |
| 70 | # `-l`: the top-level modules only; objects under packages/ are read as |
| 71 | # source by the sandbox, not imported. |
| 72 | USER dbbasic |
| 73 | RUN python -m compileall -q -j 0 -l . |
| 74 | USER root |
| 75 | |
| 76 | # Persistent state lives outside the checkout, the same separation |
| 77 | # docs/single-vm-deployment.md keeps between /opt (code) and /var/lib (data). |
| 78 | # These are the paths an operator mounts as volumes; see docker-compose.yml. |
| 79 | # PYTHONUNBUFFERED: the daemon prints its boot banner (croniter, sandbox, |
| 80 | # inert handlers) with plain print(); under Docker stdout is a pipe, so |
| 81 | # without this `docker logs` shows nothing until the buffer fills or the |
| 82 | # process stops. |
| 83 | ENV PYTHONUNBUFFERED=1 \ |
| 84 | DBBASIC_OBJECTS_DIR=/data/objects \ |
| 85 | DBBASIC_DATA_DIR=/data/state \ |
| 86 | DBBASIC_PACKAGES_DIR=/opt/dbbasic-object-server/packages |
| 87 | |
| 88 | # Pre-create the mount points and own them as the service user. When Docker |
| 89 | # mounts an empty named volume over one of these paths, it copies this |
| 90 | # layer's ownership into the new volume on first run, so the app never has |
| 91 | # to write into a root-owned directory. |
| 92 | RUN mkdir -p /data/objects /data/state \ |
| 93 | && chown -R dbbasic:dbbasic /data \ |
| 94 | && chmod 0700 /data/objects /data/state |
| 95 | |
| 96 | # The container starts as root so the entrypoint can start the sandbox |
| 97 | # launcher; it then runs the server as `dbbasic` (setpriv). For a one-off |
| 98 | # command use `docker compose exec -u dbbasic ...`, so files it writes on |
| 99 | # the volumes stay the server's. |
| 100 | USER root |
| 101 | |
| 102 | EXPOSE 8001 |
| 103 | |
| 104 | # Exec form, standard library only (no curl in the image): a connection error |
| 105 | # or a status of 400 or more fails the check, like `curl -f`. |
| 106 | HEALTHCHECK --interval=30s --timeout=5s --start-period=10s --retries=3 \ |
| 107 | CMD ["python", "-c", "import http.client,sys; c = http.client.HTTPConnection('127.0.0.1', 8001, timeout=4); c.request('GET', '/health'); sys.exit(c.getresponse().status >= 400)"] |
| 108 | |
| 109 | ENTRYPOINT ["/usr/local/bin/dbbasic-entrypoint"] |
| 110 | # Bind 0.0.0.0, unlike the bare-VM unit's 127.0.0.1 -- there, Caddy runs on |
| 111 | # the same host and reaches uvicorn over loopback; here, the container's own |
| 112 | # network namespace means Docker's port mapping / Coolify's proxy need |
| 113 | # 0.0.0.0 to reach this process at all. |
| 114 | CMD ["uvicorn", "object_server:app", "--host", "0.0.0.0", "--port", "8001"] |
| 115 | |
| 116 | # Optional "tools" image: the same server plus the few third-party libraries |
| 117 | # that ported tool objects import (image work, YAML, HTTP, Markdown). The server itself |
| 118 | # stays stdlib-only; build this only if you run such objects: |
| 119 | # docker build --target tools -t dbbasic-object-server:tools . |
| 120 | # Heavy OCR/ML stacks (tesseract, paddle, easyocr, opencv) are deliberately not |
| 121 | # here: run them as their own service. Pins are exact so rebuilds match. |
| 122 | FROM app AS tools |
| 123 | USER root |
| 124 | RUN python -m pip install --no-cache-dir \ |
| 125 | "pillow==12.0.0" "pyyaml==6.0.3" "requests==2.32.5" "markdown==3.10.2" |
| 126 | |
| 127 | # The default image (`docker build .`, docker-compose.yml) is the stdlib-only |
| 128 | # server above: Docker builds the LAST stage unless told otherwise. |
| 129 | FROM app |