在Docker中运行Python和Java [英] Running Python and Java in Docker

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

问题描述

我有一个安装了python和java的docker映像,现在我想将两个进程都作为docker的子进程来运行.

I have a docker image with installed python and java, and now I want to run both processes as child processes of docker.

我已经查看了这篇文章,其中说明了如何准备安装了docker和python的映像.它没有说明如何使它们都运行.如何在python和Java上运行Docker?

I have checked this post which explains how to prepare image with docker and python installed. It does not explain how to get both of them running. How to run Docker with python and Java?

我检查了如何在Docker中运行多个进程 https://runnable.com/docker/rails/run-一个容器中有多个进程

I checked how to run multiple processes in docker https://runnable.com/docker/rails/run-multiple-processes-in-a-container

我知道在一个容器中运行多个进程是不好的事情.这是一个紧迫性和局限性,所以我会坚持一段时间.

I know it is a bad thing to run multiple processes in one container. It is an urgency and limitation, so I will stick to it for some time.

所以Docker文档(如上)说,准备一个启动两个进程的shell文件,并在docker文件中运行它.

So docker documentation (above) says, prepare a shell file which starts two processes and run it inside docker file.

但是!!!我从主机命令行( docker exec -it container_name bash )连接到docker.我看到了( top )正在运行的进程.有Java进程正在运行,而python进程没有在运行.

However!!! I connected to my docker from host command line (docker exec -it container_name bash). I saw (top) running processes. There is Java process running and python process is not running.

我的 Dockerfile

# Prepare slim python, and install open-jdk-11
ENTRYPOINT ./startJavaAndPython.sh

startJavaAndPython.sh

java -XX:+UseContainerSupport $JAVA_OPTIONS -jar java-app.jar;
python app.py;

我还阅读了 https://askubuntu.com/questions/287350/开始一个没有阻塞终端的新过程 https://unix.stackexchange.com/questions/152310/how-to-正确地从a-shell/305769启动应用程序,在我的情况下,该方法不起作用.因为如您所见,在我的shell中,如果命令不以; 结尾,则启动docker时会出错.

I also read https://askubuntu.com/questions/287350/start-a-new-process-without-blocking-the-terminal and https://unix.stackexchange.com/questions/152310/how-to-correctly-start-an-application-from-a-shell/305769, which did not work in my case. Because as you see, in my shell, if a command does not end with ;, there will be errors when I start docker.

1)因此,&"号无济于事.

1) So, ampersands do not help.

startJavaAndPython.sh

java -XX:+UseContainerSupport $JAVA_OPTIONS -jar java-app.jar &;
python app.py &;

我也尝试过,但是没有用. startJavaAndPython.sh

I also tried, but it did not work. startJavaAndPython.sh

java -XX:+UseContainerSupport $JAVA_OPTIONS -jar java-app.jar &
python app.py &

我收到了

$ docker run my-composed-task
: not foundonJava.sh: 1: ./startJavaAndPython.sh:
: not startJavaAndPython.sh: 2: ./startJavaAndPython.sh:

2)我可以启动我的docker容器,通过ssh连接到它,然后手动运行python应用程序.它将工作!!!但是,如何自动操作,而无需动手!

2) I can start my docker container, connect to it ssh, and run the python app manually. It will work!!! But how to do it automatically, without hands!

3)我使用 Windows 10 作为主机.

3) I use Windows 10 as a host machine.

我使用了这些链接,这可能对其他人有帮助. https://askubuntu.com/questions/287350/start-a-没有阻塞终端的新过程

I used these links, which might be helpful for others. https://askubuntu.com/questions/287350/start-a-new-process-without-blocking-the-terminal

https://unix.stackexchange.com/questions/152310/how-to-正确地从a-shell/305769启动应用程序

如何进入Docker容器的外壳?/a>

How do I get into a Docker container's shell?

推荐答案

经过很多痛苦.如我所说,我使用的是 Windows .它确实在每行末尾添加 \ r .然后,我的shell脚本具有这些结尾,并且无法启动.更确切地说,它会启动第一个命令-即启动Java,但从不执行第二个命令,即启动python.

After much pain. As I said, I use Windows. It does append \r at the end of each line. Then my shell script has these endings and fails to start. More exactly, it starts first command - which is to start java, but it never executes the second command, which starts python.

startJavaAndPython.sh

java -XX:+UseContainerSupport $JAVA_OPTIONS -jar sidecarForPythonService-app.jar & python app.py;

如果您还有其他详细信息,请随时回答.

If you have other details, feel free to answer.

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

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