DockerSpawner - container deletion by user choice

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()

However

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