Control docker-compose in Java
Asked Answered
C

2

8

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?

Caylor answered 12/11, 2017 at 15:2 Comment(1)
I found another useful library: testcontainers.org. TestContainers is a Java library that supports JUnit tests, providing lightweight, throwaway instances of common databases, Selenium web browsers, or anything else that can run in a Docker container.Caylor
K
5

There might be two possible approaches that you can take:

  1. Run docker-compose up/down using normal command executor (e.g. with the help of ProcessBuilder and run OS command)
  2. Using native docker SDK, currently golang and python are officially supported, but java docker client can be found here and here. For now, I am using docker SDK with golang, and see that we can programmatically do almost everything with docker.
Kick answered 12/11, 2017 at 15:46 Comment(4)
Thank you! Regarding the second option: I already investigated these SDKs and 3rd party Java libraries you mentioned. They don't seem to support docker-compose.yml configurations and functionalities. So basically, I have to implement the docker-compose functionalities myself (parsing, starting containers, managing them, ...), which would be a massive task and reinventing the wheel. Or am I wrong here?Caylor
Yeah, Docker SDK doesn't support docker-compose for now. However, we can still configure services one by one with the current SDK and group them together. We don't really need to implement entire dock-compose.yaml parsing and processing.Kick
I will have to look into that!Caylor
Sure, I have done similar stuff to create cassandra cluster from golang, however, this is fairly simple and as each service/instance is identical. It might be troublesome if you have a complicated docker-compose setup.Kick
P
2

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.

Punchinello answered 13/11, 2017 at 7:24 Comment(2)
thanks for your sharing, it's good to know. I should have used python SDK then :)Kick
Most Docker tools would be written in Go normally. Compose was an external tool called Fig before it was bought by Docker and merged into the eco system.Punchinello

© 2022 - 2024 — McMap. All rights reserved.