如何修复 Docker 意外操作员错误?

Mis*_*one 6 node.js docker google-cloud-run

我是 Docker 方面的超级新手,最近将一个项目从 App Engine 移至 Cloud Run。很简单,很喜欢。

然而,现在我正在尝试更新图像(因为我添加了一些新代码)。我知道我需要进入实际的容器来更新图像(我认为?),但是当我尝试这样做时docker run,我收到unexpected operator错误。

这让我彻底抓狂。

我无法启动容器。我无法编辑我的图像。我无法在 Cloud Run 上上传新版本。

据我所知,unexpected operator错误必须处理 Dockerfile。这是我的 Dockerfile(由 Google 提供,用于在 Cloud Run 上部署映像)。

Dockerfile

#Use the official Node.js 10 image
#https://hub.docker.com/_/node
FROM node:10

#Create and change to the app directory
WORKDIR /usr/src/app

#Copy application dependency manifests to the container image.
#A wild card is used to ensure both package.json AND package-lock.json are copied.
#Copying this separately prevents re0running npm install on every code change.
COPY *package.json ./

#Install production dependences
RUN npm install --only=production

#COPY local code to the container image
COPY . .

#Run the web service on container startup
CMD [ "npm", "start" ]
Run Code Online (Sandbox Code Playgroud)

unexpected operator我收到的具体错误是/bin/sh: 1: [: npm.: unexpected operator

老实说,我现在不知道该怎么办。我想我需要第二双眼睛来审视它。

小智 12

我敢打赌,当您提供代码片段时,您有意或无意地清理了代码,CMD [ "npm", "start" ]几乎可以肯定是CMD [ "npm" "start" ] 在您最初构建映像并尝试将其作为容器运行时。

分解错误消息:/bin/sh: 1: [: npm.: unexpected operator

它告诉您 shell 脚本在第一行有问题。哪个 shell 脚本?由 CMD 行触发的 shell 脚本。第 1 行部分是因为,在 CMD 运行的上下文中,这是唯一的一行。

然后,它非常难以辨认地表示,在 npm 声明之后出现了问题。意外导致这种情况的最简单方法就是忘记逗号。

鉴于您显然能够稍后让事情运行,您很可能:

  • 从 Dockerfile 构建镜像时缺少逗号
  • 尝试将映像作为容器运行,但由于构建映像中的 CMD 损坏而无法工作
  • 修复了错误
  • 尝试将仍然损坏的图像作为容器运行,但它仍然无法工作,因为您使用的是相同的图像
  • 在这里发布您的代码,现在已经修复到位,但似乎不起作用,因为您仍在运行旧图像
  • 在这里得到建议,包括尝试重建
  • 从 Dockerfile 重建一个新镜像,选取固定逗号
  • 将新图像作为容器运行,一切似乎都神奇地修复了


sri*_*ian 5

我遇到了这个问题,我需要在 Dockerfile 中的 CMD 步骤中使用双引号而不是单引号

CMD [ "npm", "start" ]


min*_*zur 4

每次更改后都必须从 Dockerfile 重建映像

docker build --tag="npm_app:latest" -f Dockerfile .
docker run npm_app 
Run Code Online (Sandbox Code Playgroud)