Henosis
Henosis

Reputation: 157

How to run a python script in Java program using Docker container?

i am new to docker and trying to dockerize a java app, which in turn calls a python script

Dockerfile :

FROM openjdk:8-jdk-alpine
ARG JAR_FILE=target/*.jar
ARG SCRIPT_FILE=src/main/resources/script/test.py

COPY ${JAR_FILE} app.jar
COPY ${SCRIPT_FILE} test.py
ENTRYPOINT ["java","-jar","/app.jar"]

Now my App has a line of code which executes a python script, something like this : Code snippet :

        String interpretor = "python";
        String scriptFile = "test.py";
        String arguments = "ping";
        String[] cmd = {interpretor,scriptFile,arguments};
        
        try {
            Process p = Runtime.getRuntime().exec(cmd);
            ...
        

Exception:

java.io.IOException: Cannot run program "python": error=2, No such file or directory            

Any help appreciated. Thanks

Upvotes: 1

Views: 831

Answers (1)

Adiii
Adiii

Reputation: 59976

Python runtime does not exist in the base image openjdk:8-jdk-alpine, you need to install it first before calling python script.

You can try below

FROM openjdk:8-jdk-alpine
RUN apk add --no-cache python
ARG JAR_FILE=target/*.jar
ARG SCRIPT_FILE=src/main/resources/script/test.py
COPY ${JAR_FILE} app.jar
COPY ${SCRIPT_FILE} test.py
ENTRYPOINT ["java","-jar","/app.jar"]

Upvotes: 1

Related Questions