Test a UbiOps custom Docker image locally¶
This how-to contains an example for basic local testing of a deployment that supports the UbiOps request format. To ensure compatibility with the UbiOps request format, it runs inside a UbiOps compatible Docker image.
Run request in container¶
You need access to a UbiOps base environment image. You can pull it directly from the UbiOps environment registry with Docker. This example uses the base environment ubiops-ubuntu24-04-python3-12.
Registry URL for private installations
environments.ubiops.com is the registry for the public UbiOps platform. Private installations use a different registry URL. In that case, replace environments.ubiops.com with the registry URL of your own installation in all commands on this page.
Log in to the registry:
docker login environments.ubiops.com
- Username: the email address of a service user (
<id>.<project>@serviceuser.ubiops.com), or of a personal UbiOps account. If your UbiOps accounts logs in with single sign-on (SSO), use a service user account. - Password: the API token of that user, without the
Tokenprefix. In case of 2FA, append the 2FA token directly after your password.
Pull the base environment:
docker pull environments.ubiops.com/ubiops-ubuntu24-04-python3-12:v5.26.0
If the pull fails with pulling images is disabled, image pulls are not enabled for your account. Please contact us at support@ubiops.com to enable them.
Prepare your deployment script in a directory. This should contain the files that you would upload to UbiOps as a .zip package. You can start with this example and edit or replace it as you like. Save it as deployment/deployment.py:
import datetime
import random
class Deployment:
"""
For more information about the UbiOps deployment format, see: https://github.com/UbiOps/deployment-template
"""
def __init__(self, base_directory, context):
print(f"Deployment initialised at {datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')}")
def request(self, data, context):
if "input" in data:
print(f"Input: {data['input']}")
return {"output": random.randint(0, 10)}
Save the following script as run_deployment_docker.py, next to the deployment directory. It loads the deployment and runs one request. Adjust the input data for your deployment by updating the input_data variable.
import os
import sys
import uuid
# Deployment_directory points to base folder of the deployment
deployment_directory = "/opt/deployment/"
# Update $PATH with the deployment base directory and libraries before importing the deployment package.
sys.path = [deployment_directory, os.path.join(deployment_directory, 'libraries')] + sys.path
# Import the deployment package
import deployment
def main():
deployment_obj = deployment.Deployment(
base_directory=deployment_directory, context={}
)
# Adjust this example input as necessary. For plain deployments this can be a string instead of dictionary.
input_data = {
'input': 10.9
}
request_result = deployment_obj.request(data=input_data, context={"id": uuid.uuid4()})
print("Deployment request result: %s" % request_result)
if __name__ == '__main__':
main()
Run your deployment request in the UbiOps Docker image with a single Docker command as follows:
docker run \
-v $(pwd)/run_deployment_docker.py:/tmp/run_deployment_docker.py \
-v $(pwd)/deployment:/opt/deployment \
--entrypoint python \
environments.ubiops.com/ubiops-ubuntu24-04-python3-12:v5.26.0 \
/tmp/run_deployment_docker.py
You can adjust the image name environments.ubiops.com/ubiops-ubuntu24-04-python3-12:v5.26.0 as desired.
The command: - Mounts the run_deployment_docker.py in the container. This script will load and execute the deployment.
-
Mounts the
deploymentdirectory with the deployment code at/opt/deploymentin the container. This directory should contain the code that you would include in the deployment.zipuploaded to UbiOps. -
Runs the Docker image and executes the request.
Do not mount the deployment directory on /home/deployment. That directory contains the Python virtual environment of the image (/home/deployment/.venv), and a mount hides it. The container then fails with exec: "python": executable file not found in $PATH. If you mount the code somewhere other than /opt/deployment, update deployment_directory in run_deployment_docker.py to match.
Using a customized Docker image¶
You can also build your own Docker image with any desired software and dependencies using the UbiOps image as its base image. By doing this you can upload the resulting image as an environment in UbiOps and use it for deployments that support the UbiOps request format. Note that the Docker image should not contain your deployment package zip. This zip is added when creating a deployment.
Example Dockerfile:
FROM environments.ubiops.com/ubiops-ubuntu24-04-python3-12:v5.26.0
RUN uv pip install requests
It is important that you keep the original entrypoint of the base image, so do not define it again.
Build the image:
docker build --platform linux/amd64 -t custom-ubiops-environment -f Dockerfile .
You can now upload the image to UbiOps using the instructions on the UbiOps documentation.
Alternatively to mounting the deployment directory in the container, you can also choose to include your code files directly in the container during the build. In that case you will not have to upload a deployment .zip file when creating your deployment in UbiOps. When you do this, you should remove the mount -v $(pwd)/deployment:/opt/deployment from the docker run command, and point deployment_directory in run_deployment_docker.py to the location of the code in the image.
However, we do recommend to avoid including the code in the image where possible and to upload your code separately. This simplifies updating and re-using deployments without creating new images.
Keep container running¶
Instead of stopping your container after one request, you might want to initialise the container and deployment once and then keep it running for multiple requests. For example if the __init__ takes a long time, or for debugging purposes. To do so you can use the following Docker commands.
Start the container by overriding the entrypoint with a sleep command to keep it running for a long time:
docker run \
--name ubiops-deployment-test \
--detach \
-v $(pwd)/run_deployment_docker.py:/tmp/run_deployment_docker.py \
-v $(pwd)/deployment:/opt/deployment \
--entrypoint sleep \
environments.ubiops.com/ubiops-ubuntu24-04-python3-12:v5.26.0 \
infinity
Changes to files in the deployment directory are picked up by the next request, without restarting the container.
To run requests in the container:
docker exec -it ubiops-deployment-test python3 /tmp/run_deployment_docker.py
To stop and remove the container:
docker kill ubiops-deployment-test
docker rm ubiops-deployment-test
This approach is also useful if you want to test requests with files as input. You can copy files into the running container with docker container cp and then pass the path to this file in the request parameters, similar to how UbiOps would prepare the files in a running deployment.
docker container cp input.csv ubiops-deployment-test:/tmp/input.csv
Now set input_data = {'input': '/tmp/input.csv'} in run_deployment_docker.py and run a request with the docker exec command above.
Limitations¶
Testing your deployment scripts locally has some limitations compared to running on UbiOps:
-
The
contextparameters for the__init__andrequestmethods are not provided in the same way as they would be when running in UbiOps. You can adjust therun_deployment_docker.pyscript as necessary. See the UbiOps deployment template for more information on the contents of these parameters: https://github.com/UbiOps/deployment-template -
By default there are no CPU or memory limits. You can pass Docker resource constraints to the
docker runcommand to simulate this, for example to debug out of memory (OOM) events. -
The hardware that your deployment runs on in UbiOps is likely different than on a local test. This can affect CPU and GPU architecture, performance and more.