67

This is my very first try to create a Docker image and I'm hoping someone can help me out. My Dockerfile looks roughly like this:

FROM mybaseimage:0.1
MAINTAINER ...

ENV JAVA_HOME /usr/lib/jvm/java-7-openjdk-amd64
RUN sed 's/main$/main universe/' -i /etc/apt/sources.list

ENV DEBIAN_FRONTEND noninteractive

RUN apt-get update

RUN apt-get install -y openjdk-7-jre && apt-get clean &&\
             mkdir temp_dir  &&   cd temp_dir && \
             ${JAVA_HOME}/bin/jar -xvf somejar.jar  &&\
             cd ..
ENTRYPOINT ["somescript.sh"]

Basically I'm only installing Java so I can expand a jar file. When I run my makefile I get the following error:

/bin/sh: 1: /usr/lib/jvm/java-7-openjdk-amd64: Permission denied

I've been trying to follow this example: https://registry.hub.docker.com/u/barnybug/openjdk-7-jre/dockerfile/

Edit: per request in the comment here is my makefile:

DOCKER_REGISTRY=registry.mycompany.com
DOCKER_IMAGE=appimage-myapp
DOCKER_TAG=3.0

SUDO=

ARCHIVE_NAME=$(DOCKER_IMAGE):$(DOCKER_TAG)
  REPO_ARCHIVE_NAME=$(DOCKER_REGISTRY)/$(ARCHIVE_NAME)
  BASE_IMAGE_ARCHIVE=$(DOCKER_IMAGE)_$(DOCKER_TAG).tar.gz

all: $(BASE_IMAGE_ARCHIVE)

.PHONY: docker_image

docker_image: Dockerfile
    $(SUDO) docker build -t $(ARCHIVE_NAME) .

$(BASE_IMAGE_ARCHIVE): docker_image
  $(SUDO) docker tag -f $(ARCHIVE_NAME) $(REPO_ARCHIVE_NAME)
  $(SUDO) docker push $(REPO_ARCHIVE_NAME)
  $(SUDO) docker save $(ARCHIVE_NAME) | gzip -c > $@
  $(SUDO) docker rmi $(REPO_ARCHIVE_NAME)

which I run with

make docker_image SUDO=sudo
1
  • 1
    You had better actually post your makefile. Commented Jul 3, 2015 at 0:28

6 Answers 6

84

I was able to install OpenJDK 8 via the steps below (taken from here). My Dockerfile inherits from phusion/baseimage-docker, which is based on Ubuntu 16.04 LTS.

# Install OpenJDK-8
RUN apt-get update && \
    apt-get install -y openjdk-8-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/java-8-openjdk-amd64/
RUN export JAVA_HOME

To install OpenJDK 7 instead, you may need to prepend

add-apt-repository ppa:openjdk-r/ppa

such that the first step becomes

# Install OpenJDK-7
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;
Sign up to request clarification or add additional context in comments.

5 Comments

It does work for openJDK8, however it does NOT work for openJDK11, what can i do to install openJDK11?
It says openjdk-7-jdk is not available.. which one can I use for JDK 7.. I am using base as Ubuntu-20.04
E: Unable to locate package openjdk-8-jdk
Is there any way to install specific version of java like 8u312-b07.?apt-get install -y openjdk-8-jdk
@PeterKionga-Kamau openjdk-8 is gone at this point. If you don't care about the version, just install default-jdk
23

I was able to install Java-11 on an image using Ubuntu 18.04. I also just needed it for only one application. (The Python wrapper around Apache Tika.)

FROM python:3.8.2-buster
COPY . /usr/src/app

# Install OpenJDK-11
RUN apt-get update && \
    apt-get install -y openjdk-11-jre-headless && \
    apt-get clean;

# Install PYTHON requirements
COPY requirements.txt ./
RUN pip install --no-cache-dir -r requirements.txt

# START WEBAPP SERVICE
CMD [ "python", "/usr/src/app/main.py" ]

Hope that helps.

3 Comments

This does not work for me, i got "[91mE: Unable to locate package openjdk-11-jdk-headless [0mThe command '/bin/sh -c apt-get update && apt-get install -y openjdk-11-jdk-headless && apt-get clean;' returned a non-zero code: 100"
I got below error. "Package 'openjdk-8-jdk' has no installation candidate" I am new to docker, I am not sure if this Dockerfile has to be placed on my debian container itself or at my Windows. Any help please.
The only one config that let me combine python+Java in one container
10

FWIW, this is a Node 16 base image with OpenJDK 11:

