简体   繁体   English

如何在docker上安装java到ubuntu?

[英]How install java to ubuntu on docker?

I tried use docker.我尝试使用码头工人。 I install tool docker and run.我安装工具 docker 并运行。 I dovnload ubuntu image and run on docker.我下载 ubuntu 映像并在 docker 上运行。 I make all by this link我通过这个链接做所有的

For install ubuntu I used docker run -it ubuntu bash为了安装 ubuntu,我使用docker run -it ubuntu bash

After that I run this ubuntu docker run -i -t ubuntu:latest /bin/bash之后我运行这个 ubuntu docker run -i -t ubuntu:latest /bin/bash

After start I placed root@9bca9a2a537d:/#启动后我放置root@9bca9a2a537d:/#

Now I want install java and start some app on this java.现在我想安装java并在这个java上启动一些应用程序。

I tried install java root@cf50a6fdfc10:/# apt-get install default-jre我尝试安装 java root@cf50a6fdfc10:/# apt-get install default-jre

When this installed i try run this command java -version and I see当这个安装我尝试运行这个命令java -version我看到

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之后我退出 ubuntu

root@2e62f448f783:/# exit

and run again.并再次运行。 And when ubuntu started i try当 ubuntu 开始时,我尝试

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

How can I install java or start this java version?如何安装 java 或启动这个 java 版本?

As paulscott56 said, you can add those lines in your Dockerfile:正如paulscott56所说,您可以在 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/ https://hub.docker.com/r/pataquets/default-jre-headless/~/dockerfile/

why not use the official Java images, or the alpine Java, and just put in your Dockerfile为什么不使用官方的 Java 镜像,或者 alpine Java,只需放入你的 Dockerfile

FROM java

or或者

FROM anapsix/alpine-java

? ? You have a functional Java installed and can do whatever you want.你已经安装了一个功能性的 Java 并且可以做任何你想做的事情。

See

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

for some Java from the docker hub对于来自 docker hub 的一些 Java

You should read the good links provided by jonrsharpe您应该阅读 jonrsharpe 提供的良好链接

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正确执行,并将 java 添加到 Dockerfile 中的 RUN apt-get 行,或者
  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.根据您想要的(Ubuntu 或运行 Java 应用程序的容器),您应该使用 1. 中的方法或创建一个从 Java8 基础映像获取的新 Dockerfile。

You will have to commit the updated Image after installing Ubuntu.Try the following after installing java on the running container :您必须在安装 Ubuntu 后提交更新的映像。在运行的容器上安装 java 后尝试以下操作:

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" .它将创建一个名为 "ubuntu_with_java" 的新图像。

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.如果你想在我的镜像上安装 JDK,你可以使用这个 Dockerfile,或者你可以使用 java 和 ubuntu 进行多阶段构建。

FROM openjdk:8-jre-alpine

WORKDIR /usr/src/app

CMD ["/bin/bash"]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM