# Create a custom environment
URL: https://docs.datarobot.com/en/docs/workbench/nxt-registry/nxt-environment-workshop/nxt-create-custom-env.html

> Build a custom environment when a custom model requires something not contained in one of DataRobot's built-in environments.

# Create a custom environment

Once uploaded into DataRobot, custom models, jobs, applications, and notebooks run inside of environments—Docker containers running in Kubernetes. In other words, DataRobot copies the uploaded files defining the custom task into the image container. In most cases, adding a custom environment is not required because there are a variety of built-in environments available in DataRobot. For more information on creating a custom environment for custom models, review the guidelines below.

## Custom model environment guidelines

Python and/or R packages can be easily added to these environments by uploading a `requirements.txt` file with the code. A custom environment is only required when a custom model:

- Requires additional Linux packages.
- Requires a different operating system.
- Uses a language other than Python, R, or Java.

This document describes how to build a custom environment for these cases. To assemble and test a custom environment locally, install both Docker Desktop and the [DataRobot User Models (DRUM) CLI tool](https://docs.datarobot.com/en/docs/api/code-first-tools/drum/custom-model-drum.html) on your machine.

> [!NOTE] Note
> DataRobot recommends using an environment template and not building your own environment except for specific use cases. (For example, you don't want to use DRUM, but you still want to implement your own prediction server.)

If you'd like to use a tool, language, or framework that is not supported by our template environments, you can make your own. DataRobot recommends modifying the provided environments to suit your needs; however, to make an easy-to-use, reusable environment, you should adhere to the following guidelines:

- Your environment must include a Dockerfile that installs any requirements you may want.
- Custom models require a simple webserver to make predictions. DataRobot recommends putting this in your environment so you can reuse it with multiple models. The webserver must be listening on port8080and implement the following routes: URL prefix environment variableURL_PREFIXis an environment variable that is available at runtime. It must be added to the routes below. Mandatory endpointsDescriptionGET /URL_PREFIX/This route is used to check if your model's server is running.POST /URL_PREFIX/predict/This route is used to make predictions. Optional extension endpointsDescriptionGET /URL_PREFIX/stats/This route is used to fetch memory usage data for DataRobot Custom Model Testing.GET /URL_PREFIX/health/This route is used to check if model is loaded and functioning properly. If model loading fails, an error with the 513 response code should be returned. Failing to handle this case may cause the backend k8s container to crash and enter a restart loop for several minutes.
- An executablestart_server.shfile is required to start the model server.
- Any code andstart_server.shshould be copied to/opt/code/by your Dockerfile.

> [!NOTE] Note
> To learn more about the complete API specification, you can review the [DRUM server APIyamlfile](https://github.com/datarobot/datarobot-user-models/blob/master/custom_model_runner/drum_server_api.yaml).

### Custom model environment variables

When you build a custom environment with DRUM, your custom model code can reference several environment variables injected to facilitate access to the [DataRobot Client](https://pypi.org/project/datarobot/) and [MLOps Connected Client](https://pypi.org/project/datarobot-mlops-connected-client/):

| Environment Variable | Description |
| --- | --- |
| MLOPS_DEPLOYMENT_ID | If a custom model is running in deployment mode (i.e., the custom model is deployed), the deployment ID is available. |
| DATAROBOT_ENDPOINT | If a custom model has public network access, the DataRobot endpoint URL is available. |
| DATAROBOT_API_TOKEN | If a custom model has public network access, your DataRobot API token is available. |

### Create the environment

Once DRUM is installed, begin your environment creation by copying one of the examples from [GitHub](https://github.com/datarobot/datarobot-user-models/tree/master/public_dropin_environments). Log in to GitHub before clicking this link. Make sure:

- The environment code stays in a single folder.
- You remove theenv_info.jsonfile.

### Add Linux packages

To add Linux packages to an environment, add code at the beginning of `dockerfile`, immediately after the `FROM datarobot…` line. Use `dockerfile` syntax for an Ubuntu base. For example, the following command tells DataRobot which base to use and then to install packages `foo`, `boo`, and `moo` inside the Docker image:

```
FROM datarobot/python3-dropin-env-base
RUN apt-get update --fix-missing && apt-get install foo boo moo
```

### Add Python/R packages

In some cases, you might want to include Python/R packages in the environment. To do so, note the following:

- List packages to install inrequirements.txt. For R packages, do not include versions in the list.
- Do not mix Python and R packages in the samerequirements.txtfile. Instead, create multiple files and adjustdockerfileso DataRobot can find and use them.

### Test the environment locally

The following example illustrates how to quickly test your environment using Docker tools and DRUM. To test a custom task with a custom environment, navigate to the local folder where the task content is stored. Then, run the following, replacing placeholder names in `< >` brackets with actual names:

```
``` sh
drum fit --code-dir <path_to_task_content> --docker <path_to_a_folder_with_environment_code> --input <path_to_test_data.csv> --target-type <target_type> --target <target_column_name> --verbose
```
```

---
