Repository: docker/docker-py

Issue:
Create container bind volume with Unicode folder name
If bind volume folder name is Unicode, for example Japanese, It will raise exception:
Host volume and container volume both should handle Unicode.

```
  File "/home/vagrant/.local/share/virtualenvs/qnap/local/lib/python2.7/site-packages/docker/utils/utils.py", line 461, in create_host_config
    host_config['Binds'] = convert_volume_binds(binds)
  File "/home/vagrant/.local/share/virtualenvs/qnap/local/lib/python2.7/site-packages/docker/utils/utils.py", line 208, in convert_volume_binds
    k, v['bind'], mode
```



Discussion:
I suspect this is an issue in python2 (and not python3). Can you confirm?

Yes, only in python 2.

Don't know how to make CI works for both python 2 and python 3. Need someone help. https://github.com/docker/docker-py/pull/774

I'll take a look.

I tried to reproduce this issue but was unable to (tested with python 2.7 and 3.4). Here is the code I'm running.

```
# -*- coding: utf-8 -*-

from docker.utils import convert_volume_binds

data = {
    '/mnt/지연': {
        'bind': '/unicode/박',
        'mode': 'rw'
    }
}

print(convert_volume_binds(data) == ['/mnt/지연:/unicode/박:rw'])
```

@ColinHuang , can you provide a short example of code that fails. Also, the stacktrace you shared seems to be truncated, can you provide a full stacktrace so we can look deeper into that issue?

Thanks!

``` python
import docker

dc = docker.Client()

image = 'busybox'
host_config = dict()
create_args = dict()
host_config['binds'] = {
    u'/home/test/\u6e2c\u8a66\u6211\u662f\u4e2d\u6587': {
        u'bind': u'/a',
        u'ro': False
    }
}

create_args['host_config'] = docker.utils.create_host_config(
    binds=host_config.get('binds', None))

result = dc.create_container(image, **create_args)
```

Exception:

``` text
Traceback (most recent call last):
  File "d.py", line 36, in <module>
    binds=host_config.get('binds', None))
  File "/home/vagrant/.local/share/virtualenvs/0923/local/lib/python2.7/site-packages/docker/utils/utils.py", line 461, in create_host_config
    host_config['Binds'] = convert_volume_binds(binds)
  File "/home/vagrant/.local/share/virtualenvs/0923/local/lib/python2.7/site-packages/docker/utils/utils.py", line 208, in convert_volume_binds
    k, v['bind'], mode
UnicodeEncodeError: 'ascii' codec can't encode characters in position 11-16: ordinal not in range(128)

```


Resolving patch:
diff --git a/docker/utils/utils.py b/docker/utils/utils.py
--- a/docker/utils/utils.py
+++ b/docker/utils/utils.py
@@ -242,6 +242,9 @@ def convert_volume_binds(binds):
 
     result = []
     for k, v in binds.items():
+        if isinstance(k, six.binary_type):
+            k = k.decode('utf-8')
+
         if isinstance(v, dict):
             if 'ro' in v and 'mode' in v:
                 raise ValueError(
@@ -249,6 +252,10 @@ def convert_volume_binds(binds):
                     .format(repr(v))
                 )
 
+            bind = v['bind']
+            if isinstance(bind, six.binary_type):
+                bind = bind.decode('utf-8')
+
             if 'ro' in v:
                 mode = 'ro' if v['ro'] else 'rw'
             elif 'mode' in v:
@@ -256,11 +263,15 @@ def convert_volume_binds(binds):
             else:
                 mode = 'rw'
 
-            result.append('{0}:{1}:{2}'.format(
-                k, v['bind'], mode
-            ))
+            result.append(
+                six.text_type('{0}:{1}:{2}').format(k, bind, mode)
+            )
         else:
-            result.append('{0}:{1}:rw'.format(k, v))
+            if isinstance(v, six.binary_type):
+                v = v.decode('utf-8')
+            result.append(
+                six.text_type('{0}:{1}:rw').format(k, v)
+            )
     return result
 
 


