# Docker

Clever Cloud offers support for Docker containers. These are deployed inside virtual machines to guarantee a secure level of isolation.

## Overview

Docker containers can encapsulate any payload, and will run consistently on and between virtually any server. The same container that a developer builds and tests on a laptop will run at scale, in production, on VMs, bare-metal servers, public instances, or combinations of the above.

Clever Cloud allows you to deploy any application running inside a Docker container. This page explains how to set up your application to run it on our service.

Clever Cloud supports many languages, but some users have specific application needs. With Docker, they can create custom stacks without relying on Clever Cloud’s specific support.

> [!WARNING]
> [FS Buckets](https://www.clever.cloud/developers/doc/deploy/storage/#what-is-fs-bucket) access, Dockerfile validation, and Docker Compose functionalities are not supported.

### How it works

When you create a Docker application on Clever Cloud, the deployment process involves the following steps:

1. **Login:** The system checks for a Dockerfile and logs into the Docker registry you configured, if any, to find the necessary image.
2. **Build:** The application pulls the specified image and executes the commands specified in your Dockerfile.
3. **Run:** The application starts in a Docker container and exposes the service on port 8080 by default.

## Setting up environment variables on Clever Cloud

### With the Clever Cloud console

1. Go to the Clever Cloud console, and find the app you want to fine tune under it’s organisation.
2. Find the **Environment variables** menu and select it.
3. In this menu, you will see a form with *VARIABLE\_NAME* and *variable value* fields.
4. Fill them with the desired values then select **Add**.
5. Don’t forget to “Update Changes” at the end of the menu.

### With the Clever Tools CLI

1. Make sure you have clever-tools installed locally. Refer to our [CLI getting started](https://www.clever.cloud/developers/doc/manage/cli/).
2. In your code folder, do `clever env set <variable-name> <variable-value>`

Refer to [environment variables reference](https://www.clever.cloud/developers/doc/develop/common-configuration/environment-variables/reference/) for more details on available environment variables on Clever Cloud.

You can of course create custom ones with the interface we just demonstrated, they will be available for your application.

## Configure your Docker application

### Mandatory configuration

Be sure that you:

- Have and commit a file named **Dockerfile**, or use the `CC_DOCKERFILE` [environment variable](https://www.clever.cloud/developers/doc/develop/common-configuration/environment-variables/reference/#docker) if your Dockerfile has a different name. [Here is what it will look like](https://docs.docker.com/develop/develop-images/dockerfile_best-practices "Dockerfile").
- Run the application with `CMD` or `ENTRYPOINT` in your Dockerfile.
- Listen on HTTP **port 8080** by default (you can set your own port using `CC_DOCKER_EXPOSED_HTTP_PORT`).
- [Learn more about environment variables on Clever Cloud](https://www.clever.cloud/developers/doc/develop/common-configuration/environment-variables/reference/)

### Dockerfile contents

You can virtually put everything you want in your Dockerfile. The only mandatory instruction is:

```dockerfile
CMD <command to run>
```

**command to run**: this is the command that starts your application. Your application **must** listen on the port defined by `CC_DOCKER_EXPOSED_HTTP_PORT` (default: `8080`). It can be easier for you to put a script in your Docker image and call it with the CMD instruction.

### Docker Buildx

The default build uses `docker buildx`. To use Docker legacy build instead, set `CC_DOCKER_BUILDX` to `false`.

### Memory management

The Docker container runs with a memory limit equal to the instance’s available memory, with swap disabled. If the building step of your application crashes due to memory usage, split the building and running steps and enable [Dedicated build instance](https://www.clever.cloud/developers/doc/develop/common-configuration/lifecycle/#edit-application-configuration):

```dockerfile
# The base image
FROM outlinewiki/outline:version-0.44.0

# Run the memory intensive build on an instance with 4 GB of memory (M)
RUN yarn install && yarn build

# Start the app on a smaller instance (nano)
CMD yarn start
```

### Login to registry

As Docker Hub limits the number of image pulls without authentication, use your own account to get higher limits. You can also use a private registry where you store your images. This runs `docker login` before the build phase.

- `CC_DOCKER_LOGIN_USERNAME`: the username to use
- `CC_DOCKER_LOGIN_PASSWORD`: the password for your username
- `CC_DOCKER_LOGIN_SERVER` (optional): the server of your private registry, default is Docker Hub

> [!NOTE]
> The name of the Docker registry may vary depending on the provider (e.g. “container registry” in GitHub). If login fails, the build continues without authentication.

### Network mode

When using default ports (`CC_DOCKER_EXPOSED_HTTP_PORT=8080` and `CC_DOCKER_EXPOSED_TCP_PORT=4040`), the container runs with `--net host` for direct network access. When custom ports are specified, Docker’s port mapping is used instead (`-p 8080:<your_http_port> -p 4040:<your_tcp_port>`).

### TCP support

Clever Cloud enables you to use TCP over Docker applications using the environment variable `CC_DOCKER_EXPOSED_TCP_PORT` (default: `4040`).

- [Learn more about TCP redirections](https://www.clever.cloud/developers/doc/develop/common-configuration/tcp-redirections/)

### Docker socket access

Some containers require access to the Docker socket, to spawn sibling containers for instance.

> [!WARNING]
> Giving access to the Docker socket breaks all isolation provided by Docker. **DO NOT** give socket access to untrusted code.

You can make the Docker socket available from inside the container by adding the `CC_MOUNT_DOCKER_SOCKET=true` environment variable. In that case, Docker is started in the namespaced mode (user namespace remapping), and the container uses port mapping instead of host network mode.

### Enable IPv6 networking

You can activate the support of IPv6 with an IPv6 subnet in the Docker daemon by adding the `CC_DOCKER_FIXED_CIDR_V6=<CIDR>` environment variable (e.g. `fd00::/80`).

### Build-time variables

You can use the [ARG](https://docs.docker.com/engine/reference/builder/#arg) instruction to define build-time environment variables.

Every environment variable defined for your application is passed as a build environment variable using the `--build-arg` parameter during the `docker build` phase.

### Deployment hooks

Docker applications support all [deployment hooks](https://www.clever.cloud/developers/doc/develop/common-configuration/build-hooks/). The `CC_PRE_BUILD_HOOK`, `CC_POST_BUILD_HOOK`, and `CC_PRE_RUN_HOOK` run on the host. The `CC_RUN_SUCCEEDED_HOOK` runs inside the container after successful start.

### Docker applications as Clever Tasks

Docker containers can run as on-demand workloads on Clever Cloud. Configure an application as Tasks from the `Information` panel in [the Console](https://console.clever-cloud.com) or with [Clever Tools](https://www.clever.cloud/developers/doc/manage/cli/applications/#tasks). The container runs, executes its command, and stops.

- [Learn more about Clever Tasks](https://www.clever.cloud/developers/doc/develop/tasks/)

### Sample dockerized applications

- [Elixir App](https://github.com/CleverCloud/demo-docker-elixir/blob/master/Dockerfile)
- [Seaside / Smalltalk App](https://github.com/CleverCloud/demo-seaside)
- [Rust App](https://github.com/CleverCloud/demo-rust)

## Environment injection

Clever Cloud injects environment variables from your application settings as mentioned in [setting up environment variables](https://www.clever.cloud/developers/doc/develop/common-configuration/environment-variables/) and is also injecting in your application production environment, those from your [linked add-ons](https://www.clever.cloud/developers/doc/deploy/applications/docker/#linking-a-database-or-any-other-add-on-to-your-application).

### Custom build configurations

On Clever Cloud you can define some build configuration: like the app folder to deploy or the path to validate your application deployment is ready To do that follow the [documentation here](https://www.clever.cloud/developers/doc/develop/common-configuration/environment-variables/reference/#variables-you-can-define) and add the environment variable you need.

## Git Deployment on Clever Cloud

*You need Git on your computer to deploy via this tool. Here is the official website of Git to get more information: [git-scm.com](https://git-scm.com)*

### Setting up your remotes

1. The “Information” page of your app gives you your Git deployment URL, it looks like this:
   
   1. `git+ssh://git@push.clever-cloud.com/<your_app_id>.git`
   2. Copy it in your clipboard
2. Locally, under your code folder, type in `git init` to set up a new git repository or skip this step if you already have one
3. Add the deploy URL with `git remote add <name> <your-git-deployment-url>`
4. Add your files via `git add <files path>` and commit them via `git commit -m <your commit message>`
5. Now push your application on Clever Cloud with `git push <name> master`

Refer to [git deployments](https://www.clever.cloud/developers/doc/getting-started/) for more details.

## Linking a database or any other add-on to your application

By linking an application to an add-on, the application has the add-on environment variables in its own environment by default.

### On add-on creation

Many add-ons do exist on Clever Cloud: refer to [the full list](https://www.clever.cloud/developers/doc/deploy/addons/) and check add-ons dedicated pages for full instructions.

During add-on creation, an **Applications** screen appears, with a list of your applications. You can toggle the button to *Link* and click *next*. If you finish the process of add-on creation, the application is automatically linked to it.

### Add-on already exists

In the Clever Cloud console, under the **Service Dependencies** menu of your application, you can use the *Link add-ons* dropdown menu to select the name of the add-on you want to link and use the *add* button to finish the process.

You can also link another application from the same page in the Clever Cloud console, using the *Link applications* dropdown menu.

## More configuration

Need more configuration? To run a script at the end of your deployment? To add your private SSH key to access private dependencies?

Go check the [Configure applications page](https://www.clever.cloud/developers/doc/develop/common-configuration/).

For advanced usage of your application, read the [Develop](https://www.clever.cloud/developers/doc/develop/) section.

If you can’t find something or have a specific need like using a non supported version of a particular software, please reach out to the [support](https://www.clever.cloud/developers/doc/find-help/).

## Enable health check during deployment

The healthcheck allows you to limit downtimes. Indeed, you can provide Clever Cloud with paths to check. If these paths don’t return a `2xx` response code, the deployment fails.

Add one (or several) environment variable as such:

```bash
CC_HEALTH_CHECK_PATH=/my/awesome/path
```

Or

```bash
CC_HEALTH_CHECK_PATH_0=/my/awesome/path
CC_HEALTH_CHECK_PATH_1=/my/other/path
```

The deployment process checks all paths. All of them must reply with a `2xx` response code, such as `200 OK` or `204 No Content`. Clever Cloud retries with an increasing delay before it marks the deployment as failed, so an application that needs some time to start isn’t rejected on the first attempt.

When no health check path is configured, Clever Cloud requests the root path `/` and accepts any response code from `200` to `499`: a `404` or a `401` means your application is up and answering. For Java applications, the `deploy.pingPaths` of the `clevercloud/*.json` file also count as configured paths.

Before any HTTP request, Clever Cloud checks that a service listens on the public port, `8080`, on a public interface. Depending on your configuration, it’s your application, the port Docker maps from `CC_DOCKER_EXPOSED_HTTP_PORT`, or the first Request Flow middleware. A service on this port bound only to `localhost` or `127.0.0.1` makes the deployment fail with `Your application is listening on localhost instead of publicly`: listen on `0.0.0.0` instead.

On runtimes supporting [Request Flow](https://www.clever.cloud/developers/doc/develop/request-flow/), when middleware runs in front of your application, Clever Cloud also checks that each middleware port and your application port are listening. With `CC_REQUEST_FLOW="block"`, Clever Cloud only checks the service answering on the public port, not your application.

### Example

With the preceding paths, here are the logs of a failing health check:

```text
Checking public port (8080) is listening as expected
Response from GET /my/awesome/path is 200 (within expected range 200...300)
Response from GET /my/other/path is 500, expected 200...300
Error while checking /my/awesome/path, /my/other/path
```

In this example, the first path is OK, but the second one failed. This gives you a hint on what failed in your application.

### Best practice for healthcheck endpoints

To make the most of a healthcheck endpoint, have it check your critical dependencies. For example:

- execute `SELECT 1 + 1;` on your database
- retrieve a specific Cellar file
- ping a specific IP through a VPN

