Repository: scrapinghub/shub

Issue:
shub image commands are feeding wrong DOCKER_VERSION env var to docker client
I tried to run `shub image build` command on Circle CI by following their docker building instructions described here

https://circleci.com/docs/2.0/building-docker-images/

So basically it's docker image `docker:17.05.0-ce-git`, and you can use the docker inside the container. However, the problem is, `DOCKER_VERSION` in the container is set to `17.05.0-ce`, which is not supported by docker python client. After looking into details, it seems the `version` argument for docker python client is actually the API version. And the `DOCKER_VERSION` is the version of docker client.

https://docs.docker.com/engine/reference/commandline/cli/

> DOCKER_API_VERSION The API version to use (e.g. 1.19)

With that, if you do

```bash
apk add --no-cache py-pip=9.0.0-r1
pip install shub
```

Login your shub, setup your local shub files, and run

```bash
shub image build -v
```

then you will see error like this

```
Error: 
Detected error connecting to Docker daemon's host.

Please ensure that you have Docker installed, configured and running locally,
that's essential for running shub image command. To check that run command

    docker version

and check its output: it should contain Docker client and server versions and
should not contain any errors.

You can learn about Docker at https://www.docker.com/.
```

And if you try to upgrade docker to 2.0

```bash
pip install 'docker>2.0,<3.0'
```

Then it will be another error for parsing the docker api version

```
shub image build -v
Traceback (most recent call last):
  File "/usr/bin/shub", line 11, in <module>
    sys.exit(cli())
  File "/usr/lib/python2.7/site-packages/click/core.py", line 722, in __call__
    return self.main(*args, **kwargs)
  File "/usr/lib/python2.7/site-packages/click/core.py", line 697, in main
    rv = self.invoke(ctx)
  File "/usr/lib/python2.7/site-packages/click/core.py", line 1066, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
  File "/usr/lib/python2.7/site-packages/click/core.py", line 1066, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
  File "/usr/lib/python2.7/site-packages/click/core.py", line 895, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "/usr/lib/python2.7/site-packages/click/core.py", line 535, in invoke
    return callback(*args, **kwargs)
  File "/usr/lib/python2.7/site-packages/shub/image/build.py", line 45, in cli
    build_cmd(target, version, skip_tests, filename=filename)
  File "/usr/lib/python2.7/site-packages/shub/image/build.py", line 51, in build_cmd
    client = utils.get_docker_client()
  File "/usr/lib/python2.7/site-packages/shub/image/utils.py", line 102, in get_docker_client
    tls=tls_config)
  File "/usr/lib/python2.7/site-packages/docker/api/client.py", line 158, in __init__
    if utils.version_lt(self._version, MINIMUM_DOCKER_API_VERSION):
  File "/usr/lib/python2.7/site-packages/docker/utils/utils.py", line 153, in version_lt
    return compare_version(v1, v2) > 0
  File "/usr/lib/python2.7/site-packages/docker/utils/utils.py", line 142, in compare_version
    s1 = StrictVersion(v1)
  File "/usr/lib/python2.7/distutils/version.py", line 40, in __init__
    self.parse(vstring)
  File "/usr/lib/python2.7/distutils/version.py", line 107, in parse
    raise ValueError, "invalid version number '%s'" % vstring
ValueError: invalid version number '17.05.0-ce'
```

In the end, it seems like we are passing wrong docker client version here to the api version. I will create a PR addressing this issue shortly by using `DOCKER_API_VERSION` instead of `DOCKER_VERSION`

Discussion:

Patch:
diff --git a/shub/image/utils.py b/shub/image/utils.py
index 33003fc..987491e 100644
--- a/shub/image/utils.py
+++ b/shub/image/utils.py
@@ -15,7 +15,7 @@ from shub.exceptions import (
 )
 
 
-DEFAULT_DOCKER_VERSION = '1.17'
+DEFAULT_DOCKER_API_VERSION = '1.17'
 STATUS_FILE_LOCATION = '.releases'
 _VALIDSPIDERNAME = re.compile('^[a-z0-9][-._a-z0-9]+$', re.I)
 
@@ -96,7 +96,7 @@ def get_docker_client(validate=True):
             verify=apply_path_fun('ca.pem'),
             assert_hostname=False)
         docker_host = docker_host.replace('tcp://', 'https://')
-    version = os.environ.get('DOCKER_VERSION', DEFAULT_DOCKER_VERSION)
+    version = os.environ.get('DOCKER_API_VERSION', DEFAULT_DOCKER_API_VERSION)
     client = docker_client_cls(base_url=docker_host,
                                version=version,
                                tls=tls_config)


Test patch:
diff --git a/tests/image/test_utils.py b/tests/image/test_utils.py
index 0012549..1d77ef5 100644
--- a/tests/image/test_utils.py
+++ b/tests/image/test_utils.py
@@ -47,7 +47,7 @@ class ReleaseUtilsTest(TestCase):
             base_url=None, tls=None, version='1.17')
         # set basic test environment
         os.environ['DOCKER_HOST'] = 'http://127.0.0.1'
-        os.environ['DOCKER_VERSION'] = '1.18'
+        os.environ['DOCKER_API_VERSION'] = '1.18'
         assert get_docker_client()
         client_mock.assert_called_with(
             base_url='http://127.0.0.1', tls=None, version='1.18')