如何使用python和Java运行Docker? [英] How to run Docker with python and Java?

查看:282
本文介绍了如何使用python和Java运行Docker?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在docker容器中同时使用java和python来运行一些代码。

I need both java and python in my docker container to run some code.

这是我的dockerfile:
如果我不添加 FROM openjdk:slim

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"]

而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)

我遇到此错误:

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.

我在这里到底在做什么错?我是Docker的新手,也许我错过了一步。

What exactly am I doing wrong here? I'm new to docker, perhaps I'm missing a step.

其他详细信息

我的目标

我必须运行一个运行Java文件的python程序。我正在使用的python库需要 JAVA_HOME 的路径。

I have to run a python program that runs a Java file. The python library I'm using requires the path to JAVA_HOME.

我的问题:


  • 我不知道Java,因此无法正确运行文件。

  • I do not know Java, so I cannot run the file properly.

我的整个代码都在Python中,除了这个Java位

My entire code is in Python, except this Java bit

Python包装器以我需要的方式运行文件

The Python wrapper runs the file in a way I need it to run.

推荐答案

好,我花了一些时间才弄清楚。我要感谢这个答案

OK it took me a little while to figure it out. And my thanks go to this answer.

我认为我的方法没有之所以可以正常工作,是因为我没有基本的Linux版本。

I think my approach didn't work because I did not have a basic version of Linux.

所以它是这样的:


  1. 获取Linux(我使用Alpine是因为它是准系统)

  2. 通过包管理器获取Java

  3. 获取Python,PIP

可选:查找并设置JAVA_HOME

OPTIONAL: find and set JAVA_HOME


  1. 查找路径JAVA_HOME。也许有更好的方法可以执行此操作,但是我运行了运行容器,然后使用 docker exec -it [COINTAINER ID] bin / bash 并找到了它。

  2. 在dockerfile中设置 JAVA_HOME 并再次构建并再次运行它

  1. Find the path to JAVA_HOME. Perhaps there is a better way to do this, but I did this running the running the container, then I looked inside the container using docker exec -it [COINTAINER ID] bin/bash and found it.
  2. Set JAVA_HOME in dockerfile and build + run it all again

这是最终的Dockerfile(应该与问题中的python代码一起使用):

Here 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"]

我是Docker的新手,所以这可能不是最好的解决方案。我愿意接受建议。

I'm new to Docker, so this may not be the best possible solution. I'm open to suggestions.

更新:常见问题


  • 难以使用python软件包

  • Difficulty using python packages

Joabe Lucena 指出此处,Alpine可能会遇到某些python软件包的问题。
我建议您使用最适合您的Linux发行版,例如美分。

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.

这篇关于如何使用python和Java运行Docker?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