Overriding Docker ENTRYPOINT in Kubernetes

12/3/2021

I have a Docker image of a Spring Boot app running in Kubernetes cluster. I'm trying to override an entrypoint.

My original entrypoint was

ENTRYPOINT ["java", "-jar", "/app/myapp-exec.jar"]

So I tried to override it in spec of deployment because I need to pass some JVM arguments:

      command: ["sh", "-c", "java ${JAVA_OPTS} -jar /app/myapp-exec.jar ${0} ${@}"]
      args:
        - --spring[0].property=property_value
      env:
        - name: "JAVA_OPTS"
          value: "...jvm options..."
        - name: spring.property1
          value: property_value
        - name: spring.property2
          value: property_value

My application is launched, but properties defined under env that used to get passed to Spring app with original entry point are not passed anymore. According to Spring Docker documentation, ${0} ${@} should fix my exact issue, but it doesn't seem to work.

Any help is appreciated.

-- lovrodoe
docker
kubernetes
spring

1 Answer

12/3/2021

You can get around the various shell-quoting problems by making the main container process a very short shell script:

#!/bin/sh
exec java $JAVA_OPTS -jar /myapp-exec.jar "$@"

Then your Dockerfile can specify this script as the main container command, instead of trying to shoehorn it into the ENTRYPOINT/CMD split somehow.

COPY run-myapp.sh .
CMD ["./run-myapp.sh"]  # (I tend to prefer CMD to ENTRYPOINT)

If the only things you're trying to pass as command-line arguments are Spring properties, you can specify those as environment variables; this is much easier than trying to figure out the command-line setup. So for your use case, I would specify

containers:
  - name: myapp
    image: registry.example.com/myapp:20211203
    # neither command: nor args:
    env:
      - name: "JAVA_OPTS"
        value: "...jvm options..."
      - name: SPRING_PROPERTY1
        value: property_value
      - name: SPRING_PROPERTY2
        value: property_value
      - name: SPRING_0_PROPERTY
        value: property_value
-- David Maze
Source: StackOverflow