I would like to run docker-compose via python docker sdk. However I couldn't find any reference on how to achieve this using these reference Python SDK? I could also use subprocess but I have some other difficulty while using that. see here docker compose subprocess
Asked
Active
Viewed 2,740 times
1
-
Docker Compose v1 (previously Fig) was actually written in Python. There is an SDK around somewhere for it. – OneCricketeer Nov 11 '21 at 18:25
2 Answers
3
I created a package to make this easy: python-on-whales
Install with
pip install python-on-whales
Then you can do
from python_on_whales import docker
docker.compose.up()
docker.compose.stop()
# and all the other commands.
You can find the source for the package in my GitHub repository: https://gabrieldemarmiesse.github.io/python-on-whales/

Makyen
- 31,849
- 12
- 86
- 121

Gabriel de Marmiesse
- 31
- 2
-
5If you recommend something you've made, you must disclose your affiliation, otherwise [it's considered spam](https://stackoverflow.com/help/promotion). Simply saying "I wrote this package" is enough. – Donald Duck Nov 11 '21 at 18:20
-
It appears you're affiliated with the GitHub repository linked here. When linking to or recommending something you're affiliated with, you *must disclose that affiliation in your post*. Without affiliation disclosure, it's considered spam. Please see: [What signifies "Good" self promotion?](//meta.stackexchange.com/q/182212) and [the help center on self-promotion](/help/promotion). Disclosure must be explicit, but doesn't need to be formal. Disclosure can just be something like "my package", "my repository", "a repository I contribute to", etc. Please [edit] your post(s) to include disclosure. – Makyen Nov 11 '21 at 18:45
-
I've gone ahead and edited in disclosure of affiliation for you. You're welcome to adjust it to something else, but some type of disclosure is required. – Makyen Nov 11 '21 at 18:50
-
How do you use python_on_wheels to run docker compose in detached mode @Gabriel de Marmiesse? – abinitio Jan 10 '22 at 16:41
-
2
I am working on the same issue and was looking for answers, but nothing so far. The best shot I can give it is to simplify that docker-compose logic. For example, you have a YAML file with a network and services - create them separately using Python Docker SDK and connect containers to a network.
It gets cumbersome, but eventually you can get things working that way from Python.

Eli Halych
- 545
- 7
- 25