0

I have a spring boot application which i have deployed on docker container, everything is working fine, but i want to deploy and run the application in docker container with docker compose.

This is my DockerFile

FROM java:8
VOLUME /tmp
COPY /target/order-0.0.1-SNAPSHOT.jar order.jar
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/order.jar"]

Step 1 :- Created jar with mvn clean package
Step 2 :- docker build -t order
Step 3 :- docker run -it -d -p 8080:8080

Here everything work's fine

But if i don't execute the step 1 and want's to deploy the the application in container with docker-compose.

While trying to execute docker-compose up i am getting exception /target/order-0.0.1-SNAPSHOT.jar not found

So how to execute the mvn package command in docker-compose ?

Is their any other way's to acheive this ?

This is my docker-compose.yml

version: '3'
services:
  order:
    restart: always
    build: ./order
    working_dir: /order
    volumes:
      - /tmp:/logs
    expose:
      - "8080"
Kuldeep
  • 599
  • 11
  • 28
  • 1
    have you checked whether the .jar file is actually there? – Stultuske May 16 '19 at 06:14
  • See https://stackoverflow.com/questions/52120845/docker-compose-build-with-maven-that-re-uses-the-maven-repository. – ewramner May 16 '19 at 06:17
  • You can have a base image that already has `mvn` installed, and package your application within the `Dockerfile` itself. – Maroun May 16 '19 at 06:17
  • Thanks @Maroun , will try that, but if you can help me with DockerFile it will help me – Kuldeep May 16 '19 at 06:54
  • Can you show exactly where your Dockerfile is located, where your compose file is located, in what directories you execute these commands? Building the app inside the docker container won't help if the underlying problem is that the files are not where you expect them. – kutschkem May 16 '19 at 08:12

1 Answers1

0

try with this one Dockerfile

FROM maven:alpine AS build
COPY src /home/app/src
COPY pom.xml /home/app 
RUN mvn -f /home/app/pom.xml clean package


FROM openjdk:alpine
COPY --from=build /home/app/target/*.jar /usr/local/lib/demo.jar
ENTRYPOINT ["java","-jar","/usr/local/lib/demo.jar"]  

if you get error like Can't execute jar- file: “no main manifest attribute”
replace last command

ENTRYPOINT ["java","-cp","/usr/local/lib/demo.jar","com.packagename.classnamewithoutextension"]
Abhishek D K
  • 2,257
  • 20
  • 28