docker-py icon indicating copy to clipboard operation
docker-py copied to clipboard

create client from current docker context

Open vlcinsky opened this issue 5 years ago • 2 comments

As we are using swarm on remote servers, using docker CLI we take advantage of docker context.

My expectation was, that the docker library will behave similarly to CLI thus created client would respect currently configured docker context. This does not seem to be the case.

Anyway, it would be great to have some simple method, which would create client based on current docker context, e.g.:

import docker

client = docker.from_context()

vlcinsky avatar Jul 08 '20 21:07 vlcinsky

I've just been looking to do this - and I can't find that Contexts are usable in any way, other than to inspect/create/delete them...

schinckel avatar May 18 '22 07:05 schinckel

client = docker.from_context()

It world be better to two related changes:

client = docker.ContextAPI().get_context('context-name').get_client()

And docker.from_env() using docker context use (i.e. currentContext in ~/.docker/config.json) or $DOCKER_CONTEXT.

pyhedgehog avatar May 26 '22 23:05 pyhedgehog

Here's a workaround if you use colima:

current_ctx = docker.context.Context.load_context(docker.context.api.get_current_context_name())
url = current_ctx.endpoints["docker"]["Host"]
client = docker.DockerClient(base_url=url)

boxmein avatar Apr 14 '23 09:04 boxmein