Path: blob/master/cpd4.8/notebooks/python_sdk/deployments/custom_image/Use Custom Image, Software Specification and Runtime Definition to deploy a python function.ipynb
6405 views
Use Custom Image, Software Specification and Runtime Definition to deploy a python function with ibm-watson-machine-learning
This notebook demonstrates how to deploy in Watson Machine Learning service a python function which requires to create custom software specification and runtime definition. Familiarity with oc and python is helpful. This notebook uses Python 3.10.
Learning goals
The learning goals of this notebook are:
Creating a Custom Image
Creating custom software specification and runtime definition
Online deployment of python function
Scoring data using deployed function
Contents
This notebook contains the following parts:
Credentials
Authenticate the Watson Machine Learning service on IBM Cloud Pack for Data. You need to provide platform url
, the admin username
and api_key
or password
Install and import the ibm-watson-machine-learning
package
Note: ibm-watson-machine-learning
documentation can be found here.
The Creation of the custom image involves the following steps, not all of which can be done from a notebook. It involves:-
Downloading the runtime definition and getting the runtime image , This can be done from the notebook as shown later
Pulling, building, pushing docker images which cannot be done from the notebook. It must be done in a machine which have access to docker or podman
See https://www.ibm.com/docs/en/cloud-paks/cp-data/4.8.x?topic=runtimes-customizing-deployment-runtime-images for details.
generating auth tokens
Download the runtime definition and getting the runtime image
In this notebook we choose runtime-23.1-py3.10
See https://www.ibm.com/docs/en/cloud-paks/cp-data/4.8.x?topic=image-downloading-runtime-definition for details
The required runtime image will be in the key .launch_configuration.image
from the runtime definition we just downloaded
Pulling, building, pushing docker images
These steps cannot be run from a notebook, and need to be done in a machine which have access to docker or podman. See https://www.ibm.com/docs/en/cloud-paks/cp-data/4.8.x?topic=pbci-downloading-runtime-image for more details.
Download the runtime image for customization
If you have access to the IBM Entitled registry URL, you can download the base image directly from the above, else you need to download from private container registry used at the time of installation.
Pull the base image: Here cp.stg.icr.io/cp/cpd/
is my private container registry used at the time of installation The base image:
registry login and pulling the image:
Creating and uploading a custom image
Building the custom image
Based on the CPD release patch version. You will have two different formats for the Dockerfile here as described here https://www.ibm.com/docs/en/cloud-paks/cp-data/4.8.x?topic=image-customizing-python-deployment-images
e.g.
This prints out User wsbuild exists in this case , so we have to use wsbuild, wsuser
user base. We are going to install the program jq
as well as the python package pendulum
here. This needs to use the conda environment pointed by the variable WML_CONDA_ENV_NAME
already available in the base image. microdnf
can be used to install system packages
If this CPD release is prior to 4.8.3
; It would have been the case of User wsbuild does not exist , in which case we need to use a different user base wmlbaseuser, wmlfuser
. Dockerfile sample for this example is as below. We are going to install the program jq
as well as the python package pendulum
. This needs to use the base
conda environment for the python environment. yum
can be used to install system packages
Build the custom image wml-demo-image:test-1
:
Some sanity checks on image for this example. You can run the following, which should print the package version
Uploading the custom image
Push the custom image to the private container registry
Required custom image: cp.stg.icr.io/cp/cpd/wml-demo-image:test-1
Create a Volume service instance
namespace
here is where cc-home-pvc lives oc get pvc -A | grep cc-home-pvc
Check Volume Instances - if it is already created
Start file server on volume
Create the software specification
Custom Image software specification requires the following:
entity.software_specification.type
have to be set to "base" to indicate custom image.entity.software_specification.built_in
have to be set booleanFalse
to indicate custom image.entity.software_configuration.included_packages
array is informational, it can be empty list[]
.entity.software_configuration.platform.name
should be set topython
.
Upload file to the specified path on the volume
We can check if the file was successfully upload
Wait for about 5 mins
Refresh intervel is handled by env-spec-sync-job
cronjob
Check if the new Software Specification is activated
Warning: Do not proceed unless the new software specification is activated!
The software specification files are processed by the cronjob, and the uploaded will be available at the endpoint "/v2/software_specifications?name={sw_spec_name}" when it gets processed. If it does not show up after 5 mins, recheck that software specification file if it is a valid json and re-upload
We will create the Runtime definition file, which connects the software specification to the custom image name
Create a new runtime definition for the custom image
Runtime Definition file for Custom image:
Create runtime definitions via API
/v2/runtime_definitions
This runtime definition is the link between the software specification and the image name for custom image
If the custom image had been uploaded to the private container registry, then the value for
custom_image_fqn
would becp.stg.icr.io/cp/cpd/wml-demo-image:test-1
We already fetched the Runtime Definition template earlier under the step "Download the runtime definition and getting the runtime image". Update the follow fields in the template
We are ready to use this software specification with a deployable asset
You can use any user with Editor role on the space to be able to use the custom software specification created by the cluster admin in the ealier steps
Working with spaces
First of all, you need to create a space that will be used for your work. If you do not have space already created, you can use {PLATFORM_URL}/ml-runtime/spaces?context=icp4data
to create one.
Click New Deployment Space
Create an empty space
Go to space
Settings
tabCopy
space_id
and paste it below
Tip: You can also use SDK to prepare the space for your work. More information can be found here.
Action: Assign space ID below
You can use list method to print all existing spaces.
To be able to interact with all resources available in Watson Machine Learning, you need to set space which you will be using.
The demo python function
below showcase the package pendulum
which was installed for this custom image is now usable by returning the version
, and does nothing else useful beyond that.
We need use the software specification that was created earlier for the custom image sw_spec_name which is present in this variable
Create an online deployment
You can monitor the cluster for pods with `oc get pods -l WML_DEPLOYMENT_ID=`
example:
Predict using created deployment
It successfully printed out the pendulum
version that is present inside the custom image
Stop File server on the Volume
Delete the Volume instance
If you want to clean up all created assets:
experiments
trainings
pipelines
model definitions
models
functions
deployments
please follow up this sample notebook.
You successfully completed this notebook! You learned how to use Watson Machine Learning for function deployment and scoring with custom custom_image, software_spec and runtime definition. Check out our Online Documentation for more samples, tutorials, documentation, how-tos, and blog posts.
Author
Ginbiaksang Naulak Software Engineer at IBM.
Copyright © 2020-2025 IBM. This notebook and its source code are released under the terms of the MIT License.