# Docker

Build an image for every Dockerfile after the package it ships, and cache it as an archive when you ask.

The Docker plugin gives every Dockerfile in a package or project a task that builds its image, after the package's own `build`, so `axle run docker-build` builds what the images ship and then the images, in dependency order and side by side.

## Add it

```sh
axle add docker
```

This writes it into the root `axle.toml`:

```toml
[plugins.docker]
source = "npm:@axle-rs/plugin-docker@0.1.0"
hash = "sha256-…"
```

It needs `docker` on the `PATH`. The tasks get the `DOCKER_*` and `BUILDKIT_*` variables, such as `DOCKER_HOST`, without them changing the hash.

## Tasks

Each file directly in a package or project folder named `Dockerfile` or `Containerfile`, or `<name>.Dockerfile` or `Dockerfile.<name>`, and the same for `Containerfile`, gets a task that builds it with the folder as the build context:

| File                | Task                  | Command                                                           |
| ------------------- | --------------------- | ----------------------------------------------------------------- |
| `Dockerfile`        | `docker-build`        | `docker build --file Dockerfile --tag web:latest .`               |
| `worker.Dockerfile` | `docker-build-worker` | `docker build --file worker.Dockerfile --tag web-worker:latest .` |
| `Dockerfile.debug`  | `docker-build-debug`  | `docker build --file Dockerfile.debug --tag web-debug:latest .`   |

Files such as `Dockerfile.dockerignore` don't count, and when two files would give the same task, the first by name wins. The image is named after the package, in lower case and without the `@` of a scope, so `@acme/web` builds `acme/web`.

Each task waits for its package's `build`, and its hash counts every file in the package, the build context, along with the hashes of the tasks it waits for. Axle doesn't read `.dockerignore`: a file it leaves out of the image still changes the hash, which builds the image again when nothing in it changed, but never keeps an old one.

A repository where no package has a `build` task at all would make that wait an [error](https://axlers.dev/docs/messages/unknown-dependency.md); take it out in `axle.toml`:

```toml
[tasks.docker-build]
dependsOn = []
```

## Not cached, unless it's an archive

A built image lives in Docker, not in files, and Axle's cache stores files. A cache hit couldn't put the image back into Docker, so a cached result would say an image is there when it isn't. The tasks aren't cached and build every time, and Docker's own layer cache keeps that quick when little changed.

With `archive`, the image is written to a file instead of loaded into Docker, with BuildKit's `--output type=docker`, which Docker 23 and later use by default. That file is the task's output, so the task is cached: a hit puts the archive back, and `docker load --input <archive>` loads it into Docker with its tags.

```toml
[plugins.docker]
source = "npm:@axle-rs/plugin-docker@0.1.0"
hash = "sha256-…"
archive = "dist/{name}.tar"
```

## Settings

| Setting   | Default             | Meaning                                                                                                                                                              |
| --------- | ------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `build`   | `"docker-build"`    | the name of the task, and the start of each named Dockerfile's, or `false` to leave them out                                                                         |
| `tags`    | `["{name}:latest"]` | the image's tags; `{name}` is the image name and `{version}` the `version` in the package's `package.json`, and a tag with `{version}` is left out when there's none |
| `archive` | none                | a file in the package to write the image to, which makes the task cached; it takes `{name}`, with `-` for `/`, and `{version}`                                       |

```toml
[plugins.docker]
source = "npm:@axle-rs/plugin-docker@0.1.0"
hash = "sha256-…"
tags = ["ghcr.io/acme/{name}:{version}", "ghcr.io/acme/{name}:latest"]
```

The plugin builds images but doesn't push them; push them with `docker push` once they're built.

The plugin's answers are kept until a Dockerfile, a Containerfile or a `package.json` changes.
