Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use environment variable to set port to run on #5212

Closed
json2d opened this issue Feb 7, 2020 · 7 comments · Fixed by #5221
Closed

Use environment variable to set port to run on #5212

json2d opened this issue Feb 7, 2020 · 7 comments · Fixed by #5221

Comments

@json2d
Copy link

json2d commented Feb 7, 2020

So i get this is how you would set the port via cli:

jupyter notebook --port 9999

Is there currently a way to set this and other run options with an environment variable eg:

export JUPYTER_PORT=9999
jupyter notebook
@kevin-bates
Copy link
Member

No, this CLI option is not currently tied to an environment variable. I believe the preference in notebook is to avoid that where possible (although containerized envs make this approach attractive).

@json2d
Copy link
Author

json2d commented Feb 8, 2020

@kevin-bates yep you said it - it would be useful for working with containers. thanks for the info

@json2d json2d closed this as completed Feb 8, 2020
@kevin-bates
Copy link
Member

Hi @json2d . I'm inclined to propose we identify a small set of container-relevant options and env variable associations to them. The previously mentioned preference may have been more of a "generally speaking" thing, and I think there could be value here.

I think port and probably ip are two. You'd definitely want to disable port-retries for this, but that could be something that's part of a "baked in" configuration.

I'm going to re-open this until we determine the best approach forward here. Thanks.

@kevin-bates kevin-bates reopened this Feb 8, 2020
@json2d
Copy link
Author

json2d commented Feb 9, 2020

At the moment there are a few steps involved in running Jupyter containers with the port set via an environment variable like NB_PORT.

First you need to create a jupyter.Dockerfile in your project:

FROM jupyter/minimal-notebook:latest

ENV NB_PORT 8888

# run notebook on 0.0.0.0:8888
CMD jupyter notebook --port=$NB_PORT --no-browser

Second you need to build and tag the image:

$ docker build --file jupyter.Dockerfile --tag json2d/jupyter .

Finally you can run the image with NB_PORT set using something like:

$ export NB_PORT=9999
$ docker run --env NB_PORT -p $NB_PORT:$NB_PORT json2d/jupyter

It gets a bit messy when you go to publish/expose the port, but that's just what you have to do with Docker.

So if Jupyter could be patched to automatically use an environment like NB_PORT for the port, then potentially you could skip the first two steps and just use the official image jupyter/minimal-notebook:latest directly in the last step:

$ export NB_PORT=9999
$ docker run --env NB_PORT -p $NB_PORT:$NB_PORT jupyter/minimal-notebook:latest

@kevin-bates
Copy link
Member

Thanks @json2d. Your example reminds me about the docker-stacks images and how this could be accomplished via the various start scripts. However, there are many who might build their own images and would like this capability.

I think by adding support for specifying port via an env wihtin the application, we can allow for the existing behaviors to override. For example, the default port number would essentially come from the env, but the CLI option --port would override the env. So, if you did this, ...

export JUPYTER_PORT=8899
jupyter notebook --port 9999

Notebook would start on port 9999, but with this...

export JUPYTER_PORT=8899
jupyter notebook

it would start on port 8899. I believe this would allow for existing behaviors to not be side affected by someone now having JUPYTER_PORT in their env.

Because its imperative that port-retries be zero in these circumstances, yet its current default is 50, I think we'd also want back it by an env as well. If the port-retries env is not set, the default of 50 would still be used. That said, I don't feel as strongly about ip. So perhaps only the two port-based variables would have env backing.

I can try to put together a pull request for this if you agree. Once a PR is present, I think others might find a need to comment.

@json2d
Copy link
Author

json2d commented Feb 10, 2020

I think having the CLI option --port overriding the env and setting port-retries to zero both seem sensible. I haven't used the ip option much so I couldn't really comment about its usefulness as an env var.

kevin-bates added a commit to kevin-bates/notebook that referenced this issue Feb 11, 2020
In order to better support use cases relating to containerized environments,
this change adds environment variable support for the `--port` option.  Since
anyone setting a specific port probably doesn't want port retry logic enabled,
the `--port-retries` option has also been backed by an env.

Option `--port` will be backed by env `JUPYTER_PORT` and still defaults to `8888`.
Option `--port-retries` will be backed by env `JUPYTER_PORT_RETRIES` and still
defaults to `50`.

The CLI options will override those set via the envrionment, but environment
values override those set via configuration files.

Closes jupyter#5212
@kevin-bates
Copy link
Member

@json2d - I submitted the PR above. It would be great if you could take it for a spin in your env to see how things work out.

blink1073 pushed a commit that referenced this issue May 16, 2020
* Add env variable support for port options

In order to better support use cases relating to containerized environments,
this change adds environment variable support for the `--port` option.  Since
anyone setting a specific port probably doesn't want port retry logic enabled,
the `--port-retries` option has also been backed by an env.

Option `--port` will be backed by env `JUPYTER_PORT` and still defaults to `8888`.
Option `--port-retries` will be backed by env `JUPYTER_PORT_RETRIES` and still
defaults to `50`.

The CLI options will override those set via the envrionment, but environment
values override those set via configuration files.

Closes #5212

* Fixup after merge
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 26, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants