feat: publishing infernet-container-starter v0.1.0
This commit is contained in:
20
projects/hello-world/container/Dockerfile
Normal file
20
projects/hello-world/container/Dockerfile
Normal file
@ -0,0 +1,20 @@
|
||||
FROM python:3.11-slim as builder
|
||||
|
||||
WORKDIR /app
|
||||
|
||||
ENV PYTHONUNBUFFERED 1
|
||||
ENV PYTHONDONTWRITEBYTECODE 1
|
||||
ENV PYTHONPATH src
|
||||
|
||||
WORKDIR /app
|
||||
|
||||
RUN apt-get update
|
||||
|
||||
COPY src/requirements.txt .
|
||||
|
||||
RUN pip install --upgrade pip && pip install -r requirements.txt
|
||||
|
||||
COPY src src
|
||||
|
||||
ENTRYPOINT ["gunicorn", "app:create_app()"]
|
||||
CMD ["-b", "0.0.0.0:3000"]
|
20
projects/hello-world/container/Makefile
Normal file
20
projects/hello-world/container/Makefile
Normal file
@ -0,0 +1,20 @@
|
||||
DOCKER_ORG := ritualnetwork
|
||||
TAG := $(DOCKER_ORG)/hello-world-infernet:latest
|
||||
|
||||
.phony: build run publish
|
||||
|
||||
build:
|
||||
@docker build -t $(TAG) .
|
||||
|
||||
update-tag:
|
||||
jq ".containers[0].image = \"$(TAG)\"" config.json > updated_config.json && mv updated_config.json config.json
|
||||
|
||||
run: build
|
||||
docker run \
|
||||
-p 3000:3000 $(TAG)
|
||||
|
||||
# You may need to set up a docker builder, to do so run:
|
||||
# docker buildx create --name mybuilder --bootstrap --use
|
||||
# refer to https://docs.docker.com/build/building/multi-platform/#building-multi-platform-images for more info
|
||||
build-multiplatform:
|
||||
docker buildx build --platform linux/amd64,linux/arm64 -t $(TAG) --push .
|
163
projects/hello-world/container/README.md
Normal file
163
projects/hello-world/container/README.md
Normal file
@ -0,0 +1,163 @@
|
||||
# Creating an infernet-compatible `hello-world` container
|
||||
|
||||
In this tutorial, we'll create a simple hello-world container that can be used
|
||||
with infernet.
|
||||
|
||||
> [!NOTE]
|
||||
> This directory `containers/hello-world` already includes the final result
|
||||
> of this tutorial. Run the following tutorial in a new directory.
|
||||
|
||||
Let's get started! 🎉
|
||||
|
||||
## Step 1: create a simple flask-app and a requirements.txt file
|
||||
|
||||
First, we'll create a simple flask-app that returns a hello-world message.
|
||||
We begin by creating a `src` directory:
|
||||
|
||||
```
|
||||
mkdir src
|
||||
```
|
||||
|
||||
Inside `src`, we create a `app.py` file with the following content:
|
||||
|
||||
```python
|
||||
from typing import Any
|
||||
|
||||
from flask import Flask, request
|
||||
|
||||
|
||||
def create_app() -> Flask:
|
||||
app = Flask(__name__)
|
||||
|
||||
@app.route("/")
|
||||
def index() -> str:
|
||||
return "Hello world service!"
|
||||
|
||||
@app.route("/service_output", methods=["POST"])
|
||||
def inference() -> dict[str, Any]:
|
||||
input = request.json
|
||||
return {"output": f"hello, world!, your input was: {input}"}
|
||||
|
||||
return app
|
||||
```
|
||||
|
||||
As you can see, the app has two endpoints: `/` and `/service_output`. The first
|
||||
one is simply used to ping the service, while the second one is used for infernet.
|
||||
|
||||
We can see that our app uses the `flask` package. Additionally, we'll need to
|
||||
install the `gunicorn` package to run the app. We'll create a `requirements.txt`
|
||||
file with the following content:
|
||||
|
||||
```
|
||||
Flask>=3.0.0,<4.0.0
|
||||
gunicorn>=21.2.0,<22.0.0
|
||||
```
|
||||
|
||||
## Step 2: create a Dockerfile
|
||||
|
||||
Next, we'll create a Dockerfile that builds the flask-app and runs it.
|
||||
At the top-level directory, create a `Dockerfile` with the following content:
|
||||
|
||||
```dockerfile
|
||||
FROM python:3.11-slim as builder
|
||||
|
||||
WORKDIR /app
|
||||
|
||||
ENV PYTHONUNBUFFERED 1
|
||||
ENV PYTHONDONTWRITEBYTECODE 1
|
||||
ENV PYTHONPATH src
|
||||
|
||||
WORKDIR /app
|
||||
|
||||
RUN apt-get update
|
||||
|
||||
COPY src/requirements.txt .
|
||||
|
||||
RUN pip install --upgrade pip && pip install -r requirements.txt
|
||||
|
||||
COPY src src
|
||||
|
||||
ENTRYPOINT ["gunicorn", "app:create_app()"]
|
||||
CMD ["-b", "0.0.0.0:3000"]
|
||||
```
|
||||
|
||||
This is a simple Dockerfile that:
|
||||
|
||||
1. Uses the `python:3.11-slim` image as a base image
|
||||
2. Installs the requirements
|
||||
3. Copies the source code
|
||||
4. Runs the app on port `3000`
|
||||
|
||||
> [!IMPORTANT]
|
||||
> App must be exposed on port `3000`. Infernet's orchestrator
|
||||
> will always assume that the container apps are exposed on that port within the container.
|
||||
> Users can then remap this port to any port that they want on the host machine
|
||||
> using the `port` parameter in the container specs.
|
||||
|
||||
By now, your project directory should look like this:
|
||||
|
||||
```
|
||||
.
|
||||
├── Dockerfile
|
||||
├── README.md
|
||||
└── src
|
||||
├── __init__.py
|
||||
└── app.py
|
||||
└── requirements.txt
|
||||
```
|
||||
|
||||
## Step 3: build the container
|
||||
|
||||
Now, we can build the container. At the top-level directory, run:
|
||||
|
||||
```
|
||||
docker build -t hello-world .
|
||||
```
|
||||
|
||||
## Step 4: run the container
|
||||
|
||||
Finally, we can run the container. In one terminal, run:
|
||||
|
||||
```
|
||||
docker run --rm -p 3000:3000 --name hello hello-world
|
||||
```
|
||||
|
||||
## Step 5: ping the container
|
||||
|
||||
In another terminal, run:
|
||||
|
||||
```
|
||||
curl localhost:3000
|
||||
```
|
||||
|
||||
It should return something like:
|
||||
|
||||
```
|
||||
Hello world service!
|
||||
```
|
||||
|
||||
Congratulations! You've created a simple hello-world container that can be
|
||||
used with infernet. 🎉
|
||||
|
||||
## Step 6: request a service output
|
||||
|
||||
Now, let's request a service output. Note that this endpoint is called by
|
||||
the infernet node, not by the user. For debugging purposes however, it's useful to
|
||||
be able to call it manually.
|
||||
|
||||
In your terminal, run:
|
||||
|
||||
```
|
||||
curl -X POST -H "Content-Type: application/json" -d '{"input": "hello"}' localhost:3000/service_output
|
||||
```
|
||||
|
||||
The output should be something like:
|
||||
|
||||
```
|
||||
{"output": "hello, world!, your input was: {'input': 'hello'}"}
|
||||
```
|
||||
|
||||
Your users will never call this endpoint directly. Instead, they will:
|
||||
|
||||
1. Either [create an off-chain job request](../../../README.md#L36) through the node API
|
||||
2. Or they will make a subscription on their contracts
|
50
projects/hello-world/container/config.json
Normal file
50
projects/hello-world/container/config.json
Normal file
@ -0,0 +1,50 @@
|
||||
{
|
||||
"log_path": "infernet_node.log",
|
||||
"server": {
|
||||
"port": 4000
|
||||
},
|
||||
"chain": {
|
||||
"enabled": true,
|
||||
"trail_head_blocks": 0,
|
||||
"rpc_url": "http://host.docker.internal:8545",
|
||||
"coordinator_address": "0x5FbDB2315678afecb367f032d93F642f64180aa3",
|
||||
"wallet": {
|
||||
"max_gas_limit": 4000000,
|
||||
"private_key": "0x59c6995e998f97a5a0044966f0945389dc9e86dae88c7a8412f4603b6b78690d"
|
||||
}
|
||||
},
|
||||
"startup_wait": 1.0,
|
||||
"docker": {
|
||||
"username": "your-username",
|
||||
"password": ""
|
||||
},
|
||||
"redis": {
|
||||
"host": "redis",
|
||||
"port": 6379
|
||||
},
|
||||
"forward_stats": true,
|
||||
"containers": [
|
||||
{
|
||||
"id": "hello-world",
|
||||
"image": "ritualnetwork/hello-world-infernet:latest",
|
||||
"external": true,
|
||||
"port": "3000",
|
||||
"allowed_delegate_addresses": [],
|
||||
"allowed_addresses": [],
|
||||
"allowed_ips": [],
|
||||
"command": "--bind=0.0.0.0:3000 --workers=2",
|
||||
"env": {}
|
||||
},
|
||||
{
|
||||
"id": "anvil-node",
|
||||
"image": "ritualnetwork/infernet-anvil:0.0.0",
|
||||
"external": true,
|
||||
"port": "8545",
|
||||
"allowed_delegate_addresses": [],
|
||||
"allowed_addresses": [],
|
||||
"allowed_ips": [],
|
||||
"command": "",
|
||||
"env": {}
|
||||
}
|
||||
]
|
||||
}
|
47
projects/hello-world/container/scripts/request_node.py
Normal file
47
projects/hello-world/container/scripts/request_node.py
Normal file
@ -0,0 +1,47 @@
|
||||
from time import sleep
|
||||
|
||||
import requests
|
||||
|
||||
|
||||
def hit_server_directly():
|
||||
print("hello")
|
||||
r = requests.get("http://localhost:3000/")
|
||||
print(r.status_code)
|
||||
# server response
|
||||
print("server response", r.text)
|
||||
|
||||
|
||||
def poll_until_complete(id: str):
|
||||
status = "running"
|
||||
r = None
|
||||
while status == "running":
|
||||
r = requests.get(
|
||||
"http://localhost:4000/api/jobs",
|
||||
params={
|
||||
"id": id,
|
||||
},
|
||||
).json()[0]
|
||||
status = r.get("status")
|
||||
print("status", status)
|
||||
if status != "running":
|
||||
return r
|
||||
sleep(1)
|
||||
|
||||
|
||||
def create_job_through_node():
|
||||
r = requests.post(
|
||||
"http://localhost:4000/api/jobs",
|
||||
json={
|
||||
"containers": ["hello-world"],
|
||||
"data": {"some": "object"},
|
||||
},
|
||||
)
|
||||
|
||||
job_id = r.json().get("id")
|
||||
|
||||
result = poll_until_complete(job_id)
|
||||
print("result", result)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
create_job_through_node()
|
18
projects/hello-world/container/src/app.py
Normal file
18
projects/hello-world/container/src/app.py
Normal file
@ -0,0 +1,18 @@
|
||||
from typing import Any
|
||||
|
||||
from flask import Flask, request
|
||||
|
||||
|
||||
def create_app() -> Flask:
|
||||
app = Flask(__name__)
|
||||
|
||||
@app.route("/")
|
||||
def index() -> str:
|
||||
return "Hello world service!"
|
||||
|
||||
@app.route("/service_output", methods=["POST"])
|
||||
def inference() -> dict[str, Any]:
|
||||
input = request.json
|
||||
return {"output": f"hello, world!, your input was: {input}"}
|
||||
|
||||
return app
|
2
projects/hello-world/container/src/requirements.txt
Normal file
2
projects/hello-world/container/src/requirements.txt
Normal file
@ -0,0 +1,2 @@
|
||||
Flask>=3.0.0,<4.0.0
|
||||
gunicorn>=21.2.0,<22.0.0
|
Reference in New Issue
Block a user