英文:
How to download golang and node in docker container
问题
我正在构建一个简单的Node服务器,以在Docker中运行。我引入了一个小的Golang模块,可以通过运行以下命令来执行:
go run /root/component-review-handler/downloader/main.go -build 1621568 -outdir /usr
我目前在本地的Node服务器上通过在启动时运行以下脚本来运行它:
exec(
`cd ${process.env.ROOT_PATH}/component-review-handler && go run cmd/downloader/main.go`,
(error, stdout, stderr) => {
if (error) {
logger.error(`error: ${error.message}`)
return
}
if (stderr) {
logger.log(`stderr: ${stderr}`)
return
}
logger.log(`stdout: ${stdout}`)
}
)
但是当我在Docker中运行代码时,我得到以下错误:
error: Command failed: cd /usr/src/app/component-review-handler && go run cmd/downloader/main.go
/bin/sh: 1: go: not found
有人知道我如何在我的Docker容器中安装Node和Golang吗?
当前的Dockerfile如下:
FROM node:14
WORKDIR /usr/src/app
COPY package*.json ./
RUN npm install
COPY . .
ENV NODE_TLS_REJECT_UNAUTHORIZED='0'
EXPOSE 3000
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
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
exec(
`cd ${process.env.ROOT_PATH}/component-review-handler && go run cmd/downloader/main.go`,
(error, stdout, stderr) => {
if (error) {
logger.error(`error: ${error.message}`)
return
}
if (stderr) {
logger.log(`stderr: ${stderr}`)
return
}
logger.log(`stdout: ${stdout}`)
}
)
But When I run the code in docker, I get the following error
error: Command failed: cd /usr/src/app/component-review-handler && go run cmd/downloader/main.go
/bin/sh: 1: go: not found
Does anyone know how I can install both node and golang in my docker container?
Current Dockerfile
FROM node:14
WORKDIR /usr/src/app
COPY package*.json ./
RUN npm install
COPY . .
ENV NODE_TLS_REJECT_UNAUTHORIZED='0'
EXPOSE 3000
CMD ["node", "server.js" ]
答案1
得分: 4
Go是一种编译语言,通常不需要Go工具链来运行Go程序。
我会使用多阶段构建来实现这个。第一阶段是使用FROM golang
来获取工具链并构建二进制文件;第二阶段使用COPY --from
将第一个镜像复制到通常在搜索路径上的目录中。
FROM golang:1.17 AS downloader
WORKDIR /app # 不在/go目录下
COPY component-review-handler/ ./ # (请仔细检查COPY语法)
RUN go build -o downloader ./cmd/downloader
FROM node:14
# vvv 添加这一行
COPY --from=downloader /app/downloader /usr/local/bin/
# 与之前相同
WORKDIR /usr/src/app
COPY package*.json ./
RUN npm install
COPY . .
ENV NODE_TLS_REJECT_UNAUTHORIZED='0'
EXPOSE 3000
CMD ["node", "server.js"]
由于二进制文件现在位于/usr/local/bin
,这是默认的$PATH
目录,所以在你的代码中,你可以直接运行它,而不需要cd
或go run
部分。
const { execFile } = require('child_process');
execFile('downloader',
(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.
FROM golang:1.17 AS downloader
WORKDIR /app # not under /go
COPY component-review-handler/ ./ # (double-check this COPY syntax)
RUN go build -o downloader ./cmd/downloader
FROM node:14
# vvv add this line
COPY --from=downloader /app/downloader /usr/local/bin/
# same as before
WORKDIR /usr/src/app
COPY package*.json ./
RUN npm install
COPY . .
ENV NODE_TLS_REJECT_UNAUTHORIZED='0'
EXPOSE 3000
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
const { execFile } = require('child_process');
execFile('downloader',
(error, stdout, stderr) => { ... });
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论