Skip to content

Java/JAR Deployment

Multi-stage build

Build of container image

Source & Containerfile is available at openshift-quickstarts/undertow-servlet

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
FROM registry.access.redhat.com/ubi8/openjdk-11:latest as builder
USER root
COPY ./ /tmp/src
RUN chown -R 185:0 /tmp/src
USER 185
RUN /usr/local/s2i/assemble

FROM registry.access.redhat.com/ubi8/openjdk-11-runtime
COPY --from=builder /deployments /deployments

CMD ["java","-jar","/deployments/undertow-servlet.jar"]

Deployment

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: jar-multi-stage-example
spec:
  replicas: 1
  strategy:
    type: Recreate
  selector:
    matchLabels:
      app: jar-multi-stage
  template:
    metadata:
      labels:
        app: jar-multi-stage
    spec:
      containers:
      - name: app
        image: quay.io/openshift-examples/jar-deploy-example:multi-stage
  triggers:
  - type: ConfigChange
---
apiVersion: v1
kind: Service
metadata:
  labels:
    app: jar-multi-stage
  name: jar-multi-stage
spec:
  ports:
  - name: 8080-8080
    port: 8080
    protocol: TCP
    targetPort: 8080
  selector:
    app: jar-multi-stage
  sessionAffinity: None
  type: ClusterIP
---
apiVersion: route.openshift.io/v1
kind: Route
metadata:
  labels:
    app: jar-multi-stage
  name: jar-multi-stage
spec:
  port:
    targetPort: 8080
  to:
    kind: "Service"
    name: jar-multi-stage
    weight: null
1
oc apply -f https://examples.openshift.pub/deploy/jar/deployment-multi-stage.yaml

Unconventional: copy via initContainer into runtime

Build of initContainer image

Source & Containerfile is available at openshift-quickstarts/undertow-servlet

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
FROM registry.access.redhat.com/ubi8/openjdk-11:latest as builder
USER root
COPY ./ /tmp/src
RUN chown -R 185:0 /tmp/src
USER 185
RUN /usr/local/s2i/assemble

FROM registry.access.redhat.com/ubi8/ubi-micro
COPY --from=builder /deployments /deployments
CMD echo "Please don't start..."

Deployment

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: jar-init-container-example
spec:
  replicas: 1
  strategy:
    type: Recreate
  selector:
    matchLabels:
      app: jar-init-container
  template:
    metadata:
      labels:
        app: jar-init-container
    spec:
      volumes:
       - name: app
         emptyDir: {}
      initContainers:
      - name: copy-jar
        image: quay.io/openshift-examples/jar-deploy-example:initContainer
        volumeMounts:
          - name: app
            mountPath: /app
        command:
          - sh
          - -c
          - |
            cp -v /deployments/undertow-servlet.jar /app/undertow-servlet.jar

      containers:
      - name: app
        image: registry.access.redhat.com/ubi8/openjdk-11-runtime
        volumeMounts:
          - name: app
            mountPath: /app
        command:
          - java
          - -jar
          - /app/undertow-servlet.jar
  triggers:
  - type: ConfigChange
---
apiVersion: v1
kind: Service
metadata:
  labels:
    app: jar-init-container
  name: jar-init-container
spec:
  ports:
  - name: 8080-8080
    port: 8080
    protocol: TCP
    targetPort: 8080
  selector:
    app: jar-init-container
  sessionAffinity: None
  type: ClusterIP
---
apiVersion: route.openshift.io/v1
kind: Route
metadata:
  labels:
    app: jar-init-container
  name: jar-init-container
spec:
  port:
    targetPort: 8080
  to:
    kind: "Service"
    name: jar-init-container
    weight: null
1
oc apply -f https://examples.openshift.pub/deploy/jar/deployment-init-container.yaml

Last update: June 10, 2021