How to download golang and node in docker container

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

How to download golang and node in docker container

问题

我正在构建一个简单的Node服务器,以在Docker中运行。我引入了一个小的Golang模块,可以通过运行以下命令来执行:

  1. go run /root/component-review-handler/downloader/main.go -build 1621568 -outdir /usr

我目前在本地的Node服务器上通过在启动时运行以下脚本来运行它:

  1. exec(
  2. `cd ${process.env.ROOT_PATH}/component-review-handler && go run cmd/downloader/main.go`,
  3. (error, stdout, stderr) => {
  4. if (error) {
  5. logger.error(`error: ${error.message}`)
  6. return
  7. }
  8. if (stderr) {
  9. logger.log(`stderr: ${stderr}`)
  10. return
  11. }
  12. logger.log(`stdout: ${stdout}`)
  13. }
  14. )

但是当我在Docker中运行代码时,我得到以下错误:

  1. error: Command failed: cd /usr/src/app/component-review-handler && go run cmd/downloader/main.go
  2. /bin/sh: 1: go: not found

有人知道我如何在我的Docker容器中安装Node和Golang吗?

当前的Dockerfile如下:

  1. FROM node:14
  2. WORKDIR /usr/src/app
  3. COPY package*.json ./
  4. RUN npm install
  5. COPY . .
  6. ENV NODE_TLS_REJECT_UNAUTHORIZED='0'
  7. EXPOSE 3000
  8. CMD ["node", "server.js"]
英文:

I am building a simple node server to run in docker. I have introduced a small golang module that can be executed by running

  1. go run /root/component-review-handler/downloader/main.go -build 1621568 -outdir /usr

I am currently running this locally in my node server by running the follow script on startup

  1. exec(
  2. `cd ${process.env.ROOT_PATH}/component-review-handler && go run cmd/downloader/main.go`,
  3. (error, stdout, stderr) => {
  4. if (error) {
  5. logger.error(`error: ${error.message}`)
  6. return
  7. }
  8. if (stderr) {
  9. logger.log(`stderr: ${stderr}`)
  10. return
  11. }
  12. logger.log(`stdout: ${stdout}`)
  13. }
  14. )

But When I run the code in docker, I get the following error

  1. error: Command failed: cd /usr/src/app/component-review-handler && go run cmd/downloader/main.go
  2. /bin/sh: 1: go: not found

Does anyone know how I can install both node and golang in my docker container?
Current Dockerfile

  1. FROM node:14
  2. WORKDIR /usr/src/app
  3. COPY package*.json ./
  4. RUN npm install
  5. COPY . .
  6. ENV NODE_TLS_REJECT_UNAUTHORIZED='0'
  7. EXPOSE 3000
  8. CMD ["node", "server.js" ]

答案1

得分: 4

Go是一种编译语言,通常不需要Go工具链来运行Go程序。

我会使用多阶段构建来实现这个。第一阶段是使用FROM golang来获取工具链并构建二进制文件;第二阶段使用COPY --from将第一个镜像复制到通常在搜索路径上的目录中。

  1. FROM golang:1.17 AS downloader
  2. WORKDIR /app # 不在/go目录下
  3. COPY component-review-handler/ ./ # (请仔细检查COPY语法)
  4. RUN go build -o downloader ./cmd/downloader
  5. FROM node:14
  6. # vvv 添加这一行
  7. COPY --from=downloader /app/downloader /usr/local/bin/
  8. # 与之前相同
  9. WORKDIR /usr/src/app
  10. COPY package*.json ./
  11. RUN npm install
  12. COPY . .
  13. ENV NODE_TLS_REJECT_UNAUTHORIZED='0'
  14. EXPOSE 3000
  15. CMD ["node", "server.js"]

由于二进制文件现在位于/usr/local/bin,这是默认的$PATH目录,所以在你的代码中,你可以直接运行它,而不需要cdgo run部分。

  1. const { execFile } = require('child_process');
  2. execFile('downloader',
  3. (error, stdout, stderr) => { ... });
英文:

Go is a compiled language, and you shouldn't usually need the Go toolchain just to run a Go program.

I'd use a multi-stage build for this. The first stage is FROM golang to have the toolchain and build the binary; the second COPY --from the first image into a directory that's normally on the search path.

  1. FROM golang:1.17 AS downloader
  2. WORKDIR /app # not under /go
  3. COPY component-review-handler/ ./ # (double-check this COPY syntax)
  4. RUN go build -o downloader ./cmd/downloader
  5. FROM node:14
  6. # vvv add this line
  7. COPY --from=downloader /app/downloader /usr/local/bin/
  8. # same as before
  9. WORKDIR /usr/src/app
  10. COPY package*.json ./
  11. RUN npm install
  12. COPY . .
  13. ENV NODE_TLS_REJECT_UNAUTHORIZED='0'
  14. EXPOSE 3000
  15. CMD ["node", "server.js"]

Since the binary is now in /usr/local/bin which is a default $PATH directory, in your code you can just run it, without the cd or go run parts

  1. const { execFile } = require('child_process');
  2. execFile('downloader',
  3. (error, stdout, stderr) => { ... });

huangapple
  • 本文由 发表于 2021年12月10日 03:19:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/70295544.html
匿名

发表评论

匿名网友

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

确定