Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Control docker-compose in Java

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?

like image 420
Franz They Avatar asked Nov 12 '17 15:11

Franz They


People also ask

Can you use docker with 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.

Can I run commands from Docker compose?

Docker Compose allows us to execute commands inside a Docker container. During the container startup, we can set any command via the command instruction.

How can we control the start up order of services in Docker compose?

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:..." .


2 Answers

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.
like image 142
sayboras Avatar answered Oct 18 '22 04:10

sayboras


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.

like image 40
Matt Avatar answered Oct 18 '22 04:10

Matt