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.
Related
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.
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
I am working in a Java spring boot application, running as a docker container.
The main purose of this application was to execute python scripts.
So inside the docker container, I had to make available python environment. I added the python runtime with this code.
But seems this is very basic python verson and I can not make other important libraries available.
Like, I wanted to add 'asyncpg' library so that I can use connection pool.
But it is not letting me to add asyncpg library.
Below is the docker file.
Note: I have commented '#FROM python:3.6-alpine', if I make this open then java runtime will not be available which is 'FROM openjdk:8u191-jre-alpine3.8'
------- Docker file --------------
*FROM openjdk:8u191-jre-alpine3.8
#FROM python:3.6-alpine
## Install bash
RUN apk add --no-cache curl tar bash
ENV APP_HOME /opt/app
# Create directory structure
RUN mkdir -p ${APP_HOME}/logs
RUN mkdir -p ${APP_HOME}/config
RUN mkdir -p ${APP_HOME}/libs
RUN mkdir -p ${APP_HOME}/scripts
# Add supporting script
ADD start.sh ${APP_HOME}/start.sh
RUN chmod +x ${APP_HOME}/start.sh
ADD wait-for-it.sh ${APP_HOME}/wait-for-it.sh
RUN chmod +x ${APP_HOME}/wait-for-it.sh
ADD load-ext-packages.sh ${APP_HOME}/load-ext-packages.sh
RUN chmod +x ${APP_HOME}/load-ext-packages.sh
## Add Spring Boot runnable jar
ADD *.jar ${APP_HOME}/
WORKDIR ${APP_HOME}
VOLUME [ "${APP_HOME}/logs" ]
VOLUME [ "${APP_HOME}/config" ]
VOLUME [ "${APP_HOME}/libs" ]
VOLUME [ "${APP_HOME}/scripts" ]
# Install Python
RUN apk add build-base
RUN apk add --update gcc
RUN apk --update add gcc build-base freetype-dev libpng-dev openblas-dev
RUN apk add --no-cache python3 python3-dev libevent-dev && \
python3 -m ensurepip && \
rm -r /usr/lib/python*/ensurepip && \
pip3 install --upgrade pip setuptools && \
pip3 install wheel && \
pip3 install --no-cache-dir asyncpg && \
if [ ! -e /usr/bin/pip ]; then ln -s pip3 /usr/bin/pip ; fi && \
if [[ ! -e /usr/bin/python ]]; then ln -sf /usr/bin/python3 /usr/bin/python; fi && \
rm -r /root/.cache
CMD ["./start.sh"]*
I have got one solution .. that at the first line of the docker file, I will first decleare the 'FROM alpine:3.7', and then inside the docker file I will keep adding the required runtimes and the dependent libraries accordingly. This way we can add mutiple runtimes.
Below the working docker compose where I have commented out both the lines for openjdk and python runtime and added FROM alpine:3.7:
Also some trick to add asyncpg library to the python runtime. This way I can now add any dependencies.
I can now work on asyncpg.
-------- Docker file ----------------
*#FROM openjdk:8u191-jre-alpine3.8
#FROM python:3.6-alpine
FROM alpine:3.7
## Install bash
RUN apk add --no-cache curl tar bash
ENV APP_HOME /opt/app
# Create directory structure
RUN mkdir -p ${APP_HOME}/logs
RUN mkdir -p ${APP_HOME}/config
RUN mkdir -p ${APP_HOME}/libs
RUN mkdir -p ${APP_HOME}/scripts
# Add supporting script
ADD start.sh ${APP_HOME}/start.sh
RUN chmod +x ${APP_HOME}/start.sh
ADD wait-for-it.sh ${APP_HOME}/wait-for-it.sh
RUN chmod +x ${APP_HOME}/wait-for-it.sh
ADD load-ext-packages.sh ${APP_HOME}/load-ext-packages.sh
RUN chmod +x ${APP_HOME}/load-ext-packages.sh
## Add Spring Boot runnable jar
ADD *.jar ${APP_HOME}/
WORKDIR ${APP_HOME}
VOLUME [ "${APP_HOME}/logs" ]
VOLUME [ "${APP_HOME}/config" ]
VOLUME [ "${APP_HOME}/libs" ]
VOLUME [ "${APP_HOME}/scripts" ]
RUN apk add build-base --no-cache python3 python3-dev && \
python3 -m ensurepip && \
rm -r /usr/lib/python*/ensurepip && \
pip3 install --upgrade pip setuptools && \
pip3 install --no-cache-dir asyncpg && \
if [ ! -e /usr/bin/pip ]; then ln -s pip3 /usr/bin/pip ; fi && \
if [[ ! -e /usr/bin/python ]]; then ln -sf /usr/bin/python3 /usr/bin/python; fi && \
rm -r /root/.cache
RUN apk update \
&& apk upgrade \
&& apk add --no-cache bash \
&& apk add --no-cache --virtual=build-dependencies unzip \
&& apk add --no-cache curl \
&& apk add --no-cache openjdk8-jre
CMD ["./start.sh"]*
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
I want to create and execute performance tests against the application. For now, my idea was to use multi-stage build on the first stage to build the application, on the second - build the performance testing and start both application and performance tests in the same container.
My Dockerfile looks like this
# build stage
FROM gradle:jdk11
ARG ARTIFACT_PATH=json-comparison-application/build/libs
ARG ARTIFACT_NEW=app.jar
ARG ARTIFACT_OLD=json-comparison-application-0.0.1-SNAPSHOT.jar
RUN echo ${ARTIFACT_PATH}
RUN apt-get install git && git clone https://github.com/xxxx/json-comparison.git
WORKDIR json-comparison
RUN chmod +x gradlew && \
./gradlew clean build -x pmdMain -x spotbugsMain -x checkstyleMain --no-daemon && \
cd ${ARTIFACT_PATH} && mv ${ARTIFACT_OLD} ${ARTIFACT_NEW}
# performance test stage
FROM ubuntu:18.04
# simplified adoptopenjdk/11 without CMD entry point, probably better move to some external dockerfile
ARG ESUM='6dd0c9c8a740e6c19149e98034fba8e368fd9aa16ab417aa636854d40db1a161'
ARG BINARY_URL='https://github.com/AdoptOpenJDK/openjdk11-binaries/releases/download/jdk-11.0.5%2B10/OpenJDK11U-jdk_x64_linux_hotspot_11.0.5_10.tar.gz'
ENV LANG='en_US.UTF-8' LANGUAGE='en_US:en' LC_ALL='en_US.UTF-8'
ENV JAVA_VERSION jdk-11.0.5+10
RUN apt-get update \
&& apt-get install -y --no-install-recommends curl ca-certificates fontconfig locales \
&& echo "en_US.UTF-8 UTF-8" >> /etc/locale.gen \
&& locale-gen en_US.UTF-8 \
&& rm -rf /var/lib/apt/lists/*
RUN set -eux; \
curl -LfsSo /tmp/openjdk.tar.gz ${BINARY_URL}; \
echo "${ESUM} */tmp/openjdk.tar.gz" | sha256sum -c -; \
mkdir -p /opt/java/openjdk; \
cd /opt/java/openjdk; \
tar -xf /tmp/openjdk.tar.gz --strip-components=1; \
rm -rf /tmp/openjdk.tar.gz;
ENV JAVA_HOME=/opt/java/openjdk \
PATH="/opt/java/openjdk/bin:$PATH"
# custom part of the stage
ARG DEBIAN_FRONTEND=noninteractive
RUN apt-get update && apt-get install -y python default-jre-headless python-tk python-pip python-dev \
libxml2-dev libxslt-dev zlib1g-dev net-tools && \
pip install bzt
WORKDIR /home/tmp
ARG ARTIFACT_PATH=json-comparison/json-comparison-application/build/libs
ARG ARTIFACT_NEW=app.jar
RUN echo ${ARTIFACT_PATH}
COPY --from=0 /${ARTIFACT_PATH}/${ARTIFACT_NEW} .
# prototype for test
CMD ["bzt", "quick_test.yml"]
But it fails during build with error
Step 23/24 : COPY --from=0 /${ARTIFACT_PATH}/${ARTIFACT_NEW} .
COPY failed: stat /var/lib/docker/overlay2/f227e7b77fba105ba0769aa355458900d202add59c98583f0fd0936cbf4dfc11/merged/json-comparison/json-comparison-application/build/libs/app.jar: no such file or directory
What is the problem?
At first sight the problem comes from the (absolute vs. relative) paths of the .jar in your two stages.
Can you verify that they are the same?
For example, you may replace the last command of the first stage with
RUN […] && \
cd ${ARTIFACT_PATH} && mv ${ARTIFACT_OLD} ${ARTIFACT_NEW} && readlink -f ${ARTIFACT_NEW}
and relaunch the build. If you don't obtain
/json-comparison/json-comparison-application/build/libs/app.jar
but a longer path, then you'll know the correct path to put in ARTIFACT_PATH within the second stage…
Alternatively, you could just get rid of the relative path in the first stage and replace WORKDIR json-comparison with:
WORKDIR /json-comparison
As an aside, it can be useful to name your build stages in the following way:
FROM gradle:jdk11 as builder
[…]
FROM ubuntu:18.04 as test
[…]
Then, this allows you to only build the first stage by running:
$ docker build --target builder -t $IMAGE_NAME .