Uses jupyter/notebook option to pass explicit hostname

pull/358/head
Gladys Nalvarte 2018-07-31 11:28:20 +02:00
rodzic e0b92796a4
commit 4c0cb69b80
2 zmienionych plików z 29 dodań i 17 usunięć

Wyświetl plik

@ -503,11 +503,26 @@ class Repo2Docker(Application):
Returns running container
"""
client = docker.from_env(version='auto')
docker_host = os.environ.get('DOCKER_HOST')
if docker_host:
host_name = urlparse(docker_host).hostname
else:
host_name = '127.0.0.1'
self.hostname = host_name
if not self.run_cmd:
port = str(self._get_free_port())
self.port = port
run_cmd = ['jupyter', 'notebook', '--ip', '0.0.0.0',
'--port', port]
# To use the option --NotebookApp.custom_display_url
# make sure the base-notebook image is updated:
# docker pull jupyter/base-notebook
run_cmd = [
'jupyter', 'notebook',
'--ip', '0.0.0.0',
'--port', port,
"--NotebookApp.custom_display_url=http://{}:{}".format(host_name, port),
]
ports = {'%s/tcp' % port: port}
else:
# run_cmd given by user, if port is also given then pass it on
@ -519,13 +534,6 @@ class Repo2Docker(Application):
# store ports on self so they can be retrieved in tests
self.ports = ports
docker_host = os.environ.get('DOCKER_HOST')
if docker_host:
host_name = urlparse(docker_host).host_name
else:
host_name = '127.0.0.1'
self.hostname = host_name
container_volumes = {}
if self.volumes:
api_client = docker.APIClient(
@ -545,7 +553,6 @@ class Repo2Docker(Application):
self.output_image_spec,
publish_all_ports=self.all_ports,
ports=ports,
hostname=host_name,
detach=True,
command=run_cmd,
volumes=container_volumes,

Wyświetl plik

@ -6,35 +6,40 @@ import sys
import pytest
import requests
import time
import urllib.parse
from repo2docker.app import Repo2Docker
def test_env_yml(tmpdir):
def test_connect_url(tmpdir):
tmpdir.chdir()
#q = tmpdir.join("environment.yml")
#q.write("dependencies:\n"
# " - notebook==5.6.0rc1")
# " - notebook==5.6.0")
p = tmpdir.join("requirements.txt")
p.write("notebook==5.6.0rc1")
p.write("notebook==5.6.0")
app = Repo2Docker()
argv = [str(tmpdir), ]
app.initialize(argv)
app.debug = True
app.run = False
app.start() # This just build the image and does not run it.
container = app.start_container()
port = app.port
hostname = app.hostname
# wait a bit for the container to be ready
container_url = 'http://{}:{}/api'.format(hostname, port)
container_url = 'http://{}:{}/api'.format(app.hostname, app.port)
expected_url = 'http://{}:{}'.format(app.hostname, app.port)
# wait a bit for the container to be ready
# give the container a chance to start
time.sleep(1)
try:
# try a few times to connect
success = False
for i in range(1, 4):
container.reload()
assert container.status == 'running'
if expected_url not in container.logs().decode("utf8"):
time.sleep(i * 3)
continue
try:
info = requests.get(container_url).json()
except Exception as e: