Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't pass host device or volume to container if host path contains colon #2041

Closed
kavefish opened this issue May 26, 2018 · 6 comments
Closed

Comments

@kavefish
Copy link

Steps to reproduce

  1. Create a host path containing a colon
sudo mkdir /test:dir
  1. Use "recommended dictionary notation" to pass volume to container
import docker
client = docker.from_env()
vols={'/test:dir': {'bind': '/test'}}
client.containers.create("ubuntu:latest", volumes=vols)

Error

APIError: 500 Server Error: Internal Server Error ("invalid volume specification: '/test:dir:/testdir:rw'")

Stack trace

APIError                                  Traceback (most recent call last)                                                                                       <ipython-input-11-9017b6f19e04> in <module>()                                                                                                                            
----> 1 client.containers.create("ubuntu:latest", volumes=vols)                                                                                                          
                                                                                                                                                                         
/home/kavefish/Documents/devel/docker-py-bug/docker/models/containers.pyc in create(self, image, command, **kwargs)                                                      
    822         kwargs['version'] = self.client.api._version                                                                                                             
    823         create_kwargs = _create_container_args(kwargs)                                                                                                           
--> 824         resp = self.client.api.create_container(**create_kwargs)
    825         return self.get(resp['Id'])
    826

/home/kavefish/Documents/devel/docker-py-bug/docker/api/container.pyc in create_container(self, image, command, hostname, user, detach, stdin_open, tty, ports, environm$
nt, volumes, network_disabled, name, entrypoint, working_dir, domainname, host_config, mac_address, labels, stop_signal, networking_config, healthcheck, stop_timeout, r$
ntime)
    408             stop_timeout, runtime
    409         )
--> 410         return self.create_container_from_config(config, name)
    411
    412     def create_container_config(self, *args, **kwargs):

/home/kavefish/Documents/devel/docker-py-bug/docker/api/container.pyc in create_container_from_config(self, config, name)
    419         }
    420         res = self._post_json(u, data=config, params=params)
--> 421         return self._result(res, True)
    422
    423     def create_host_config(self, *args, **kwargs):

/home/kavefish/Documents/devel/docker-py-bug/docker/api/client.pyc in _result(self, response, json, binary)
    229     def _result(self, response, json=False, binary=False):
    230         assert not (json and binary)
--> 231         self._raise_for_status(response)
    232
    233         if json:

/home/kavefish/Documents/devel/docker-py-bug/docker/api/client.pyc in _raise_for_status(self, response)
    225             response.raise_for_status()
    226         except requests.exceptions.HTTPError as e:
--> 227             raise create_api_error_from_http_exception(e)
    228
    229     def _result(self, response, json=False, binary=False):

/home/kavefish/Documents/devel/docker-py-bug/docker/errors.pyc in create_api_error_from_http_exception(e)
     29         else:
     30             cls = NotFound
---> 31     raise cls(e, response=response, explanation=explanation)

APIError: 500 Server Error: Internal Server Error ("invalid volume specification: '/test:dir:/testdir:rw'")

Environment

docker-py: 3.3.0
Python: 2.7.15rc1
engine: 17.12.1-ce
Ubuntu 18.04

@kavefish
Copy link
Author

The same error occurs if the colon appears in the bind mount path for the container, e.g.

import docker
client = docker.from_env()
vols={'/somedir': {'bind': '/test:path'}}
client.containers.create("ubuntu:latest", volumes=vols)

@thaJeztah
Copy link
Member

The volumes format is known to have some limitations, because it uses a colon as separator between options; does it work if you useMount option instead? https://docker-py.readthedocs.io/en/stable/api.html#docker.types.Mount

Looks like you want to do a bind-mount, in which case you have to set type to "bind"

@kavefish
Copy link
Author

@thaJeztah, thanks very much for the suggestion.

Yes, I do want a bind mount. I thought I was specifying a bind mount by using the dictionary notation which specifies "bind" in the dictionary.

Is the recommendation that the low-level API is required to accomplish a bind mount with a colon in the path?

@thaJeztah
Copy link
Member

I'll have to defer that one to @shin-, I'm not very familiar with the docker-py sdk (or python, for that matter) 😊

@shin-
Copy link
Contributor

shin- commented May 31, 2018

Sorry for the confusion on the earlier thread.

  1. mounts and binds are 2 different things, as explained here (in the context of the CLI). mounts is more modern and a better choice 99% of the time.
  2. Because of the way the binds API was designed, {'/test:dir': {'bind': '/test'}} has to be transformed into ['test:dir:/test'], which the daemon fails to parse (as it expects the colon to be a path separator). This is not a SDK issue. (See here for the Docker daemon API)
  3. As @thaJeztah pointed out, the correct approach to this problem is to use mounts instead
import docker
c = docker.from_env()
mnt = docker.types.Mount(type='bind', source='/tmp/colon:colon/', target='/colon')
ctnr = c.containers.create('busybox', mounts=[mnt]) # <Container: 38869806cb>

HTH

@ijc
Copy link
Contributor

ijc commented Apr 5, 2019

Closing since this seems to have been answered.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants