Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
182 changes: 182 additions & 0 deletions .github/scripts/smoke-test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
#!/usr/bin/env bash
# Boots the Laravel for Rust dev image the way Ingot runs it and checks it
# does what the README promises, so a broken image never gets a tag. Run by
# build.yml on each architecture's own runner; also runnable by hand:
#
# .github/scripts/smoke-test.sh <image>
# .github/scripts/smoke-test.sh laravel-rust:test
#
# It makes a new app with `laravel new --rust` inside the image, then:
# - runs `laravel migrate` on its own (no npm install for that);
# - runs the image's default `laravel dev` and waits for /up to answer 200
# (node_modules is installed first, by the entrypoint);
# - checks the files it wrote belong to the calling user;
# - edits src/ and waits for the server to restart;
# - checks `docker stop` is prompt.
# Needs network access (crates.io, npm, the starter app) and Docker. Leaves
# nothing behind: containers, network, volumes and the app folder are
# removed on exit.
set -euo pipefail

if [ "$#" -ne 1 ]; then
echo "usage: $0 <image>" >&2
exit 2
fi
image=$1

# Seconds to wait for each step. The first compile of a new app is cold in
# CI; the defaults leave plenty of room on a GitHub-hosted runner.
up_timeout=${SMOKE_UP_TIMEOUT:-900}
restart_timeout=${SMOKE_RESTART_TIMEOUT:-300}

# Logs are read into a variable before searching: with pipefail,
# `docker logs | grep -q` fails whenever grep exits at its first match
# before docker logs has finished writing.
logs() { docker logs "$@" "$prefix-app" 2>&1 || true; }

fail() { echo "SMOKE TEST FAILED ($image): $*" >&2; exit 1; }
step() { echo "--- $*"; }

prefix="laravel-rust-smoke-$$"
work=$(mktemp -d)
uid=$(id -u)

cleanup() {
docker rm -f "$prefix-app" "$prefix-db" >/dev/null 2>&1 || true
docker network rm "$prefix" >/dev/null 2>&1 || true
for v in registry git build; do
docker volume rm "$prefix-cargo-$v" >/dev/null 2>&1 || true
done
# The app's files belong to $uid, but a failed run can leave some that
# don't; remove them as root from inside a container to be sure.
docker run --rm --user 0 -v "$work:/w" --entrypoint sh "$image" -c 'rm -rf /w/* /w/.[!.]*' >/dev/null 2>&1 || true
rm -rf "$work" || true
}
trap cleanup EXIT

step "toolchain"
docker run --rm --entrypoint sh "$image" -c '
set -e
cargo --version; rustc --version; cargo fmt --version; cargo clippy --version
node --version; npm --version
laravel --version
command -v tini
' || fail "a tool is missing or doesn't run"
image_uid=$(docker run --rm --entrypoint id "$image" -u) || fail "id did not run"
[ "$image_uid" != 0 ] || fail "runs as root by default"

step "services"
docker network create "$prefix" >/dev/null
docker run -d --name "$prefix-db" --network "$prefix" \
-e POSTGRES_USER=app -e POSTGRES_PASSWORD=app -e POSTGRES_DB=app \
postgres:17-alpine >/dev/null
for v in registry git build; do
docker volume create "$prefix-cargo-$v" >/dev/null
done
volumes=(
-v "$prefix-cargo-registry:/usr/local/cargo/registry"
-v "$prefix-cargo-git:/usr/local/cargo/git"
-v "$prefix-cargo-build:/usr/local/cargo/build"
)
env=(
-e APP_URL=https://example-app.test
-e DATABASE_URL="postgres://app:app@$prefix-db/app"
)

# The calling user's UID in group 0, as Ingot runs it. On a GitHub runner
# that UID is 1001, not the image's default 1000, which is the point: the
# cache folders and HOME must work for any UID in group 0.
step "laravel new (as $uid:0)"
docker run --rm --user "$uid:0" -v "$work:/work" -w /work "${volumes[@]}" \
"$image" laravel new example-app --rust </dev/null \
|| fail "laravel new failed"
app="$work/example-app"
[ -f "$app/Cargo.toml" ] || fail "laravel new made no Cargo.toml"
[ -f "$app/package.json" ] || fail "laravel new made no package.json"
# A cloned project arrives without node_modules; start from that.
rm -rf "$app/node_modules"

