在Docker映像中运行jar文件 [英] Run jar file in docker image
问题描述
我用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屋!