简体   繁体   中英

No such file while indicating application properties using Dockerfile for a Spring Boot jar

I'm using Docker Compose to create a container for a Spring Boot application.

I'm receiving the following errors when I do docker-compose up :

Recreating backend_springboot ... error

ERROR: for backend_springboot Cannot start service service: OCI runtime create failed: container_linux.go:346: starting container process caused "exec: \\"sh -c java -Dspring.config.location=/application.properties -Djava.security.egd=file:/dev/./urandom $JAVA_OPTS -jar /app.jar\\": stat sh -c java -Dspring.config.location=/application.properties -Djava.security.egd=file:/dev/./urandom $JAVA_OPTS -jar /app.jar: no such file or directory": unknown

ERROR: for service Cannot start service service: OCI runtime create failed: container_linux.go:346: starting container process caused "exec: \\"sh -c java -Dspring.config.location=/application.properties -Djava.security.egd=file:/dev/./urandom $JAVA_OPTS -jar /app.jar\\": stat sh -c java -Dspring.config.location=/application.properties -Djava.security.egd=file:/dev/./urandom $JAVA_OPTS -jar /app.jar: no such file or directory": unknown

My structure:

├── docker-compose.yml
├── spring-boot
│   ├── Dockerfile
│   ├── application.properties
│   └── backofficeservices-0.0.1.jar

docker-compose.yml

version: '3'
services:
  service:
    container_name: backend_springboot
    build: ./spring-boot
    ports:
      - "80:8080"
    restart: always

spring-boot/Dockerfile

FROM openjdk:8-jre-alpine as gradle

COPY backofficeservices-0.0.1.jar /app.jar

COPY application.properties /application.properties

ENV JAVA_OPTS=""

ENTRYPOINT ["sh -c java -Dspring.config.location=/application.properties -Djava.security.egd=file:/dev/./urandom $JAVA_OPTS -jar /app.jar"]

EXPOSE 8080

As far as I know, the application.properties is not found. I will appreciate any help to detect my error.

My goal is to run my Spring Boot app applying the application.properties I have there.

You have specified an ENTRYPOINT using a single "word". When you launch the container, it is trying to run that as a single "word" – it is looking for a binary named sh -c java ... , with spaces and all as part of the filename. If your command has multiple "words" and you're using the JSON-array form, you have to correctly manually split it up into words yourself.

ENTRYPOINT ["sh", "-c", "java -Dspring.config.location=..."]

A Dockerfile CMD and ENTRYPOINT are fairly similar; both provide part of the command the container ultimately runs. If you only need one of them I'd recommend using CMD instead, for two reasons: it makes it easier to get a debugging shell on the built image docker run --rm -it myimage /bin/sh , and there's a fairly standard pattern of using an ENTRYPOINT script to do some initial setup and then exec "$@" to run the CMD.

CMD ["sh", "-c", "java -Dspring.config.location=..."]

If you have this form, Docker can provide the sh -c ... wrapper for you.

CMD java -Dspring.config.location=...

With a little bit more cleanup, that would leave the final Dockerfile something like

FROM openjdk:8-jre-alpine
COPY backofficeservices-0.0.1.jar /app.jar
COPY application.properties /application.properties
ENV JAVA_OPTS=""
CMD java \
      -Dspring.config.location=/application.properties \
      -Djava.security.egd=file:/dev/./urandom \
      $JAVA_OPTS \
      -jar /app.jar
EXPOSE 8080

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM