Cannot find contexts if Docker config file is not present
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 7.2k
- Forks
- 1.7k
- Avg merge
- 13d 8h
- Merged PRs (30d)
- 2
Description
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.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start in docker/context/config.py at get_context_dir(), then reproduce the behavior with ContextAPI.contexts() in a Python REPL. Verify that contexts are discovered when config.json is absent, using the platform-specific Docker directories and DOCKER_CONFIG described in the issue.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- docker, python
- Domain
- cli, tooling
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 48/100