Docker communication between two container with Java
Asked Answered
F

3

7

I don't find my answer on any post.

I use a container with a project under PHP on a container which works fine. I want to link Java which is launch on another container.

I use the "java:8" image configure like this :

engine:
    build: ./docker/engine/
    volumes:
        - ".:/home/docker:rw"
        - "./docker/engine/php.ini:/usr/local/etc/php/conf.d/custom.ini:ro"
    links:
        - "db:db"
        - "java:java"
    working_dir: "/home/docker"
java:
    image: java:8
    tty: true
    ports:
        - "999:999"

On my docker PHP container (call "engine"), I have this environment variable.

JAVA_1_ENV_CA_CERTIFICATES_JAVA_VERSION=20140324
JAVA_1_ENV_JAVA_DEBIAN_VERSION=8u72-b15-1~bpo8+1
JAVA_1_ENV_JAVA_HOME=/usr/lib/jvm/java-8-openjdk-amd64
JAVA_1_ENV_JAVA_VERSION=8u72
JAVA_1_ENV_LANG=C.UTF-8
JAVA_1_NAME=/recetteetudiant_engine_1/java_1
JAVA_1_PORT=tcp://172.17.0.3:999
JAVA_1_PORT_999_TCP=tcp://172.17.0.3:999
JAVA_1_PORT_999_TCP_ADDR=172.17.0.3
JAVA_1_PORT_999_TCP_PORT=999
JAVA_1_PORT_999_TCP_PROTO=tcp
JAVA_ENV_CA_CERTIFICATES_JAVA_VERSION=20140324
JAVA_ENV_JAVA_DEBIAN_VERSION=8u72-b15-1~bpo8+1
JAVA_ENV_JAVA_HOME=/usr/lib/jvm/java-8-openjdk-amd64
JAVA_ENV_JAVA_VERSION=8u72
JAVA_ENV_LANG=C.UTF-8
JAVA_NAME=/recetteetudiant_engine_1/java
JAVA_PORT=tcp://172.17.0.3:999
JAVA_PORT_999_TCP=tcp://172.17.0.3:999
JAVA_PORT_999_TCP_ADDR=172.17.0.3
JAVA_PORT_999_TCP_PORT=999
JAVA_PORT_999_TCP_PROTO=tcp
RECETTEETUDIANT_JAVA_1_ENV_CA_CERTIFICATES_JAVA_VERSION=20140324
RECETTEETUDIANT_JAVA_1_ENV_JAVA_DEBIAN_VERSION=8u72-b15-1~bpo8+1
RECETTEETUDIANT_JAVA_1_ENV_JAVA_HOME=/usr/lib/jvm/java-8-openjdk-amd64
RECETTEETUDIANT_JAVA_1_ENV_JAVA_VERSION=8u72
RECETTEETUDIANT_JAVA_1_ENV_LANG=C.UTF-8
RECETTEETUDIANT_JAVA_1_NAME=/recetteetudiant_engine_1/recetteetudiant_java_1
RECETTEETUDIANT_JAVA_1_PORT=tcp://172.17.0.3:999
RECETTEETUDIANT_JAVA_1_PORT_999_TCP=tcp://172.17.0.3:999
RECETTEETUDIANT_JAVA_1_PORT_999_TCP_ADDR=172.17.0.3
RECETTEETUDIANT_JAVA_1_PORT_999_TCP_PORT=999
RECETTEETUDIANT_JAVA_1_PORT_999_TCP_PROTO=tcp

Ping command works fine. But how can I use java with that? I try to use that command

root@639144f7c95f:/home/docker# echo $JAVA_1_PORT$RECETTEETUDIANT_JAVA_1_ENV_JAVA_HOME
tcp://172.17.0.3:999/usr/lib/jvm/java-8-openjdk-amd64
root@639144f7c95f:/home/docker# /recetteetudiant_engine_1/java_1
bash: /recetteetudiant_engine_1/java_1: No such file or directory
root@639144f7c95f:/home/docker# $JAVA_1_PORT$RECETTEETUDIANT_JAVA_1_ENV_JAVA_HOME     
bash: tcp://172.17.0.3:999/usr/lib/jvm/java-8-openjdk-amd64: No such file or directory
root@639144f7c95f:/home/docker# 

Maybe I have to share a volume ? Can I use Java through TCP protocol?

Flannery answered 23/2, 2016 at 8:51 Comment(3)
If you want to communicate between containers, use a network for that. "java" container will be available from "engine" by network name "java"Dispensary
Do you want to link the Java binary/command? That is not possible or lets say recommendable. Do you want to link to an already running java process? This is possible.Orthodontics
I want to link to the Java process in order to execute my .jar file in engine container. How can i do this with to use java process in this dedicated container ? Thought tcp ?Flannery
C
0

You have 2 containers in your compose file. The one which seems to host a php application and one which has java installed.

From "inside" the containers, it behaves as if you had 2 different machines (they are not machines, but containers): one "machine" with a hostname called "engine" and one "machine" with a hostname called "java".

You somehow what to connect to the "machine" called "engine" and run java there. Java application is installed on the other "machine".

What you are trying to do does not seem to make sense.

Cassidycassie answered 23/2, 2016 at 12:3 Comment(1)
The purpose is to use java process in a container which is linked to php process, i have a jar file which minify my css, like a library in my php framework project. It's for separate process, i dont want to install java on my php container but I keep java library on php container, it's a basic command that i want to execute, simple YUI library on jar formatFlannery
S
0

You can't (or at the very least should not) use java over TCP - not in the way you want to, which seems to be to somehow invoke java executable which is basically on another machine (or docker container in this case). Maybe there is some way to achieve this with some remote call, but even if possible it would still be wrong. Simply add JRE to your php container. Or make your jar work like a WS. Docker containers are not meant to be used in a way that container1 has java executable so call it from there, container2 has vi, container3 grep etc...

Snavely answered 23/2, 2016 at 22:32 Comment(1)
I'm not a big fan of Java :) I want to use this library and it's a JAR file yui.github.io/yuicompressor so make it in WS, I don't know how to do that...and I think Docker is split on processus, so I think i can use java:8 image easily because it exists and should be use on the same network. How can I connect directly from a container to another like with SSH ? Ok, it's louder than install JAR file on PHP container....Flannery
E
0

Some ways "interacting" between containers(with one common volume):

  1. Orchestrate in host.

    docker exec -t php command-prepare

    docker exec -t java-app-jdk java -jar yuicompressor.jar bla-bla

    docker exec -t php command-post

  2. Create simple app who listen port and start command in JavaContainer (IMHO best way)

  3. Create "cron" who look volume in JavaContainer, for example,

    a) phpContainer put files to volume and put "indicator" file

    b) javaContainer look "indicator" file, and start work. Post complete remove "indicator" file and put "work log" file.

    c) phpContainer wait some time, and get "work log" file. Work depending on the result parse "work log" file.

UPD. Also you can do something similar to docker.spotter https://github.com/discordianfish/docker-spotter.

Endogen answered 12/5, 2016 at 6:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.