Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fix Docker Unexpected Operator Error?

I'm a super novice vis-a-vis Docker, and recently moved a project from App Engine to Cloud Run. Was easy-peasy, loved it.

Now, however, I'm trying to update the image (since I added some new code). I understand that I need to get into an actual container to update an image (I think?) but when I attempt to docker run, I get an unexpected operator error.

It's driving me absolutely batty.

I can't start the container. I can't edit my image. I can't upload a new version on Cloud Run.

From what I can gather, an unexpected operator error has to deal with the Dockerfile. So, here's my Dockerfile (as given by Google for deploying an image on 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" ]

The specific unexpected operator error I am getting is /bin/sh: 1: [: npm.: unexpected operator

I honestly don't know what to do at this point. I think I need a second set of eyes to just look it over.

like image 255
Misha Stone Avatar asked Jul 10 '26 10:07

Misha Stone


2 Answers

I'm going to bet you cleaned up your code, intentionally or unintentionally, when you gave the code snippet CMD [ "npm", "start" ] It was almost certainly CMD [ "npm" "start" ] when you initially built the image and tried running it as a container.

Breaking apart the error message: /bin/sh: 1: [: npm.: unexpected operator

It is telling you that the shell script is having an issue on line one. Which shell script? The shell script that's triggered by the CMD line. The line 1 part is because, taken in context of CMD running, that's the one and only line.

It's then saying, pretty illegibly that it's having an issue after the npm statement. The easiest way to accidentally cause that is to forget the comma.

Given that you were apparently able to get things running later, it's most likely that you:

  • Built the image from the Dockerfile with the missing comma
  • Tried running the image as a container and it didn't work because of the broken CMD in the built image
  • Fixed the error
  • Tried running the still broken image as the container and it didn't still didn't work as you were using the same image
  • Posted your code here, now with the fix in place but seeming to not work because you were still running the old image
  • Got advice here including trying a rebuild
  • Rebuilt a new image from the Dockerfile, picking up the fixed comma
  • Ran the new image as a container and everything seemed to magically fix
like image 95
Nicholas Davison Avatar answered Jul 14 '26 07:07

Nicholas Davison


I had this issue and I needed to use double quotes instead of single quotes at the CMD step in my Dockerfile

CMD [ "npm", "start" ]

like image 35
srian Avatar answered Jul 14 '26 09:07

srian



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!