Test patch:
diff --git a/tests/utils_test.py b/tests/utils_test.py
--- a/tests/utils_test.py
+++ b/tests/utils_test.py
@@ -1,15 +1,20 @@
+# -*- coding: utf-8 -*-
+
 import os
 import os.path
 import shutil
 import tempfile
 
+import pytest
+import six
+
 from docker.client import Client
 from docker.constants import DEFAULT_DOCKER_API_VERSION
 from docker.errors import DockerException
 from docker.utils import (
     parse_repository_tag, parse_host, convert_filters, kwargs_from_env,
     create_host_config, Ulimit, LogConfig, parse_bytes, parse_env_file,
-    exclude_paths,
+    exclude_paths, convert_volume_binds,
 )
 from docker.utils.ports import build_port_bindings, split_port
 from docker.auth import resolve_repository_name, resolve_authconfig
@@ -17,7 +22,6 @@
 from . import base
 from .helpers import make_tree
 
-import pytest
 
 TEST_CERT_DIR = os.path.join(
     os.path.dirname(__file__),
@@ -192,6 +196,89 @@ def generate_tempfile(self, file_content=None):
         local_tempfile.close()
         return local_tempfile.name
 
+    def test_convert_volume_binds_empty(self):
+        self.assertEqual(convert_volume_binds({}), [])
+        self.assertEqual(convert_volume_binds([]), [])
+
+    def test_convert_volume_binds_list(self):
+        data = ['/a:/a:ro', '/b:/c:z']
+        self.assertEqual(convert_volume_binds(data), data)
+
+    def test_convert_volume_binds_complete(self):
+        data = {
+            '/mnt/vol1': {
+                'bind': '/data',
+                'mode': 'ro'
+            }
+        }
+        self.assertEqual(convert_volume_binds(data), ['/mnt/vol1:/data:ro'])
+
+    def test_convert_volume_binds_compact(self):
+        data = {
+            '/mnt/vol1': '/data'
+        }
+        self.assertEqual(convert_volume_binds(data), ['/mnt/vol1:/data:rw'])
+
+    def test_convert_volume_binds_no_mode(self):
+        data = {
+            '/mnt/vol1': {
+                'bind': '/data'
+            }
+        }
+        self.assertEqual(convert_volume_binds(data), ['/mnt/vol1:/data:rw'])
+
+    def test_convert_volume_binds_unicode_bytes_input(self):
+        if six.PY2:
+            expected = [unicode('/mnt/지연:/unicode/박:rw', 'utf-8')]
+
+            data = {
+                '/mnt/지연': {
+                    'bind': '/unicode/박',
+                    'mode': 'rw'
+                }
+            }
+            self.assertEqual(
+                convert_volume_binds(data), expected
+            )
+        else:
+            expected = ['/mnt/지연:/unicode/박:rw']
+
+            data = {
+                bytes('/mnt/지연', 'utf-8'): {
+                    'bind': bytes('/unicode/박', 'utf-8'),
+                    'mode': 'rw'
+                }
+            }
+            self.assertEqual(
+                convert_volume_binds(data), expected
+            )
+
+    def test_convert_volume_binds_unicode_unicode_input(self):
+        if six.PY2:
+            expected = [unicode('/mnt/지연:/unicode/박:rw', 'utf-8')]
+
+            data = {
+                unicode('/mnt/지연', 'utf-8'): {
+                    'bind': unicode('/unicode/박', 'utf-8'),
+                    'mode': 'rw'
+                }
+            }
+            self.assertEqual(
+                convert_volume_binds(data), expected
+            )
+        else:
+            expected = ['/mnt/지연:/unicode/박:rw']
+
+            data = {
+                '/mnt/지연': {
+                    'bind': '/unicode/박',
+                    'mode': 'rw'
+                }
+            }
+            self.assertEqual(
+                convert_volume_binds(data), expected
+            )
+
     def test_parse_repository_tag(self):
         self.assertEqual(parse_repository_tag("root"),
                          ("root", None))