# Write a Dockerfile for Reverhost

Every Reverhost app builds from a Dockerfile stored on the app. These rules keep it fast to build, small and safe.

## Start from a template

```
npx ezhst build dockerfile template
npx ezhst build dockerfile template node > Dockerfile
```

Templates exist for [Node.js](/languages/nodejs), [Bun](/languages/bun), [Python](/languages/python), [Go](/languages/go), [Ruby](/languages/ruby), [PHP](/languages/php), [Rust](/languages/rust) and [Zig](/languages/zig). Any other language works if you write the Dockerfile yourself.

## Pick the base image with FROM

The `FROM` line decides your language version. Prefer `-slim` or `-alpine` variants, such as `python:3.12-slim` or `node:22-alpine`, because the final image must stay under 1 GB.

## Copy manifests before source

Copy the dependency manifest and lockfile first, install, then copy the rest. Dependency layers are reused when only your code changes.

```Dockerfile
COPY package*.json ./
RUN npm ci
COPY . .
```

## Listen on 0.0.0.0 and PORT

Reverhost sends traffic to port `3000` and sets `PORT=3000`. Bind to `0.0.0.0`, not localhost, and add `EXPOSE 3000` for clarity. Write `CMD` with `sh -c "exec ..."` when it needs `$PORT`, so the variable expands and your process still receives signals.

## Pass build-time values as a secret

Your variables are runtime env in the container. A build step that needs them mounts the `appenv` secret:

```Dockerfile
RUN --mount=type=secret,id=appenv,target=/run/secrets/appenv sh -c 'set -a; . /run/secrets/appenv; set +a; npm run build'
```

Never use `ARG` or `ENV` for secrets. See [build-time environment variables](/guides/build-time-environment-variables).

## Use multi-stage builds

Compile in one stage and copy only the output into a small runtime image. Compilers, caches and dev dependencies then stay out of the final image. The [Rust or Go binary guide](/guides/deploy-rust-or-go-binary) shows a full example.

## Keep the upload small with .dockerignore

`ezhst deploy` already skips `node_modules`, `.git`, `.env*`, Python virtual environments, Rust `target` and Zig output. Add a `.dockerignore` for anything else that should not reach the build, such as test fixtures or local data.

```.dockerignore
node_modules
coverage
*.log
data/
```

## Save it to the app

```
npx ezhst build dockerfile set -f Dockerfile
npx ezhst deploy
```

A Dockerfile in your repository is not used until you upload it. After editing it, upload again and run `npx ezhst redeploy`, which rebuilds the latest upload even if it failed.

## Frequently asked questions

### Can I keep the Dockerfile in a subfolder?

Yes. Upload it from anywhere with `ezhst build dockerfile set -f path/to/Dockerfile`. Its `COPY` paths are relative to the build context, which `ezhst build set --root-dir` controls.

### What happens when the image is too large?

The build fails with the image size and the limit. Use a slimmer base image or a multi-stage build.
