11

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?

user5620472
  • 2,722
  • 8
  • 44
  • 97
  • Are you trying to use a Docker container like a regular virtual machine? That's not really how it works. You should write a `Dockerfile` that starts with the base Ubuntu image you want, installs Java, copies across your app and sets it up to run. – jonrsharpe Jul 21 '16 at 05:39
  • @jonrsharpe OK. let's say I've created a file docker. Then what I do with it? I imagine this is so - I run docker. install all need Programs (java, db, some utilites). test how it works. If all is well then I create an image. then I install that image in another docker on another computer. – user5620472 Jul 21 '16 at 05:45
  • In Docker data are not persist in the container unless it is associated with a volume that is mapped to a file system. – Samuel Toh Jul 21 '16 at 05:46
  • then I do not understand how it works – user5620472 Jul 21 '16 at 05:47
  • 1
    Well have you considered reading the [introductory tutorial](https://docs.docker.com/engine/getstarted/) or the docs (e.g. [`Dockerfile` reference](https://docs.docker.com/engine/reference/builder/))? You supply a list of steps that tells Docker how to build the container you want, then it's easily repeatable, rather than having to set it up in a running machine. See e.g. https://github.com/textbook/flash/blob/master/Dockerfile, from one of my own projects. – jonrsharpe Jul 21 '16 at 05:50

5 Answers5

18

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/

Gautier
  • 1,066
  • 1
  • 13
  • 24
4

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

user2915097
  • 30,758
  • 6
  • 57
  • 59
  • 2
    What if I'm already using a different base image, and need to add java to it? It's not possible to have two base images. – callum May 22 '17 at 13:04
  • see https://stackoverflow.com/questions/33322103/multiple-froms-what-it-means and VonC excellent answer – user2915097 May 22 '17 at 13:16
1

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.

paulscott56
  • 496
  • 4
  • 7
0

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" .

Rambler
  • 4,994
  • 2
  • 20
  • 27
  • 2
    [*"Generally, it is better to use Dockerfiles to manage your images in a documented and maintainable way.*](https://docs.docker.com/engine/reference/commandline/commit/) – jonrsharpe Jul 21 '16 at 05:53
-1

You can use this Dockerfile if you want to have JDK on my images, or you can have a multi-stage build with java with ubuntu.

FROM openjdk:8-jre-alpine

WORKDIR /usr/src/app

CMD ["/bin/bash"]

csaju
  • 182
  • 2
  • 11