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

create client from current docker context #2616

Open
vlcinsky opened this issue Jul 8, 2020 · 3 comments
Open

create client from current docker context #2616

vlcinsky opened this issue Jul 8, 2020 · 3 comments

Comments

@vlcinsky
Copy link

vlcinsky commented Jul 8, 2020

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()
@schinckel
Copy link

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...

@pyhedgehog
Copy link

pyhedgehog commented May 26, 2022

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.

@boxmein
Copy link

boxmein commented Apr 14, 2023

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)

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