I need both java and python in my docker container to run some code.
This is my dockerfile: It works perpectly if I don't add the FROM openjdk:slim
#get python FROM python:3.6-slim RUN pip install --trusted-host pypi.python.org flask #get openjdk FROM openjdk:slim COPY . /targetdir WORKDIR /targetdir # Make port 81 available to the world outside this container EXPOSE 81 CMD ["python", "test.py"]
And the test.py app is in the same directory:
from flask import Flask import os app = Flask(__name__) @app.route("/") def hello(): html = "<h3>Test:{test}</h3>" test = os.environ['JAVA_HOME'] return html.format(test = test) if __name__ == '__main__': app.run(debug=True,host='0.0.0.0',port=81)
I'm getting this error:
D:\MyApps\Docker Toolbox\Docker Toolbox\docker.exe: Error response from daemon: OCI runtime create failed: container_linux.go:348: starting container process caused "exec: \"python\": executable file not found in $PATH": unknown.
What exactly am I doing wrong here? I'm new to docker, perhaps I'm missing a step.
Additional details
My goal
I have to run a python program that runs a Java file. The python library I'm using requires the path to JAVA_HOME
.
My issues:
I do not know Java, so I cannot run the file properly.
My entire code is in Python, except this Java bit
The Python wrapper runs the file in a way I need it to run.
You can use Docker to run a Java application in a container with a specific runtime environment.
A Python library for the Docker Engine API. It lets you do anything the docker command does, but from within Python apps – run containers, manage containers, manage Swarms, etc. For more information about the Engine API, see its documentation.
You can run both Linux and Windows programs and executables in Docker containers. The Docker platform runs natively on Linux (on x86-64, ARM and many other CPU architectures) and on Windows (x86-64). Docker Inc. builds products that let you build and run containers on Linux, Windows and macOS.
An easier solution to the above issue is to use multi-stage docker containers where you can copy the content from one to another. In the above case you can have openjdk:slim
as the base container and then use content from a python container to be copied over into this base container as follows:
FROM openjdk:slim COPY --from=python:3.6 / / ... <normal instructions for python container continues> ...
This feature is available as of Docker 17.05 and there are more things you can do using multi-stage build as in copying only the content you need from one to another.
Reference documentation
OK it took me a little while to figure it out. And my thanks go to this answer.
I think my approach didn't work because I did not have a basic version of Linux.
So it goes like this:
OPTIONAL: find and set JAVA_HOME
docker exec -it [COINTAINER ID] bin/bash
and found it.JAVA_HOME
in dockerfile and build + run it all againHere is the final Dockerfile ( it should work with the python code in the question) :
### 1. Get Linux FROM alpine:3.7 ### 2. Get Java via the package manager RUN apk update \ && apk upgrade \ && apk add --no-cache bash \ && apk add --no-cache --virtual=build-dependencies unzip \ && apk add --no-cache curl \ && apk add --no-cache openjdk8-jre ### 3. Get Python, PIP RUN apk add --no-cache python3 \ && python3 -m ensurepip \ && pip3 install --upgrade pip setuptools \ && rm -r /usr/lib/python*/ensurepip && \ if [ ! -e /usr/bin/pip ]; then ln -s pip3 /usr/bin/pip ; fi && \ if [[ ! -e /usr/bin/python ]]; then ln -sf /usr/bin/python3 /usr/bin/python; fi && \ rm -r /root/.cache ### Get Flask for the app RUN pip install --trusted-host pypi.python.org flask #### #### OPTIONAL : 4. SET JAVA_HOME environment variable, uncomment the line below if you need it #ENV JAVA_HOME="/usr/lib/jvm/java-1.8-openjdk" #### EXPOSE 81 ADD test.py / CMD ["python", "test.py"]
I'm new to Docker, so this may not be the best possible solution. I'm open to suggestions.
UPDATE: COMMON ISUUES
As Joabe Lucena pointed out here, Alpine can have issues certain python packages. I recommend that you use a Linux distro that works best for you, e.g. centos.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With