Search code examples
javadockerdocker-compose

Dockerfile and compose.yml with volume but ENTRYPOINT fails


I have a small Spring Boot 3.3.3 rest project that serves up images found in a specified folder. It runs fine with java -jar app.jar, it sees the folders and images within /opt/rest.galleries/galleries on the host.

/opt/rest.galleries 
❯ tree
.
└── galleries
    ├── Artsy
    │   ├── DSC_0074.webp
    │   ...
    ├── Cookbook
    │   ├── 8f13ca315674e4ed7e53c579e12c0b0e001.webp
    │   ...
    └── Family
        ├── dsc_0255.webp
        ...

I'm having trouble getting the app to run in a container though. I want it to access the folders and their image files through a VOLUME but it fails at the ENTRYPOINT that should start the application.

Dockerfile:

FROM amd64/eclipse-temurin:22-jre
VOLUME /opt/rest.galleries
RUN mkdir -p /opt/rest.galleries/logs
ARG JAR_FILE
ADD ${JAR_FILE} /opt/rest.galleries/app.jar
EXPOSE 8000
ENTRYPOINT ["java","-jar","/opt/rest.galleries/app.jar"]

compose.yml:

services:
  rest_galleries:
    image: rest.galleries:3.5.0
    ports:
      - 8000:8000
    volumes:
      - /opt/rest.galleries/:/opt/rest.galleries/
❯ docker compose up
[+] Running 1/0
 ✔ Container cafootewarerestgalleries-rest_galleries-1  Created 0.0s 
Attaching to rest_galleries-1
rest_galleries-1  | Error: Unable to access jarfile /opt/rest.galleries/app.jar
rest_galleries-1 exited with code 1

It looks like ENTRYPOINT ["java","-jar","/opt/rest.galleries/app.jar"] didn't work. I don't want to use docker run because there will be other services with dependencies. What am I doing wrong?


Solution

  • In your Compose file, bind mount only the subdirectory you need.

    volumes:
      - ./galleries:/opt/rest.galleries/galleries
    

    The bind mount hides anything that might have been in the image in the target directory (the right-hand side of the volumes: line). If that's the parent directory containing the jar file, then the jar file won't be visible in the container unless an equivalent file exists on the host.

    In the Dockerfile, you do not need the VOLUME line and it can have some unintended consequences (the following RUN mkdir line will never have an effect for example). Delete that line as well.