Installing Java on online Gitlab ci - java

Here is my .gitlab-ci.yml script.
before_script:
- uname -a
- apt-get install default-jre default-jdk openjdk-7-jre openjdk-7-jdk
- java -version
- export MODE="service"
- export PID_FOLDER="/var/run/gitlab-runner-test"
dev:
script:
- chmod +x gradlew
- ./gradlew assembleDebug
I am trying to run the script on gitlab.com page to compile a android project. I checked with some alterations to my script that there is no java installed on the ci
linux Linux runner-8a2f473d-project-881036-concurrent-0 4.5.0-coreos-r1 #2 SMP Thu May 5 07:27:26 UTC 2016 x86_64 GNU/Linux.
I tried installing java, just like a sample which was shown for ruby, but it does not work, and gives an Unable to locate package error.
I am not sure what should be the package as it seems like a ubuntu system, but the command which works on my ubuntu does not work here.
This is not a local installation.

I believe you should be able to use the image feature described here. I found success with the anapsix/alpine-java:jdk8 Docker image.
I am using my own Docker gitlab-runner with a custom Docker image as I need Maven with Oracle java. I'm not 100% on if the shared runner on gitlab.com allows you to use your own image.
My Dockerfile for that (which I upload to Gitlab and use their new Docker container register feature)
FROM anapsix/alpine-java:jdk8
ENV MAVEN_VERSION 3.3.3
RUN apk update && apk upgrade && apk add curl wget bash tar rsync openssh-client
RUN mkdir -p /usr/share/maven \
&& curl -fsSL http://apache.osuosl.org/maven/maven-3/$MAVEN_VERSION/binaries/apache-maven-$MAVEN_VERSION-bin.tar.gz \
| tar -xzC /usr/share/maven --strip-components=1 \
&& ln -s /usr/share/maven/bin/mvn /usr/bin/mvn \
&& rm -rf /tmp/* /var/cache/apk/*;
ENV MAVEN_HOME /usr/share/maven
ENTRYPOINT []
CMD ["bash"]

Related

The command `bin/sh` returned a non-zero code: 100

So I am trying to install OpenJDK in a Dockerfile but I am having issues. It always errors with the following message: Sub-process /usr/bin/dpkg returned an error code (1) and then underneath The command bin/sh returned a non-zero code: 100. This is the command that failed to execute. Currently on Ubuntu 20.04 VM
FROM mcr.microsoft.com/dotnet/sdk:5.0 AS build-env
WORKDIR /app
# Copy csproj and restore as distinct layers
COPY Folder/*.csproj ./
RUN dotnet restore
# Copy everything else and build
COPY . ./
RUN dotnet build -c Release -o out
# Build runtime image
FROM mcr.microsoft.com/dotnet/runtime:5.0
# Install OpenJDK-14
RUN apt-get update && \
apt-get install -y default-jdk && \
apt-get install -y ant && \
apt-get clean;
# Fix certificate issues
RUN apt-get update && \
apt-get install ca-certificates-java && \
apt-get clean && \
update-ca-certificates -f;
# Setup JAVA_HOME -- useful for docker commandline
ENV JAVA_HOME /usr/lib/jvm/default/
RUN export JAVA_HOME
RUN apt-get install -y supervisor # Installing supervisord
ADD supervisord.conf /etc/supervisor/conf.d/supervisord.conf
WORKDIR /app
COPY Folder/Lavalink/* ./
COPY --from=build-env /app/out .
#ENTRYPOINT ["dotnet", "Application.dll"]
ENTRYPOINT ["/usr/bin/supervisord"]
Here is the supervisord as well
[supervisord]
nodaemon=true
[program:folder]
command=dotnet /app/Application.dll
[program:lavalink]
command=java -jar /app/Lavalink.jar
This is a Visual Studio project written in 5.0 with a .jar file that needs to be executed.
These didn't seem to help:
apt-get update' returned a non-zero code: 100, Docker File Non-Zero Code 100 Error When Building Basically what I am trying to achieve is to install java within a container. Preferably java 13 but this issue prevents me from doing so. Last, it is important to let you know that the same commands works on another container.
Add this before installing the jdk :
RUN mkdir -p /usr/share/man/man1/
This is a problem in the debian slim images and this image is based on buster-slim. Alternatively you can try to use one of the dotnet/runtime images based on Ubuntu (5.0-focal) or Alpine (5.0-alpine).

How to install java in an airflow container using docker-compose.yaml

I am using this docker-compose.yaml file to run airflow on docker container.
https://airflow.apache.org/docs/apache-airflow/2.0.2/docker-compose.yaml
I need to install JRE in one of the containers. How do I add instruction to add java to the docker-compose.yaml file?
Try the following:
Create the following Dockerfile in the directory where you also have the docker-compose.yml:
FROM apache/airflow:2.0.2
USER root
# Install OpenJDK-11
RUN apt update && \
apt-get install -y openjdk-11-jdk && \
apt-get install -y ant && \
apt-get clean;
# Set JAVA_HOME
ENV JAVA_HOME /usr/lib/jvm/java-11-openjdk-amd64/
RUN export JAVA_HOME
USER airflow
WORKDIR /app
COPY requirements.txt /app
RUN pip install --trusted-host pypi.python.org -r requirements.txt
In order to install packages via apt, you have to switch to root user. Later, we switch back to user airflow. The specification of the working directory and the installation of python packages through requirements.txt might not apply, depending on your case.
Then, in your docker-compose.yml, add build: . after &airflow-common.
Finally, build your pipeline using docker-compose up -d --build.
For more information, look here: https://airflow.apache.org/docs/docker-stack/build.html#building-the-image
You have to build your own airflow image.
In your Dockerfile you have to apt install the airflow and export JAVA_HOME.
Dockerfile example:
FROM apache/airflow:2.5.1
USER root
RUN apt-get update \
&& apt-get install -y --no-install-recommends \
openjdk-11-jre-headless \
&& apt-get autoremove -yqq --purge \
&& apt-get clean \
&& rm -rf /var/lib/apt/lists/*
USER airflow
ENV JAVA_HOME=/usr/lib/jvm/java-11-openjdk-amd64
RUN pip install --no-cache-dir apache-airflow-providers-apache-spark==2.1.3
Build command example:
docker build -t airflow-with-java .
After successfully image building edit your docker-compose by replacing airflow image:
x-airflow-common:
&airflow-common
image: ${AIRFLOW_IMAGE_NAME:-airflow-with-java}
More information you can look here: https://airflow.apache.org/docs/docker-stack/build.html

Run gauge tests inside a docker container

I'm trying to Dockerize a Gauge test automation project so I can run specs inside a Docker container. The project is written in Java and Spring Boot.
I saw this tutorial in Gauge documentation.
This is the DockerFile in the tutorial:
FROM ubuntu
# Install Java.
RUN apt-get update && apt-get install -q -y \
openjdk-8-jdk \
apt-transport-https \
gnupg2 \
ca-certificates
# Install gauge
RUN apt-key adv --keyserver hkp://ipv4.pool.sks-keyservers.net --recv-keys 023EDB0B && \
echo deb https://dl.bintray.com/gauge/gauge-deb stable main | tee -a /etc/apt/sources.list
RUN apt-get update && apt-get install gauge
# Install gauge plugins
RUN gauge install java && \
gauge install screenshot
ENV PATH=$HOME/.gauge:$PATH
As you see, there's no "ADD"/"COPY" there in the DokcerFile.
Is it just suggesting an alternative to install Gauge and the other packages on the host?
Any ideas on how to run the specs inside a Docker container?
Here is what I did to get the test running in the docker container.
I have a specs folder beside src in my project structure meaning the gauge tests will run using the JAR file but they're not part of the JAR file themselves.
--MyProject
----specs
----src
...
I used maven to run the test inside the container. That's why I preferred to build the project inside the container so I get the JAR file ready with the same version of maven I run the test with.
Here is the DockerFile. I developed a bash script to run the test. You may run the script with CMD or ENTRYPOINT:
FROM maven:3.6.1-jdk-8
# add any project resources needed
ADD env /home/e2e/env
ADD specs /home/e2e/specs
ADD src /home/e2e/src
ADD src/main/scripts/entrypoint.sh /home/e2e/
ADD pom.xml /home/e2e/
RUN ["chmod", "+x", "./home/e2e/entrypoint.sh"]
# Install Gauge, web browser and webdriver in your preferred way...
ENV PATH=$HOME/.gauge:$PATH
# I'm keeping the cntainer running. But it's all up to you.
CMD /home/e2e/entrypoint.sh && tail -f /dev/null
And then here is the simple entrypoint.sh script:
#!/bin/bash
cd /home/e2e/
mvn clean package
gauge --version
google-chrome --version
mvn -version
mvn gauge:execute -DspecsDir=specs/myTest.spec
Of course, you could just use a ready JAR instead of building it inside the container. Or you could build the JAR while creating the docker image.

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

Create Docker container with both Java and Node.js

I am not sure why I expected this to work:
# Dockerfile
FROM node:6
FROM java:8
but it doesn't really work - looks like the first command is ignored, and second command works.
Is there a straightforward way to install both Node.js and Java in a Docker container?
Ultimately the problem I am trying to solve is that I am getting an ENOENT error when running Selenium Webdriver -
[20:38:50] W/start - Selenium Standalone server encountered an error: Error: spawn java ENOENT
And right now I assume it's because Java is not installed in the container.
The best way for you is to take java (which is officially deprecated and it suggests you use openjdk image) and install node in it.
So, start with
FROM openjdk:latest
This will use the latest openjdk image, which is 8u151 at this time. Then install node and other dependencies you might need:
RUN apt-get install -y curl \
&& curl -sL https://deb.nodesource.com/setup_9.x | bash - \
&& apt-get install -y nodejs \
&& curl -L https://www.npmjs.com/install.sh | sh
You might want to install things like grunt afterwards, so this might come in handy as well.
RUN npm install -g grunt grunt-cli
In total you will get the following Dockerfile:
FROM openjdk:latest
RUN apt-get install -y curl \
&& curl -sL https://deb.nodesource.com/setup_9.x | bash - \
&& apt-get install -y nodejs \
&& curl -L https://www.npmjs.com/install.sh | sh \
RUN npm install -g grunt grunt-cli
You may clone the Dockerfile from my gitlab repo here
You can use single FROM per generated image.
Try to use node as a base image and install java to it.
Dockerfile
FROM node:latest
RUN apt-get -y install default-jre
You can choose the version you need:
apt install default-jre
apt install openjdk-11-jre-headless
apt install openjdk-8-jre-headless
You can also use the node image and then install the default-jre:
# Dockerfile
FROM node:latest
RUN apt-get -y install default-jre
You can choose the version you need:
apt install default-jre
apt install openjdk-11-jre-headless
apt install openjdk-8-jre-headless
Perhaps, you might want to try this https://hub.docker.com/r/timbru31/java-node one to create the docker file.
This docker image comes with both Java and Node pre-installed. It comes in handy when both are required as a dependency.
Something like,
FROM timbru31/java-node:<tag>
The FROM inside your dockerfile simply tells docker from which image it should start the configuration. You can't simply concatenate multiple images together. There are already multiple container images available which offer preinstalled Java 8 and node JS. I don't want to recommend any image specifically but will direct you to docker-hub for you to go search on your own and use the container that suites your needs the best.
With version 14 of node it works perfectly for me:
FROM openjdk:latest
RUN apt-get install -y curl \
&& curl -sL https://deb.nodesource.com/setup_14.x | bash - \
&& apt-get install -y nodejs \
&& curl -L https://www.npmjs.com/install.sh | sh \
RUN npm install -g grunt grunt-cli
this worked for me:
FROM openjdk:16-slim-buster
RUN apt-get update; apt-get install -y curl \
&& curl -sL https://deb.nodesource.com/setup_14.x | bash - \
&& apt-get install -y nodejs \
&& curl -L https://www.npmjs.com/install.sh | sh
This works for me with node v16.15.0, be careful with the packages version of java, I'm using the latest by default. The packages used for java are:
Java Runtime Environment (jre)
Java Development Kit (jdk)
FROM node:16.15.0
RUN apt-get update \
&& apt-get install default-jre -y \
&& apt-get install default-jdk -y
RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app
COPY package*.json ./
RUN npm install
RUN npm install --global nodemon
COPY . .
EXPOSE 3000
CMD ["npm", "run", "dev"]
I hope this works for you all

Categories

Resources