I'm trying to run a Spring Boot app as one of the services in a Docker Compose setup, but whenever I run docker-compose up -d the container running the Spring Boot app fails and exits immediately. For purposes of this post I'm using generic names like "mycontainer" and "myfile".
Running docker-compose logs core shows that the error message is:
mycontainer | Error: unable to access jarfile myjar.jar
My docker-compose file is:
version: '3.3'
services:
  webapp:
    image: webapp_image
    container_name: webapp-container
    build:
      context: ./webapp
      dockerfile: Dockerfile
    volumes:
      - './webapp:/usr/src/app'
    ports:
      - 3000:3000
      - 35729:35729
    environment:
      - NODE_ENV=development
      - env_file=./webapp/.env
  core:
    image: core_image
    container_name: core-container
    build:
      context: ./core
      dockerfile: Dockerfile
    volumes:
      - './core:/usr/src/core'
      - './algorithms:/usr/src/algorithms'
    ports:
      - 8080:8080
    environment:
      - JARFILE=myjar.jar
      - JAVA_OPTIONS=-Djava.security.egd=file:/dev/./urandom
The Dockerfile for the core service, which is the one running the Spring Boot app, is this:
FROM openjdk:8-jdk-alpine
RUN mkdir -p /usr/src/core
RUN mkdir -p /usr/src/algorithms
ADD ./myproject/target/myjar.jar /usr/src/core
ADD ./algorithms /usr/src/algorithms
WORKDIR /usr/src/core
CMD java $JAVA_OPTIONS -jar $JARFILE
If I run an individual container from the core_image image, and set the same environment vars that I defined in the docker-compose file, I can launch the Spring Boot app successfully, like so:
$ docker run -it -p 8080:8080 --name mycontainer core_image sh
$ /usr/src/core # ls -l
total 15852
-rwxr-xr-x    1 root     root      16228579 Apr 21 22:44 myjar.jar
$ /usr/src/core # java $JAVA_OPTIONS -jar $JARFILE
But if I run a container from the same image as part of docker-compose up -d the container fails with the error that Java couldn't access the jarfile. I've tried specifying the absolute path to the jarfile in the CMD of the Dockerfile, but that doesn't help when running from Docker Compose.
Does anyone know what might be causing this? I'm on a Windows 10 host. Any help is greatly appreciated.
It seems you are overriding content of /usr/src/core [where you copy the jar file to] by mounting content from ./core in docker-compose. Remove below mount from services->core->volumes and give a try.
  volumes:
      - './core:/usr/src/core'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With