简体   繁体   English

如何使用Docker运行简单的Java文件

[英]How to run simple java file with docker

I am extremely new to the world of docker. 我对Docker领域非常陌生。 I am trying to learn on the most basic example in Java. 我正在尝试学习Java中最基本的示例。 I have a project folder called docker-java, and a Dockerfile inside along with a Hello.java file that souts out hello. 我有一个名为docker-java的项目文件夹,并且内部有一个Dockerfile以及发出问候的Hello.java文件。 I am trying to build the image so I may run it and see the output in the terminal. 我正在尝试构建映像,以便可以运行它并在终端中查看输出。 Here is my dockerfile. 这是我的dockerfile。 I am being told Hello.java can not be found. 我被告知找不到Hello.java。

FROM java

RUN javac Hello.java

CMD java Hello

Add following code to copy your Hello.java (I assume the java file is in the same folder as your dockerfile) 添加以下代码以复制Hello.java(我假设Java文件与dockerfile位于同一文件夹中)

FROM java

RUN mkdir -p /usr/src/your_workdir

WORKDIR /usr/src/your_workdir

COPY Hello.java ./

RUN javac Hello.java

CMD java Hello

You basicly have to copy your java file into your container. 基本上,您必须将Java文件复制到容器中。 It is also recommended to create a personal workdir to avoid possible conflict with native files. 还建议创建一个个人工作目录,以避免与本机文件发生冲突。

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

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