Skip to content

Cannot find contexts if Docker config file is not present #2622

Description

@Forinil

Docker client version: 19.03.3
Docker daemon version: 19.03.12
Python version: 3.8.3
Docker SDK version: 4.2.2
docker-compose version: 1.26.0
OS: Windows 10
OS Version: 2004, build 19041.329

Issue: docker-py does not see configured contexts if config.json is not present

Explanation:
I have installed Docker client and Docker Python SDK on my Windows 10 laptop to interact with Docker daemon installed on remove server running Ubuntu 18.04. I have created two contexts - one to connect to the daemon via SSH ('docker-ssh') and another to connect via TCP ('docker-tcp').

I verified that both contexts work using Docker CLI and that they are present in directory %USERPROFILE%\.docker\contexts.

Following that I attempted to start a simple service using docker-compose. I received error that given context was not found.
I investigated this and realised that docker-compose loads contexts using ContextAPI.get_context(name) from docker-py, which eventually calls find_config_file() and then tries to locate contexts relative to Docker config file.

The problem is that there was no config.json or .dockercfg on my system. This causes docker-py to fail to locate my contexts and return an error.

I tested this by running the folowing code in Python REPL:

from docker.context import ContextAPI
ContextAPI.contexts()

and sure enough the response I got was:

[<Context: 'default'>]

Then I proceeded to create file %USERPROFILE%\.docker\config.json and reran ContextAPI.contexts() (without restarting REPL) and got:

[<Context: 'default'>, <Context: 'docker-tcp'>, <Context: 'docker-ssh'>]

I think method get_context_dir() from docker/context/config.py should be rewritten to return %USERPROFILE%\.docker\contexts on Windows, $HOME/.docker/contexts on Linux and $DOCKER_CONFIG/contexts if variable DOCKER_CONFIG is set.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions