From feaf46e5fc0e051a6ef6ef88af10e393f4bf16ac Mon Sep 17 00:00:00 2001 From: Min RK Date: Thu, 25 May 2017 15:09:26 -0700 Subject: [PATCH 1/4] respect docker env in docker buildpack --- repo2docker/detectors.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/repo2docker/detectors.py b/repo2docker/detectors.py index 3d539653..d7476460 100644 --- a/repo2docker/detectors.py +++ b/repo2docker/detectors.py @@ -3,6 +3,7 @@ import sys import subprocess import docker +from docker.utils import kwargs_from_env from traitlets import Unicode, Dict, Bool from traitlets.config import LoggingConfigurable @@ -37,7 +38,7 @@ class DockerBuildPack(BuildPack): return os.path.exists(os.path.join(workdir, 'Dockerfile')) def build(self, workdir, ref, output_image_spec): - client = docker.APIClient(base_url='unix://var/run/docker.sock', version='auto') + client = docker.APIClient(version='auto', **kwargs_from_env()) for progress in client.build( path=workdir, tag=output_image_spec, From db66f1dc06e03292e317aa1e3c8a2fef73ce54b4 Mon Sep 17 00:00:00 2001 From: Min RK Date: Thu, 25 May 2017 15:10:08 -0700 Subject: [PATCH 2/4] only log build output if capture=true use stdout.write otherwise --- repo2docker/detectors.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/repo2docker/detectors.py b/repo2docker/detectors.py index d7476460..75e94acc 100644 --- a/repo2docker/detectors.py +++ b/repo2docker/detectors.py @@ -45,7 +45,12 @@ class DockerBuildPack(BuildPack): decode=True ): if 'stream' in progress: - self.log.info(progress['stream'], extra=dict(phase='building')) + if self.capture: + self.log.info(progress['stream'], extra=dict(phase='building')) + else: + sys.stdout.write(progress['stream']) + +_legacybinder_docker_appendix = """ class S2IBuildPack(BuildPack): From 766eee29e65485894f233720a7a0f02965800f69 Mon Sep 17 00:00:00 2001 From: Min RK Date: Thu, 25 May 2017 15:15:00 -0700 Subject: [PATCH 3/4] add legacy binder dockerfile buildpack - stages files, which previous binder did automatically and repo2docker does not - installs required versions of ipykernel, jupyterhub, notebook - registers kernelspecs explicitly - launches notebook server with python3 env - removes nb_conda_kernels, which was causing trouble - sets JUPYTER_PATH so kernelspecs and extensions that were being installed into the python 2 env will still be picked up --- repo2docker/app.py | 7 +++++-- repo2docker/detectors.py | 39 ++++++++++++++++++++++++++++++++++++++- 2 files changed, 43 insertions(+), 3 deletions(-) diff --git a/repo2docker/app.py b/repo2docker/app.py index 1ba3e621..9a1acb0e 100644 --- a/repo2docker/app.py +++ b/repo2docker/app.py @@ -24,7 +24,10 @@ from docker.utils import kwargs_from_env import subprocess -from .detectors import BuildPack, PythonBuildPack, DockerBuildPack, CondaBuildPack +from .detectors import ( + BuildPack, PythonBuildPack, DockerBuildPack, LegacyBinderDockerBuildPack, + CondaBuildPack, +) from .utils import execute_cmd from . import __version__ @@ -90,7 +93,7 @@ class Repo2Docker(Application): buildpacks = List( Type(BuildPack), - [DockerBuildPack, CondaBuildPack, PythonBuildPack], + [LegacyBinderDockerBuildPack, DockerBuildPack, CondaBuildPack, PythonBuildPack], config=True, help=""" Ordered list of BuildPacks to try to use to build a git repository. diff --git a/repo2docker/detectors.py b/repo2docker/detectors.py index 75e94acc..0e215a63 100644 --- a/repo2docker/detectors.py +++ b/repo2docker/detectors.py @@ -50,7 +50,44 @@ class DockerBuildPack(BuildPack): else: sys.stdout.write(progress['stream']) -_legacybinder_docker_appendix = """ +_legacy_binder_docker_appendix = """ +USER root +COPY . /home/main/notebooks +RUN chown -R main:main /home/main/notebooks +USER main +WORKDIR /home/main/notebooks +ENV PATH /home/main/anaconda2/envs/python3/bin:$PATH +RUN conda install -n python3 notebook==5.0.0 ipykernel==4.6.0 && \ + pip install jupyterhub==0.7.2 && \ + conda remove -n python3 nb_conda_kernels && \ + conda install -n root ipykernel==4.6.0 && \ + /home/main/anaconda2/envs/python3/bin/ipython kernel install --sys-prefix && \ + /home/main/anaconda2/bin/ipython kernel install --prefix=/home/main/anaconda2/envs/python3 +ENV JUPYTER_PATH /home/main/anaconda2/share/jupyter:$JUPYTER_PATH +CMD jupyter notebook --ip 0.0.0.0 +""" + +class LegacyBinderDockerBuildPack(DockerBuildPack): + + name = Unicode('Legacy Binder Dockerfile') + def detect(self, workdir): + dockerfile = os.path.join(workdir, 'Dockerfile') + if not os.path.exists(dockerfile): + return False + with open(dockerfile, 'r') as f: + for line in f: + if line.startswith('FROM'): + if 'andrewosh/binder-base' in line.split('#')[0].lower(): + self.amend_dockerfile(dockerfile) + return True + else: + return False + # No FROM?! + return False + + def amend_dockerfile(self, dockerfile): + with open(dockerfile, 'a') as f: + f.write(_legacy_binder_docker_appendix) class S2IBuildPack(BuildPack): From 82ccb38e3a94e60fb83a197582ede1438dea37d7 Mon Sep 17 00:00:00 2001 From: Min RK Date: Thu, 25 May 2017 15:37:33 -0700 Subject: [PATCH 4/4] move dockerfile appendix into LegacyBinderDockerBuildPack.dockerfile_appendix and make it overrideable --- repo2docker/detectors.py | 37 ++++++++++++++++++++----------------- 1 file changed, 20 insertions(+), 17 deletions(-) diff --git a/repo2docker/detectors.py b/repo2docker/detectors.py index 0e215a63..e38d7191 100644 --- a/repo2docker/detectors.py +++ b/repo2docker/detectors.py @@ -1,6 +1,7 @@ import os import sys import subprocess +from textwrap import dedent import docker from docker.utils import kwargs_from_env @@ -50,26 +51,27 @@ class DockerBuildPack(BuildPack): else: sys.stdout.write(progress['stream']) -_legacy_binder_docker_appendix = """ -USER root -COPY . /home/main/notebooks -RUN chown -R main:main /home/main/notebooks -USER main -WORKDIR /home/main/notebooks -ENV PATH /home/main/anaconda2/envs/python3/bin:$PATH -RUN conda install -n python3 notebook==5.0.0 ipykernel==4.6.0 && \ - pip install jupyterhub==0.7.2 && \ - conda remove -n python3 nb_conda_kernels && \ - conda install -n root ipykernel==4.6.0 && \ - /home/main/anaconda2/envs/python3/bin/ipython kernel install --sys-prefix && \ - /home/main/anaconda2/bin/ipython kernel install --prefix=/home/main/anaconda2/envs/python3 -ENV JUPYTER_PATH /home/main/anaconda2/share/jupyter:$JUPYTER_PATH -CMD jupyter notebook --ip 0.0.0.0 -""" class LegacyBinderDockerBuildPack(DockerBuildPack): name = Unicode('Legacy Binder Dockerfile') + dockerfile_appendix = Unicode(dedent(r""" + USER root + COPY . /home/main/notebooks + RUN chown -R main:main /home/main/notebooks + USER main + WORKDIR /home/main/notebooks + ENV PATH /home/main/anaconda2/envs/python3/bin:$PATH + RUN conda install -n python3 notebook==5.0.0 ipykernel==4.6.0 && \ + pip install jupyterhub==0.7.2 && \ + conda remove -n python3 nb_conda_kernels && \ + conda install -n root ipykernel==4.6.0 && \ + /home/main/anaconda2/envs/python3/bin/ipython kernel install --sys-prefix && \ + /home/main/anaconda2/bin/ipython kernel install --prefix=/home/main/anaconda2/envs/python3 + ENV JUPYTER_PATH /home/main/anaconda2/share/jupyter:$JUPYTER_PATH + CMD jupyter notebook --ip 0.0.0.0 + """), config=True) + def detect(self, workdir): dockerfile = os.path.join(workdir, 'Dockerfile') if not os.path.exists(dockerfile): @@ -86,8 +88,9 @@ class LegacyBinderDockerBuildPack(DockerBuildPack): return False def amend_dockerfile(self, dockerfile): + print(self.dockerfile_appendix) with open(dockerfile, 'a') as f: - f.write(_legacy_binder_docker_appendix) + f.write(self.dockerfile_appendix) class S2IBuildPack(BuildPack):