Skip to content
Snippets Groups Projects

Async docker launch

Merged Lauri Himanen requested to merge async-docker-launch into develop
1 file
+ 20
13
Compare changes
  • Side-by-side
  • Inline
@@ -17,6 +17,7 @@
#
from typing import List
import asyncio
from fastapi import APIRouter, HTTPException, Request, Depends
from collections import deque
from datetime import datetime, timedelta
@@ -112,18 +113,24 @@ async def post_instances(request: Request, instance: InstanceModel, token=Depend
path = current_containers[0].labels['path']
return InstanceResponseModel(path=path)
docker_client.containers.run(
image="jupyter/datascience-notebook",
command=(
f'start-notebook.sh --NotebookApp.base_url={path}'
' --NotebookApp.token="" --NotebookApp.password=""'
),
ports={"8888": int(f'1000{channel}')},
detach=True,
name=container_name,
user="1000:1000",
group_add=["1000"],
labels={"path": path}
)
# We use an async function to run the container so that the API does not
# get blocked even if docker needs to do some heavier container startup
# routines (e.g. download the container image).
async def run_container():
docker_client.containers.run(
image="jupyter/datascience-notebook",
command=(
f'start-notebook.sh --NotebookApp.base_url={path}'
' --NotebookApp.token="" --NotebookApp.password=""'
),
ports={"8888": int(f'1000{channel}')},
detach=True,
name=container_name,
user="1000:1000",
group_add=["1000"],
labels={"path": path}
)
loop = asyncio.get_event_loop()
loop.create_task(run_container())
return InstanceResponseModel(path=path)
Loading