Docker container doesn't exit after completing program

5/20/2019

I'm trying to run Java container as one time job in Kubernetes. The base image which I'm using is gcr.io/distroless/java. The problem is sometimes the container keeps running forever after the program completes.

Here is Kubernetes Job configuration:

apiVersion: batch/v1
kind: Job
metadata:
  namespace: default
  annotations:
    cluster-autoscaler.kubernetes.io/safe-to-evict: false
spec:
  template:
    spec:
      containers:
      - name: myapp
        image: gcr.io/my_company/image:test
        imagePullPolicy: Always
        resources:
          limits:
            memory: 3Gi
          requests:
            memory: 1Gi
      restartPolicy: Never
      terminationGracePeriodSeconds: 0
  backoffLimit: 0

Here is the sample of my Java code:

package com.mypackage.prod;

import *some packages*;

public class Main {
    /*Static resources*/

    public static void main(String[] args) {
        LOG.info("Starting");

        /*
        Doing some work and calling couple of functions!
        */

        LOG.info("Done with my work. Hitting System exit");
        System.exit(0);
    }
}

I've also tried using gcr.io/distroless/java:debug and checked the logs via docker attach. After the program completes, I see the last log statement but the Pod & Job still exists. Pod remains in active status while Job in running status.

Running on Google Kubernetes Engine with Master version: 1.12.7-gke.10

I've to then manually clean such jobs and pods using kubectl delete jobs $job-name --grace-period=0 --force

-- Varun Dhall
container-image
docker
google-kubernetes-engine
java
kubernetes

0 Answers