在 docker 镜像中运行 jar 文件 [英] Run jar file in docker image

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

问题描述

我用 java 创建了一个 Docker 映像,并将 jar 文件复制到映像中.我的 Dockerfile 是:

I created a Docker image with java, and am copying the jar file into the image. My Dockerfile is :

FROM anapsix/alpine-java
MAINTAINER myNAME 
COPY testprj-1.0-SNAPSHOT.jar /home/testprj-1.0-SNAPSHOT.jar
RUN java -jar /home/testprj-1.0-SNAPSHOT.j

执行以下命令后:

docker build -t imageName.

在控制台中,我看到了应用程序的输出,一切都很好.但是当我停止图像时,我不知道如何再次运行图像?当我执行以下命令时:

In the console I see the output from the application and everything is fine. But when I stop the image, I don`t know how to run the image again ? When I execute the following command :

docker run -i -t imageName java -jar /home/testprj-1.0-SNAPSHOT.jar

应用程序再次运行,但在我的 Dockerfile 中我已经编写了此命令.如何在没有此命令的情况下运行映像并让应用程序自动运行?

The application runs again, but in my Dockerfile I have already written this command. How can I run the image without this command and have the application run automatically?

推荐答案

图片和容器是有区别的.

There is a difference between images and containers.

  • 图像将被构建一次
  • 您可以从图像启动容器

在你的情况下:

改变你的形象:

FROM anapsix/alpine-java
MAINTAINER myNAME 
COPY testprj-1.0-SNAPSHOT.jar /home/testprj-1.0-SNAPSHOT.jar
CMD ["java","-jar","/home/testprj-1.0-SNAPSHOT.jar"]

建立你的形象:

docker build -t imageName .

现在在容器中调用您的程序:

Now invoke your program inside a container:

docker run --name myProgram imageName

现在通过重启容器来重启你的程序:

Now restart your program by restarting the container:

docker restart myProgram

你的程序改变了吗?重建图像!:

Your program changed? Rebuild the image!:

docker rmi imageName
docker build -t imageName .

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

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