如何使用 Node.js 12、Java、GCC、G++、Python 3、Monocs 创建 Docker 镜像

huangapple go评论128阅读模式
英文:

How to create docker image with nodejs 12, java, gcc, g++,python3, monocs

问题

  1. 我正在尝试将我的Node.jsExpress API容器化。在我的API中,我使用了https://www.npmjs.com/package/compile-run 包来编译和运行C、Cpp、Java、JavaScript(Node.js环境)和Python语言。
  2. 这个包需要服务器上安装有这5种编译器(gccg++、nodejspython3javac)。如果缺少任何一个编译器,它都会抛出错误。
  3. 在我的本地环境(非容器化)中,在WindowsUbuntu上都能正常工作(因为我在它们上面安装了编译器)。
  4. 我正试图在我的Docker镜像中复制同样的环境。但是我卡住了。
  5. 看看我的
  6. **Dockerfile**:
  7. FROM node:12
  8. WORKDIR /app
  9. COPY package.json /app
  10. RUN npm install
  11. COPY . /app
  12. CMD ["npm","start"]
  13. 我认为 node-12 镜像自带了 gccg++、python3 nodejs(当然)。
  14. 但问题在于 **Java**。我无法编译Java代码。
  15. 所以我尝试了这种方式:
  16. FROM node:12
  17. RUN apt-get -y install default-jre
  18. # RUN java -version
  19. RUN apt -y install default-jre
  20. RUN apt install openjdk-11-jre-headless
  21. RUN java -version
  22. WORKDIR /app
  23. COPY package.json /app
  24. RUN npm install
  25. COPY . /app
  26. CMD ["npm","start"]
  27. 但我无法使用 apt/apt-get 安装 open-jdk open-jre
  28. 配置Docker的正确方法是什么?
  29. 这是我的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:

  1. FROM node:12
  2. WORKDIR /app
  3. COPY package.json /app
  4. RUN npm install
  5. COPY . /app
  6. 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

  1. FROM node:12
  2. RUN apt-get -y install default-jre
  3. # RUN java -version
  4. RUN apt -y install default-jre
  5. RUN apt install openjdk-11-jre-headless
  6. RUN java -version
  7. WORKDIR /app
  8. COPY package.json /app
  9. RUN npm install
  10. COPY . /app
  11. 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 命令来查看可用的内容,

  1. FROM node:12
  2. RUN apt-get update && apt-get install -y openjdk-8-jdk

例如,

  1. $ cat Dockerfile
  2. FROM node:12
  3. RUN apt-get update && apt-get install -y openjdk-8-jdk
  4. $ docker build --tag mynode:1.0 .
  5. $ docker run -it mynode:1.0 /bin/bash
  6. root@d70858199dd1:/# java -version
  7. openjdk version "1.8.0_265"
  8. OpenJDK Runtime Environment (build 1.8.0_265-8u265-b01-0+deb9u1-b01)
  9. OpenJDK 64-Bit Server VM (build 25.265-b01, mixed mode)
  10. root@d70858199dd1:/# javac -version
  11. javac 1.8.0_265
  12. root@d70858199dd1:/#

如果确实需要 Java 11,有多种方式和位置可以获取 openjdk-11。其中一个是 bell-sw。例如,

  1. $ cat Dockerfile
  2. FROM node:12
  3. RUN apt-get update && apt-get install -y libasound2 libxtst6
  4. RUN wget https://download.bell-sw.com/java/11.0.7+10/bellsoft-jdk11.0.7+10-linux-amd64.deb && \
  5. apt install ./bellsoft-jdk11.0.7+10-linux-amd64.deb
  6. $ docker build --tag mynode:1.1 .
  7. $ docker run -it mynode:1.1 /bin/bash
  8. root@37771ce98727:/# java -version
  9. openjdk version "11.0.7" 2020-04-14 LTS
  10. OpenJDK Runtime Environment (build 11.0.7+10-LTS)
  11. OpenJDK 64-Bit Server VM (build 11.0.7+10-LTS, mixed mode)
  12. root@37771ce98727:/# javac -version
  13. 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,

  1. FROM node:12
  2. RUN apt-get update && apt-get install -y openjdk-8-jdk

For example,

  1. $ cat Dockerfile
  2. FROM node:12
  3. RUN apt-get update && apt-get install -y openjdk-8-jdk
  4. $ docker build --tag mynode:1.0 .
  5. $ docker run -it mynode:1.0 /bin/bash
  6. root@d70858199dd1:/# java -version
  7. openjdk version "1.8.0_265"
  8. OpenJDK Runtime Environment (build 1.8.0_265-8u265-b01-0+deb9u1-b01)
  9. OpenJDK 64-Bit Server VM (build 25.265-b01, mixed mode)
  10. root@d70858199dd1:/# javac -version
  11. javac 1.8.0_265
  12. 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,

  1. $ cat Dockerfile
  2. FROM node:12
  3. RUN apt-get update && apt-get install -y libasound2 libxtst6
  4. RUN wget https://download.bell-sw.com/java/11.0.7+10/bellsoft-jdk11.0.7+10-linux-amd64.deb && \
  5. apt install ./bellsoft-jdk11.0.7+10-linux-amd64.deb
  6. $ docker build --tag mynode:1.1 .
  7. $ docker run -it mynode:1.1 /bin/bash
  8. root@37771ce98727:/# java -version
  9. openjdk version "11.0.7" 2020-04-14 LTS
  10. OpenJDK Runtime Environment (build 11.0.7+10-LTS)
  11. OpenJDK 64-Bit Server VM (build 11.0.7+10-LTS, mixed mode)
  12. root@37771ce98727:/# javac -version
  13. javac 11.0.7

huangapple
  • 本文由 发表于 2020年8月23日 03:23:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/63540237.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定