DockerSpawner - container deletion by user choice

I was wondering about a feature for JupyterHub + DockerSpawner. Right now we are using this option in the config file
c.DockerSpawner.remove = True

However, some users would like to keep the container running after their server is stopped, so it would be nice to be given an option whether they want to delete the container or keep running the old one, when they are starting their server again. We cannot remove the line from config, because more users use restarting the server to get a new container. I was just wondering if there is any way to give them a choice of deleting/continuing.

I’m guessing you mean you want to be able to stop the container but not delete it, rather than keep it running?

If so you might be able to achieve this with

by creating two profiles, with and without remove. Unfortunately this would have to be specified when the user starts their server.

Alternatively you could probably subclass DockerSpawner, set remove = False, and add parameter that controls whether an existing container is re-used at startup, which is set with WrapSpawner.

1 Like

Thanks for your suggestion, it looks quite interesting, although I dont think it would entirely solve our problem.
I have been looking up some information and found out the best practise seems to be to leave the remove config False. I suspect that as a ordinary user you cannot in any way delete your container and start a new one. We could probably persuade our users to leave the config on False, but another worry of ours is never deleting the containers and they just growing in size. Is there any consensus on this subject? Like having a monthly or so container deletion for every user?

I suspect that as a ordinary user you cannot in any way delete your container and start a new one

DockerSpawner does delete on start if DockerSpawner.remove, so setting remove=True for this start will delete the previous container if there was one. The reasoning behind this is that remove config could have changed since the last shutdown, and it should take effect immediately, not after the next shutdown and restart. So the only thing missing is changing spawner.remove after startup. If you always set it to False after startup, then setting ‘remove’ in your profiles would mean “start a clean session” vs “reuse previous session”.

class MyDockerSpawner(DockerSpawner):
    def stop(self):
        # never remove containers on stop, only (optionally) remove them on next start!
        self.remove = False
        return super().stop()


Is there any consensus on this subject?

I think the best practice is to always use remove = True and document what is persisted (e.g. user’s home directory) and what is not (e.g. the root Python environment). Then ask your users what it is that they want persisted that isn’t being persisted. Is it user-installed packages? If so, document creating environments in the home directory which will be persisted, etc.

1 Like