Added: Changed DockerSpawner

This commit is contained in:
2025-09-15 15:31:53 +02:00
parent 990cc84c5f
commit 8d9291d312
2 changed files with 28 additions and 56 deletions

View File

@@ -1,6 +1,3 @@
# THIS IS AN EXAMPLE
# CHANGE IT IN PRODUCTION
# Postgres Setup # Postgres Setup
POSTGRES_DB=jupyter POSTGRES_DB=jupyter
POSTGRES_USER=jupyter POSTGRES_USER=jupyter
@@ -18,8 +15,11 @@ SPAWNER_DEFAULT_URL=/lab
NOTEBOOK_MEMORY_LIMIT=500M NOTEBOOK_MEMORY_LIMIT=500M
NOTEBOOK_CPU_LIMIT=1.0 NOTEBOOK_CPU_LIMIT=1.0
HUB_IP=jupyterhub # Name of the Docker container HUB_IP=jupyterhub # Name of the Docker container
DOCKER_NOTEBOOK_DIR=/home/jovyan/work
DOCKER_SPAWNER_DEBUG=0 # Set to non Zero to enable
# Docker # Docker
COMPOSE_PROJECT_NAME=ifn_stack
DOCKER_NETWORK_NAME=jupyter_network DOCKER_NETWORK_NAME=jupyter_network
POSTGRES_PORT=5432 POSTGRES_PORT=5432
JUPYTERHUB_PORT=8000 JUPYTERHUB_PORT=8000

View File

@@ -95,61 +95,44 @@ c.JupyterHub.template_paths = ["/etc/jupyterhub/templates"]
# Hub environment for containers # Hub environment for containers
# Spawn Single-User-Servers as Docker Containers
c.JupyterHub.spawner_class = DockerSpawner c.JupyterHub.spawner_class = DockerSpawner
c.Spawner.ip = "0.0.0.0"
# Spawn Containers from this Image
c.DockerSpawner.image = os.environ.get( c.DockerSpawner.image = os.environ.get(
"NOTEBOOK_IMAGE", "jupyter/scipy-notebook:latest" "NOTEBOOK_IMAGE", "jupyter/scipy-notebook:latest"
) )
'''network_name = os.environ.get('DOCKER_NETWORK_NAME', 'stack_default')
c.DockerSpawner.network_name = network_name
c.DockerSpawner.extra_host_config = {
'network_mode': network_name
}
c.DockerSpawner.remove = True
c.DockerSpawner.debug = True
c.DockerSpawner.default_url = os.environ.get('SPAWNER_DEFAULT_URL', '/lab')
c.DockerSpawner.use_internal_ip = True
# Resources # Connect Containers to this network
c.DockerSpawner.mem_limit = os.environ.get('NOTEBOOK_MEMORY_LIMIT', '500M') network_name = os.environ.get("DOCKER_NETWORK_NAME", "stack_default")
c.DockerSpawner.cpu_limit = float(os.environ.get('NOTEBOOK_CPU_LIMIT', '1.0')) c.DockerSpawner.use_internal_ip = True # Let docker manage network communications
c.DockerSpawner.network_name = network_name
# Volume # Explicitly set notebook directory because we'll mounting a volume to it.
c.DockerSpawner.volumes = { notebook_dir = os.environ.get("DOCKER_NOTEBOOK_DIR", "/home/jovyan/work")
'/var/run/docker.sock': '/var/run/docker.sock' c.DockerSpawner.notebook_dir = notebook_dir
# './data/jupyter/users/{username}': '/home/jovyan/work',
# './data/jupyter/nbgrader/exchange': '/srv/nbgrader/exchange',
# './data/jupyter/nbgrader/courses': '/srv/nbgrader/courses',
}
c.DockerSpawner.notebook_dir = '/home/jovyan/work' # Mount real User docker volume to the host
c.DockerSpawner.volumes = {"jupyterhub-user-{username}": notebook_dir}
# Container Environment # Remove Containers once there stopped
c.DockerSpawner.environment = { c.DockerSpawner.remove = True
'GRANT_SUDO': '0',
'JUPYTER_ENABLE_LAB': os.environ.get('ENABLE_LAB', '1'),
'JUPYTERHUB_SINGLEUSER_APP': 'jupyter_server.serverapp.ServerApp'
}
#c.DockerSpawner.hub_ip_connect = os.environ.get('HUB_IP', 'jupyterhub') # For Debbugging (passed to spawned Containers)
#c.DockerSpawner.hub_port_connect = 8081 ds_debug = True if int(os.environ.get("DOCKER_SPAWNER_DEBUG", "0")) else False
c.DockerSpawner.debug = ds_debug
def pre_spawn_hook(spawner): # Dont mess with this
"""Create user directories before spawning""" # The Spawner sits inside a docker container which manages
username = spawner.user.name # everything network related
user_dir = f"./data/jupyter/users/{username}" c.Spawner.ip = "0.0.0.0"
import os
import stat
if not os.path.exists(user_dir): # Set Resource Limits
os.makedirs(user_dir, mode=0o755, exist_ok=True) c.DockerSpawner.mem_limit = os.environ.get("NOTEBOOK_MEMORY_LIMIT", "500M")
os.chown(user_dir, 1000, 1000) c.DockerSpawner.cpu_limit = float(os.environ.get("NOTEBOOK_CPU_LIMIT", "1.0"))
# TODO Nbgrader dirs
c.DockerSpawner.pre_spawn_hook = pre_spawn_hook
'''
""" """
# Services configuration for NBGrader # Services configuration for NBGrader
@@ -223,14 +206,3 @@ c.JupyterHub.concurrent_spawn_limit = 10
# Allow named servers # Allow named servers
c.JupyterHub.allow_named_servers = True c.JupyterHub.allow_named_servers = True
c.JupyterHub.named_server_limit_per_user = 3 c.JupyterHub.named_server_limit_per_user = 3
print("JupyterHub configuration loaded successfully!")
print(f"Base URL: {c.JupyterHub.base_url}")
print(f"Bind URL: {c.JupyterHub.bind_url}")
print(f"Database URL: {c.JupyterHub.db_url}")
print(f"Docker Network: {c.DockerSpawner.network_name}")
print(f"Docker Image: {c.DockerSpawner.image}")
print("Postgres available", is_service_available_cmd("postgres", 5432))
with open("/srv/jupyterhub/cookie_secret") as f:
print(f.readlines())