英文:
How to create docker image with nodejs 12, java, gcc, g++,python3, monocs
问题
我正在尝试将我的Node.js和Express API容器化。在我的API中,我使用了https://www.npmjs.com/package/compile-run 包来编译和运行C、Cpp、Java、JavaScript(Node.js环境)和Python语言。
这个包需要服务器上安装有这5种编译器(gcc、g++、nodejs、python3、javac)。如果缺少任何一个编译器,它都会抛出错误。
在我的本地环境(非容器化)中,在Windows和Ubuntu上都能正常工作(因为我在它们上面安装了编译器)。
我正试图在我的Docker镜像中复制同样的环境。但是我卡住了。
看看我的
**Dockerfile**:
FROM node:12
WORKDIR /app
COPY package.json /app
RUN npm install
COPY . /app
CMD ["npm","start"]
我认为 node-12 镜像自带了 gcc、g++、python3 和 nodejs(当然)。
但问题在于 **Java**。我无法编译Java代码。
所以我尝试了这种方式:
FROM node:12
RUN apt-get -y install default-jre
# RUN java -version
RUN apt -y install default-jre
RUN apt install openjdk-11-jre-headless
RUN java -version
WORKDIR /app
COPY package.json /app
RUN npm install
COPY . /app
CMD ["npm","start"]
但我无法使用 apt/apt-get 安装 open-jdk 或 open-jre。
配置Docker的正确方法是什么?
这是我的Node.js API存储库:https://github.com/yogendramaarisetty/online-compiler-api
英文:
I am trying to dockerize my NodeJs & Express API . In my API iam using https://www.npmjs.com/package/compile-run package to compile and run C,
Cpp,Java,JavaScript(Node.js env), Python languages.
This package requires all 5 compilers(gcc,g++,nodejs,python3,javac) installed on the server. If any compiler misses, it throws error.
In my local(undockerized) the API is working completely fine on both windows & ubuntu(As I have installed compilers on them).
I am trying to replicate the same on my docker image. But I am stuck.
Look at my
Dockerfile:
FROM node:12
WORKDIR /app
COPY package.json /app
RUN npm install
COPY . /app
CMD ["npm","start"]
I think node-12 image comes along with gcc, g++,python3 and nodejs(obviously).
But the issue is with java. I am not able to compile java code.
so I tried it this way
FROM node:12
RUN apt-get -y install default-jre
# RUN java -version
RUN apt -y install default-jre
RUN apt install openjdk-11-jre-headless
RUN java -version
WORKDIR /app
COPY package.json /app
RUN npm install
COPY . /app
CMD ["npm","start"]
But I am not able to install open-jdk or open-jre with apt/apt-get.
What is the right way to configure docker?
This is my nodeJS API repository https://github.com/yogendramaarisetty/online-compiler-api
答案1
得分: 7
首先,您必须使用 apt-get update
命令更新软件包列表,然后可以安装 openjdk-8。该发行版不支持 openjdk-11。我使用了 docker run -it node:12 /bin/bash
命令来查看可用的内容,
FROM node:12
RUN apt-get update && apt-get install -y openjdk-8-jdk
例如,
$ cat Dockerfile
FROM node:12
RUN apt-get update && apt-get install -y openjdk-8-jdk
$ docker build --tag mynode:1.0 .
$ docker run -it mynode:1.0 /bin/bash
root@d70858199dd1:/# java -version
openjdk version "1.8.0_265"
OpenJDK Runtime Environment (build 1.8.0_265-8u265-b01-0+deb9u1-b01)
OpenJDK 64-Bit Server VM (build 25.265-b01, mixed mode)
root@d70858199dd1:/# javac -version
javac 1.8.0_265
root@d70858199dd1:/#
如果确实需要 Java 11,有多种方式和位置可以获取 openjdk-11。其中一个是 bell-sw。例如,
$ cat Dockerfile
FROM node:12
RUN apt-get update && apt-get install -y libasound2 libxtst6
RUN wget https://download.bell-sw.com/java/11.0.7+10/bellsoft-jdk11.0.7+10-linux-amd64.deb && \
apt install ./bellsoft-jdk11.0.7+10-linux-amd64.deb
$ docker build --tag mynode:1.1 .
$ docker run -it mynode:1.1 /bin/bash
root@37771ce98727:/# java -version
openjdk version "11.0.7" 2020-04-14 LTS
OpenJDK Runtime Environment (build 11.0.7+10-LTS)
OpenJDK 64-Bit Server VM (build 11.0.7+10-LTS, mixed mode)
root@37771ce98727:/# javac -version
javac 11.0.7
如果您确实需要 Java 11,有多种方式和地方可以获取 openjdk-11,其中一个是 bell-sw。例如,
英文:
First you must update the package list with apt-get update
, then you can install openjdk-8. openjdk-11 isn't available with that distribution of node. I used docker run -it node:12 /bin/bash
to see what there was,
FROM node:12
RUN apt-get update && apt-get install -y openjdk-8-jdk
For example,
$ cat Dockerfile
FROM node:12
RUN apt-get update && apt-get install -y openjdk-8-jdk
$ docker build --tag mynode:1.0 .
$ docker run -it mynode:1.0 /bin/bash
root@d70858199dd1:/# java -version
openjdk version "1.8.0_265"
OpenJDK Runtime Environment (build 1.8.0_265-8u265-b01-0+deb9u1-b01)
OpenJDK 64-Bit Server VM (build 25.265-b01, mixed mode)
root@d70858199dd1:/# javac -version
javac 1.8.0_265
root@d70858199dd1:/#
If you really do need Java 11, there are multiple ways and places to get openjdk-11. One is bell-sw. For example,
$ cat Dockerfile
FROM node:12
RUN apt-get update && apt-get install -y libasound2 libxtst6
RUN wget https://download.bell-sw.com/java/11.0.7+10/bellsoft-jdk11.0.7+10-linux-amd64.deb && \
apt install ./bellsoft-jdk11.0.7+10-linux-amd64.deb
$ docker build --tag mynode:1.1 .
$ docker run -it mynode:1.1 /bin/bash
root@37771ce98727:/# java -version
openjdk version "11.0.7" 2020-04-14 LTS
OpenJDK Runtime Environment (build 11.0.7+10-LTS)
OpenJDK 64-Bit Server VM (build 11.0.7+10-LTS, mixed mode)
root@37771ce98727:/# javac -version
javac 11.0.7
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论