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

Docker CLI contexts not used or supported #3146

Open
brettdh opened this issue Jun 22, 2023 · 6 comments
Open

Docker CLI contexts not used or supported #3146

brettdh opened this issue Jun 22, 2023 · 6 comments

Comments

@brettdh
Copy link

brettdh commented Jun 22, 2023

Testing with Docker Desktop on a mac, I have this docker context state:

➜ docker context list
NAME                TYPE                DESCRIPTION                               DOCKER ENDPOINT                                       KUBERNETES ENDPOINT   ORCHESTRATOR
colima              moby                colima                                    unix:///Users/brhiggins/.colima/default/docker.sock
default             moby                Current DOCKER_HOST based configuration   unix:///var/run/docker.sock
desktop-linux *     moby                                                          unix:///Users/brhiggins/.docker/run/docker.sock

The desktop-linux context is active, and /var/run/docker.sock does not exist. docker version correctly uses the active context and returns the server version info. However, docker.from_env() throws an error:

../../../.pyenv/versions/3.11.4/envs/myproj/lib/python3.11/site-packages/docker/client.py:96: in from_env
    return cls(
../../../.pyenv/versions/3.11.4/envs/myproj/lib/python3.11/site-packages/docker/client.py:45: in __init__
    self.api = APIClient(*args, **kwargs)
../../../.pyenv/versions/3.11.4/envs/myproj/lib/python3.11/site-packages/docker/api/client.py:197: in __init__
    self._version = self._retrieve_server_version()
../../../.pyenv/versions/3.11.4/envs/myproj/lib/python3.11/site-packages/docker/api/client.py:221: in _retrieve_server_version
    raise DockerException(
E   docker.errors.DockerException: Error while fetching server API version: ('Connection aborted.', FileNotFoundError(2, 'No such file or directory'))

If I set DOCKER_HOST=unix:///Users/brhiggins/.docker/run/docker.sock in my shell or in Python before calling docker.from_env(), it succeeds.

I'll concede that the name from_env sort of sounds like it only considers environment variables, but I had read it as creating a client from "the current environment" more broadly, and I had expected it to behave the same as the docker CLI.

@martimors
Copy link

martimors commented Sep 8, 2023

Facing the exact issue.

NAME       DESCRIPTION                               DOCKER ENDPOINT                                           ERROR
colima *   colima                                    unix:///Users/dingobar/.colima/default/docker.sock
default    Current DOCKER_HOST based configuration   unix:///var/run/docker.sock

In this scenario I'd expect docker-py to use the Colima context.

My awful workaround is

echo 'export DOCKER_HOST="unix:///Users/dingobar/.colima/default/docker.sock"' >> ~/.zshrc

@milas milas changed the title docker.from_env() ignores active context Docker CLI contexts not used or supported Nov 21, 2023
@milas milas pinned this issue Nov 21, 2023
@AstraLuma
Copy link

I'm doing this:

def get_client(use: str | None = None) -> docker.DockerClient:
    """
    Get a docker client for the given docker context
    """
    context = docker.ContextAPI.get_context(use)
    if context is None:
        raise ContextNotExistError(f"Docker context {use!r} not found")
    return docker.DockerClient(
        base_url=context.endpoints["docker"]["Host"],
        tls=context.TLSConfig
    )

@AstraLuma
Copy link

You might be running into #3190?

@martimors
Copy link

I don't want to write code that selects a specific context, docker-py should know to use the current context.

@Khushiyant
Copy link
Contributor

Hey, anyone working on this issue? I have some idea for this issue.

@David234-star
Copy link

The Docker CLI context not used or supported error message means that the Docker CLI cannot find a context that matches the specified name. This can happen for a few reasons:

  • The context does not exist.
  • The context is not accessible to the Docker CLI.
  • The context is not supported by the Docker CLI.

To resolve this error, you can try the following:
Check the name of the context and make sure that it is correct.
Make sure that the context is accessible to the Docker CLI. You can do this by running the docker context ls command. This will list all of the available contexts.
Make sure that the context is supported by the Docker CLI. You can do this by running the docker info command. This will show you the version of the Docker CLI that you are using.
If you are still having problems, you can contact Docker support for assistance.

is this correct? guys, I am new to this docker world, so please let me know. everything is AI-generated except me, help me to explore and tackle the open source multiverse

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants