# Dockerfile builds

Source: https://openpanel.sh/docs/builds/dockerfile

Build a service with a Dockerfile you name, and the folder Docker builds it from.

A service built from Git uses Railpack until you name a Dockerfile. Then Docker builds it with that file, on your own server.

## The settings [#the-settings]

| Setting | What it is | When you give none |
|---|---|---|
| **Dockerfile** | The Dockerfile's path, inside the service's folder in the repository, such as `Dockerfile` or `docker/web.Dockerfile`. | `Dockerfile` |
| **Context** | The folder Docker builds from, inside the service's folder. Files outside it cannot be copied into the image. | The service's folder itself |

Both paths start at the service's folder: for a service whose folder is `apps/web`, the Dockerfile `Dockerfile` is `apps/web/Dockerfile` in the repository. Neither may lead out of the repository.

## Build with a Dockerfile [#build-with-a-dockerfile]

<Tabs items={["CLI","API"]}>
<Tab value="CLI">
```sh
openpanel services update web --project shop --dockerfile docker/Dockerfile
openpanel deploy web --project shop --wait
```

`--dockerfile` also works on `services create`. It sets the Dockerfile's path; the context stays the service's folder.
</Tab>

<Tab value="API">
```sh
curl -X PATCH "https://openpanel.sh/api/v1/services/$SERVICE_ID" \
  -H "Authorization: Bearer $OPENPANEL_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{ "build": { "kind": "dockerfile", "dockerfile": "docker/Dockerfile", "context": "" } }'
```

`context` is a folder inside the service's folder; `""` is the service's folder itself.
</Tab>
</Tabs>

The change takes effect at the next deploy.

## Go back to Railpack [#go-back-to-railpack]

Send `"build": { "kind": "auto" }`, or `"build": null`, in the same `PATCH`. The CLI has no option for this yet.

## How Docker runs the build [#how-docker-runs-the-build]

- The agent runs `docker buildx build` with BuildKit on the server, and loads the image into the server's Docker.
- Docker's usual rules apply inside the context, such as a `.dockerignore` file.
- The image is built from the Dockerfile's last stage. Choosing another target stage is not offered yet.
- No build arguments or build secrets are passed, and the service's variables do not reach the build. See [How builds work](/docs/builds#variables-do-not-reach-the-build).
- The image runs the command your Dockerfile sets, unless the service has a start command of its own.

## What can go wrong [#what-can-go-wrong]

| Message | What it means |
|---|---|
| "The build failed: apps/web/docker/Dockerfile is not in the repository" | The Dockerfile, or the context folder, does not exist in the commit that was built. The message gives the path from the repository's root: the service's folder, then your path. |
| "The build failed: there is no Dockerfile at apps/web/docker" | The path names a folder, not a file. |
| "The build failed: … leads out of the repository" | The path, or a symbolic link on it, points outside the clone. |
| "A path stays inside the repository." | The path you sent holds `..`. |
| "Build settings are for a service built from Git." | You sent build settings while creating a service that runs an image, which has nothing to build. |

See [A build fails](/docs/troubleshooting/build-fails) for the rest.