skip to Main Content

i trying run my application with docker

Dockerfile

FROM openjdk:latest  
CMD ["./mvmw", "clean", "package"]  
COPY target/doIt-0.0.1.jar app.jar  
RUN chmod 755 app.jar  
ENTRYPOINT ["java", "-jar", "app.jar"]  

docker-compose.yml

version: '3'

services:
  application:
      container_name: doIt_service
      build:
        dockerfile: Dockerfile
      ports:
        - "8081:8081"
      volumes:
        - ./:/app
      working_dir: /
      command: ["java", "-jar", "app.jar"]
      restart: always

it works
but

  1. app.jar is position root directory
  2. all files of project in /app/

so i changed file

FROM openjdk:latest  
CMD ["./mvmw", "clean", "package"]  
COPY target/doIt-0.0.1.jar /app/app.jar  
RUN chmod 755 /app/app.jar  
ENTRYPOINT ["java", "-jar", "/app/app.jar"]  

and

version: '3'

services:
  application:
      container_name: doIt_service
      build:
        dockerfile: Dockerfile
      ports:
        - "8081:8081"
      volumes:
        - ./:/app
      working_dir: /app
      command: ["java", "-jar", "/app/app.jar"]
      restart: always

than i type
docker compose up
its not work!!
(err message : "Unable to access jarfile /app/app.jar")

i browse "view file" in docker and show file is right location
‘app/app.jar’

why is not work?
what is my fault?

i want to change /app.jar to /app/app.jar

i don’t like "app.jar" in root directory

2

Answers


  1. You are copying the jar to /app/app.jar, but after that in the docker-compose.yaml you are mounting volume in /app, which replaces all the contents, and app.jar is not there anymore.

    • One solution is to remove the volume, if you need only the jar and not other files from the source code
    version: '3'
    
    services:
      application:
          container_name: doIt_service
          build:
            dockerfile: Dockerfile
          ports:
            - "8081:8081"
          working_dir: /app
          command: ["java", "-jar", "/app/app.jar"]
          restart: always
    
    • Another option is to use different directories for the jar and /app for the jar and /code

    • Another option is to copy the content of the app and remove the volume from docker-compose.yaml

    CMD ["./mvmw", "clean", "package"]
    RUN mkdir -p /app
    COPY ./ /app
    COPY target/doIt-0.0.1.jar /app/app.jar
    RUN chmod 755 /app/app.jar
    ENTRYPOINT ["java", "-jar", "/app/app.jar"]  
    
    version: '3'
    
    services:
      application:
          container_name: doIt_service
          build:
            dockerfile: Dockerfile
          ports:
            - "8081:8081"
          working_dir: /app
          command: ["java", "-jar", "/app/app.jar"]
          restart: always
    
    Login or Signup to reply.
  2. You can change the WORKDIR in the Dockerfile. This sets the current directory, so both the directory that COPY will copy into and the current directory when you say java -jar app.jar. I’d put that directive before the first COPY line. WORKDIR creates the directory if it does not exist and you do not need to RUN mkdir.

    FROM openjdk:latest
    WORKDIR /app
    COPY target/doIt-0.0.1.jar app.jar  
    ENTRYPOINT ["java", "-jar", "app.jar"]  
    

    You also do not need to RUN chmod the file (you are not directly running it as ./app.jar), nor do you need to set a CMD (arguments that get passed to the ENTRYPOINT).

    In the Compose file, in turn, you need to delete the volumes: block that replaces /app in the image and injects the content you don’t want. You have several other unnecessary options as well. I’d trim this down to just

    version: '3.8'
    services:
      application:
          build: .
          ports:
            - "8081:8081"
          restart: always
    

    The other options either override defaults Compose provides (container_name:, build: { dockerfile: }) or things in the Dockerfile (working_dir:, command:) and can be safely removed.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search