Answer a question

Below is my docker-compose.yml, which is pretty much simple.

version: "3.7"

services:
  book-api:
    image: openjdk:11
    container_name: book-api
    volumes:
      - ./target/demo-0.0.1-SNAPSHOT.jar:/bookapi.jar
    command: ["java","-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=*:8080","-Dspring.profiles.active=local","-jar","bookapi.jar"]
    ports:
      - "8080:8080"

But when I do docker-compose up, I get the below error:

Creating book-api ... done
Attaching to book-api
book-api    | Error: Unable to access jarfile bookapi.jar
book-api exited with code 1

I don't see an error with mounting volumes. jar file (target/demo-0.0.1-SNAPSHOT.jar) is created and is in the correct location. enter image description here

I also tried to change command to below, thinking that was causing issue:

["java","-jar","bookapi.jar"]

But I'm getting the same error. I'm running docker-compose on mac.

To troubleshoot the issue, I changed the command to check the file paths, permissions etc.

command: >
      sh -c "ls -l && pwd && java -jar bookapi.jar"

And then I see below results:

rahulraj@my-pc % docker-compose up
Starting book-api ... done
Attaching to book-api
book-api    | total 46764
book-api    | drwxr-xr-x   1 root root     4096 Jun 23 15:20 bin
book-api    | -rwxrwxrwx   1 root root 47821378 Jun 27 15:25 bookapi.jar
book-api    | drwxr-xr-x   2 root root     4096 Mar 19 13:46 boot
book-api    | drwxr-xr-x   5 root root      340 Jun 28 06:50 dev
book-api    | drwxr-xr-x   1 root root     4096 Jun 28 06:48 etc
book-api    | drwxr-xr-x   2 root root     4096 Mar 19 13:46 home
book-api    | drwxr-xr-x   1 root root     4096 Jun 22 00:00 lib
book-api    | drwxr-xr-x   2 root root     4096 Jun 22 00:00 media
book-api    | drwxr-xr-x   2 root root     4096 Jun 22 00:00 mnt
book-api    | drwxr-xr-x   2 root root     4096 Jun 22 00:00 opt
book-api    | dr-xr-xr-x 183 root root        0 Jun 28 06:50 proc
book-api    | drwx------   1 root root     4096 Jun 23 15:21 root
book-api    | drwxr-xr-x   3 root root     4096 Jun 22 00:00 run
book-api    | drwxr-xr-x   1 root root     4096 Jun 23 01:11 sbin
book-api    | drwxr-xr-x   2 root root     4096 Jun 22 00:00 srv
book-api    | dr-xr-xr-x  13 root root        0 Jun 28 06:50 sys
book-api    | drwxrwxrwt   1 root root     4096 Jun 23 15:21 tmp
book-api    | drwxr-xr-x   1 root root     4096 Jun 22 00:00 usr
book-api    | drwxr-xr-x   1 root root     4096 Jun 22 00:00 var
book-api    | /
book-api    | Error: Unable to access jarfile bookapi.jar
book-api exited with code 1

Seems like the jar file is having full permissions, current working directory is / and path of jar file is /bookapi.jar. But the command java -jar /bookapi.jar is failing with the above mentioned error. I tried with java -jar bookapi.jar too.

Am I doing something wrong here? Kindly suggest. Thanks!

Answers

I'm not sure why it was not working while using only docker-compose.yml. So, the above mentioned issue is still a mystery unresolved, however I changed the docker config to below and no more issues observed.

  1. Created a docker image of the application with a Dockerfile

    From openjdk:11
    copy ./target/app.jar app.jar
    CMD ["java","-jar","app.jar"]
    

    and then docker image build -t book-api .

  2. Modified docker-compose.yml as below:

    services:
        book-api:
          image: book-api
          container_name: book-api
          ports:
             - "8080:8080"
    

    and then docker-compose up worked without any issues and container is up and running.

Thank you @knittl for your inputs.

Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