IBM Cloud Pak® for Data Version 4.8 will reach end of support (EOS) on 31 July, 2025. For more information, see the Discontinuance of service announcement for IBM Cloud Pak for Data Version 4.X.
Upgrade to IBM Software Hub Version 5.1 before IBM Cloud Pak for Data Version 4.8 reaches end of support. For more information, see Upgrading from IBM Cloud Pak for Data Version 4.8 to IBM Software Hub Version 5.1.
Customizing Python deployment images in Watson Machine Learning
You can add customizations to your Python image by using Docker or Podman.
Adding customizations to Python image
Use the following sample to add customizations to your Python image by using Docker or Podman.
Sample Dockerfile:
ARG base_image_tag
FROM ${base_image_tag}
# For installing OS packages, use root:root
# e.g.
USER root:root
RUN yum install -y gdb
# For installing to conda site-packages,
# use wmlfuser:condausers
# e.g.
USER wmlfuser:condausers
RUN umask 002 && \
pip install gensim
#
# Alternatively, for Installing to location other than conda site-packages.
# It can be installed to
# --target <any_file_system_dir_path>
# Provided <any_file_system_dir_path> full path is accessible to any user with group "condausers"
#
# Important:
# The <any_file_system_path> must then be added to the environment variable PYTHONPATH
#
# for example
# use wmlfuser:condausers to install to the path /home/wmlfuser/mypkgs and add
# the path to PYTHONPATH environment variable
#
USER wmlfuser:condausers
RUN umask 002 && \
mkdir -p /home/wmlfuser/mypkgs && \
pip install pydot --target /home/wmlfuser/mypkgs
ENV PYTHONPATH=$PYTHONPATH:/home/wmlfuser/mypkgs
# If pip --user is used for installing package.
# The installation path needs to be explicitly added to PYTHONPATH
# e.g.
#
# for user wmlfuser:condausers it by default goes to user install directory
# (See the Python documentation for site.USER_BASE for full details.)
# use `python -m site --user-site` command to get the path.
#
# for wml-deployment-runtime-py39-1, it goes to
# /home/wmlfuser/.local/lib/python3.9/site-packages
#
#
Use the docker build command to build this Dockerfile.
docker build -t <custom-image-name>:<custom-image-tag> \
--build-arg base_image_tag=<downloaded-base-image> -f <path_to_dockerfile> .
Running the docker build command returns the following output:
Sample output for Docker:
docker build -t test-custom-image:test-1 --build-arg base_image_tag=cp.stg.icr.io/cp/cpd/wml-dep-rt23-1-ms@sha256:5c7ba4fa9c1e6b9ab3bb3ce6285130216db80546ed02c63d1a2370e738a4e119 -f Dockerfile .
... snippet...
=> exporting to image 0.2s
=> => exporting layers 0.2s
=> => writing image sha256:9df9d2d3b16e63fc56b1a9003f237790a77c7f4ffb2f96df4fd0abffe1a6187d 0.0s
=> => naming to docker.io/library/test-custom-image:test-1
You can also use Podman to add customizations to your Python file:
Sample code for Podman:
podman build -t <custom-image-name>:<custom-image-tag> \
--build-arg base_image_tag=<downloaded-base-image> -f <path_to_dockerfile>
The <downloaded-base-image> is copied from the .entity.launch_configuration.image section of the runtime-definition file. In the example, the <downloaded-base-image> is copied from theruntime-23.1-py3.10-server.json file.
Running the podman build command returns the following output sample:
Sample output for Podman:
podman build -t test-custom-image:test-1 \
> --build-arg base_image_tag=cp.stg.icr.io/cp/cpd/wml-dep-rt23-1-ms@sha256:5c7ba4fa9c1e6b9ab3bb3ce6285130216db80546ed02c63d1a2370e738a4e119 -f Dockerfile .
... snippet...
COMMIT test-custom-image:test-1
--> 1dc344ed12a0
Successfully tagged localhost/test-custom-image:test-1
1dc344ed12a0fb17e38b895db70e5cd5acc124bb4336f7388083ba0a4c83d637
Reusing base image for Watson Machine Learning
Beginning Cloud Pak for Data version 4.8.4, you can reuse a Watson Studio notebook runtime for Watson Machine Learning as the base image for x86 platform to ensure that you use the same runtime environment to build and deploy the model. This feature is not available in Cloud Pak for Data versions 4.8.3 and earlier.
Check for base image
To check if your Watson Machine Learning base image is built from Watson Studio runtime image, run the following command:
Sample code for Docker:
docker run --rm <downloaded-base-image> grep -q '^wsbuild:' /etc/passwd && echo "User wsbuild exists" || echo "User wsbuild does not exist"
Sample code for Podman:
podman run --rm <downloaded-base-image> grep -q '^wsbuild:' /etc/passwd && echo "User wsbuild exists" || echo "User wsbuild does not exist"
The output shows whether wsbuild user exists or not.
Case 1: wsbuild user doesn't exist
If wsbuild user doesn't exist, use the sample code to add customizations to your Python image. For more information, see Adding customizations to Python image.
Case 2: wsbuild user exists
If wsbuild user exists, the base custom image for Watson Machine Learning is reused from Watson Studio notebook runtime image. In this case, you can optionally install system and user packages.
The following sample code shows how to install packages:
Sample Dockerfile:
ARG base_image_tag
FROM ${base_image_tag}
# For installing system packages, use root:root
# e.g.
USER root:root
RUN microdnf install -y jq
# For installing to conda site-packages,
# use wsbuild:wsbuild
# e.g.
USER wsbuild:wsbuild
RUN source activate $WML_CONDA_ENV_NAME && \
pip install pendulum
USER wsuser:wscommon
You can run this Dockerfile by using the docker build command, which returns the following output sample:
Sample code for Docker:
docker build -t test-custom-image:test-1 --build-arg base_image_tag=cp.stg.icr.io/cp/cpd/wml-dep-rt23-1-ms@sha256:5c7ba4fa9c1e6b9ab3bb3ce6285130216db80546ed02c63d1a2370e738a4e119 -f Dockerfile .
[+] Building 11.5s (7/7) FINISHED docker:default
=> [internal] load build definition from Dockerfile 0.0s
=> => transferring dockerfile: 362B 0.0s
=> [internal] load .dockerignore 0.0s
=> => transferring context: 2B 0.0s
=> [internal] load metadata for cp.stg.icr.io/cp/cpd/wml-dep-rt23-1-ms@sha256:5c7ba4fa9c1e6b9ab3bb3ce6285130216db80546ed02c63d1a2370e738a4e119 0.0s
... snippet ...
=> => exporting layers 0.2s
=> => writing image sha256:9df9d2d3b16e63fc56b1a9003f237790a77c7f4ffb2f96df4fd0abffe1a6187d 0.0s
=> => naming to docker.io/library/test-custom-image:test-1
To use the sample code, you must update the following parameters:
- Set
user:groupparameter toroot:rootto install system packages. - After installing system packages, set
user:groupparameter towsbuild:wsbuildto install python packages. - After installing Python packages, update
condaenvironment name for$WML_CONDA_ENV_NAMEparameter. - After updating the
condaenvironment, setuser:grouptowsuser:wscommon.
You can also run the Dockerfile in Podman by using the podman build command, which returns the following output sample:
Sample code for Podman:
podman build -t test-custom-image:test-1 \
> --build-arg base_image_tag=cp.stg.icr.io/cp/cpd/wml-dep-rt23-1-ms@sha256:5c7ba4fa9c1e6b9ab3bb3ce6285130216db80546ed02c63d1a2370e738a4e119 -f Dockerfile .
STEP 1/6: FROM cp.stg.icr.io/cp/cpd/wml-dep-rt23-1-ms@sha256:5c7ba4fa9c1e6b9ab3bb3ce6285130216db80546ed02c63d1a2370e738a4e119
STEP 2/6: USER root:root
--> 1a710ccc1dae
... snippet...
--> f0ef896379fe
STEP 6/6: USER wsuser:wscommon
COMMIT test-custom-image:test-1
--> 1dc344ed12a0
Successfully tagged localhost/test-custom-image:test-1
1dc344ed12a0fb17e38b895db70e5cd5acc124bb4336f7388083ba0a4c83d637
As best practice, set the default USER of the custom image to wsuser:wscommon. The HOME environment variable is set to /home/wsuser in the deployment service.
Parent topic: Creating a custom image