docker-maven-plugin icon indicating copy to clipboard operation
docker-maven-plugin copied to clipboard

The plugin does not honor the JAVA_HOME I set in ENV directive of Dockfile

Open SgrAlpha opened this issue 5 years ago • 12 comments

Description

The plugin does not honor the JAVA_HOME I set in ENV directive of Dockfile, it replaces it to the JAVA_HOME I set on host machine, for example /Library/Java/JavaVirtualMachines/jdk1.8.0_192.jdk/Contents/Home

Info

  • d-m-p version : 0.31.0
  • Maven version (mvn -v) :
Maven home: /usr/local/Cellar/maven/3.6.2/libexec
Java version: 1.8.0_192, vendor: Oracle Corporation, runtime: /Library/Java/JavaVirtualMachines/jdk1.8.0_192.jdk/Contents/Home/jre
Default locale: en_US, platform encoding: UTF-8
OS name: "mac os x", version: "10.14.6", arch: "x86_64", family: "mac"
  • Docker version :
Client: Docker Engine - Community
 Version:           19.03.3
 API version:       1.40
 Go version:        go1.12.10
 Git commit:        a872fc2
 Built:             Tue Oct  8 00:55:12 2019
 OS/Arch:           darwin/amd64
 Experimental:      false

Server: Docker Engine - Community
 Engine:
  Version:          19.03.3
  API version:      1.40 (minimum version 1.12)
  Go version:       go1.12.10
  Git commit:       a872fc2
  Built:            Tue Oct  8 01:01:15 2019
  OS/Arch:          linux/amd64
  Experimental:     true
 containerd:
  Version:          v1.2.10
  GitCommit:        b34a5c8af56e510852c35414db4c1f4fa6172339
 runc:
  Version:          1.0.0-rc8+dev
  GitCommit:        3e425f80a8c931f88e6d94a8c831b9d5aa481657
 docker-init:
  Version:          0.18.0
  GitCommit:        fec3683
  • How to reproduce :
  1. Clone the sample project from https://github.com/SgrAlpha/dmp-bug.git
  2. Build it using mvn clean package
    • Expected: The build will success
    • Actually: The build failed. If you take a look at <repo>/target/docker/sgrio/sample_webapp/1.0.0-SNAPSHOT/build/Dockerfile, you will see around line 47 the JAVA_HOME variable has been incorrectly replaced to the JAVA_HOME on host machine, but it should use the JAVA_HOME defined here: https://github.com/SgrAlpha/dmp-bug/blob/master/src/main/docker/Dockerfile#L5

SgrAlpha avatar Nov 11 '19 03:11 SgrAlpha

Sorry, I can't reproduce the issue. The build succeeds for me.

The generated Dockerfile looks like this, the JAVA_HOME is not replaced for me.

I'm running this also on a Mac, so I'm not sure why it fails for you.

Dockerfile
FROM centos:7
MAINTAINER SgrAlpha <[email protected]>

ENV DEBIAN_FRONTEND=noninteractive \
    JAVA_HOME="/usr/lib/jvm/java" \
    CATALINA_HOME="/opt/apache-tomcat" \
    PATH="/opt/apache-tomcat/bin:$PATH"

EXPOSE 8005 8080 8009 8443 45564 4000

