# Sidecars

> A sidecar is a program of yours that runs beside your services, a copy beside each environment that adds it, brought by that environment's deployments.

**A sidecar is a program of yours that runs beside your services: an app marked as a sidecar, which any environment adds, and whose copy each of that environment's deployments brings to its hosts, where it changed.**

Made: like any app, from a package that runs as a service
Runs: a copy beside each environment that adds it, on each of its hosts
Changes: with that environment's next deployment, where it differs

A log shipper, a metrics exporter, a watchdog of your service: anything you'd install next to an app, on every host it runs on.

## A sidecar is an app {#app}

Any package that runs as a service can be a sidecar: Java, Kotlin, Go, Rust, Python, TypeScript, Elixir, or your own scripts. In the Environments app, tick **A sidecar** in **New app**, or choose **Make it a sidecar** in an app's **⋯** menu. Each environment under it is then a sidecar environment, such as `LogShipper/Prod`.

A sidecar environment has no targets of its own: it runs where the environments adding it run. **Pick a revision…** on it picks the release they bring, a deployment that goes to no server and is current at once. A pipeline picks one the same way, stage after stage.

## Adding it to an environment {#add}

On an environment's page, the **Sidecars** card sits under **Targets**. **Add a sidecar** lists your organization's sidecar environments: choose one, such as `LogShipper/Prod`. Nothing changes on the hosts until the environment deploys again: the card says its next deployment brings it.

An environment adds up to ten sidecars. Its sub-environments don't inherit them, as they don't inherit its targets.

## What a deployment brings {#brings}

Each deployment of the environment, a restart and a host catching up included, ends each host's turn with a step for each sidecar whose copy there doesn't run its picked release, or got other values:

```text
 (8/10) Check its health
 (9/10) Keep the newest five releases
(10/10) LogShipper/Prod #31 → #33
```

A copy that's up to date takes no step: the deployment doesn't stop, restart or replace it, and the log says so once. The **Deploy** page lists what will happen under **Sidecars it brings**, before you deploy.

## Each copy {#copy}

A copy runs as a service of its own, beside the environment's, and starts with it when the host boots. The environment's deployments and restarts leave it running: only a step of its own, when it changed, touches it. It runs as the environment's service does, as the same user or as a system service. Two environments on one host get a copy each.

It gets the sidecar's configuration, and where it runs:

| Variable | What it is | Example |
|---|---|---|
| `PLINTO_BESIDE` | the environment it runs beside | `Billing/Prod` |
| `PLINTO_BESIDE_UNIT` | that environment's service | `plinto-Billing_Prod` |
| `PLINTO_BESIDE_PORT` | its port, empty without one | `8080` |
| `PLINTO_BESIDE_HOME` | its release's folder | `/srv/plinto/Billing+Prod/current/app/Billing` |
| `PLINTO_HOST` | the host's name in plinto | `web-2` |
| `PLINTO_ENVIRONMENT`, `PLINTO_VERSION` | the sidecar environment, and the sidecar's version | `LogShipper/Prod`, `1.0-3` |

A sidecar's **Deploy settings** are its `health-url`, `health-timeout` and `stop-timeout`, and an image's `volumes` and `ports`; the rest comes from the environment it runs beside. A `health-url` may name the sidecar's keys, such as `http://localhost:${PORT}/health`, each copy filling in its own value.

## Declared keys {#declared}

A sidecar can let the environment give some of its values: tick **Declared** on a key in its configuration. The sidecar's value is the default, and an environment whose own configuration has the key gives its own:

| Where | Configuration | The copy beside Billing/Prod gets |
|---|---|---|
| `LogShipper` | `LOGS_FOLDER = /var/log/app`, **Declared**; `SHIP_TO = logs.internal` | `SHIP_TO = logs.internal` |
| `Billing` | `LOGS_FOLDER = /srv/billing/logs`; `DATABASE_URL` | `LOGS_FOLDER = /srv/billing/logs` |

Keys it doesn't declare never reach the copy: `DATABASE_URL` stays Billing's. Changing a declared key's value in the environment changes its copies at its next deployment.

## When a copy fails {#warning}

A copy that doesn't start, or doesn't come up healthy, is a warning, never the deployment's failure: the host keeps the environment's new release, and the copy keeps what it ran before. The deployment ends **Succeeded** with its warnings, the environment's page shows them on its current deployment and in the **Sidecars** card, and its next deployment tries again.

## Taking it off {#off}

**Remove** on the **Sidecars** card takes the sidecar's copies off with the environment's next deployment: a step that stops the copy and removes its service and its folder.

Deleting an environment and a host leaving its targets take things off too. See [Environments](environments.md#delete).

## Sidecars and images {#images}

A sidecar may be a [Docker image](../guides/docker-images.md), and so may the service it runs beside. Its copy is still a service of its own, started after the app's and left alone when it hasn't changed:

| | Beside an archive | Beside an image |
|---|---|---|
| An archive's copy | a unit on the host, at `localhost` and the app's port | a unit on the host, at `localhost` and the port the app publishes |
| An image's copy | a container on the host's network, at `localhost` | a container on the app's own network, reaching it by its name |

`PLINTO_BESIDE_ADDRESS` and `PLINTO_BESIDE_PORT` say where the app is, whichever pair it is: `billing` and `8080` inside its container for an image beside an image. An image's copy has deploy settings of its own, `volumes` and `ports`, which may name the sidecar's keys, as its health URL may: `${LOGS_FOLDER}:/logs:ro` mounts the folder the environment's `LOGS_FOLDER` names. Files go between the two through such a folder.

- [Run a sidecar beside your service](../tutorials/sidecar-beside-your-service.md): make one, step by step
- [Environments](environments.md): where it's added
- [Deployments](deployments.md): what each host's turn does
