Search code examples
javadockerspring-bootjardocker-compose

Docker unable to access jar file


The docker container is not able to access the jar file, that is being accessed over the mount point /my/project/dir. I am certain it is not a permission issue, because I changed the access rights locally, so it should be able to read/write/execute it.

This is the Dockerfile:

FROM tomcat:9-jre8
RUN apt-get update && apt-get install librrds-perl rrdtool -y
VOLUME ["/data/rrdtool", "/my/project/dir"]
ENTRYPOINT [ "java","-jar","/my/project/dir/build/libs/spring-project-0.1.0.jar" ]

And this is the docker-compose.yml file:

version: '2'
services:
 db:
   container_name: db1
   image: mysql:8
   restart: always
   environment:
     MYSQL_ROOT_PASSWORD: password123
     MYSQL_USER: user123
     MYSQL_PASSWORD: pasw
     MYSQL_DATABASE: mydb
   expose:
     - "3307"
 db2:
   container_name: db2
   image: mysql:8
   restart: always
   environment:
     MYSQL_ROOT_PASSWORD: password123
     MYSQL_USER: user123
     MYSQL_PASSWORD: pasw
     MYSQL_DATABASE: mydb2
   expose:
     - "3308"
 spring:
   container_name: spring-boot-project
   build: 
     context: ./
     dockerfile: Dockerfile
   links:
     - db:db1
     - db2:db2
   depends_on:
     - db
     - db2
   expose:
     - "8081"
   ports:
     - "8081:8081"
   restart: always

This is the output from docker-compose logs spring:

Error: Unable to access jarfile /my/project/dir/build/libs/spring-project-0.1.0.jar

Solution

  • I don't see you copying the jar into the container anywhere. You should try moving a VOLUME declaration from Dockerfile to the compose file into the spring service like this:

      volumes:
        - /my/project/dir:/app
    

    And then inside Dockerfile you should point to the dir:

    ENTRYPOINT [ "java","-jar","/app/build/libs/spring-project-0.1.0.jar" ]
    

    Later on if you'd like to deploy it (for example) you should copy the project files directly into the image instead of utilizing the volumes approach. So in Dockerfile you'd then do:

    COPY . /app
    

    instead of VOLUME [..]

    Putting it all together:

    development:

    Dockerfile:

    FROM tomcat:9-jre8
    RUN apt-get update && apt-get install librrds-perl rrdtool -y
    ENTRYPOINT [ "java","-jar","/app/build/libs/spring-project-0.1.0.jar" ]
    

    compose-file:

    version: '2'
    services:
        [..]
        spring:
           container_name: spring-boot-project
           build: .
           links:
             - db:db1
             - db2:db2
           depends_on:
             - db
             - db2
           ports:
             - "8081:8081"
           restart: always
           volumes:
             - /my/project/dir:/app
    

    deployment:

    Dockerfile (that is placed inside project's folder, docker build requires it's build context to be in a current directory):

    FROM tomcat:9-jre8
    RUN apt-get update && apt-get install librrds-perl rrdtool -y
    COPY . /app
    ENTRYPOINT [ "java","-jar","/app/build/libs/spring-project-0.1.0.jar" ]
    

    compose-file:

    version: '2'
    services:
        [..]
        spring:
           container_name: spring-boot-project
           build: .
           links:
             - db:db1
             - db2:db2
           depends_on:
             - db
             - db2
           expose:
             - "8081"