简体   繁体   English

在 Docker 映像中运行 Java 程序

[英]Running Java program in a Docker Image

So I have a Docker network that has a Docker file with a bunch of information.所以我有一个 Docker 网络,它有一个包含大量信息的 Docker 文件。 I have a java program that is going to bring up the enviorment and then produce several commands to run within this enviorment.我有一个 java 程序,它将调出环境,然后生成几个命令在这个环境中运行。 To be clear, the first command I need to run is NOT inside the Docker enviorment.需要明确的是,我需要运行的第一个命令不在 Docker 环境中。 I am having some challenges with the Process and Runtime classes.我在 Process 和 Runtime 类方面遇到了一些挑战。

First, say I wanted my java program to launch a new gnome terminal and then run a command to get into the docker network.首先,假设我希望我的 java 程序启动一个新的 gnome 终端,然后运行一个命令进入 docker 网络。 I have this command,我有这个命令,

Process process = Runtime.getRuntime().exec(new String[]{"gnome-terminal"});

Gnome terminal sucessfully comes up but any additional arguments I give in this array are just ignored. Gnome 终端成功出现,但我在此数组中提供的任何其他参数都将被忽略。 For example,例如,

Process process = Runtime.getRuntime().exec(new String[]{"gnome-terminal","ls"});

Does not work.不工作。 The command I ultimatly want to run would look something like this,我最终想要运行的命令看起来像这样,

Process process = Runtime.getRuntime().exec(new String[]{"gnome-terminal","sudo","docker","exec","-it","sawtooth-shell-default", "bash"});

Second, Once I have this running, will additional commmands I run work within the Docker enviorment?其次,一旦我运行了这个,我运行的其他命令会在 Docker 环境中工作吗? I have a python file with a Stream handler that specifies the correct commands to run.我有一个带有 Stream 处理程序的 python 文件,它指定要运行的正确命令。 Other documentation on related issues was limited.有关相关问题的其他文件有限。

I made sure my code was wrapped in a runtime exception try catch and that I was running the correct .class file.我确保我的代码包含在运行时异常 try catch 中,并且我正在运行正确的 .class 文件。 Any help on this would be great!对此的任何帮助都会很棒!

Edit: I have also tried to run this in another linux terminal like Hyper and Tilda编辑:我也尝试在 Hyper 和 Tilda 等另一个 linux 终端中运行它

I also am able to get a sudo sign in when I run the command like so,当我像这样运行命令时,我也能够获得 sudo 登录,

Process process = Runtime.getRuntime().exec(new String[]{"gnome-terminal","--","sudo","docker","exec","-it","sawtooth-shell-default", "bash"});

However it closes immediatly after authorizing.但是,它在授权后立即关闭。

Okay this is what I was attempting to do.好的,这就是我试图做的。 https://www.atlassian.com/blog/software-teams/deploy-java-apps-with-docker-awesome This site is outdated and I had to use this link for getting that latest version of the java PPA. https://www.atlassian.com/blog/software-teams/deploy-java-apps-with-docker-awesome这个网站已经过时了,我不得不使用这个链接来获取最新版本的 java PPA。

This process basically installs java into the docker contatiner so that I can run a java program that uses Runtime.这个过程基本上将java安装到docker容器中,这样我就可以运行一个使用Runtime的java程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM