# Dockerfile builds

If your repository has a Dockerfile, Zaroz builds it as written. This page is for people who write their own Dockerfile. The rule that decides between a Dockerfile and automatic detection is in [How builds are detected](/git-builds/detection).

## Which Dockerfile is used

By default Zaroz looks for a file called `Dockerfile` in the service's root directory. Set another path with `build.dockerfile`, relative to the root directory:

```toml
[build]
builder = "dockerfile"
dockerfile = "docker/production.Dockerfile"
```

- If `build.builder` is `auto` (the default) and the file exists, it is used.
- Set `build.builder = "dockerfile"` to be sure it is used. If the file is missing, the build stops with [`dockerfile_not_found`](/git-builds/troubleshooting#dockerfile_not_found).
- Set `build.builder = "railpack"` to ignore a Dockerfile you have.

## Build context

The **context** is the folder sent to the build. `COPY` and `ADD` can only read from it. By default it is the service's root directory.

A service in `apps/api` that copies shared code from `packages/` needs the repository root as its context:

```toml
[services.api]
root = "apps/api"
build.dockerfile = "Dockerfile"   # apps/api/Dockerfile
build.context = "."                # the repository root
```

A `.dockerignore` file is honoured. Zaroz reads `<context>/.dockerignore`, and `<Dockerfile name>.dockerignore` next to your Dockerfile when you have one. Ignore `node_modules`, `.git` and build output to send less and build faster.

## Multi-stage builds and the target

A multi-stage Dockerfile builds in one stage and copies only the result into a small final stage. This keeps compilers, caches and source out of the image:

```dockerfile
FROM node:22 AS build
WORKDIR /app
COPY package*.json ./
RUN npm ci
COPY . .
RUN npm run build

FROM node:22-slim AS runtime
WORKDIR /app
COPY --from=build /app/dist ./dist
COPY --from=build /app/node_modules ./node_modules
CMD ["node", "dist/server.js"]
```

The image is the **last stage**. To build another stage, set `build.target` to its name, for example `target = "runtime"`.

## Build arguments

`build.args` passes build arguments. Declare each with `ARG` in the Dockerfile, in the stage that uses it:

```dockerfile
ARG APP_VERSION_LABEL
RUN echo "$APP_VERSION_LABEL" > /app/version
```

Their values are **visible in the image**, so never put a password or a token there. See [Variables at build time](/git-builds/variables-at-build-time).

## Secrets: secret mounts

A build secret reaches your build as a BuildKit **secret mount**. The value exists only for that one `RUN` step and is never written into a layer, the image, the cache or the log:

```dockerfile
RUN --mount=type=secret,id=NPM_TOKEN,env=NPM_TOKEN npm ci
```

- `id` is the name of the secret, as in `build.secrets`.
- `env=NAME` puts the value in an environment variable for that step. Without it, the value is a file at `/run/secrets/<id>`.

Do not use `ARG` or `ENV` for a secret. Both end up in the image. See [Build secrets](/git-builds/build-secrets) for recipes. After changing a secret, choose **Rebuild without cache**, because Docker may reuse a cached layer that used the old one.

## How the app starts

The image's `CMD` and `ENTRYPOINT` start your app. To use another command without editing the Dockerfile, set `deploy.start`, which **replaces `CMD`**.

Your app must listen on `$PORT` (8080 by default), on all addresses. An `EXPOSE` line is used only as a starting value for the port in the console, never to route traffic. Set **Port** or `deploy.port` to change it. See [Languages](/git-builds/languages#the-port-your-app-must-listen-on-port).

## Pre-deploy command

The pre-deploy command runs from your image with `/bin/sh`, so the image needs a shell. An image with no shell fails the command. See [Pre-deploy and health checks](/git-builds/pre-deploy-and-health-checks).

## Size

An image can be up to **10 GiB**, compressed. Over that the build stops with [`image_too_large`](/git-builds/troubleshooting#image_too_large). To make an image smaller:

- use a multi-stage build and copy only what the app needs;
- start from a slim or distroless base image;
- delete package caches in the same `RUN` that creates them;
- use a `.dockerignore`.

Smaller images also deploy faster.

## What the builder can and cannot do

- **Base images** are pulled from public registries. Zaroz keeps a mirror of common ones (Docker Hub, GHCR, Quay and others), so builds do not depend on their rate limits. If the mirror is down, the build goes to the original registry.
- **Your base image is yours.** `FROM node:22` follows that tag. The build page shows the digest it resolved to. A tag that moves can change a rebuild of an old commit, which is why [rolling back](/git-builds/rollbacks) deploys the stored image and does not rebuild.
- **The build can reach the internet**, for example package registries, but **not private networks**: addresses such as `10.0.0.0/8`, `172.16.0.0/12` and `192.168.0.0/16`, link-local addresses, and your other services. Nothing can connect in. Port 25 is blocked.
- **The build runs in an isolated virtual machine, on its own.** It does not share a daemon, cache or disk with any other customer's build. There is no Docker socket, so `docker build` inside a `RUN` step, and `--privileged`, are not available.
- **Time and size limits apply.** A build stops at its timeout and the repository at 2 GiB when cloned. See [Limits](/git-builds/limits).
- **There is no access to your running service** while building. A migration that needs your database belongs in the [pre-deploy command](/git-builds/pre-deploy-and-health-checks), not in the Dockerfile.
