访问运行在 Docker 容器上的 Jupiter 笔记本电脑

我用 python 库和 Jupiter 创建了一个 docker 映像。 我使用选项 -p 8888:8888启动容器,以链接主机和容器之间的端口。 当我在容器中启动 Jupiter 内核时,它运行在 localhost:8888上(没有找到浏览器)。我使用了命令 jupyter notebook

但是从我的主机,什么是 IP 地址,我必须使用主机的浏览器与木星工作?

用指令 ifconfig,我找到 eth0dockerwlan0lo..。

谢谢!

111934 次浏览

You need to run your notebook on 0.0.0.0: jupyter notebook -i 0.0.0.0. Running on localhost make it available only from inside the container.

To get the link to your Jupyter notebook server:

After your docker run command, a hyperlink should be automatically generated. It looks something like this: http://localhost:8888/?token=f3a8354eb82c92f5a12399fe1835bf8f31275f917928c8d2 :: /home/jovyan/work

If you want to get the link again later down the line, you can type docker exec -it <docker_container_name> jupyter notebook list.

In the container you can run the following to make it available on your local machine (using your docker machine's ip address).

jupyter notebook --ip 0.0.0.0 --allow-root

You may not need to provide the --allow-root flag depending on your container's setup.

Host machine: docker run -it -p 8888:8888 image:version

Inside the Container : jupyter notebook --ip 0.0.0.0 --no-browser --allow-root

Host machine access this url : localhost:8888/tree‌​

When you are logging in for the first time there will be a link displayed on the terminal to log on with a token.

You can use the command jupyter notebook --allow-root --ip[of your container] or give access to all ip using option --ip0.0.0.0.

The docker run command is mandatory to open a port for the container to allow the connection from a host browser, assigning the port to the docker container with -p, select your jupyter image from your docker images.

docker run -it -p 8888:8888 image:version

Inside the container launch the notebook assigning the port you opened:

jupyter notebook --ip 0.0.0.0 --port 8888 --no-browser --allow-root

Access the notebook through your desktops browser on http://localhost:8888 The notebook will prompt you for a token which was generated when you create the notebook.

The below is how I get it running on Windows 7 with docker toolbox.

If you are using docker toolbox, open up the Docker quickstart terminal, and note the IP here:

docker is configured to use the default machine with IP 192.168.99.100
For help getting started, check out the docs at https://docs.docker.com

Once you run the docker commands from the tensorflow installation website:

docker pull tensorflow/tensorflow                  # Download latest image
docker run -it -p 8888:8888 tensorflow/tensorflow  # Start a Jupyter notebook server

You will receive a message like this:

Copy/paste this URL into your browser when you connect for the first time,
to login with a token:
http://127.0.0.1:8888/?token=d6e80acaf08e09853dc72f6b0f022b8225f94f

In the host, replace 127.0.0.1 with 192.168.99.100 and use the rest of that URL

As an alternative to building your own Docker image, you can also use the ML Workspace image. The ML Workspace is an open-source web IDE that combines Jupyter, VS Code, a Desktop GUI, and many other tools & libraries into one convenient Docker image. Deploying a single workspace instance is as simple as:

docker run -p 8080:8080 mltooling/ml-workspace:latest

All tools are accessible from the same port and integrated into the Jupyter UI. You can find further documentation here.

docker run -i -t -p 8888:8888 continuumio/anaconda3 /bin/bash -c "/opt/conda/bin/conda install jupyter -y --quiet && mkdir /opt/notebooks && /opt/conda/bin/jupyter notebook --notebook-dir=/opt/notebooks --ip='*' --port=8888 --no-browser --allow-root"

i had to add --allow-root to the command and now its running

The Makefile below encapsulates the previous answers and ensures that jupyter and docker agree on the port. And if I just click/copy the link provided by jupyter, that solves port mismatch problems.

To use, just make jupyter or make jupyter PORT=xxxx from the proper folder. Then click the link in the jupyter output.

Remote Containers

If your container is on a remote host (say an AWS EC2), then you'll also need to set up an ssh tunnel with the correct port. For example, on the local machine:

ssh -N -f -L localhost:8888:localhost:8888 username@remote-host

But at least that's only one place I can manually mismatch ports.

Makefile

# Set your default jupyter port here.
# Avoid 8888 if you run local notebooks on that!
PORT=8888
# Your app root in the container.
APP_DIR=/app
# The docker image to launch.  *** SET TO YOUR image:version! ***
APP_TAG=image:version


jupyter: ##
## Launch jupyter notebook from our container, mapping two folders
##    Local          Container       Notes
##    -----------------------------------------------------
##    ./data      -> /data           Put data here!
##    ./notebooks -> /notebooks      Find notebooks here!
##    -----------------------------------------------------
## Arg:  PORT - specify port [${PORT}]
docker run \
-p $(PORT):$(PORT) \
-v $(PWD)/notebooks/:$(APP_DIR)/notebooks/ \
-v $(PWD)/data:/data \
$(APP_TAG) \
jupyter notebook --ip 0.0.0.0 --port $(PORT) \
--no-browser --allow-root

Go in the Docker and check cat /etc/jupyter/jupyter_notebook_config.py :

  • You should see / add this line :

c.NotebookApp.allow_origin = 'https://colab.research.google.com'