imagename and tag not sanitized in name_template
vncntprvst opened this issue · 2 comments
Bug description
Using {imagename}
in name_template
produces a Bad Request "Invalid container name"
error when starting the server.
Expected behaviour
The container should spawn without error.
In addition, the container name should be based on the image, e.g., datascience-notebook
for jupyter/datascience-notebook
.
Actual behaviour
When launching the Jupyter notebook container, Jupyterhub sends this error:
Bad Request ("Invalid container name (jupyterhub-singleuser:3.1-vncntprvst–), only [a-zA-Z0-9][a-zA-Z0-9_.-] are allowed")
How to reproduce
Define c.DockerSpawner.name_template = '{imagename}-{username}–{servername}'
in jupyterhub_config.py
and
c.DockerSpawner.allowed_images = {
'Data science':'jupyter/datascience-notebook:latest'
}
then select 'Data science' from the drop-down menu
Your personal set up
The server code repository is found here: https://github.com/wanglab-neuro/jupyterlab_server
- OS: jupyterhub container
- Version(s):
jupyterhub/jupyterhub:3.1.1
withdockerspawner 12.1.0
# JupyterHub configuration
import os, sys, pwd, subprocess, re
import docker
############################# Authentication ########################
### GitHub OAuth authentication
from oauthenticator.github import GitHubOAuthenticator
<...>
############################# Generic ########################
## Admin access: give admins permission to log in to the single user notebook servers owned by other users
c.JupyterHub.admin_access = True
############################# Permissions ########################
# Updating permissions for volumes mounted from host
c.DockerSpawner.extra_create_kwargs = {'user': 'root'}
c.DockerSpawner.environment = {
<...>
}
############################# Pre-spawn Hook ########################
def pre_spawn_hook(spawner):
<...>
c.Spawner.pre_spawn_hook = pre_spawn_hook
c.DockerSpawner.post_start_cmd = 'bash -c "/media/link_package_files.sh"'
############################# Docker Spawner Configuration ########################
c.JupyterHub.spawner_class = 'dockerspawner.DockerSpawner'
c.DockerSpawner.network_name = os.environ['DOCKER_NETWORK_NAME']
c.JupyterHub.hub_ip = os.environ['HUB_IP']
# Pick a docker image.
c.JupyterHub.allow_named_servers=True
c.DockerSpawner.name_template = '{imagename}-{username}–{servername}'
# c.DockerSpawner.name_template = '{imagename.split(":")[0]}-{username}--{servername}'
c.DockerSpawner.allowed_images = {
'Data science':'jupyter/datascience-notebook:latest'
}
## access GPU
c.DockerSpawner.extra_host_config = {
"device_requests": [
docker.types.DeviceRequest(
count=-1,
capabilities=[["gpu"]],
),
],
}
## Remove containers once they are stopped
c.DockerSpawner.remove = True
# User data persistence
home_dir = os.environ.get('DOCKER_NOTEBOOK_DIR') or '/home/jovyan' #os.path.join('/home', '{username}')
notebook_dir = home_dir # this is the root directory for the Jupyterlab sidebar
data_dir = home_dir + '/data'
c.DockerSpawner.notebook_dir = notebook_dir #home_dir
c.DockerSpawner.default_url = '/lab'
c.DockerSpawner.volumes = {
<...>
}
############################# Services ########################
c.JupyterHub.load_roles = [
{
"name": "jupyterhub-idle-culler-role",
"scopes": [
"list:users",
"read:users:activity",
"delete:servers",
# "admin:users", # if using --cull-users
],
# assignment of role's permissions to:
"services": ["jupyterhub-idle-culler-service"],
}
]
c.JupyterHub.services = [
{
"name": "jupyterhub-idle-culler-service",
"command": [
sys.executable,
"-m", "jupyterhub_idle_culler",
"--timeout=3600",
],
"admin": True, # Has to be disabled version>2.0
}
]
Thank you for opening your first issue in this project! Engagement like this is essential for open source projects! 🤗
If you haven't done so already, check out Jupyter's Code of Conduct. Also, please try to follow the issue template as it helps other other community members to contribute more effectively.
You can meet the other Jovyans by joining our Discourse forum. There is also an intro thread there where you can stop by and say Hi! 👋
Welcome to the Jupyter community! 🎉
A couple things I tried to solve this but didn't work.
Adding this bit of code to the pre_spawn_hook:
# Remove repository and image tag from image name
image_name = spawner.image.split(':')[0].split('/')[-1]
# Sanitize image name
sanitized_image_name = re.sub(r'[^a-zA-Z0-9_.-]', '', image_name)
# Set name_template with sanitized imagename, username, and servername
spawner.name_template = f"{sanitized_image_name}-{spawner.user.name}--{spawner.name}"
This had no impact.
Also, defining name_template
as follow:
c.DockerSpawner.name_template = '{imagename.split(":")[0]}-{username}--{servername}'
produced an Error 500
.