|
| 1 | +# System Packages |
| 2 | + |
| 3 | +Cortex uses Docker images to run workloads. These Docker images can be replaced with custom images based on Cortex images and augmented with your system packages and libraries. Your custom images need to be pushed to a container registry (e.g. Docker Hub, ECR, GCR) that can be accessed by your cluster. |
| 4 | + |
| 5 | +See `Image paths` section in [cortex config](../cluster/config.md) for all images that can be customized. |
| 6 | + |
| 7 | +The example below demonstrates how to create a custom Docker image and configure Cortex to use it. |
| 8 | + |
| 9 | +## Create Custom Image |
| 10 | + |
| 11 | +Create a Dockerfile to build your custom image: |
| 12 | + |
| 13 | +``` |
| 14 | +mkdir my-api && cd my-api && touch Dockerfile |
| 15 | +``` |
| 16 | + |
| 17 | +Specify the base image you want to override followed by your customizations. The sample Dockerfile below inherits from Cortex's ONNX Serving image and installs the `tree` system package. |
| 18 | + |
| 19 | +``` |
| 20 | +# Dockerfile |
| 21 | +
|
| 22 | +FROM cortexlabs/onnx-serve |
| 23 | +
|
| 24 | +RUN apt-get update \ |
| 25 | + && apt-get install -y tree \ |
| 26 | + && apt-get clean && rm -rf /var/lib/apt/lists/* |
| 27 | +``` |
| 28 | + |
| 29 | +## Build and Push to a Container Registry |
| 30 | + |
| 31 | +Create a repository to store your image: |
| 32 | + |
| 33 | +``` |
| 34 | +# We create a repository in AWS ECR |
| 35 | +
|
| 36 | +export AWS_ACCESS_KEY_ID="***" |
| 37 | +export AWS_SECRET_ACCESS_KEY="***" |
| 38 | +
|
| 39 | +eval $(aws ecr get-login --no-include-email --region us-west-2) |
| 40 | +
|
| 41 | +aws ecr create-repository --repository-name=org/my-api --region=us-west-2 |
| 42 | +# take note of repository url |
| 43 | +``` |
| 44 | + |
| 45 | +Build the image based on your Dockerfile and push to its repository in AWS ECR: |
| 46 | + |
| 47 | +``` |
| 48 | +docker build . -t org/my-api:latest -t <repository_url>:latest |
| 49 | +
|
| 50 | +docker push <repository_url>:latest |
| 51 | +``` |
| 52 | + |
| 53 | +## Configure Cortex |
| 54 | + |
| 55 | +Set the environment variable of the image to your `my-api` image repository url: |
| 56 | + |
| 57 | +``` |
| 58 | +export CORTEX_IMAGE_ONNX_SERVE="<repository_url>:latest" |
| 59 | +
|
| 60 | +./cortex.sh update |
| 61 | +``` |
| 62 | + |
| 63 | +## Use System Package in Workloads |
| 64 | + |
| 65 | +Cortex will use your image to launch ONNX serving workloads. You will have access to any customizations you made: |
| 66 | + |
| 67 | +``` |
| 68 | +# request_handler.py |
| 69 | +
|
| 70 | +import subprocess |
| 71 | +
|
| 72 | +def pre_inference(sample, metadata): |
| 73 | + subprocess.run(["tree"]) |
| 74 | + ... |
| 75 | +``` |
0 commit comments