docker-py
docker-py copied to clipboard
create client from current docker context
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()
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...
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.
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)