FROM node:16
RUN apt-get update && \
    apt-get install -y openjdk-11-jdk ca-certificates-java && \
    apt-get clean && \
    update-ca-certificates -f
ENV JAVA_HOME /usr/lib/jvm/java-11-openjdk-amd64/
RUN export JAVA_HOME
CMD ["java", "-version"]

You can check the JDK installation by building the image and running the container:

docker build -t java11 . && docker run java11

which should produce the following output:

openjdk version "11.0.13" 2021-10-19
OpenJDK Runtime Environment (build 11.0.13+8-post-Debian-1deb10u1)
OpenJDK 64-Bit Server VM (build 11.0.13+8-post-Debian-1deb10u1, mixed mode, sharing)

Comments

3

Here is how to install java 11 on any Debian/Debian slim based containers

FROM python:3.7-slim-buster
# Install JRE
RUN mkdir -p /usr/share/man/man1 /usr/share/man/man2 && \
    apt-get update &&\
    apt-get install -y --no-install-recommends openjdk-11-jre && \
    apt-get install ca-certificates-java -y && \
    apt-get clean && \
    update-ca-certificates -f;
ENV JAVA_HOME /usr/lib/jvm/java-11-openjdk-amd64/
CMD ["sleep", "infinity"]

Build the image, run and exec into the container.

docker build -t java11 .
docker run -d --name java-container java11
docker exec -it java-container /bin/bash

check the version in the container

root@a9a0011f0ab6:/# java -version
openjdk version "11.0.16" 2022-07-19
OpenJDK Runtime Environment (build 11.0.16+8-post-Debian-1deb10u1)
OpenJDK 64-Bit Server VM (build 11.0.16+8-post-Debian-1deb10u1, mixed mode, sharing)

In Debian Slim based containers you might come across below error.

error creating symbolic link '/usr/share/man/man1/java.1.gz.dpkg-tmp': No such file or directory

Setting up openjdk-11-jre-headless:amd64 (11.0.16+8-1~deb10u1) ...
#11 66.48 update-alternatives: using /usr/lib/jvm/java-11-openjdk-amd64/bin/java to provide /usr/bin/java (java) in auto mode
#11 66.48 update-alternatives: error: error creating symbolic link '/usr/share/man/man1/java.1.gz.dpkg-tmp': No such file or directory
#11 66.48 dpkg: error processing package openjdk-11-jre-headless:amd64 (--configure):
#11 66.48  installed openjdk-11-jre-headless:amd64 package post-installation script subprocess returned error exit status 2

In such scenarios simply include below line before installing java. This will create couple of directories in the container which is left out in slim variants to reduce the overall container size.

mkdir -p /usr/share/man/man1 /usr/share/man/man2

EDIT 1: Removed RUN export JAVA_HOME from the Dockerfile as its not required.

Comments

2

I recently faced this issue. Tried to have python as base image. But the concept can be applied to any other flow.

After resolving few hurdles, the following is the final working solution:

FROM python:3.6.13
ENV JAVA_HOME /usr/lib/jvm/java-8-openjdk-amd64/
WORKDIR /app
COPY requirements.txt ./
RUN apt update -y && apt-get install -y software-properties-common && \
    apt-add-repository 'deb http://security.debian.org/debian-security stretch/updates main' && apt update -y && \
    apt-get install -y openjdk-8-jdk-headless && \
    pip install --no-cache-dir -r requirements.txt && \
    export JAVA_HOME && \
    apt-get clean
  • We need software-properties-common to get apt-add-repository
  • openjdk-8-jdk - we need to add source for this.
  • deb http://security.debian.org/debian-security stretch/updates main is the proper source. Let's not point to unknown ppa sources
  • openjdk-8-jdk-headless - Found it optimal for docker based usages.

Comments

-7

There is already an official repo for java, which uses Open JDK.
Its very easy to use it. Have a look over this repo, which demonstrates very basic hello world program.
Dockerfile:

FROM java:7
COPY src /home/root/java/src
WORKDIR /home/root/java
RUN mkdir bin
RUN javac -d bin src/HelloWorld.java
ENTRYPOINT ["java", "-cp", "bin", "HelloWorld"]

HelloWorld.java file:

public class HelloWorld{
    public static void main(String[] args){
        System.out.println("Hello World!!!");
    }
}

2 Comments

I know about this repo but I can't inherit from that repo -- AFAIK Docker allows single inheritance only and I have another image that I need to inherit from...mybaseimage
This doesn't actually answer the question. This isn't how to install java in docker, it is how to use a java base-image.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.