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
- app.jar is position root directory
- 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
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.
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
You can change the
WORKDIR
in the Dockerfile. This sets the current directory, so both the directory thatCOPY
will copy into and the current directory when you sayjava -jar app.jar
. I’d put that directive before the firstCOPY
line.WORKDIR
creates the directory if it does not exist and you do not need toRUN mkdir
.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 aCMD
(arguments that get passed to theENTRYPOINT
).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 justThe other options either override defaults Compose provides (
container_name:
,build: { dockerfile: }
) or things in the Dockerfile (working_dir:
,command:
) and can be safely removed.