使用 Java 和 Node.js 创建 Docker 容器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43769730/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 07:42:59  来源:igfitidea点击:

Create Docker container with both Java and Node.js

javanode.jsseleniumdockerdockerfile

提问by Alexander Mills

I am not sure why I expected this to work:

我不知道为什么我希望这会起作用:

 # Dockerfile    
 FROM node:6
 FROM java:8

but it doesn't really work - looks like the first command is ignored, and second command works.

但它并没有真正起作用 - 看起来第一个命令被忽略,第二个命令起作用。

Is there a straightforward way to install both Node.js and Java in a Docker container?

有没有一种直接的方法可以在 Docker 容器中同时安装 Node.js 和 Java?

Ultimately the problem I am trying to solve is that I am getting an ENOENT error when running Selenium Webdriver -

最终,我试图解决的问题是运行 Selenium Webdriver 时出现 ENOENT 错误 -

[20:38:50] W/start - Selenium Standalone server encountered an error: Error: spawn java ENOENT

And right now I assume it's because Java is not installed in the container.

现在我认为这是因为容器中没有安装 Java。

采纳答案by Max Farsikov

You can use single FROMper generated image. Try to use nodeas a base image and install java to it.

您可以FROM为每个生成的图像使用单个。尝试node用作基础映像并为其安装 java。

回答by Alex Karshin

The best way for you is to take java (which is officially deprecated and it suggests you use openjdkimage) and install node in it.

对您来说最好的方法是使用 java(官方已弃用它并建议您使用openjdk图像)并在其中安装节点。

So, start with

所以,从

FROM openjdk:latest

This will use the latest openjdk image, which is 8u151at this time. Then install nodeand other dependencies you might need:

这将使用最新的 openjdk 映像,即8u151此时。然后安装node您可能需要的其他依赖项:

RUN apt-get install -y curl \
  && curl -sL https://deb.nodesource.com/setup_9.x | bash - \
  && apt-get install -y nodejs \
  && curl -L https://www.npmjs.com/install.sh | sh

You might want to install things like grunt afterwards, so this might come in handy as well.

你可能想在之后安装 grunt 之类的东西,所以这也可能派上用场。

RUN npm install -g grunt grunt-cli

In total you will get the following Dockerfile:

总的来说,您将获得以下 Dockerfile:

FROM openjdk:latest

RUN apt-get install -y curl \
  && curl -sL https://deb.nodesource.com/setup_9.x | bash - \
  && apt-get install -y nodejs \
  && curl -L https://www.npmjs.com/install.sh | sh \
RUN npm install -g grunt grunt-cli

You may clone the Dockerfile from my gitlab repo here

你可以从我的gitlab回购克隆Dockerfile这里

回答by Fabian

The FROMinside your dockerfile simply tells docker from which image it should start the configuration. You can't simply concatenate multiple images together. There are already multiple container images available which offer preinstalled Java 8 and node JS. I don't want to recommend any image specifically but will direct you to docker-hubfor you to go search on your own and use the container that suites your needs the best.

FROM您的dockerfile里面只是告诉搬运工从图像应该开始配置。您不能简单地将多个图像连接在一起。已经有多个容器镜像提供预安装的 Java 8 和 node JS。我不想特别推荐任何图像,但会引导您到docker-hub,让您自行搜索并使用最适合您需求的容器。