我有一个运行npm install
的docker文件。当我将此提交到gcloud builds submit --tag <tag>
时,出现以下错误:
....
npm ERR! path git
npm ERR! code ENOENT
npm ERR! errno ENOENT
npm ERR! syscall spawn git
npm ERR! enoent Error while executing:
npm ERR! enoent undefined ls-remote -h -t ssh://git@github.com/web3-js/WebSocket-Node.git
npm ERR! enoent
npm ERR! enoent
npm ERR! enoent spawn git ENOENT
npm ERR! enoent This is related to npm not being able to find a file.
npm ERR! enoent
从上面的错误消息中,以及对“未定义的ls-remote -h -t ssh://git@github.com”进行谷歌搜索的结果,看来问题出在git路径未定义。 >
是否有解决方法?
编辑:
# reference: https://www.docker.com/blog/keep-nodejs-rockin-in-docker/
# Operating System
FROM node:10.16.3-slim
# create app directory
WORKDIR /usr/src/app
COPY package-lock.json ./
COPY package.json ./
RUN npm install --no-optional
# for production: RUN npm ci
COPY . .
#EXPOSE 8080
# Environment variables
ENV mode help
CMD ["sh", "-c", "node src/app.js ${mode}"]
我现在认为这是因为我使用了-slim
版本的nodejs docker映像(根据docker博客文章中的建议进行操作)。我还没有意识到这些图像还包括其他程序,例如git等,它们是nodejs经常需要的。
答案 0 :(得分:1)
有两点不可混合:
tag
tag
git tag
是您放入存储库中的值,以便在特定时间获取代码。 git ls-remote
命令是正确的选择。但是,标签为空,ls-remote
使用ssh://
git url作为标签名称。
Cloud Build tag
(如果是gcr docker hub中的容器名称)。通常是gcr.io/<project_id>/<name that you want>
为解决您的问题,您有2种解决方案:
docker build
命令。使用tag
来命名容器,并使用环境变量-e GIT_TAG=xxx
在Dockerfile中使用以指定git标签cloudbuild.yaml
,并在Dockerfile
中对环境变量使用相同的逻辑。要将GIT_TAG
传递到Cloud Build,请使用substitution variables。您可以使用自己的替换变量(必须以下划线开头),也可以使用预定义的TAG_NAME
变量。在这两种情况下,您都必须在运行Cloud Build命令时指定它命令:gcloud builds submit --substitutions=TAG_NAME="test"
或gcloud builds submit --substitutions=_MY_TAG_NAME="test"
cloudbuild.yaml
文件
- name: 'gcr.io/cloud-builders/docker'
args: ['build', '-t', 'gcr.io/$PROJECT_ID/my-image', '.', '-e', 'GIT_TAG=$_MY_TAG_NAME']
# my-image is pushed to Container Registry
images:
- 'gcr.io/$PROJECT_ID/my-image'
您会看到docker build
命令行和Cloud Build定义完全相同。
通过这种方式,您可以在本地(或在Cloud Shell上)使用docker build进行测试,以进行更快的测试和迭代,然后将其打包在cloudbuild.yaml
文件中。
更新
使用其他详细信息,您的基本映像中没有安装git。
在您的npm install
RUN apt-get update && apt-get install -y git