# Over TCP, as the app connects: during its first start the image runs
# a temporary server on the Unix socket only.
step "waiting for Postgres"
for _ in $(seq 1 60); do
docker exec "$prefix-db" pg_isready -h 127.0.0.1 -q -U app -d app 2>/dev/null && break
sleep 1
done
docker exec "$prefix-db" pg_isready -h 127.0.0.1 -q -U app -d app || fail "Postgres didn't start"

step "laravel migrate without laravel dev"
docker run --rm --user "$uid:0" --network "$prefix" -v "$app:/app" \
"${volumes[@]}" "${env[@]}" "$image" laravel migrate --force \
|| fail "laravel migrate failed"
[ ! -e "$app/node_modules" ] || fail "laravel migrate installed node_modules"

step "laravel dev"
docker run -d --name "$prefix-app" --user "$uid:0" --network "$prefix" \
-p 127.0.0.1::80 -v "$app:/app" "${volumes[@]}" "${env[@]}" "$image" >/dev/null
port=$(docker port "$prefix-app" 80/tcp | head -n1 | sed 's/.*://')
url="http://127.0.0.1:$port/up"

wait_up() {
local timeout=$1 start code
start=$(date +%s)
while :; do
code=$(curl -s -o /dev/null -w '%{http_code}' --max-time 5 "$url" || true)
[ "$code" = 200 ] && return 0
[ -n "$(docker ps -q -f "name=^$prefix-app\$")" ] || return 1
[ $(( $(date +%s) - start )) -lt "$timeout" ] || return 1
sleep 5
done
}

start=$(date +%s)
if ! wait_up "$up_timeout"; then
docker logs --tail 60 "$prefix-app" >&2 || true
fail "/up did not answer 200 within ${up_timeout}s"
fi
echo "/up answered 200 after $(( $(date +%s) - start ))s"

[[ "$(logs)" == *"docker-entrypoint: node_modules isn't installed"* ]] \
|| fail "the entrypoint didn't install node_modules"
for path in target node_modules; do
owner=$(stat -c %u "$app/$path") || fail "$path is missing"
[ "$owner" = "$uid" ] || fail "$path belongs to UID $owner, not $uid"
done

step "restart on an edit to src/"
main=$(grep -rl 'fn main' "$app/src" | head -n1)
[ -n "$main" ] || fail "no fn main in src/"
since=$(date +%s)
echo "// smoke test edit" >>"$main"
start=$(date +%s)
restarted=
while [ $(( $(date +%s) - start )) -lt "$restart_timeout" ]; do
# The old server keeps answering /up until nodemon has killed it, so
# wait for cargo to finish the new build after the restart too.
recent=$(logs --since "$since")
if [[ "$recent" == *'restarting due to changes'* && "${recent#*restarting due to changes}" == *Finished* ]]; then
restarted=1
break
fi
sleep 2
done
if [ -z "$restarted" ]; then
docker logs --tail 40 "$prefix-app" >&2 || true
fail "the server didn't restart within ${restart_timeout}s of an edit"
fi
if ! wait_up "$restart_timeout"; then
docker logs --tail 40 "$prefix-app" >&2 || true
fail "/up did not answer 200 again after the restart"
fi
echo "restarted after $(( $(date +%s) - start ))s"

step "docker stop"
start=$(date +%s)
docker stop "$prefix-app" >/dev/null
took=$(( $(date +%s) - start ))
# docker stop's own timeout is 10s; reaching it means the signal didn't get
# through and the processes were killed.
[ "$took" -lt 5 ] || fail "docker stop took ${took}s"
echo "stopped in about ${took}s"

echo "smoke test passed: $image"
170 changes: 170 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
name: Build

# Builds the Laravel for Rust dev image on a native runner for each
# architecture and boots it with a new app (.github/scripts/smoke-test.sh).
#
# Publishing is off until the repository variable PUBLISH_IMAGES is "true"
# (Settings > Secrets and variables > Actions > Variables). With it on, a
# push to main, the weekly rebuild or a manual run on main also pushes each
# architecture's image by digest before its smoke test, and tags the
# multi-arch image only once both have passed. Pull requests never publish.
on:
pull_request:
paths-ignore: ["**.md"]
push:
branches: [main]
paths-ignore: ["**.md"]
# Weekly rebuild, so fixes in the rust and node base images and Debian's
# packages reach the tag without a Dockerfile change. Scheduled runs skip
# the layer cache.
schedule:
- cron: "0 11 * * 1" # Mondays 11:00 UTC
workflow_dispatch:

