user5620472
user5620472

Reputation: 2882

How install java to ubuntu on docker?

I tried use docker. I install tool docker and run. I dovnload ubuntu image and run on docker. I make all by this link

For install ubuntu I used docker run -it ubuntu bash

After that I run this ubuntu docker run -i -t ubuntu:latest /bin/bash

After start I placed root@9bca9a2a537d:/#

Now I want install java and start some app on this java.

I tried install java root@cf50a6fdfc10:/# apt-get install default-jre

When this installed i try run this command java -version and I see

root@2e62f448f783:/# java -version
openjdk version "1.8.0_91"
OpenJDK Runtime Environment (build 1.8.0_91-8u91-b14-0ubuntu4~16.04.1-b14)
OpenJDK 64-Bit Server VM (build 25.91-b14, mixed mode)

after that i exit from ubuntu

root@2e62f448f783:/# exit

and run again. And when ubuntu started i try

root@20cefe55e2eb:/# java -version
bash: java: command not found

How can I install java or start this java version?

Upvotes: 15

Views: 30556

Answers (5)

user2314737
user2314737

Reputation: 29307

Here's how to create Ubuntu Docker image and container with Java from the OpenJDK.

First of all, you need to create an empty directory containing a file named Dockerfile with the following the content:

# Use Ubuntu as the base image
FROM ubuntu:latest

# Update Ubuntu packages
RUN apt-get update -y

# set JAVA_VERSION
ARG JAVA_VERSION=openjdk-19-jre-headless

# Install Java
RUN apt-get install -y $JAVA_VERSION

# Setup JAVA_HOME
ENV JAVA_HOME=/usr/lib/jvm/java-19-openjdk-amd64

# Set entry point to show Java version and environment variable $JAVA_HOME
ENTRYPOINT ["/bin/bash", "-c", "java --version && echo -n JAVA_HOME= && printenv JAVA_HOME"]

In the directory where the Dockerfile is located, build a container named ubuntu-java19 with the command:

docker build . -t ubuntu-java19

Run the container

% docker run ubuntu-java19 
# Out:                    
# openjdk 19.0.2 2023-01-17
# OpenJDK Runtime Environment (build 19.0.2+7-Ubuntu-0ubuntu322.04)
# OpenJDK 64-Bit Server VM (build 19.0.2+7-Ubuntu-0ubuntu322.04, mixed mode, sharing)
# JAVA_HOME=/usr/lib/jvm/java-19-openjdk-amd64

I used openjdk-19-jre-headless as an ARG (variable available during the build process).

To see other available OpenJDK Java packages you can run apt list -a openjdk* inside the Docker container.

% docker run -it --entrypoint /bin/sh  ubuntu-java19

and inside the container issue the command:

# apt list -a openjdk*

To get list of all available openjdk Java versions use:

apt-cache search openjdk
   

Upvotes: 2

Gautier
Gautier

Reputation: 1146

As paulscott56 said, you can add those lines in your Dockerfile:

RUN apt-get update && \
    DEBIAN_FRONTEND=noninteractive \
    apt-get -y install default-jre-headless && \
    apt-get clean && \
    rm -rf /var/lib/apt/lists/*

https://hub.docker.com/r/pataquets/default-jre-headless/~/dockerfile/

Upvotes: 19

paulscott56
paulscott56

Reputation: 506

The container is a single contained entity. All changes that you make to it are essentially lost when you quit and restart it. There are 2 solutions to his though:

  1. Do it properly, and add java to a RUN apt-get line in your Dockerfile, OR
  2. (Bad bad bad) Add it and hope your host never goes down.

Depending on what you want (Ubuntu or a container to run a Java app), you should either use the method in 1. or create a new Dockerfile that grabs FROM Java8 base image.

Upvotes: 1

user2915097
user2915097

Reputation: 32156

why not use the official Java images, or the alpine Java, and just put in your Dockerfile

FROM java

or

FROM anapsix/alpine-java

? You have a functional Java installed and can do whatever you want.

See

http://hub.docker.com/search/?isAutomated=0&isOfficial=0&page=1&pullCount=0&q=java&starCount=0

for some Java from the docker hub

You should read the good links provided by jonrsharpe

Upvotes: 4

Rambler
Rambler

Reputation: 5482

You will have to commit the updated Image after installing Ubuntu.Try the following after installing java on the running container :

docker ps -l #get current container ID , let's sat it is "container_id"

Then :

docker commit container_id ubuntu_with_java

It would create a new Image with name "ubuntu_with_java" .

Upvotes: 0

Related Questions