# syntax=docker/dockerfile:1@sha256:ecfaec9ed6d810b56388c508f4121597bfbba70d41a6dfeee4d8cad5f295fc32
#
# Vallus (TypeScript) - the image built from a CUSTOMER PACKAGE.
#
# Not the same file as the one in the repository, and deliberately so. That one
# compiles the server and the frontend from source; a package contains neither.
# It ships `dist/` already built, so this image only has to provide the runtime
# around it. Building the repository's Dockerfile inside a package fails on a
# missing `server/src`, which is a confusing way to learn the difference.
#
# Build context is the PACKAGE ROOT (the directory holding dist/), not this
# folder - see docker-compose.yml.

ARG PLAYWRIGHT_VERSION=v1.60.0-jammy

# bun, the alternative to npm for a project's install and scripts. Copied from the
# official image rather than fetched with its install script, so the version is
# the pinned one. Pinned like the Docker CLI; the per-run images take oven/bun:1,
# so keep this on 1.x. Update with `docker manifest inspect oven/bun:<version>`.
ARG BUN_VERSION=1.4.4
FROM oven/bun:${BUN_VERSION} AS bun
FROM mcr.microsoft.com/playwright:${PLAYWRIGHT_VERSION}

# build-essential: test projects pull native npm dependencies that node-gyp has
# to compile, and the Playwright base carries browsers, not a C/C++ toolchain.
#
# The static Docker CLI is what the container executor talks to; there is no
# daemon in here, only a client for the socket mounted from the host.
RUN apt-get update \
 && apt-get install -y --no-install-recommends build-essential curl ca-certificates \
 && rm -rf /var/lib/apt/lists/* \
 && set -eux; \
    arch="$(dpkg --print-architecture)"; \
    case "$arch" in \
      amd64) tarball="x86_64" ;; \
      arm64) tarball="aarch64" ;; \
      *) echo "unsupported architecture: $arch" >&2; exit 1 ;; \
    esac; \
    curl -fsSL "https://download.docker.com/linux/static/stable/${tarball}/docker-27.3.1.tgz" \
      | tar -xz --strip-components=1 -C /usr/local/bin docker/docker; \
    docker --version

# bunx is the same binary under another name, as in the official image. The
# version check fails the build if the binary cannot run on this base.
COPY --from=bun /usr/local/bin/bun /usr/local/bin/bun
RUN ln -s bun /usr/local/bin/bunx && bun --version

WORKDIR /app
COPY package.json package-lock.json ./
# better-sqlite3 is stripped from the package (it is native and ships per
# platform), so it is fetched here for linux/x64 - the platform this image runs
# on, whatever the machine that built the package was.
RUN --mount=type=cache,target=/root/.npm npm install --omit=dev --no-audit --no-fund

COPY dist ./dist

# The runner's own data directory, and the working directory it resolves
# relative paths against.
WORKDIR /data

# HTTPS by default, from the runner itself: with no certificate given it issues
# one from a local CA it keeps in /data/.vallus/tls, so mount /data to keep the CA
# you trusted. VALLUS_PROTOCOL=http for plain HTTP (behind a TLS proxy, as the
# shipped docker-compose.yml does); VALLUS_TLS_CERT/VALLUS_TLS_KEY for a
# certificate of your own; VALLUS_TLS_HOSTS for the names and addresses the
# generated one should cover besides localhost.
ENV VALLUS_PROTOCOL=https

ENTRYPOINT ["node", "/app/dist/index.js"]
CMD ["--serve", "--port", "3500"]
