konveyor / move2kube

Move2Kube is a command-line tool for automating creation of Infrastructure as code (IaC) artifacts. It has inbuilt support for creating IaC artifacts for replatforming to Kubernetes/Openshift.
https://move2kube.konveyor.io/
Apache License 2.0
383 stars 118 forks source link

bug: Dockerfile that java-gradle transformer generates is broken #729

Closed HarikrishnanBalagopal closed 2 years ago

HarikrishnanBalagopal commented 2 years ago

Describe the bug There are some issues with the Dockerfile that is generated by the java-gradle transformer which causes the build to fail:

#   Copyright IBM Corporation 2021
#
#   Licensed under the Apache License, Version 2.0 (the "License");
#   you may not use this file except in compliance with the License.
#   You may obtain a copy of the License at
#
#        http://www.apache.org/licenses/LICENSE-2.0
#
#   Unless required by applicable law or agreed to in writing, software
#   distributed under the License is distributed on an "AS IS" BASIS,
#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#   See the License for the specific language governing permissions and
#   limitations under the License.

FROM registry.access.redhat.com/ubi8/ubi:latest AS builder
RUN yum install -y java-1.8.0-openjdk-devel
RUN yum install -y wget
RUN yum install -y unzip
RUN wget https://services.gradle.org/distributions/gradle-6.6-bin.zip -P /tmp
RUN unzip -d /opt/gradle /tmp/gradle-6.6-bin.zip
ENV PATH="$PATH:/opt/gradle/gradle-6.6/bin/"
COPY . /app
WORKDIR /app
RUN gradle build

FROM registry.access.redhat.com/ubi8/ubi-minimal:latest
ENV PORT 8080
RUN microdnf update \
   && microdnf install --nodocs \
      java-1.8.0-openjdk-devel \
   && microdnf clean all
COPY --from=builder /app/target/myproject-java-gradlejar .
EXPOSE 8080
CMD ["java", "-jar", "myproject-java-gradlejar"]

To Reproduce

  1. Run plan and transform on the language-platforms.zip

Expected behavior

  1. Remove the gradle install. This is taken care of by the gradlew wrapper script.
    RUN wget https://services.gradle.org/distributions/gradle-6.6-bin.zip -P /tmp
    RUN unzip -d /opt/gradle /tmp/gradle-6.6-bin.zip
    ENV PATH="$PATH:/opt/gradle/gradle-6.6/bin/"
  2. Related to 1. RUN gradle build should be RUN /gradlew build . gradlew is a script which downloads the correct version of gradle, caches it locally and runs the task (in this case build).
  3. COPY --from=builder /app/target/myproject-java-gradlejar . This is wrong because for webapps the output is a war file. This can be detected by looking for the war plugin in the build.gradle https://github.com/konveyor/move2kube-demos/blob/main/samples/language-platforms/java-gradle/build.gradle#L3
  4. Related to 3. The war file location is also incorrect. The default location is build/libs/<project-name>.war. The default archive name is <project-name>.war https://docs.gradle.org/current/dsl/org.gradle.api.tasks.bundling.War.html The project name is given in the settings.gradle file https://github.com/konveyor/move2kube-demos/blob/main/samples/language-platforms/java-gradle/settings.gradle#L1 The default filename can also be changed (in this case ROOT.war) https://github.com/konveyor/move2kube-demos/blob/main/samples/language-platforms/java-gradle/build.gradle#L13
  5. FROM registry.access.redhat.com/ubi8/ubi-minimal:latest The runner image does not have Tomcat or a similar web server to run the .war file that is generated.
  6. CMD ["java", "-jar", "myproject-java-gradlejar"] The final command to run is also wrong. It should start up the webserver. (startup.sh in case of Tomcat).
  7. RUN yum install -y java-1.8.0-openjdk-devel We can potentially use the latest JDK because Gradle allows you to select the exact version you want https://github.com/konveyor/move2kube-demos/blob/main/samples/language-platforms/java-gradle/build.gradle#L8 This has been tested and allows building for jdk 8 using jdk 17.

Working Dockerfile (build and deploy suceeds)

#   Copyright IBM Corporation 2021
#
#   Licensed under the Apache License, Version 2.0 (the "License");
#   you may not use this file except in compliance with the License.
#   You may obtain a copy of the License at
#
#        http://www.apache.org/licenses/LICENSE-2.0
#
#   Unless required by applicable law or agreed to in writing, software
#   distributed under the License is distributed on an "AS IS" BASIS,
#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#   See the License for the specific language governing permissions and
#   limitations under the License.

FROM registry.access.redhat.com/ubi8/ubi:latest AS builder
RUN yum install -y java-1.8.0-openjdk-devel
WORKDIR /app
COPY . .
RUN ./gradlew build

FROM tomcat:10-jdk8-corretto
COPY --from=builder /app/build/libs/ROOT.war /usr/local/tomcat/webapps
EXPOSE 8080
HarikrishnanBalagopal commented 2 years ago

The maven build fails for a similar reason

[+] Building 2.0s (17/17) FINISHED
=> [internal] load build definition from Dockerfile 0.0s
=> => transferring dockerfile: 1.57kB 0.0s
=> [internal] load .dockerignore 0.0s
=> => transferring context: 2B 0.0s
=> [internal] load metadata for registry.access.redhat.com/ubi8/ubi-minimal:latest 1.4s
=> [internal] load metadata for registry.access.redhat.com/ubi8/ubi:latest 1.9s
=> [builder 1/6] FROM registry.access.redhat.com/ubi8/ubi:latest@sha256:228824aa581f3b31bf79411f8448b798291c667a37155bdea61cfa128b2833f2 0.0s
=> [stage-1 1/6] FROM registry.access.redhat.com/ubi8/ubi-minimal:latest@sha256:cf1c63e3247e4074ee3549a064b8798a1a2513ad57dd79c9edb979836355b469 0.0s
=> [internal] load build context 0.0s
=> => transferring context: 4.31kB 0.0s
=> CACHED [stage-1 2/6] RUN microdnf update && microdnf install -y java-11-openjdk-devel wget tar && microdnf clean all 0.0s
=> CACHED [stage-1 3/6] WORKDIR /usr/local 0.0s
=> CACHED [stage-1 4/6] RUN wget https://downloads.apache.org/tomcat/tomcat-9/v9.0.56/bin/apache-tomcat-9.0.56.tar.gz && tar -zxf apache-tomcat-9.0.56.tar.gz && rm -f apache-tomcat-9.0.56.tar.gz && mv apache-tomcat-9.0.56 tomcat9 0.0s
=> CACHED [stage-1 5/6] RUN useradd -r tomcat && chown -R tomcat:tomcat tomcat9 0.0s
=> CACHED [builder 2/6] RUN yum install -y java-1.8.0-openjdk-devel 0.0s
=> CACHED [builder 3/6] RUN yum install -y maven 0.0s
=> CACHED [builder 4/6] COPY . /app 0.0s
=> CACHED [builder 5/6] WORKDIR /app 0.0s
=> CACHED [builder 6/6] RUN mvn package -Dmaven.test.skip 0.0s
=> ERROR [stage-1 6/6] COPY --chown=tomcat:tomcat --from=builder /app/target/simplewebapp-1.0.war /usr/local/tomcat9/webapps/ 0.0s
------
> [stage-1 6/6] COPY --chown=tomcat:tomcat --from=builder /app/target/simplewebapp-1.0.war /usr/local/tomcat9/webapps/:
------
failed to compute cache key: "/app/target/simplewebapp-1.0.war" not found: not found
/Users/user/Desktop/tutorial/myproject