# A newer push supersedes whatever this workflow is still building for an
# older commit of the same branch or pull request. On main a running run
# is never cancelled, because it could stop its publish job between the
# `dev` and `dev-<sha>` tags; a newer run waits for it (and a run still
# waiting is replaced by a newer one, so that commit gets no `dev-<sha>`).
concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: ${{ github.ref != 'refs/heads/main' }}

env:
REGISTRY: ghcr.io
IMAGE_NAME: ${{ github.repository_owner }}/laravel-rust
# Only from main: a manual run on another branch must not move `dev`.
PUBLISH: ${{ vars.PUBLISH_IMAGES == 'true' && github.event_name != 'pull_request' && github.ref == 'refs/heads/main' }}

jobs:
build:
name: build (${{ matrix.arch }})
runs-on: ${{ matrix.arch == 'arm64' && 'ubuntu-24.04-arm' || 'ubuntu-latest' }}
# A hung laravel new or npm install would otherwise hold the runner
# for GitHub's 6-hour default.
timeout-minutes: 45
permissions:
contents: read
packages: write
strategy:
fail-fast: false
matrix:
arch: [amd64, arm64]
steps:
- name: Checkout code
uses: actions/checkout@v7

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v4

- name: Build
id: build_local
if: ${{ env.PUBLISH != 'true' }}
uses: docker/build-push-action@v7
with:
context: laravel-rust
file: laravel-rust/Dockerfile.dev
platforms: linux/${{ matrix.arch }}
load: true
tags: laravel-rust:test
no-cache: ${{ github.event_name == 'schedule' }}
cache-from: type=gha,scope=laravel-rust-dev-${{ matrix.arch }}
cache-to: type=gha,mode=max,scope=laravel-rust-dev-${{ matrix.arch }}

- name: Log in to GHCR
if: ${{ env.PUBLISH == 'true' }}
uses: docker/login-action@v4
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}

# By digest only, with no tag: the smoke test below runs this exact
# image, and the merge job tags it only if both architectures pass.
- name: Build and push by digest
id: build_push
if: ${{ env.PUBLISH == 'true' }}
uses: docker/build-push-action@v7
with:
context: laravel-rust
file: laravel-rust/Dockerfile.dev
platforms: linux/${{ matrix.arch }}
outputs: type=image,name=${{ env.REGISTRY }}/${{ env.IMAGE_NAME }},push-by-digest=true,name-canonical=true,push=true
no-cache: ${{ github.event_name == 'schedule' }}
cache-from: type=gha,scope=laravel-rust-dev-${{ matrix.arch }}
cache-to: type=gha,mode=max,scope=laravel-rust-dev-${{ matrix.arch }}

- name: Smoke test
env:
IMAGE: ${{ env.PUBLISH == 'true' && format('{0}/{1}@{2}', env.REGISTRY, env.IMAGE_NAME, steps.build_push.outputs.digest) || 'laravel-rust:test' }}
run: .github/scripts/smoke-test.sh "$IMAGE"

- name: Export digest
if: ${{ env.PUBLISH == 'true' }}
env:
DIGEST: ${{ steps.build_push.outputs.digest }}
run: |
mkdir -p /tmp/digests
touch "/tmp/digests/${DIGEST#sha256:}"

