从外部网络访问docker上的jupyterhub

时间:2019-10-02 15:37:48

标签: docker jupyter jupyterhub

Docker对我来说还很陌生,我正在创建一个像这样的jupyterhub容器

FROM ubuntu:18.04
LABEL maintainer="Jupyter Project <jupyter@googlegroups.com>"

# install nodejs, utf8 locale, set CDN because default httpredir is unreliable
ENV DEBIAN_FRONTEND noninteractive
RUN apt-get -y update && \
apt-get -y upgrade && \
apt-get -y install wget git bzip2 && \
apt-get purge && \
apt-get clean && \
rm -rf /var/lib/apt/lists/*
ENV LANG C.UTF-8

# install Python + NodeJS with conda
RUN wget -q https://repo.continuum.io/miniconda/Miniconda3-4.5.11-Linux-x86_64.sh -O /tmp/miniconda.sh  && \
echo 'e1045ee415162f944b6aebfe560b8fee */tmp/miniconda.sh' | md5sum -c - && \
bash /tmp/miniconda.sh -f -b -p /opt/conda && \
/opt/conda/bin/conda install --yes -c conda-forge \
  python=3.6 sqlalchemy tornado jinja2 traitlets requests pip pycurl \
  nodejs configurable-http-proxy && \
/opt/conda/bin/pip install --upgrade pip && \
rm /tmp/miniconda.sh
ENV PATH=/opt/conda/bin:$PATH

ADD . /src/jupyterhub
WORKDIR /src/jupyterhub

RUN npm install -g configurable-http-proxy
RUN python3 -m pip install jupyterhub
RUN python3 -m pip install dockerspawner 

WORKDIR /srv/jupyterhub/
EXPOSE 8000

COPY jupyterhub_config.py /srv/jupyterhub/jupyterhub_config.py

LABEL org.jupyter.service="jupyterhub"

CMD ["jupyterhub"]

具有以下基于this discussion的配置文件

# launch with docker
c.JupyterHub.spawner_class = 'dockerspawner.DockerSpawner'

# we need the hub to listen on all ips when it is in a container
c.JupyterHub.hub_ip = '0.0.0.0'
c.JupyterHub.port = 8000
c.DockerSpawner.extra_host_config = {'network_mode': 'host'}
c.DockerSpawner.use_internal_ip = True
c.DockerSpawner.network_name = 'host'

然后我这样做:

docker run -i -p 8000:8000 --name jupyterhub jupyterhub:latest jupyterhub -f jupyterhub_config.py

它可以在我的内部网络上正常工作,但是我不能从外部访问它。如果我这样做:

my_ip:8000

我无法连接。

对于在docker上运行的Flask应用程序,我要做的就是在0.0.0.0 ip中运行该应用程序,并且它可以正常工作,我对如何配置网络感到困惑。任何帮助都将受到欢迎。

谢谢。

2 个答案:

答案 0 :(得分:1)

您可以在localhost上配置nginx,女巫将代理来自外部世界的端口请求,这些请求将监听您的docker容器中的本地端口

server {

    server_name <your_ip>;
    location / {
        proxy_pass http://0.0.0.0:8000;
    }
    listen 7000; 
}

现在您可以从外部

打开<your_ip>:7000

答案 1 :(得分:1)

经过一段时间的努力,我基于讨论here和文档here找到了解决方案。我的jupyterhub_config.py变成了:

c.JupyterHub.bind_url = 'http://0.0.0.0:8000/jhub'

并按照我提供的链接/etc/nginx/sites-enabled/jupyterhub.conf

# top-level http config for websocket headers
# If Upgrade is defined, Connection = upgrade
# If Upgrade is empty, Connection = close
map $http_upgrade $connection_upgrade {
    default upgrade;
    ''      close;
}

server {
  listen 80;
  listen [::]:80;

  server_name my_ip;

  location /jhub {
      rewrite /jhub/(.*) /jhub/$1 break;
      proxy_pass http://0.0.0.0:8000/;
      proxy_set_header Host $host;
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

      # websocket headers
      proxy_set_header Upgrade $http_upgrade;
      proxy_set_header Connection $connection_upgrade;
  }
  location / {
      proxy_pass http://0.0.0.0:5000/;
  }

  location ~ /.well-known {
     allow all;
  }
}