mikey1
mikey1

Reputation: 31

docker-compose exec causes [Errno 2] No such file or directory: 'docker-compose': 'docker-compose' in docker container

I am using docker compose to create a network of containers, where one of the containers requests another to run a process. The client also has to monitor these process in case of errors or when it completes. My approach is to use python's subprocess Popen like this:

process = subprocess.Popen(cmd,stdout=subprocess.PIPE, stderr=subprocess.STDOUT)

where cmd is ['docker-compose', 'exec', 'service2', 'sh', '-c', 'cp sourcefile /destination && python run.py']

But I get this error

[Errno 2] No such file or directory: 'docker-compose': 'docker-compose'

I tried executing the same command in bash mode on the client container and got

bash: docker-compose: command not found. I thought of doing a dind, but is that really necessary? This command works on my host machine docker-compose run --rm service2 python apples.py shell. What's the right approach bearing in mind I need to query the returncode of the process running in container service2 from service1 at anytime. I declared a bridge-network. Thanks

Upvotes: 1

Views: 3338

Answers (2)

mikey1
mikey1

Reputation: 31

I tried suggestion from here added

COPY --from=library/docker:latest /usr/local/bin/docker /usr/bin/docker COPY --from=docker/compose:1.23.2 /usr/local/bin/docker-compose /usr/bin/docker-compose to my dockerfile and was able to use the original docker-compose command. Still not sure if best practice but it worked.

EDIT: added to the above I set also set an environment variable in my docker file for docker host like RUN export DOCKER_HOST="tcp://0.0.0.0:2375" now docker-compose service2 exec -c 'cmd'works with subprocess.Popen created inside the service1 container.

Upvotes: 1

matias elgart
matias elgart

Reputation: 1181

In your command, use the absolute path to the docker-compose executable on your machine.

Upvotes: 0

Related Questions