# Free Zig hosting

Run Zig, a low-level language with simple tooling, in an always-on container built from your Dockerfile. Start from the Zig template, deploy with the CLI and add Postgres and storage for $0.

## What you can run

Any Zig web app or API that listens on HTTP, including the standard `std.http.Server`, httpz and Zap.

- [Rust or Go binary](/guides/deploy-rust-or-go-binary)

## The starter Dockerfile

This is the `zig` template, exactly as `ezhst build dockerfile template zig` prints it.

```Dockerfile
FROM debian:bookworm-slim AS build
RUN apt-get update && apt-get install -y --no-install-recommends ca-certificates curl xz-utils git && rm -rf /var/lib/apt/lists/*
RUN ARCH="$(uname -m)" && FILE="zig-${ARCH}-linux-0.17.1.tar.xz" && \
    for URL in "https://zig.squirl.dev/${FILE}" "https://pkg.machengine.org/zig/${FILE}" "https://ziglang.org/download/0.17.1/${FILE}"; do \
      curl -fsSL --speed-limit 200000 --speed-time 20 -o /tmp/zig.tar.xz "$URL" && break; \
    done && \
    tar -xJf /tmp/zig.tar.xz -C /opt && rm /tmp/zig.tar.xz && \
    ln -s "/opt/zig-${ARCH}-linux-0.17.1/zig" /usr/local/bin/zig
WORKDIR /src
COPY . .
RUN --mount=type=secret,id=appenv,target=/run/secrets/appenv sh -c 'set -a; . /run/secrets/appenv; set +a; zig build --release=fast -Dcpu=baseline && mkdir -p /out && cp -r zig-out/. /out/ && mv /out/bin/* /out/app && rmdir /out/bin'

FROM debian:bookworm-slim
RUN apt-get update && apt-get install -y --no-install-recommends ca-certificates && rm -rf /var/lib/apt/lists/*
WORKDIR /app
COPY --from=build /out/ /app/
EXPOSE 3000
CMD ["/app/app"]
```

## Adapt it to your app

The template downloads Zig 0.17.1 from fast community mirrors, runs `zig build --release=fast -Dcpu=baseline` and copies everything in `zig-out` (the binary from `zig-out/bin` as `/app/app`, plus folders like `static`) into a clean Debian image, with `/app` as the working directory. Keep `-Dcpu=baseline`: without it the binary targets the build machine's CPU and can crash on the server that runs it. Change the version in the download line if your code needs another Zig release.

- Listen on `0.0.0.0` and the `PORT` variable, which is `3000`. A server bound to localhost never receives traffic.
- Your variables are runtime env in the container. If a build command needs them, run it in a `RUN` step that mounts the `appenv` secret, as the template does. See [build-time environment variables](/guides/build-time-environment-variables).
- Never put secrets in `ARG` or `ENV`, because they end up in the image.

```src/main.zig
const std = @import("std");

pub fn main() !void {
    const port_text = std.posix.getenv("PORT").?;
    const port = try std.fmt.parseInt(u16, port_text, 10);
    const address = try std.net.Address.parseIp("0.0.0.0", port);
    var server = try address.listen(.{ .reuse_address = true });
    defer server.deinit();
}
```

## Deploy it

```
npx ezhst projects create my-project --link
npx ezhst apps create my-app
npx ezhst build dockerfile template zig --apply
npx ezhst env import .env
npx ezhst deploy
```

`--apply` saves the template to the app. To use an edited copy instead, save it as `Dockerfile` and run `npx ezhst build dockerfile set -f Dockerfile`. Read [how to write a Dockerfile for Reverhost](/guides/write-a-dockerfile) for the general rules.

## Limits

Each app gets 256 MB of RAM with no swap, 0.5 CPU and images up to 1 GB. A project holds 2 apps, a 128 MB Postgres database and 256 MB of S3 storage. One build runs at a time per app, with up to 10 builds a day on the free plan. See [pricing and limits](/pricing).

## Connect Postgres and S3

```
npx ezhst db create main
npx ezhst db info --json
npx ezhst storage create files
npx ezhst storage info --json
```

Use a Zig Postgres client such as pg.zig with the value of `DATABASE_URL`. Call the S3 API with your own signed requests, or generate presigned URLs in another service and upload to them from Zig. See the [Postgres](/products/postgres) and [storage](/products/storage) product pages.

## Frequently asked questions

### Is Zig hosting on Reverhost free?

Yes. It costs $0 per month with no credit card and no trial period, within the published limits.

### Is a Dockerfile in my repository used automatically?

No. The Dockerfile is stored on the app. Save it with `ezhst build dockerfile set -f Dockerfile`, or apply a template, before you deploy.

### Which Zig version does the template use?

Zig 0.17.1, downloaded from community mirrors with ziglang.org as the fallback. Change the version in the download line of your Dockerfile if you need another release.
