I created a fairly amount of docker-compose scripts which spawn up several services. I now want to control docker-compose in the JVM. Basically, I want to be able to execute up
and down
, ideally with -p <project name>
parameter, so I can spawn multiple instances at the same time.
Is this possible in Java?
You can use Docker to run a Java application in a container with a specific runtime environment. This tutorial describes how to create a Dockerfile for running a simple Java application in a container with OpenJDK 17. It also shows how to create a Docker image with your application to share it with others.
Docker Compose allows us to execute commands inside a Docker container. During the container startup, we can set any command via the command instruction.
You can control the order of service startup and shutdown with the depends_on option. Compose always starts and stops containers in dependency order, where dependencies are determined by depends_on , links , volumes_from , and network_mode: "service:..." .
There might be two possible approaches that you can take:
Docker Compose is a python utility that talks directly to the same Docker API as the all the other Docker clients. There's nothing fundamentally different about the commands it sends, but it does manage a lot of Docker container life cycle for you inside it's code.
Compose is based on the docker
python module which is just another python Docker API client.
It would probably take a lot to reimplement the same in Java, here is the up
method. Maybe try pulling that in with Jython if you really need to do it from the JVM or stick with executing the docker-compose
commands from Java.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With