-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Custom base images for functions (#1297)
- Loading branch information
Showing
28 changed files
with
424 additions
and
73 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
FROM icr.io/quantum-public/quantum-serverless-ray-node:0.9.0-py39 | ||
|
||
# install all necessary dependencies for your custom image | ||
|
||
# copy our function implementation in `/runner.py` of the docker image | ||
RUN mkdir /runner | ||
COPY ./runner.py /runner/runner.py |
Empty file.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
|
||
|
||
class Runner: | ||
def run(self, arguments: dict) -> dict: | ||
return { | ||
**arguments, | ||
**{ | ||
"answer": 42 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
================================== | ||
Building custom image for function | ||
================================== | ||
|
||
|
||
In this tutorial we will describe how to build custom docker image for function. | ||
|
||
In this tutorial we will be following 3 steps to deploy our function with custom docker image: | ||
|
||
* implement function template | ||
* define dockerfile, build it and push to registry | ||
* upload | ||
|
||
All of our custom image files will be located in a folder `custom_function`, which will 2 files: `Dockerfile` and `runner.py`. | ||
|
||
.. code-block:: | ||
:caption: Custom image folder source files | ||
/custom_function | ||
/runner.py | ||
/Dockerfile | ||
First we will implement function entrypoint by following template. All functions with custom docker images must follow same template structure. | ||
|
||
We need to create class `Runner` and implement `run` method that will be called during invocation of the function and results of the run method will be returned as result of the function. | ||
|
||
Let's create `runner.py` file with following content | ||
|
||
.. code-block:: | ||
:caption: `runner.py` - Runner class implementation. This is an entrypoint to you custom image function. | ||
class Runner: | ||
def run(self, arguments: dict) -> dict: | ||
# this is just an example | ||
# your function can call for other modules, function, etc. | ||
return { | ||
**arguments, | ||
**{ | ||
"answer": 42 | ||
} | ||
} | ||
As a next step let's define and build our custom docker image. | ||
|
||
Dockerfile will be extending base serverless node image and adding required packages and structure to it. | ||
|
||
In our simple case it will look something like this | ||
|
||
.. code-block:: | ||
:caption: Dockerfile for custom image function. | ||
FROM icr.io/quantum-public/quantum-serverless-ray-node:0.10.1-py310 | ||
# install all necessary dependencies for your custom image | ||
# copy our function implementation in `/runner.py` of the docker image | ||
COPY ./runner.py ./runner.py | ||
and then we need to build it | ||
|
||
.. code-block:: | ||
:caption: Build and push image. | ||
docker build -t icr.io/quantum-public/my-custom-function-image:1.0.0 ./custom_function | ||
docker push icr.io/quantum-public/my-custom-function-image:1.0.0 | ||
We got to our final step of function development - uploading to serverless. | ||
|
||
Let define `QiskitFunction` with image we just build, give it a name and upload it. | ||
|
||
.. code-block:: | ||
:caption: Uploading and using function with custom image. | ||
import os | ||
from quantum_serverless import QiskitFunction, ServerlessClient | ||
serverless = ServerlessClient( | ||
token=os.environ.get("GATEWAY_TOKEN", "awesome_token"), | ||
host=os.environ.get("GATEWAY_HOST", "http://localhost:8010"), | ||
) | ||
serverless | ||
function_with_custom_image = QiskitFunction( | ||
title="custom-image-function", | ||
image="icr.io/quantum-public/my-custom-function-image:1.0.0" | ||
) | ||
function_with_custom_image | ||
serverless.upload(function_with_custom_image) | ||
functions = {f.title: f for f in serverless.list()} | ||
my_function = functions.get("custom-image-function") | ||
my_function | ||
job = my_function.run(test_argument_one=1, test_argument_two="two") | ||
job | ||
job.result() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,3 +13,4 @@ compute resources. | |
local | ||
cloud | ||
client_configuration | ||
deploying_custom_image_function |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
"""Cleanup resources command.""" | ||
|
||
import logging | ||
|
||
from django.conf import settings | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
"""Cleanup resources command.""" | ||
|
||
import json | ||
import logging | ||
import time | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
"""Cleanup resources command.""" | ||
|
||
import logging | ||
|
||
from concurrency.exceptions import RecordModifiedError | ||
|
38 changes: 38 additions & 0 deletions
38
gateway/api/migrations/0022_program_image_alter_program_artifact_and_more.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
# Generated by Django 4.2.11 on 2024-04-25 20:42 | ||
|
||
import api.models | ||
import django.core.validators | ||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
dependencies = [ | ||
("api", "0021_alter_program_options_program_instances"), | ||
] | ||
|
||
operations = [ | ||
migrations.AddField( | ||
model_name="program", | ||
name="image", | ||
field=models.CharField(blank=True, max_length=511, null=True), | ||
), | ||
migrations.AlterField( | ||
model_name="program", | ||
name="artifact", | ||
field=models.FileField( | ||
blank=True, | ||
null=True, | ||
upload_to=api.models.get_upload_path, | ||
validators=[ | ||
django.core.validators.FileExtensionValidator( | ||
allowed_extensions=["tar"] | ||
) | ||
], | ||
), | ||
), | ||
migrations.AlterField( | ||
model_name="program", | ||
name="entrypoint", | ||
field=models.CharField(default="main.py", max_length=255), | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.