# Dockerfile clusters

Build your app from its Dockerfile and run it as containers, with deploys that never drop a request

A Dockerfile cluster builds your app's Dockerfile into an image once, on one server, and runs your processes as containers on every server. Deploys swap containers behind Caddy one server at a time, so they don't drop requests, and a rollback starts an image that's already built.

Dockerfile clusters are in beta. Ask us in support to turn them on for your account.

## Choosing a cluster type

When you create a cluster, Hatchbox asks how your app builds:

- **Let Hatchbox install everything.** Ruby, Node and your dependencies are installed on each server, every server builds each deploy, and your processes run as systemd units. This is how Hatchbox has always worked and it needs nothing in your repository.
- **Use the Dockerfile in my repo.** Hatchbox builds your Dockerfile and runs containers. A Rails app without a Dockerfile gets one generated on each deploy (see *The generated Dockerfile*).

The choice is fixed for the cluster. To move an app over, create a Dockerfile cluster, create the app on it with the same env vars, deploy, then point your domain at the new servers. Processes, cron jobs, env vars, domains, SSL and logs work the same way on both kinds; the difference is how your code gets onto the servers and what a deploy does.

## What you need

- **A Dockerfile, or a Rails app.** Hatchbox builds the Dockerfile at the root of your repository, or at the path you set in the app's settings, as it is. Rails 7.1 and newer generate one with `rails new`. A Rails app whose repository has no Dockerfile gets a generated one.
- **A health check path** that returns a 2xx or 3xx when the app is ready. Rails 7.1 and newer answer `/up`. An app without that route counts as ready once it answers `/up` at all, even with a 404. Set another path in the app's Health check setting for a stricter check.
- **Ubuntu 22.04 or newer** on the servers. Hatchbox installs Docker from Docker's own repository.
- **One architecture per cluster.** The image is built on one server and run on the others, so every server must be amd64 or every one arm64.

## What a deploy does

1. **Build.** The build server (the first app server you provisioned, shown on the cluster page) checks out the commit, with its git submodules, and runs `docker build`. The image is tagged with the commit and pushed to a registry that runs on the build server, over TLS, on the private network.
2. **Pull.** Every other app server pulls the image.
3. **Migrate.** The pre-deploy command runs once, in a throwaway container from the new image, with your env vars. For a Rails app that's `bin/rails db:migrate`. A failure stops the deploy before anything changes. Change or blank the command under **Before each deploy** in the app's settings.
4. **Swap, one server at a time.** New containers start for each of the app's processes. Hatchbox waits for your health check path to answer, points Caddy at the new web container and reloads it, then stops the old containers. Workers swap the same way without the health check.
5. **Clean up.** The last five images stay on each server for rollback.

Old and new containers run side by side during a swap, so leave each server room for about twice your app's memory.

Rollback from the app's releases list starts the retained image and swaps to it, with no rebuild. Restarting the app and changing env vars swap containers the same way, so neither drops requests.

A private submodule on the same host as your app's repository needs a relative URL (`../theme.git`), which is fetched with the app's access. Absolute URLs are fetched without credentials and only work for public repositories. The build context has no `.git` directory.

## Deploy hooks

`.hatchbox/post-deploy` and `.hatchbox/failed-deploy` in your repository run in a container from the new image, once per deploy, with `RELEASE`, `REVISION`, `BRANCH` and `LOG_ID` in the environment. The pre-build, build and post-build hooks aren't used: the Dockerfile is the build.

## Consoles and one-off commands

Each deploy writes `/home/deploy/<app>/bin/run` on every app server. It runs a command in a new container from the current image, with the app's env vars and volumes:

```
ssh deploy@<server>
~/myapp/bin/run bin/rails console
~/myapp/bin/run bin/rails db:migrate:status
```

## Logs

Containers log to the server's journal under the tag `<app>.<process>`, which is what the process's log page shows. Over SSH:

```
journalctl -t myapp.server -f
```

Anything your app writes to files inside the container is lost when the container is replaced. The Rails Dockerfile logs to stdout.

## What isn't supported yet

- Generated Dockerfiles for apps that aren't Rails, and static sites without a Dockerfile.
- The `whenever` gem's crontab. Use `.hatchbox/cron.yml` instead (see *Cron Jobs*).
- Mixing amd64 and arm64 servers in one cluster.