Call specific java main class inside of docker container running on K8s cluster

5/28/2019

I am trying to call specific java class, which contain main methods, using Kubernetes. However, every combination of command/args in my deployment yaml ends with an error when creating the container.

deployment.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: reports
spec:
  replicas: 1
  selector:
    matchLabels:
      app: reports
  template:
    metadata:
      labels: 
        app: reports
    spec:
      containers:
      - name: reports
        command: ["java -cp reports.jar com.gswsa.is.reports.TestRunner"]
        image: blah/reports:latest
        volumeMounts:
        - mountPath: /tmp/logs/reports
          name: logvolume

Dockerfile

FROM openjdk:8-jdk-alpine
RUN apk add --no-cache tzdata
RUN apk --update add fontconfig ttf-dejavu
ENV TZ America/New_York
VOLUME /tmp/logs/reports
ARG JAR_FILE=target/reports-1.0.0.jar
COPY ${JAR_FILE} /reports.jar
#ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/reports.jar"]
#CMD ["/usr/bin/java", "-jar", "-Dspring.profiles.active=PROD", "/reports.jar"]

I expect to be able to create multiple deployment scripts and call them whenever I would like with their specific arguments. If there is a better way, I am all ears! Thanks

-- user2888259
docker
java
kubernetes
spring

1 Answer

5/28/2019

You can specify the command line to start in the container only in the Dockerfile. That is, it is fixed for a given container image. However, you can pass environment variables to your container from the deployment and have your command line behave differently.

That is, you can have one single main class in Java and have that do different things based on the environment variable. Or you can have a wrapper shell script that you define as the entry point in your Dockerfile and have that script execute java command line with specific Java class based on the env variable.

-- vrtx54234
Source: StackOverflow