2
votes

I created the following Dockerfile in my project to containerize Spring app

FROM java:8

EXPOSE 8080

VOLUME /tmp

ADD ./spring-boot-app.jar /app/app.jar

ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app/app.jar"]

Whether or not I try providing the complete project path of jar file in ADD ./spring-boot-app.jar /app/app.jar, it still results in the below error anyway-

Execution failed for task ':buildDocker'. Docker execution failed Command line [docker build -t spring-boot-app:latest /Users/sh/temp/service/build/docker] returned: ADD failed: stat /var/lib/docker/tmp/docker-builder448261255/spring-boot-app.jar: no such file or directory

Additionally, if there is an easier way or alternative solution , appreciate the suggestions.

Based on comments, I tried this in Dockerfile and was able to get pass the previous erro:

FROM openjdk:8-jre-alpine
ADD build/libs/*.jar app.jar
ENTRYPOINT exec java $JAVA_OPTS -Djava.security.egd=file:/dev/./urandom -jar /app.jar

But when running command docker-compose up , it isn't linking spring boot app container with mysql container and giving communication link failure error. It's possibly because of Dockerfile ENTRYPOINT. Here is docker-compose.yml :

version: '3'

services:
  demo-mysql:
    image: mysql:latest
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: smthing
      MYSQL_DATABASE: smthing
      MYSQL_USER: smthing
      MYSQL_PASSWORD: smthing
  spring-boot-docker-webapp:
    build:
      context: .
      dockerfile: Dockerfile
    depends_on:
      - demo-mysql
    ports:
      - 8080:8080
    environment:
      DATABASE_HOST: demo-mysql
      DATABASE_USER: smthing
      DATABASE_PASSWORD: smthing
      DATABASE_NAME: smthing
      DATABASE_PORT: 3306
2

2 Answers

1
votes

Ok it looks like you've moved on a bit now and this has become another question. If you want to communicate with mysql from your Spring boot app, you'll need to put them on a network. You can declare the network in your docker compose file anywhere (I like to do it all the bottom) but then for each service you need to add your service to that network. You then need to set an environment variable for your Spring boot service that = the container name of the mysql container. In your case mysql-demo. You'll need to update your application.properties in the Spring boot project to accept that variable e.g.

MYSQL_HOST=localhost spring.datasource.url=jdbc:mysql://${MYSQL_HOST}/YOUR-DB-NAME

That way your app will default to localhost unless you pass the environment variable in, which you'll do from your docker compose file and this will tell your app to point to the mysql container you've put on the same network as your app.

1
votes

Basically the path to your jar is wrong but here are some slightly different instructions anyway. Make sure your Dockerfile is in the root repository (next to the pom.xml) and that you've packaged your project into a jar (it will be put into the target folder). Then try these lines instead of your ADD line (obviously replace 'YOUR-APP' with your app's name):

# The application's jar file
ARG JAR_FILE=target/YOUR-APP-0.0.1-SNAPSHOT.jar

# Add the application's jar to the container
ADD ${JAR_FILE} YOUR-APP.jar

#Then replace the run line with

ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/YOUR- 
APP.jar"]