# How to dynamically chose DockerSpawner image based on user and container name in jupyterhub\_config?

**URL:** <https://discourse.jupyter.org/t/how-to-dynamically-chose-dockerspawner-image-based-on-user-and-container-name-in-jupyterhub-config/12866>\
**Category:** JupyterHub\
**Tags:** docker\
**Created:** [February 3, 2022, 12:48am UTC](https://discourse.jupyter.org/t/how-to-dynamically-chose-dockerspawner-image-based-on-user-and-container-name-in-jupyterhub-config/12866 "2022-02-03T00:48:39Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![yannsartori](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.jupyter.org/yannsartori/32/6368_2.png) [@yannsartori](https://discourse.jupyter.org/u/yannsartori)\
**Post date:** [February 3, 2022, 12:48am UTC](https://discourse.jupyter.org/t/how-to-dynamically-chose-dockerspawner-image-based-on-user-and-container-name-in-jupyterhub-config/12866/1 "2022-02-03T00:48:39Z")

</div>

Hello,  
I am trying to set up JuptyerHub using `DockerSpawner` as my spawner class. To specify the image to use, I would like to do it in function of of the username of the user trying to spawn the image, as well as the container name (e.g. use the image f’base-{username}-{container\_name}’). I don’t think I am able to do this with the more static

```python
c.DockerSpawner.image = ...

```

formulation, as I don’t have access to either of those variables. The closest I’ve came to my desired outcome is using the `allowed_images` attribute of `DockerSpawner` in the following manner:

```python
def allowed_images_filter(spawner):
    username = spawner.user.name
    container_name = spawner.container_name
    image = f'base-{username}-{container_name}'
    return {
       '': image,
       image: image,
   }
c.DockerSpawner.allowed_images = allowed_images_filter

```

However, this forces the user to chose an image when spinning up a server. Ideally, I would like this to happen automatically. I tried returning the dictionary without the empty-string entry, but then I get an error that the image name is not in the allowed images.

Thank you in advance.
