Added checks to gracefully handle docker connect errors
Print actual error message on Docker client init errors
MODIFY
repo2docker/app.py
repo2docker/app.py
@@ -23,6 +23,7 @@ from traitlets.config import Application
from traitlets import Unicode, List, default, Tuple, Dict, Int
import docker
from docker.utils import kwargs_from_env
+from docker.errors import DockerException
import subprocess
@@ -524,6 +525,19 @@ class Repo2Docker(Application):
def start(self):
+ # Check if r2d can connect to docker daemon
+ if self.build:
+ try:
+ client = docker.APIClient(version='auto',
+ **kwargs_from_env())
+ del client
+ except DockerException as e:
+ print("Docker client initialization error. Check if docker is running on the host.")
+ print(e)
+ if self.log_level == logging.DEBUG:
+ raise e
+ sys.exit(1)
+
if self.repo_type == 'local':
checkout_path = self.repo
else:

MODIFY
tests/argumentvalidation.py
tests/argumentvalidation.py
@@ -6,13 +6,16 @@ import os
import subprocess
-def validate_arguments(builddir, args_list, expected):
+def validate_arguments(builddir, args_list, expected, disable_dockerd=False):
try:
cmd = ['repo2docker']
for k in args_list:
cmd.append(k)
cmd.append(builddir)
- subprocess.check_output(cmd, stderr=subprocess.STDOUT)
+ env = os.environ.copy()
+ if disable_dockerd:
+ env['DOCKER_HOST'] = "INCORRECT"
+ subprocess.check_output(cmd, env=env, stderr=subprocess.STDOUT)
return True
except subprocess.CalledProcessError as e:
output = e.output.decode()
@@ -187,3 +190,34 @@ def test_invalid_container_port_protocol_mapping_fail():
args_list = ['-p', '80:8000/upd', builddir, 'ls']
assert not validate_arguments(builddir, args_list, 'Invalid port mapping')
+
+
+def test_docker_handle_fail():
+ """
+ Test to check if r2d fails with minimal error message on not being able to connect to docker daemon
+ """
+ args_list = []
+ builddir = os.path.dirname(__file__) + '/../'
+
+ assert not validate_arguments(builddir, args_list, "Docker client initialization error. Check if docker is running on the host.", True)
+
+
+def test_docker_handle_debug_fail():
+ """
+ Test to check if r2d fails with stack trace on not being able to connect to docker daemon and debug enabled
+ """
+ args_list = ['--debug']
+ builddir = os.path.dirname(__file__) + '/../'
+
+ assert not validate_arguments(builddir, args_list, "docker.errors.DockerException", True)
+
+
+def test_docker_no_build_success():
+ """
+ Test to check if r2d succeeds with --no-build argument with not being able to connect to docker daemon
+ """
+ args_list = ['--no-build', '--no-run']
+ builddir = os.path.dirname(__file__) + '/../'
+
+ assert validate_arguments(builddir, args_list, "", True)
+

Added checks to gracefully handle docker connect errors
Print actual error message on Docker client init errors