compose: Context not found when given on docker-compose CLI

Description of the issue

When using the docker-compose command line --context parameter, the context reports not found, even though it’s found when using docker commands.

Context information (for bug reports)

Output of docker-compose version

docker-compose version 1.26.0-rc4, build d279b7a8
docker-py version: 4.2.0
CPython version: 3.7.7
OpenSSL version: OpenSSL 1.1.0l  10 Sep 2019

Output of docker version

Client: Docker Engine - Community
 Version:           19.03.8
 API version:       1.40
 Go version:        go1.12.17
 Git commit:        afacb8b7f0
 Built:             Wed Mar 11 01:25:46 2020
 OS/Arch:           linux/amd64
 Experimental:      false

Server: Docker Engine - Community
 Engine:
  Version:          19.03.8
  API version:      1.40 (minimum version 1.12)
  Go version:       go1.12.17
  Git commit:       afacb8b7f0
  Built:            Wed Mar 11 01:24:19 2020
  OS/Arch:          linux/amd64
  Experimental:     false
 containerd:
  Version:          1.2.13
  GitCommit:        7ad184331fa3e55e52b890ea95e65ba581ae3429
 runc:
  Version:          1.0.0-rc10
  GitCommit:        dc9208a3303feef5b3839f4323d9beb36df0a9dd
 docker-init:
  Version:          0.18.0
  GitCommit:        fec3683

Output of docker-compose config (Make sure to add the relevant -f and other flags)

services:
  test:
    container_name: test
    image: alpine
version: '3.6'

Steps to reproduce the issue

  1. Create a context (in this case with ssh):
docker context create testctx --default-stack-orchestrator swarm --docker "host=ssh://user@remote"
  1. Verify context is functional:
docker --context testctx ps
  1. Issue a compose command on the context (this is what fails):
docker-compose --context testctx up -d
  1. Issue the same compose command except with the host parameter to verify endpoint (this works for me):
docker-compose -H ssh://user@remote up -d

Observed result

ERROR: Context 'testctx' not found

Expected result

The docker-compose should remotely execute on the context’s machine.

Stacktrace / full error message

(with --verbose)

docker.utils.config.find_config_file: Trying paths: ['/home/user/.docker/config.json', '/home/user/.dockercfg']
docker.utils.config.find_config_file: No config file found
ERROR: compose.cli.main.main: Context 'testctx' not found

Additional information

OS version / distribution, docker-compose install method, etc.

Ubuntu 18.04.

Docker installed via this ansible role, docker-compose installed via ansible task:

- name: Update docker-compose to bleeding edge # Required for docker context support
  get_url:
    url: https://github.com/docker/compose/releases/download/1.26.0-rc4/docker-compose-Linux-x86_64
    dest: /usr/local/bin/docker-compose
    checksum: sha256:3ad2031c7daae160b8987538a60261475b3e17dfccf6fb96c1ae67d23afecdb8

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Reactions: 13
  • Comments: 16

Commits related to this issue

Most upvoted comments

Maybe I have solved it.

Before running docker-compose --context testctx, I executed docker context use testctx. Then docker-compose recognized my context, even if I set the context back to “default”.

However, I’m not sure why it works. So I cannot guarantee that it will work in your environment.

$ docker context use testctx
$ docker context use default
$ docker-compose --context testctx up
# docker-compose finds testctx and works fine

Looks like this is due to docker/docker-py#2622 So just an initial use of docker context use default should be enough to at least work around this issue.

Looks like this is due to docker/docker-py#2622 So just an initial use of docker context use default should be enough to at least work around this issue.

Does not fix the problem for me sadly. This is on a fresh install of Docker using getdocker.sh and also after having installed docker-compose and docker-py via pip3.

Getting this:

Traceback (most recent call last):
  File "/usr/local/bin/docker-compose", line 5, in <module>
    from compose.cli.main import main
  File "/usr/local/lib/python3.8/dist-packages/compose/cli/main.py", line 29, in <module>
    from ..metrics.decorator import metrics
  File "/usr/local/lib/python3.8/dist-packages/compose/metrics/decorator.py", line 3, in <module>
    from compose.metrics.client import MetricsCommand
  File "/usr/local/lib/python3.8/dist-packages/compose/metrics/client.py", line 5, in <module>
    from docker import ContextAPI
ImportError: cannot import name 'ContextAPI' from 'docker' (/usr/local/lib/python3.8/dist-packages/docker/__init__.py)

+1 docker-compose should respect the DOCKER_CONTEXT environment variable and the current user’s docker context config files

Its never a good sign when you find a 2 year old thread about an issue.

Versions

me:api$ docker -v
Docker version 20.10.7, build 20.10.7-0ubuntu5~20.04.2
me:api$ docker-compose  -v
docker-compose version 1.29.2, build unknown

I created the context like so:

docker context create prod --docker "host=ssh://user@host.com"

I can then run the following and get an output of running containers

docker --context prod ps

However running with docker-compose the command fails

docker-compose --context prod -f docker-compose.yml -f docker-compose.prod.yml up -d
ERROR: Context 'prod' not found

I tired the work around of setting the context and moving it back to default but that did not work either.

@pandanoir sure, issuing a docker context use <> would work because then you’re in the proper context, but the docker-compose --context argument would be pointless if that was required. The --context argument should either switch contexts only for that command, or only execute the given compose file on the context given.