How to install openjdk on seleniarm/standalone-chromium (debian) - java

I need to run both selenium and OpenJdk. I have a current Dockerfile that does work on M1 Mac which as the ARM architecture, (below). But the JDK version that it uses is 11.0.14+9-post-Debian-1deb11u1:
FROM seleniarm/standalone-chromium:101.0.4951.41-chromedriver-101.0.4951.41-20220429
ENV CHROMEDRIVER_PORT 4444
ENV CHROMEDRIVER_WHITELISTED_IPS "127.0.0.1"
ENV CHROMEDRIVER_URL_BASE ''
EXPOSE 4444
EXPOSE 8080
EXPOSE 5005
ARG JAR_FILE=target/*.jar
COPY ${JAR_FILE} app.jar
ENTRYPOINT ["java","-jar", "-Xmx600m","/app.jar"]
My prior Dockerfile is:
FROM maven:3.6.3-openjdk-15
# Google Chrome
ARG CHROME_VERSION=96.0.4664.45-1
ADD google-chrome.repo /etc/yum.repos.d/google-chrome.repo
RUN microdnf install -y google-chrome-stable-$CHROME_VERSION \
&& sed -i 's/"$HERE\/chrome"/"$HERE\/chrome" --no-sandbox/g' /opt/google/chrome/google-chrome
## ChromeDriver
ARG CHROME_DRIVER_VERSION=96.0.4664.45
RUN microdnf install -y unzip \
&& curl -s -o /tmp/chromedriver.zip https://chromedriver.storage.googleapis.com/$CHROME_DRIVER_VERSION/chromedriver_linux64.zip \
&& unzip /tmp/chromedriver.zip -d /opt \
&& rm /tmp/chromedriver.zip \
&& mv /opt/chromedriver /opt/chromedriver-$CHROME_DRIVER_VERSION \
&& chmod 755 /opt/chromedriver-$CHROME_DRIVER_VERSION \
&& ln -s /opt/chromedriver-$CHROME_DRIVER_VERSION /usr/bin/chromedriver
ENV CHROMEDRIVER_PORT 4444
ENV CHROMEDRIVER_WHITELISTED_IPS "127.0.0.1"
ENV CHROMEDRIVER_URL_BASE ''
EXPOSE 4444
EXPOSE 8080
EXPOSE 5005
ARG JAR_FILE=target/*.jar
COPY ${JAR_FILE} app.jar
ENTRYPOINT ["java","-jar", "-Xmx600m","/app.jar"]
Note that FROM maven:3.6.3-openjdk-15 naturally says the jdk version and allows that. But I do not know how to achieve this with standalone-chromium. Does anyone know how to install openjdk (in Docker) with a given version in this case?

I'm not exactly sure what your goal is, whether you're intending to run a Selenium server alongside some other JAR file or if you're just looking to use some of the components from seleniarm/standalone-chromium. I assume the latter, since ENTRYPOINT appears to override seleniarm/standalone-chromium's call to /opt/entry-point.sh.
Regardless of the specific use-case, here's what you can do to install a different OpenJDK:
FROM seleniarm/standalone-chromium:latest
USER root
COPY openjdk-15.0.2_linux-aarch64_bin.tar.gz /opt
RUN mkdir -p /opt/jdk \
# && curl -L https://download.java.net/java/GA/jdk15.0.2/0d1cfde4252546c6931946de8db48ee2/7/GPL/openjdk-15.0.2_linux-aarch64_bin.tar.gz -o /opt/openjdk-15.0.2_linux-aarch64_bin.tar.gz \
&& cd /opt/jdk && tar xvfz /opt/openjdk-15.0.2_linux-aarch64_bin.tar.gz \
&& export PATH=/opt/jdk/jdk-15.0.2/bin:$PATH \
#&& echo "export PATH=/opt/jdk/jdk-15.0.2/bin:$PATH" >> /home/seluser/.bashrc \
#&& echo "export PATH=/opt/jdk/jdk-15.0.2/bin:$PATH" >> /root/.bashrc \
&& rm /usr/bin/java \
&& ln -s /opt/jdk/jdk-15.0.2/bin/java /usr/bin/java \
&& java -version
ENV CHROMEDRIVER_PORT 4444
ENV CHROMEDRIVER_WHITELISTED_IPS "127.0.0.1"
ENV CHROMEDRIVER_URL_BASE ''
EXPOSE 4444
EXPOSE 8080
EXPOSE 5005
USER 1200
#ARG JAR_FILE=target/*.jar
#COPY ${JAR_FILE} app.jar
ENTRYPOINT ["java","-version"]
Then build the container image:
$ docker build -t local/jdk15:latest .
And then run it and get the java version output:
$ docker run --rm -it local/jdk15:latest
openjdk version "15.0.2" 2021-01-19
OpenJDK Runtime Environment (build 15.0.2+7-27)
OpenJDK 64-Bit Server VM (build 15.0.2+7-27, mixed mode)
I left some of the commented script to show what didn't work. Setting the PATH in the .bashrc worked when bashing into the container, but it didn't work with ENTRYPOINT. Instead, creating a symlink to the /usr/bin/java location resolved the issues. Note that this replaces the default Java installation launcher, and this should not be a problem within a container image.
I also copied a downloaded JDK tarball while debugging as this is faster than downloading the JDK each time we rebuild. In production, just comment out the COPY command and uncomment the curl line, which downloads OpenJDK 15 and places it in the /opt folder.
You can also uncomment the commands that copy your JAR file to the container image. I didn't have a JAR file to test with, but this should help get you started. Hope this helps.

Related

Error: Could not find or load main class in Docker

I try to containerized my selenium code. In order to do the same I created a jar of my code that contains the .class file and dependencies.
My Dockerfile is
FROM ubuntu:18.
FROM openjdk:8u191-jre-alpine3.8
RUN apk add --no-cache curl tar bash procps
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 openjdk8 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"
ENV JAVA_HOME /usr/lib/jvm/java-1.8-openjdk
ENV CLASSPATH /usr/lib/jvm/java-1.8-openjdk/lib
# Define default command.
CMD ["mvn", "--version"]
WORKDIR E:/Advanced_Selenium_Test_Automation_Framework-master
COPY target/AdavencedLevel.QDPM-0.0.1-SNAPSHOT-jar-with-dependencies.jar AdavencedLevel.QDPM-0.0.1-SNAPSHOT-jar-with-dependencies.jar
ENTRYPOINT ["java","-jar","/E:/Advanced_Selenium_Test_Automation_Framework-master/AdavencedLevel.QDPM-0.0.1-SNAPSHOT-jar-with-dependencies.jar"]
after running the docker image getting Error:
Error: Could not find or load main class TestRunner
I try to set ENV through Dockerfile to avoid this issue
I am expecting that as jdk is already present in the Image then that issue should not occur.

Docker how to recompile jar file with maven

I am new to docker, and I am trying to setup docker for spring boot project.
Here is my Dockerfile
FROM maven:3.6.3-jdk-11-slim AS build
WORKDIR usr/src/springboot
COPY . ./
RUN mvn install
RUN mvn clean package
#
# Package stage
#
FROM openjdk:11-jre-slim
ARG JAR_NAME="springboot-0.0.1-SNAPSHOT"
WORKDIR /usr/src/springboot
EXPOSE 8080
COPY --from=build /usr/src/springboot/target/${JAR_NAME}.jar ./springboot.jar
CMD ["java","-jar", "./springboot.jar"]
Which works completely fine and I can access hello world from localhost:8080
But my confusion is how to make any changes in java file reflect in the docker container? how do I recompile the .jar file.
I tried something like docker exec -it strange_shaw "mvn clean package"
But it throws error exec: "mvn clean package": executable file not found in $PATH: unknown
when you are using double FROM instruction inside your Dockerfile , Docker will keep only the latest FROM and use the previous FROM to build the next one.
So it is simple to find this error because FROM maven:3.6.3-jdk-11-slim AS build only used to build the next step FROM openjdk:11-jre-slim and will be removed from the final image(this strategy used to minimize the docker image size).
I hope that give you a clear idea about Dockerfile with multiple stage.
Build your jar outside after your Dockerfile will be like that:
FROM openjdk:11-jre-slim
ARG JAR_NAME="springboot-0.0.1-SNAPSHOT"
WORKDIR /usr/src/springboot
EXPOSE 8080
COPY /usr/src/springboot/target/${JAR_NAME}.jar ./springboot.jar
CMD ["java","-jar", "./springboot.jar"]
The issue was that maven was not installed within the container. So I changed my Dockerfile to
FROM openjdk:11
ARG JAR_NAME="springboot-0.0.1-SNAPSHOT"
WORKDIR /usr/src/springboot
EXPOSE 8080
#COPY --from=build /usr/src/springboot/target/${JAR_NAME}.jar ./springboot.jar
#CMD ["java","-jar", "./springboot.jar"]
RUN apt-get update; apt-get install curl -y
ARG MAVEN_VERSION=3.6.3
# 2- Define a constant with the working directory
ARG USER_HOME_DIR="/root"
# 4- Define the URL where maven can be downloaded from
ARG BASE_URL=https://apache.osuosl.org/maven/maven-3/${MAVEN_VERSION}/binaries
# 5- Create the directories, download maven, validate the download, install it, remove downloaded file and set links
RUN mkdir -p /usr/share/maven /usr/share/maven/ref \
&& echo "Downloading maven" \
&& curl -fsSL -o /tmp/apache-maven.tar.gz ${BASE_URL}/apache-maven-${MAVEN_VERSION}-bin.tar.gz \
\
&& echo "Unziping maven" \
&& tar -xzf /tmp/apache-maven.tar.gz -C /usr/share/maven --strip-components=1 \
\
&& echo "Cleaning and setting links" \
&& rm -f /tmp/apache-maven.tar.gz \
&& ln -s /usr/share/maven/bin/mvn /usr/bin/mvn
# 6- Define environmental variables required by Maven, like Maven_Home directory and where the maven repo is located
ENV MAVEN_HOME /usr/share/maven
ENV MAVEN_CONFIG "$USER_HOME_DIR/.m2"
This will install maven inside the container and set $PATH
docker run -it -v "$(pwd)":/usr/src/springboot -p 8080:8080 spring-boot-app
Run above command to start container and map your project folder to container folder and local port to container port. So both are in sync if you do any changes in project.
docker exec -it <container_name> mvn verify
Above command will create the .jar file. Restart the container.
docker exec -it <container_name> java -jar target/<file_name>.jar
Will execute the jar file in port 8080 and will be accessible in your localhost:8080

How to install java in container while writing it in Dockerfile?

I am naive in software development. I want to run a jar file from Dkron Scheduler using cron job. I am running dkron in docker(using docker-compose up). I am passing "command": "java --version" to see if I can run java from Dkron. As docker do not have java installed I changed the dockerfile.hub file to this:
FROM alpine
LABEL maintainer="Victor Castell <victor#victorcastell.com>"
RUN set -x \
&& buildDeps='bash ca-certificates openssl tzdata' \
&& apk add --update $buildDeps \
&& apk add openjava8 #add this line to install java
&& rm -rf /var/cache/apk/* \
&& mkdir -p /opt/local/dkron
ENV JAVA_HOME /usr/lib/jvm/java-1.8-openjdk #add this line to install java
ENV PATH $PATH:$JAVA_HOME/bin #add this line to install java
EXPOSE 8080 8946
ENV SHELL /bin/bash
WORKDIR /opt/local/dkron
COPY dkron .
COPY dkron-* ./
ENTRYPOINT ["/opt/local/dkron/dkron"]
CMD ["--help"]
When I again do docker-compose up it do not give any error, still on passing "command": "java --version" by json through UI, dkron shows error - exec : "java" : executable file not found in $PATH.
What can I do to resolve it?
Thanks in advance.
I was able to create JVM in docker container using dkron as the base image, (publically available) and build another image on top of it.
Here is dockerfile I created for running java application
FROM dkron/dkron
WORKDIR /root/hello-world
COPY hello.java /root/hello-world
RUN apk add openjdk8
ENV JAVA_HOME /usr/lib/jvm/java-1.8-openjdk
ENV PATH $JAVA_HOME/bin:$PATH
RUN javac hello.java
Then I build image. Let id be xxx
Then I ran the image and build dkron server as
docker run -p 8080:8080 xxx agent --server --bootstrap-expect=1 --node-name=node1
Try this file
FROM alpine
LABEL maintainer="Victor Castell <victor#victorcastell.com>"
RUN set -x \
&& buildDeps='bash ca-certificates openssl tzdata' \
&& apk add --update $buildDeps \
&& apk add openjdk8 \
&& rm -rf /var/cache/apk/* \
&& mkdir -p /opt/local/dkron
ENV JAVA_HOME=/usr/lib/jvm/java-1.8-openjdk
ENV PATH="$JAVA_HOME/bin:${PATH}"
EXPOSE 8080 8946
ENV SHELL /bin/bash
WORKDIR /opt/local/dkron
COPY dkron .
COPY dkron-* ./
ENTRYPOINT ["/opt/local/dkron/dkron"]
CMD ["--help"]
alpine package doesn't have openjava8 packages.
Edit: Update ENV variables

How can I set up and run a very simple php-apache server in a docker container that's already using another image?

I'm tasked with creating a very simple, web browser accessible gui that can run a specific java file within a docker container. To do this I've chosen to set up a php-apache server that serves an index.php document with the gui. The Dockerfile looks like this:
FROM php:7.0-apache
COPY src /var/www/html
EXPOSE 80
This gets the gui (index.php is inside the src folder) I've written up and running no problem, but it cannot access and run the required java files (obviously, since this creates a separate container).
The Question:
How can I set up a php-apache server inside the existing Dockerfile (provided below) doing the same thing as the Dockerfile above? My aim is to run the java file using php scripts and display the result to the user.
FROM openjdk:8-jre-slim
WORKDIR /usr/src/app
COPY ["./build/libs/*.jar", "./fooBar.jar"]
ENV JAVA_OPTS=${FOO_JAVA_OPTS}
CMD ["/usr/bin/tail", "-f", "/dev/null"]
I have not written the java file myself, only being tasked with running specific commands using it.
As it is Debian based images. one way of doing it, install packages in the container and create the new images from that.
root#310c94d8d75f:/usr/src/app# cat /etc/os-release
PRETTY_NAME="Debian GNU/Linux 9 (stretch)"
NAME="Debian GNU/Linux"
VERSION_ID="9"
VERSION="9 (stretch)"
2: root#310c94d8d75f:/usr/src/app# apt update
3- root#310c94d8d75f:/usr/src/app# apt install apache2
4- root#310c94d8d75f:/usr/src/app# apt install php
finally run : docker commit
after this, you will get a new image with the mentioned name.
Ref: https://docs.docker.com/engine/reference/commandline/commit/
2: you can add the same command in Dockerfile and rebuild.
FROM openjdk:8-jre-slim
WORKDIR /usr/src/app
COPY ["./build/libs/*.jar", "./fooBar.jar"]
ENV JAVA_OPTS=${FOO_JAVA_OPTS}
CMD ["/usr/bin/tail", "-f", "/dev/null"]
RUN apt update && apt install apache2 -y && apt install php -y
There appears to be no easy way of merging images like I initially hoped (You cannot have multiple FROM statements in your Dockerfile). What I eventually ended up doing was to manually merge the two images (openjdk and php) into something like this:
FROM php:7.0-apache
ENV LANG C.UTF-8
RUN { \
echo '#!/bin/sh'; \
echo 'set -e'; \
echo; \
echo 'dirname "$(dirname "$(readlink -f "$(which javac || which java)")")"'; \
} > /usr/local/bin/docker-java-home \
&& chmod +x /usr/local/bin/docker-java-home
RUN ln -svT "/usr/lib/jvm/java-8-openjdk-$(dpkg --print-architecture)" /docker-java-home
ENV JAVA_HOME /docker-java-home/jre
ENV JAVA_VERSION 8u212
ENV JAVA_DEBIAN_VERSION 8u212-b01-1~deb9u1
RUN set -ex; \
if [ ! -d /usr/share/man/man1 ]; then \
mkdir -p /usr/share/man/man1; \
fi; \
apt-get update; \
apt-get install -y --no-install-recommends openjdk-8-jre-headless="$JAVA_DEBIAN_VERSION"; \
rm -rf /var/lib/apt/lists/*; \
[ "$(readlink -f "$JAVA_HOME")" = "$(docker-java-home)" ]; \
update-alternatives --get-selections | awk -v home="$(readlink -f "$JAVA_HOME")" 'index($3, home) == 1 { $2 = "manual"; print | "update-alternatives --set-selections" }'; \
update-alternatives --query java | grep -q 'Status: manual'
COPY ["./build/libs/*.jar", "./FooBar.jar"]
ENV JAVA_OPTS=${FOO_JAVA_OPTS}
COPY gui/src /var/www/html
EXPOSE 80
Both are Debian based images so the merging were relatively easy (I also removed much of the cluttering comments from the original image source) and since the openjdk image were simpler, I added it on top of the php image instead of the other way around.

Docker alpine + oracle java: cannot find java

I've been trying to create an alpine-based docker image with Oracle Java (rather than openjdk). I have been specifically asked to create our own image here.
This is the Dockerfile I've come up with:
FROM alpine:3.6
RUN apk add --no-cache curl wget
RUN mkdir /opt/ && \
wget -c --header "Cookie: oraclelicense=accept-securebackup-cookie"\
http://download.oracle.com/otn-pub/java/jdk/8u131-b11/d54c1d3a095b4ff2b6607d096fa80163/jdk-8u131-linux-x64.tar.gz && \
tar xvf jdk-8u131-linux-x64.tar.gz -C /opt/ && \
rm jdk-8u131-linux-x64.tar.gz && \
ln -s /opt/jdk1.8.0_131 /opt/jdk
ENV JAVA_HOME /opt/jdk
ENV PATH $PATH:/opt/jdk/bin
RUN echo $JAVA_HOME && \
echo $PATH
RUN which java
RUN java -version
There are some unnecessary commands (like echoing the JAVA_HOME dir) which were added to help with debugging, but now I'm stuck: RUN which java returns /opt/jdk/bin/java as expected, but RUN java -version returns /bin/sh: java: not found.
I've tried a few things, including symlinking the executable(s) into /usr/bin, to no avail.
What am I missing?
EDIT:
Final output from docker is:
The command '/bin/sh -c java -version' returned a non-zero code: 127
Final edit:
Thanks to diginoise for putting me on to MUSL vs libc. I found adding the following to my Dockerfile allowed me to build a working image:
RUN apk --no-cache add ca-certificates && \
wget -q -O /etc/apk/keys/sgerrand.rsa.pub https://raw.githubusercontent.com/sgerrand/alpine-pkg-glibc/master/sgerrand.rsa.pub && \
wget https://github.com/sgerrand/alpine-pkg-glibc/releases/download/2.25-r0/glibc-2.25-r0.apk && \
apk add glibc-2.25-r0.apk
Found at: https://github.com/sgerrand/alpine-pkg-glibc
You cannot achieve what you want
Alpine Linux uses MUSL as a Standard C library.
Oracle's Java for linux depends on GNU Standard C library (gclib).
Here is a bit more detailed info and official stance from Oracle on the topic
the JDK source code has not yet been ported to Alpine Linux, or more specifically, the musl C library. That is, it turns out that the thing about Alpine Linux that sticks out/is different from a JDK source code perspective is the C library.
The solution
If you looking for small Java Docker images, use OpenJDK ones.
openjdk:11-jre-slim image is only 77MB.
If you insist, on your head be it...
There is theoretical way, but it is not as trivial as you think.
You can find many examples of Alpine images running with OracleJDK like here or see expert's answer to this question as well.
They add the missing Standard GNU C library.
Be warned however...
All of these solutions could be in breach of Oracle's license agreement stating that the license is non-transferable, and the distributable is non-modifiable.
In the Dockerfiles you will find however:
Cookie: oraclelicense=accept-securebackup-cookie"
and many entries similar to
rm -rf ${JAVA_HOME}/*src.zip
For further details about legality of prepackaged Oracle's JRE or JDK Docker images see this article.
In case anyone needs fresh version of JDK I maintain Alpine Oracle JDK images https://hub.docker.com/r/expert/docker-java-minimal
Sources
https://github.com/unoexperto/docker-java-minimal/
I found an answer that works finally. All is needed is just the GlibC; huge thanks to S. Gerrand, this is such a big help.
Here's how to get the old JDK 8 running in Alpine 1.13:
FROM alpine:3.13
RUN apk --no-progress --purge --no-cache upgrade \
&& apk --no-progress --purge --no-cache add --upgrade \
curl \
wget \
openssh \
&& apk --no-progress --purge --no-cache upgrade \
&& rm -vrf /var/cache/apk/* \
&& curl --version
# Install vanilla GLibC: https://github.com/sgerrand/alpine-pkg-glibc
RUN curl -o /etc/apk/keys/sgerrand.rsa.pub https://alpine-pkgs.sgerrand.com/sgerrand.rsa.pub \
&& curl -LO https://github.com/sgerrand/alpine-pkg-glibc/releases/download/2.32-r0/glibc-2.32-r0.apk \
&& apk add glibc-2.32-r0.apk
RUN wget -c --header "Cookie: oraclelicense=accept-securebackup-cookie" \
http://download.oracle.com/otn-pub/java/jdk/8u131-b11/d54c1d3a095b4ff2b6607d096fa80163/jdk-8u131-linux-x64.tar.gz \
&& tar xvf jdk-8u131-linux-x64.tar.gz -C /opt \
&& rm jdk-8u131-linux-x64.tar.gz \
&& ln -s /opt/jdk1.8.0_131 /opt/jdk
ENV JAVA_HOME /opt/jdk
ENV PATH $PATH:/opt/jdk/bin
RUN echo $JAVA_HOME && \
echo $PATH
RUN which java
RUN java -version
ENTRYPOINT [ "java" ]
# To test run: docker run -t khalifahks/alpine-java -version
# docker export <container-id> | docker import - khalifahks/alpine-java:exported
# quick interative termnal: docker run -it --entrypoint=sh khalifahks/alpine-java sh

Categories

Resources