docker/docker-py

[Question] How run docker-py inside docker container on Windows?

yogrr opened this issue · 3 comments

replica of q

but need actual info for windows and python code sample

im running into port problem when passing

DOCKER_HOST=host.docker.internal
2024-01-09 02:12:40 INFO:     Will watch for changes in these directories: ['/app']
2024-01-09 02:12:40 INFO:     Uvicorn running on http://0.0.0.0:8000 (Press CTRL+C to quit)
2024-01-09 02:12:40 INFO:     Started reloader process [1] using StatReload
2024-01-09 02:12:41 INFO:     Started server process [8]
2024-01-09 02:12:41 INFO:     Waiting for application startup.
2024-01-09 02:12:41 INFO:     Application startup complete.
2024-01-09 02:18:56 1
2024-01-09 02:18:56 INFO:     172.20.0.1:48308 - "POST /v1/handlers/20e41d15-1d51-4351-a623-3a54fd87dead/ HTTP/1.1" 400 Bad Request
2024-01-09 02:18:56 ERROR:    Exception in ASGI application
2024-01-09 02:18:56 Traceback (most recent call last):
2024-01-09 02:18:56   File "/usr/local/lib/python3.11/site-packages/uvicorn/protocols/http/h11_impl.py", line 408, in run_asgi
2024-01-09 02:18:56     result = await app(  # type: ignore[func-returns-value]
2024-01-09 02:18:56              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2024-01-09 02:18:56   File "/usr/local/lib/python3.11/site-packages/uvicorn/middleware/proxy_headers.py", line 84, in __call__
2024-01-09 02:18:56     return await self.app(scope, receive, send)
2024-01-09 02:18:56            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2024-01-09 02:18:56   File "/usr/local/lib/python3.11/site-packages/fastapi/applications.py", line 1054, in __call__
2024-01-09 02:18:56     await super().__call__(scope, receive, send)
2024-01-09 02:18:56   File "/usr/local/lib/python3.11/site-packages/starlette/applications.py", line 116, in __call__
2024-01-09 02:18:56     await self.middleware_stack(scope, receive, send)
2024-01-09 02:18:56   File "/usr/local/lib/python3.11/site-packages/starlette/middleware/errors.py", line 186, in __call__
2024-01-09 02:18:56     raise exc
2024-01-09 02:18:56   File "/usr/local/lib/python3.11/site-packages/starlette/middleware/errors.py", line 164, in __call__
2024-01-09 02:18:56     await self.app(scope, receive, _send)
2024-01-09 02:18:56   File "/usr/local/lib/python3.11/site-packages/starlette/middleware/cors.py", line 83, in __call__
2024-01-09 02:18:56     await self.app(scope, receive, send)
2024-01-09 02:18:56   File "/usr/local/lib/python3.11/site-packages/starlette/middleware/exceptions.py", line 62, in __call__
2024-01-09 02:18:56     await wrap_app_handling_exceptions(self.app, conn)(scope, receive, send)
2024-01-09 02:18:56   File "/usr/local/lib/python3.11/site-packages/starlette/_exception_handler.py", line 55, in wrapped_app
2024-01-09 02:18:56     raise exc
2024-01-09 02:18:56   File "/usr/local/lib/python3.11/site-packages/starlette/_exception_handler.py", line 44, in wrapped_app
2024-01-09 02:18:56     await app(scope, receive, sender)
2024-01-09 02:18:56   File "/usr/local/lib/python3.11/site-packages/starlette/routing.py", line 746, in __call__
2024-01-09 02:18:56     await route.handle(scope, receive, send)
2024-01-09 02:18:56   File "/usr/local/lib/python3.11/site-packages/starlette/routing.py", line 288, in handle
2024-01-09 02:18:56     await self.app(scope, receive, send)
2024-01-09 02:18:56   File "/usr/local/lib/python3.11/site-packages/starlette/routing.py", line 75, in app
2024-01-09 02:18:56     await wrap_app_handling_exceptions(app, request)(scope, receive, send)
2024-01-09 02:18:56   File "/usr/local/lib/python3.11/site-packages/starlette/_exception_handler.py", line 55, in wrapped_app
2024-01-09 02:18:56     raise exc
2024-01-09 02:18:56   File "/usr/local/lib/python3.11/site-packages/starlette/_exception_handler.py", line 44, in wrapped_app
2024-01-09 02:18:56     await app(scope, receive, sender)
2024-01-09 02:18:56   File "/usr/local/lib/python3.11/site-packages/starlette/routing.py", line 70, in app
2024-01-09 02:18:56     response = await func(request)
2024-01-09 02:18:56                ^^^^^^^^^^^^^^^^^^^
2024-01-09 02:18:56   File "/usr/local/lib/python3.11/site-packages/fastapi/routing.py", line 299, in app
2024-01-09 02:18:56     raise e
2024-01-09 02:18:56   File "/usr/local/lib/python3.11/site-packages/fastapi/routing.py", line 294, in app
2024-01-09 02:18:56     raw_response = await run_endpoint_function(
2024-01-09 02:18:56                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2024-01-09 02:18:56   File "/usr/local/lib/python3.11/site-packages/fastapi/routing.py", line 191, in run_endpoint_function
2024-01-09 02:18:56     return await dependant.call(**values)
2024-01-09 02:18:56            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2024-01-09 02:18:56   File "/app/src/api/v1/endpoints/handlers.py", line 206, in request_handler
2024-01-09 02:18:56     raise e
2024-01-09 02:18:56   File "/app/src/api/v1/endpoints/handlers.py", line 200, in request_handler
2024-01-09 02:18:56     containers: dict = await create_container(workspace)  # контейнеры
2024-01-09 02:18:56                        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2024-01-09 02:18:56   File "/app/src/api/v1/endpoints/handlers.py", line 133, in create_container
2024-01-09 02:18:56     container = tasks.run_container(
2024-01-09 02:18:56                 ^^^^^^^^^^^^^^^^^^^^
2024-01-09 02:18:56   File "/app/src/tasks_queue/tasks.py", line 21, in run_container
2024-01-09 02:18:56     client = from_env(environment={"DOCKER_HOST": "host.docker.internal"})
2024-01-09 02:18:56              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2024-01-09 02:18:56   File "/usr/local/lib/python3.11/site-packages/docker/client.py", line 94, in from_env
2024-01-09 02:18:56     return cls(
2024-01-09 02:18:56            ^^^^
2024-01-09 02:18:56   File "/usr/local/lib/python3.11/site-packages/docker/client.py", line 45, in __init__
2024-01-09 02:18:56     self.api = APIClient(*args, **kwargs)
2024-01-09 02:18:56                ^^^^^^^^^^^^^^^^^^^^^^^^^^
2024-01-09 02:18:56   File "/usr/local/lib/python3.11/site-packages/docker/api/client.py", line 136, in __init__
2024-01-09 02:18:56     base_url = utils.parse_host(
2024-01-09 02:18:56                ^^^^^^^^^^^^^^^^^
2024-01-09 02:18:56   File "/usr/local/lib/python3.11/site-packages/docker/utils/utils.py", line 287, in parse_host
2024-01-09 02:18:56     raise errors.DockerException(
2024-01-09 02:18:56 docker.errors.DockerException: Invalid bind address format: port is required: host.docker.internal

adding

extra_hosts:
    - host.docker.internal:host-gateway

to docker compose

didn't the trick, as host-gateway isn't a valid number port

it seems that we need access to Docker Engine's REST API, but how I can enable it?

I can't reach any at http://localhost:2375 or http://localhost:2376

Im trying to add something like this to dockerd startup config:

  "hosts": [
    "tcp://0.0.0.0:4243"
  ]

but http://localhost:4243 is still inaccessable

solved with

DOCKER_HOST=unix://var/run/docker.sock