install java dependency when building my own processing container

0

A python package I'm using requires java dependency. I put the following lines in my docker file when building processing container, and got apt-add-repository: command not found error. What is the correct commands to put in a dockerfile to install java?

RUN add-apt-repository ppa:openjdk-r/ppa &&
apt-get update &&
apt-get install -y openjdk-7-jdk &&
apt-get install -y ant &&
apt-get clean;

RUN apt-get update &&
apt-get install ca-certificates-java &&
apt-get clean &&
update-ca-certificates -f;

ENV JAVA_HOME /usr/lib/jvm/java-8-openjdk-amd64/ RUN export JAVA_HOME

2 Respostas
0

The error message "apt-add-repository: command not found" indicates that the software-properties-common package, which provides the add-apt-repository command, is not installed in your Docker image. You can install it using the apt-get command.


FROM ubuntu:latest

RUN apt-get update && \
    apt-get install -y software-properties-common && \
    add-apt-repository ppa:openjdk-r/ppa && \
    apt-get update && \
    apt-get install -y openjdk-8-jdk ant && \
    apt-get clean && \
    update-ca-certificates -f && \
    export JAVA_HOME=/usr/lib/jvm/java-8-openjdk-amd64/

# rest of your Dockerfile...

This Dockerfile does the following:

Starts from a base Ubuntu image. Updates the package lists for upgrades and new package installations. Installs the software-properties-common package, which provides the add-apt-repository command. Adds the OpenJDK repository. Installs OpenJDK 8 and ant. Cleans up the package list to keep the Docker image size smaller. Updates the CA certificates. Sets JAVA_HOME environment variable. Remember, the export JAVA_HOME=/usr/lib/jvm/java-8-openjdk-amd64/ command in the Dockerfile only affects the current RUN command. If you want JAVA_HOME to be set for all subsequent commands and layers, you should use the ENV command:

ENV JAVA_HOME /usr/lib/jvm/java-8-openjdk-amd64/
profile picture
ESPECIALISTA
respondido há 10 meses
0

Hello,

Greetings of the day!!

From the query explained, I can understand that you encountered “apt-add-repository: command not found error” when using python package which requires java dependency in dockerfile. You want to know how to install java in dockerfile using commands.

From analyzing the error “apt-add-repository: command not found error”, It looks like docker run time do not have the packages that are required to work apt-add-repository command.

Further, Using the same dockerfile shared over the query, got “add-apt-repository: not found”.

Dockerfile used:

FROM ubuntu

RUN add-apt-repository ppa:openjdk-r/ppa &&
apt-get update &&
apt-get install -y openjdk-7-jdk &&
apt-get install -y ant &&
apt-get clean;

RUN apt-get update &&
apt-get install ca-certificates-java &&
apt-get clean &&
update-ca-certificates -f;

ENV JAVA_HOME /usr/lib/jvm/java-8-openjdk-amd64/ RUN export JAVA_HOME

Error Encountered:

[2/3] RUN add-apt-repository ppa:openjdk-r/ppa && apt-get update && apt-get install -y openjdk-7-jdk && apt-get install -y ant && apt-get clean;: #5 0.176 /bin/sh: 1: add-apt-repository: not found

Referring to the link [1], observed that adding below command to the dockerfile will mitigate the error as software-properties-common package will give add-apt-repository command.

$ RUN apt-get update &&
apt-get install -y software-properties-common &&
rm -rf /var/lib/apt/lists/*

After adding the above lines to dockerfile, docker build failed with below error:

#6 13.46 E: Unable to locate package openjdk-7-jdk

executor failed running [/bin/sh -c add-apt-repository ppa:openjdk-r/ppa && apt-get update && apt-get install -y openjdk-7-jdk && apt-get install -y ant && apt-get clean;]: exit code: 100

While finding the resolution to above error, checked the link [2] and understood that using openjdk-8-jdk solved the “Unable to locate package openjdk-7-jdk” error. Hence changed the dockerfile as below. The image build was successful with below dockerfile.

Resolution:


FROM ubuntu

RUN apt-get update && \
apt-get install -y software-properties-common && \
rm -rf /var/lib/apt/lists/*

RUN add-apt-repository ppa:openjdk-r/ppa && \
apt-get update && \
apt-get install -y openjdk-8-jdk && \
apt-get install -y ant && \
apt-get clean;

RUN apt-get update && \
apt-get install ca-certificates-java && \
apt-get clean && \
update-ca-certificates -f;

ENV JAVA_HOME /usr/lib/jvm/java-8-openjdk-amd64/ RUN export JAVA_HOME

Below are the changes made to original dockerfile in the query:

a) Added below command to dockerfile

$ RUN apt-get update &&
apt-get install -y software-properties-common &&
rm -rf /var/lib/apt/lists/*

b) Changed “openjdk-7-jdk” to “openjdk-8-jdk”

c) Added " \ " at the end of every line of command.

References:

[1] https://stackoverflow.com/questions/32486779/apt-add-repository-command-not-found-error-in-dockerfile

[2] https://askubuntu.com/questions/796302/cant-able-to-install-openjdk-7-jdk-and-openjdk-7-jre

AWS
respondido há 10 meses

Você não está conectado. Fazer login para postar uma resposta.

Uma boa resposta responde claramente à pergunta, dá feedback construtivo e incentiva o crescimento profissional de quem perguntou.

Diretrizes para responder a perguntas