docker / docker/docker-py

Mount with host path doesn't work

Open
#2,663 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
7.2k
Forks
1.7k
Avg merge
13d 8h
Merged PRs (30d)
2

Description

Hi,
I am trying to use mounts in client.containers.run:

import docker
from docker.types import Mount
import os

client = docker.from_env()

print(docker.__version__)

container=client.containers.run(
    'ubuntu',
    device_requests=[
        docker.types.DeviceRequest(count=1, capabilities=[['gpu']])
    ],
    detach=True,
    stdin_open=True,
    tty = True,
    ports={'8000/tcp': 8000, '8001/tcp': 8001, '8002/tcp': 8002},
    mounts=[Mount(target='/abcde',
                  source='/home/e6dn/dev/path/to/dir/')]
)

print(container.exec_run('/bin/sh -c "ls"', workdir='/abcde'))
container.kill()

I got following error:

4.3.1
Traceback (most recent call last):
  File "/home/e6dn/.local/lib/python3.8/site-packages/docker/api/client.py", line 259, in _raise_for_status
    response.raise_for_status()
  File "/usr/lib/python3/dist-packages/requests/models.py", line 940, in raise_for_status
    raise HTTPError(http_error_msg, response=self)
requests.exceptions.HTTPError: 400 Client Error: Bad Request for url: http+docker://localhost/v1.40/containers/create

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "runner/test.py", line 10, in <module>
    container=client.containers.run(
  File "/home/e6dn/.local/lib/python3.8/site-packages/docker/models/containers.py", line 809, in run
    container = self.create(image=image, command=command,
  File "/home/e6dn/.local/lib/python3.8/site-packages/docker/models/containers.py", line 868, in create
    resp = self.client.api.create_container(**create_kwargs)
  File "/home/e6dn/.local/lib/python3.8/site-packages/docker/api/container.py", line 430, in create_container
    return self.create_container_from_config(config, name)
  File "/home/e6dn/.local/lib/python3.8/site-packages/docker/api/container.py", line 441, in create_container_from_config
    return self._result(res, True)
  File "/home/e6dn/.local/lib/python3.8/site-packages/docker/api/client.py", line 265, in _result
    self._raise_for_status(response)
  File "/home/e6dn/.local/lib/python3.8/site-packages/docker/api/client.py", line 261, in _raise_for_status
    raise create_api_error_from_http_exception(e)
  File "/home/e6dn/.local/lib/python3.8/site-packages/docker/errors.py", line 31, in create_api_error_from_http_exception
    raise cls(e, response=response, explanation=explanation)
docker.errors.APIError: 400 Client Error: Bad Request ("create /home/e6dn/dev/path/to/dir: "/home/e6dn/dev/path/to/dir" includes invalid characters for a local volume name, only "[a-zA-Z0-9][a-zA-Z0-9_.-]" are allowed. If you intended to pass a host directory, use absolute path")

but /home/e6dn/dev/path/to/dir is absolute path.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the example through client.containers.run, then inspect docker/models/containers.py and docker/api/container.py where container creation is delegated. Check how the Mount object is translated for create_container, and add or update coverage if an existing test location is found; done means an absolute host path creates the requested mount without the 400 error.

Written by the indexing model from the issue text.

Assessment

Tech stack
docker, python
Domain
api
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.