Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run jar file in docker image

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 

After executing following command :

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 

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?

like image 690
Svetoslav Angelov Avatar asked Jan 28 '16 12:01

Svetoslav Angelov


1 Answers

There is a difference between images and containers.

  • Images will be build ONCE
  • You can start containers from Images

In your case:

Change your image:

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"] 

Build your image:

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 . 
like image 128
blacklabelops Avatar answered Oct 14 '22 06:10

blacklabelops