RUN TOMCAT_VERSION=7.0.88 && \
    APR_VERSION=1.7.0 && \
    TC_NATIVE_VERSION=1.2.23 && \
    yum update -y && \
    yum install -y \
        java-1.8.0-openjdk-devel && \
    curl --silent --location --retry 3 \
        http://archive.apache.org/dist/tomcat/tomcat-7/v${TOMCAT_VERSION}/bin/apache-tomcat-${TOMCAT_VERSION}.tar.gz \
        | tar xz -C /tmp && \
    mv /tmp/apache-tomcat-${TOMCAT_VERSION} /opt/apache-tomcat-${TOMCAT_VERSION} && \
    ln -s /opt/apache-tomcat-${TOMCAT_VERSION} ${CATALINA_HOME} && \
    rm -rf ${CATALINA_HOME}/bin/*.bat \
        ${CATALINA_HOME}/bin/*.exe \
        ${CATALINA_HOME}/bin/*.tar.gz \
        ${CATALINA_HOME}/bin/*.original \
        ${CATALINA_HOME}/conf/*.original \
        ${CATALINA_HOME}/logs/* \
        ${CATALINA_HOME}/temp/* \
        ${CATALINA_HOME}/webapps/* \
        ${CATALINA_HOME}/work/* && \
    yum install -y \
        gcc \
        make \
        openssl-devel && \
    curl --silent --location --retry 3 \
        http://archive.apache.org/dist/apr/apr-${APR_VERSION}.tar.gz \
        | tar xz -C /tmp && \
    cd /tmp/apr-${APR_VERSION} && \
        ./configure && \
        make clean && make && make install && \
    ln -s /usr/local/apr/lib/libapr-1.so.0 /usr/lib/libapr-1.so.0 && \
    ln -s /usr/local/apr/lib/libapr-1.so /usr/lib/libapr-1.so && \
    curl --silent --location --retry 3 \
        http://archive.apache.org/dist/tomcat/tomcat-connectors/native/${TC_NATIVE_VERSION}/source/tomcat-native-${TC_NATIVE_VERSION}-src.tar.gz \
        | tar xz -C /tmp && \
    cd /tmp/tomcat-native-${TC_NATIVE_VERSION}-src/native && \
        ./configure -with-apr=/usr/local/apr/ -with-ssl=yes -with-java-home=${JAVA_HOME} && \
        make clean && make && make install && \
    ln -s /usr/local/apr/lib/libtcnative-1.so.0 /usr/lib/libtcnative-1.so.0 && \
    ln -s /usr/local/apr/lib/libtcnative-1.so /usr/lib/libtcnative-1.so && \
    yum remove -y \
        gcc \
        make \
        openssl-devel && \
    yum autoremove -y && yum clean all && \
    rm -rf /var/cache/yum /tmp/* /var/tmp/* && \
    mkdir -p /billing/data/tomcat/logs /billing/config && \
    touch /billing/config/config-env.properties && \
    touch /billing/config/config-instance.properties && \
    rm -rf /opt/apache-tomcat/logs && ln -sf /billing/data/tomcat/logs /opt/apache-tomcat/logs

WORKDIR ${CATALINA_HOME}

CMD ${CATALINA_HOME}/bin/catalina.sh run

ADD ./tomcat/bin/setenv.sh /opt/apache-tomcat/bin/setenv.sh
ADD ./tomcat/conf/server.xml /opt/apache-tomcat/conf/server.xml
ADD ./tomcat/webapps/apps.war /opt/apache-tomcat/webapps/

rhuss avatar Nov 21 '19 08:11 rhuss

I can reproduce it on two different macOS (v10.14 and v10.15) machines with Maven 3.6.2 (installed via brew) and Oracle JDK 1.8. What's the Maven version in your runtime?

SgrAlpha avatar Nov 22 '19 01:11 SgrAlpha

Also I'm using bash.

SgrAlpha avatar Nov 22 '19 01:11 SgrAlpha

  • macOS 10.14.6 (18G1012)
  • zsh
mvn --version
Apache Maven 3.6.0 (97c98ec64a1fdfee7767ce5ffb20918da4f719f3; 2018-10-24T20:41:47+02:00)
Maven home: /Users/roland/.sdkman/candidates/maven/current
Java version: 11.0.1, vendor: Oracle Corporation, runtime: /Library/Java/JavaVirtualMachines/jdk-11.0.1.jdk/Contents/Home
Default locale: en_DE, platform encoding: UTF-8
OS name: "mac os x", version: "10.14.6", arch: "x86_64", family: "mac"

Interestingly, when I run mvn with Java 8 I get the error you are mentioning. So it's really Java 8 vs Java 11. Which again puzzles me a bit ;-) I have no idea yet Maven resolves ${JAVA_HOME} when copying over your Dockerfile to the target directory. Have to check, but at least we could reproduce the issue. which is good ;-)

rhuss avatar Nov 22 '19 09:11 rhuss

I didn't try Java 11 before, but I after tried I can still reproduce it. My gut tells me it should be related to Maven 3.6.2 or even 3.6.x instead of differences between Java versions.

SgrAlpha avatar Nov 22 '19 10:11 SgrAlpha

Yes, probably. How maybe just that I haven't set the JAVA_HOME variable locally 😬

It's just as easy as this: If you have JAVA_HOME set, its broken. If not, the sample works.

So the real problem is, that environment variables are interpolated into a given Dockerfile when copied over into the target directory. I will have to look closer whether this is intentional (e.g. to allow your Dockerfiles to be parameterized) or not.

rhuss avatar Nov 22 '19 12:11 rhuss

I found this in /usr/local/bin/mvn

#!/bin/bash
JAVA_HOME="${JAVA_HOME:-$(/usr/libexec/java_home)}" exec "/usr/local/Cellar/maven/3.6.2/libexec/bin/mvn" "$@"

and also in /usr/local/Cellar/maven/3.6.2/libexec/bin/mvn

# -----------------------------------------------------------------------------
# Apache Maven Startup Script
#
# Environment Variable Prerequisites
#
#   JAVA_HOME       Must point at your Java Development Kit installation.
#   MAVEN_OPTS      (Optional) Java runtime options used when Maven is executed.
#   MAVEN_SKIP_RC   (Optional) Flag to disable loading of mavenrc files.
# -----------------------------------------------------------------------------

It looks like JAVA_HOME is a must have env varible for Maven.

SgrAlpha avatar Nov 24 '19 10:11 SgrAlpha

Are there any update for that ticket? We have a similar problem, if we set an JAVA_HOME ENV in our Dockerfile and if we try to use it, we got that exeption:

[INFO] DOCKER> ---> Using cache
[INFO] DOCKER> ---> 350aea7640a1
[INFO] DOCKER> Step 60/101 : ENV local_java_home "/Library/Java/JavaVirtualMachines/adoptopenjdk-8.jdk/Contents/Home"
...
INFO] DOCKER> ---> Using cache
[INFO] DOCKER> ---> e497488c1665
[INFO] DOCKER> Step 67/101 : RUN ${local_java_home}/bin/keytool -export -alias s1as -keystore ${GLASSFISH_PATH}/domains/domain1/config/cacerts.jks -rfc -file ${GLASSFISH_PATH}/domains/domain1/config/s1as.cert
[INFO] DOCKER> 
[INFO] DOCKER> ---> Running in 6638a2f4a897
[INFO] DOCKER> [91m/bin/sh: /Library/Java/JavaVirtualMachines/adoptopenjdk-8.jdk/Contents/Home/bin/keytool: No such file or directory

[INFO] DOCKER> Removing intermediate container 6638a2f4a897
[ERROR] DOCKER> Unable to build image [edict/docker-edict-payara] : "The command '/bin/sh -c ${local_java_home}/bin/keytool -export -alias s1as -keystore ${GLASSFISH_PATH}/domains/domain1/config/cacerts.jks -rfc -file ${GLASSFISH_PATH}/domains/domain1/config/s1as.cert' returned a non-zero code: 127"  ["The command '/bin/sh -c ${local_java_home}/bin/keytool -export -alias s1as -keystore ${GLASSFISH_PATH}/domains/domain1/config/cacerts.jks -rfc -file ${GLASSFISH_PATH}/domains/domain1/config/s1as.cert' returned a non-zero code: 127" ]

sabf81 avatar Jun 22 '20 06:06 sabf81

We have found a soltuion for that:

  1. Parent Dockerfile now do that: RUN export JAVA_HOME
  2. Child Dockerfile now use it like that: ENV local_java_home "$JAVA_HOME" (before it was ENV local_java_home "${JAVA_HOME}" Now it works for us.

sabf81 avatar Jun 22 '20 09:06 sabf81

I can reproduce this :) woho happy happy joy joy. I see that the entire ENV PATH is replaced with my local path and that is not my expected behaviour, at least not when building from a Dockerfile :(

I would like this issue to be prioritized

paul8620 avatar Jun 27 '22 14:06 paul8620

@paul8620 : Would you like to work on this? I think maybe you can find code related to Dockerfile interpolation in DockerFileUtil

rohanKanojia avatar Jun 27 '22 16:06 rohanKanojia

Found the issue

public class BuildImageConfiguration implements Serializable {

    public static final String DEFAULT_FILTER = "${*}";

this is where the problem is if you have something like

ENV PATH ${JAVA_HOME}/bin:${PATH}

in your Dockerfile it will substitute the entire line with your PATH switched my ENV line to

ENV PATH $JAVA_HOME/bin:$PATH

and it seems to not do the substitution

@rohanKanojia thanks for suggesting me to fix it otherwise i wouldn't be able to find the issue. Though i still think the behaviour is buggy with the default filter being what it is

I would considering adding a new feature where the Dockerfile used is not rewritten that would be enabled by a property under

I really consider people using Dockerfiles to be knowledgeable people and also i totally understand the interpolation feature where you want to add build props in the Dockerfile but i believe there are one two many ways to do that.

The proper way to do it is by passing args to Dockerifle and that is less confusing to anyone.

I'll quit rambling since I fixed my issue thanks again

paul8620 avatar Jun 28 '22 06:06 paul8620