- name: Upload digest
if: ${{ env.PUBLISH == 'true' }}
uses: actions/upload-artifact@v4
with:
name: digest-laravel-rust-dev-${{ matrix.arch }}
path: /tmp/digests/*
if-no-files-found: error
retention-days: 1

# Puts the two tested digests under one multi-arch tag.
publish:
needs: build
if: ${{ vars.PUBLISH_IMAGES == 'true' && github.event_name != 'pull_request' && github.ref == 'refs/heads/main' }}
runs-on: ubuntu-latest
timeout-minutes: 10
# One publish at a time, across all runs.
concurrency:
group: publish-laravel-rust-dev
cancel-in-progress: false
permissions:
contents: read
packages: write
steps:
# Exact artifact names: a missing architecture fails here instead of
# publishing a single-arch tag.
- name: Download digest (amd64)
uses: actions/download-artifact@v4
with:
path: /tmp/digests/amd64
name: digest-laravel-rust-dev-amd64

- name: Download digest (arm64)
uses: actions/download-artifact@v4
with:
path: /tmp/digests/arm64
name: digest-laravel-rust-dev-arm64

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v4

- name: Log in to GHCR
uses: docker/login-action@v4
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}

# `dev` is the tag Ingot runs (project.LaravelRustImage); `dev-<sha>`
# pins this exact build.
- name: Create manifest list and push
working-directory: /tmp/digests
run: |
test "$(find amd64 -type f | wc -l)" -eq 1
test "$(find arm64 -type f | wc -l)" -eq 1
amd64=$(find amd64 -type f -printf '%f')
arm64=$(find arm64 -type f -printf '%f')
docker buildx imagetools create \
-t "$REGISTRY/$IMAGE_NAME:dev" \
-t "$REGISTRY/$IMAGE_NAME:dev-$GITHUB_SHA" \
"$REGISTRY/$IMAGE_NAME@sha256:${amd64}" \
"$REGISTRY/$IMAGE_NAME@sha256:${arm64}"
34 changes: 34 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,37 @@
# docker-rust

Docker images for Rust apps, made for local development with [Ingot](https://github.com/aliasproject/ingot). The PHP images live in [docker-php](https://github.com/aliasproject/docker-php).

| Image | Folder | Tag |
|---|---|---|
| Laravel for Rust dev image | `laravel-rust/` | `ghcr.io/aliasproject/laravel-rust:dev` (not published yet) |

## Laravel for Rust dev image

`laravel-rust/Dockerfile.dev` is a development image for [Laravel for Rust](https://laravel.com/rust/docs) apps (the `laravel-rust` crate), as Ingot runs them. Build it from its folder:

```sh
docker build -f laravel-rust/Dockerfile.dev -t laravel-rust:dev laravel-rust
```

It has Rust 1.99 with `rustfmt` and `clippy`, Node 24, and the Laravel CLI 0.3.0. The CLI is a pinned release archive, checked against the release's `sha256.sum`. The image is Debian-based because the CLI has no Alpine build for arm64.

The project is mounted at `/app`, and the container runs `laravel dev`, which runs the app's `npm run dev`. In an app made with `laravel new`, that script runs three processes: the server, which `nodemon` restarts with `cargo run` when `src/`, `Cargo.toml`, `Cargo.lock` or `.env` changes; a queue worker; and Vite. If `node_modules` isn't installed, the container's own `laravel dev` command installs it first. Other commands, such as `laravel migrate`, don't, and neither does `laravel dev` wrapped in a shell or with options before `dev`. After a lockfile change, run `npm ci` in the container yourself. Those packages are built for Linux. On macOS or Windows, where `/app` is your host folder, a host-side `npm run dev` then fails on them, so run npm in the container, or run `npm ci` again on the host before using it there.

- **Address:** the app listens on `SERVER_HOST=0.0.0.0` and `SERVER_PORT=80`. Real environment variables take precedence over the app's `.env`.
- **User:** run the container as your own UID in group 0 (`--user "$(id -u):0"`), so the files it writes to `/app` (`target/`, `node_modules/`, `public/build/`) belong to you. `HOME` is `/home/app`, writable by group 0.
- **Caches:** `CARGO_HOME` is `/usr/local/cargo`. Mount volumes at its `registry`, `git` and `build` folders to keep downloaded crates and compiled dependencies across containers. New apps compile into `build/`, through Cargo's `build-dir`. The folders exist in the image, writable by group 0, so a new volume mounted there is writable too. Don't mount a volume over `CARGO_HOME` itself: it also holds `cargo` and `rustc`, which the volume would pin to an old version.
- **Stopping:** `tini` is PID 1 and passes `docker stop`'s signal to the whole process group, so `laravel dev` and the processes it runs stop promptly.
- **Console commands:** use `laravel migrate`, `laravel route:list` and so on.

A Laravel for Rust app ships to production as one release binary, built by the app's own Dockerfile; this image is only for development.

## Checks and publishing

`.github/workflows/build.yml` builds the image on a native amd64 runner and a native arm64 runner, on pull requests and pushes to `main` (except changes to Markdown files only), every Monday and on demand. On each, `.github/scripts/smoke-test.sh` makes a new app with `laravel new --rust`, runs it with `laravel dev` as a UID other than the image's default, and checks that `/up` answers, that an edit restarts the server, that the files belong to the calling user and that `docker stop` is prompt. Run it by hand against any local tag:

```sh
.github/scripts/smoke-test.sh laravel-rust:dev
```

Publishing is off. To turn it on, set the repository variable `PUBLISH_IMAGES` to `true`. Then pushes to `main`, the weekly rebuild and manual runs on `main` push each architecture's image by digest, smoke-test that exact image, and tag the pair as `dev` and `dev-<commit>` only once both pass. Pull requests never publish. After the first publish, make the `laravel-rust` package public in its GHCR settings, because Ingot pulls without logging in.
Loading