2

I would like to run docker compose from my java code - for example when I transform my data into the right format run docker compose with logstash which will pour data to the elasticsearch.

How can I do that (also independently on the platform) ?

Thank you for any help.

Druudik
  • 955
  • 1
  • 10
  • 32
  • 1
    Seems like an XY problem to me. Why is your Java application transforming data then poking logstash? – Boris the Spider Dec 11 '18 at 21:10
  • It is performing some logic like validating input etc. based on more complex rules, it is doable via logstash plugin written in ruby but since I am not so good at it I prefer Java way. – Druudik Dec 11 '18 at 23:54
  • 1
    But logstash is a streaming log interpreter - it follows logs and groks them into ES. I don't understand how your approach works. I would expect your Java application to do the same - stream from some location and then transform and push into Logstash using a port based input. – Boris the Spider Dec 12 '18 at 02:37
  • Rather than have the java program spin up another docker container, it's more idiomatic to use `docker-compose` to bring up both containers at the same time and have the java program talk to the other program over the "network". – Floegipoky Dec 18 '18 at 17:11

2 Answers2

2

I think in this case, you can use Test Containers.

Long story short. More details are in the documentation. You can create easily docker compose which is based on compose-test.yml

public static DockerComposeContainer environment =
    new DockerComposeContainer(new File("src/test/resources/compose-test.yml"))
            .withExposedService("redis_1", REDIS_PORT)
            .withExposedService("elasticsearch_1", ELASTICSEARCH_PORT);

compose-test.yml looks like:

redis:
  image: redis
elasticsearch:
  image: elasticsearch

Here is the link to documentation:

https://www.testcontainers.org/usage/docker_compose.html

Ice
  • 1,783
  • 4
  • 26
  • 52
  • What dependency do I have to add in order to be able to use DockerComposeContainer ? – Fluch Oct 06 '21 at 17:18
  • org.testcontainers : testcontainers: https://mvnrepository.com/artifact/org.testcontainers/testcontainers – denu Dec 03 '22 at 12:31
0

If you just want to trigger the docker compose on the command line, you can issue a shell command from java. For an example, see Want to invoke a linux shell command from Java.

Alcamtar
  • 1,478
  • 13
  • 19
  • 1
    Obviously OP does not want to trigger the command from the shell! He wants to use Java code from Eclipse or IntelliJ. – Fluch Oct 06 '21 at 17:18