Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@LuanComputacao
Last active October 30, 2023 10:47
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save LuanComputacao/6cf199fc577f7ee5fb85a504176d78ca to your computer and use it in GitHub Desktop.
Save LuanComputacao/6cf199fc577f7ee5fb85a504176d78ca to your computer and use it in GitHub Desktop.
Dockerfile to install maven
#
# Oracle Java 7 Dockerfile
#
# https://github.com/dockerfile/java
# https://github.com/dockerfile/java/tree/master/oracle-java7
#
# Pull base image.
FROM alpine as build
ARG MAVEN_VERSION=3.6.3
ARG USER_HOME_DIR="/root"
ARG BASE_URL=https://apache.osuosl.org/maven/maven-3/${MAVEN_VERSION}/binaries
# Install Java.
RUN apk --update --no-cache add openjdk7 curl
RUN mkdir -p /usr/share/maven /usr/share/maven/ref \
&& curl -fsSL -o /tmp/apache-maven.tar.gz ${BASE_URL}/apache-maven-${MAVEN_VERSION}-bin.tar.gz \
&& tar -xzf /tmp/apache-maven.tar.gz -C /usr/share/maven --strip-components=1 \
&& rm -f /tmp/apache-maven.tar.gz \
&& ln -s /usr/share/maven/bin/mvn /usr/bin/mvn
ENV MAVEN_HOME /usr/share/maven
ENV MAVEN_CONFIG "$USER_HOME_DIR/.m2"
# Define working directory.
WORKDIR /data
# Define commonly used JAVA_HOME variable
ENV JAVA_HOME /usr/lib/jvm/default-jvm/
# Define default command.
CMD ["mvn", "--version"]
# Spring docker sample
# FROM openjdk:8-jdk-alpine
# VOLUME /tmp
# ARG JAR_FILE
# COPY ${JAR_FILE} app.jar
# ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]
@zerjioang
Copy link

@LuanComputacao @antonTroyan you need to update the version defined of MVN to at least 3.6.3. See available versions on remote at: https://apache.osuosl.org/maven/maven-3/

@LuanComputacao
Copy link
Author

Thanks, @zerjioang
I did the update!

@ludelaorden
Copy link

ludelaorden commented Jul 14, 2022

Hello, I'm trying to install java but from a databricks image. Do you know if this is posible? I got this error:

=> ERROR [4/9] RUN apk --update --no-cache add openjdk7 curl

[4/9] RUN apk --update --no-cache add openjdk7 curl:
#8 0.314 /bin/sh: 1: apk: not found
executor failed running [/bin/sh -c apk --update --no-cache add openjdk7 curl]: exit code: 127

and this is my code:
`FROM databricksruntime/standard

WORKDIR /app

COPY . .

ARG MAVEN_VERSION=3.6.3
ARG USER_HOME_DIR="/root"
ARG BASE_URL=https://apache.osuosl.org/maven/maven-3/${MAVEN_VERSION}/binaries
RUN apk --update --no-cache add openjdk7 curl

RUN mkdir -p /usr/share/maven /usr/share/maven/ref
&& curl -fsSL -o /tmp/apache-maven.tar.gz ${BASE_URL}/apache-maven-${MAVEN_VERSION}-bin.tar.gz
&& tar -xzf /tmp/apache-maven.tar.gz -C /usr/share/maven --strip-components=1
&& rm -f /tmp/apache-maven.tar.gz
&& ln -s /usr/share/maven/bin/mvn /usr/bin/mvn

ENV MAVEN_HOME /usr/share/maven
ENV MAVEN_CONFIG "$USER_HOME_DIR/.m2"

RUN mvn org.apache.maven.plugins:maven-dependency-plugin:2.1:get
-DrepoUrl=https://mvnrepository.com/artifact/com.crealytics/spark-excel_2.12/0.14.0
-Dartifact=com.crealytics:spark-excel_2.12:0.14.0 &&
mvn org.apache.maven.plugins:maven-dependency-plugin:2.1:get
-DrepoUrl=https://mvnrepository.com/artifact/mysql/mysql-connector-java
-Dartifact=mysql:mysql-connector-java:8.0.29

RUN apt-get update && apt-get install -y python3-pip

RUN sudo apt-get install -y libpq-dev

RUN pip install -r /app/requirements.txt

CMD ["python3"]`

@zerjioang
Copy link

zerjioang commented Oct 11, 2022 via email

@Haarolean
Copy link

This is an absolutely horrible solution to the problem. Once they delete the version on the remote you can't build the image anymore and are required to change it manually every time a new minor version is out.

@LuanComputacao
Copy link
Author

This is a sketch script. You can improve it using many techniques. I suggest you use env vars or a script that automates versioning as needed for your code base. And not least, be kind!
@Haarolean

@Haarolean
Copy link

@LuanComputacao sorry if I offended your sketch or you personally. Didn't seem sketchy enough for me, and considering that it's the core feature of this sketch, I find it lacking flexibility if I need to update my dockerfile every week or so.

@Haarolean
Copy link

For the ones not wishing to manually replace maven version in a dockerfile every time a new release is out (since the repo used in this dockerfile deletes the old packages once the new is out), here's my take on automatically downloading the latest stable maven version:
https://gist.github.com/Haarolean/a2a8acf07dfd60cbd68bfdfd20ca9195
Cheers

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment