7

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?

Franz They
  • 945
  • 9
  • 17
  • 1
    I found another useful library: https://www.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.* – Franz They Nov 13 '17 at 18:39

2 Answers2

4

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.
sayboras
  • 4,897
  • 2
  • 22
  • 40
  • 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? – Franz They Nov 12 '17 at 16:05
  • 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. – sayboras Nov 12 '17 at 16:14
  • I will have to look into that! – Franz They Nov 12 '17 at 16:21
  • 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. – sayboras Nov 12 '17 at 16:50
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.

Matt
  • 68,711
  • 7
  • 155
  • 158
  • thanks for your sharing, it's good to know. I should have used python SDK then :) – sayboras Nov 13 '17 at 11:13
  • 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. – Matt Nov 13 '17 at 22:30