/bin/sh: =: not found when running docker container from custom image - node.js

I try to run a container from a built image and get an error /bin/sh: =:. Is there anyone know how to fix it. Below is my Dockerfile. Both the index.js and package.json are in the same folder with the Dockerfile.
package.json
{
"dependencies":{
"express":"*"
},
"scripts":{
"start":"node index.js"
}
}
Dockerfile
FROM node:14.4.0-alpine3.10
COPY ./ ./
RUN npm install
CMD = ['npm', 'start']

The input to CMD is parsed as a JSON array, which means you have to use double quotes and not single quotes. Additionally, get rid of the =
CMD ["npm", "start"]

Related

Docker typescript node.js can't start app?

I have an index.ts, then this is my script
"scripts": {
"test": "jest",
"start": "ts-node-dev ./index.ts"
},
I tried to dockerize it, what should I do? do I need to add another command for npm build? or generate a .js file?
my dockerfile like this
FROM node:10-alpine
WORKDIR /
# copy configs to /app folder
COPY package*.json ./
COPY tsconfig.json ./
COPY . .
# check files list
RUN ls -a
RUN npm install
EXPOSE 3001
CMD [ "npm", "start"]
I can't access localhost:3001 in my browser after I run
docker build -t testApp .
then
docker run -p 80:3001 testApp

Nodejs syntax error inside docker but not outside

I have a super simple node.js file that runs perfectly fine outside of docker, but throws the following syntax error when I try to run it in a container:
./app/index.js: line 1: syntax error: unexpected word (expecting ")")
index.js is literally one line:
console.log("HI");
My Docker file is:
FROM node:14-alpine
WORKDIR /usr/app/
COPY . ./
CMD yarn start
I'm running the same command inside and out, same version of node etc. I'm on Windows 10 using the WSL 2 backed docker desktop. Could any of that have something to do with it? I'm going nuts...
Its working fine with me.
index.js
console.log("HI");
Dockerfile
FROM node:14-alpine
WORKDIR /usr/app/
COPY . ./
CMD yarn start
package.json
{
"name": "my-package",
"scripts": {
"start": "node index.js"
}
}
docker-compose.yml
version: '3.7'
services:
node_v14_test:
build:
context: .
dockerfile: Dockerfile
Then build and run docker container with docker compose.
docker-compose up --build
Result
try this
FROM node:14-alpine
WORKDIR /usr/app/
COPY . .
CMD [ "yarn", "start" ]
Seems like you need to convert file from dos to Linux issue or might the case image is using wrong code?
But the simplest way to debug that issue is not with Docker environment is to generate file inside Dcokerfile
FROM node:14-alpine
WORKDIR /usr/app/
COPY package.json .
RUN echo "console.log(\"HI\")" >> index.js
CMD yarn start

Dockerfile to run nodejs static-content in docker container

Need an advice to dockerize and run a node JS static-content app on K8s cluster.
I have a static web-content which I run "npm run build” into the terminal which generates /build and direct my IIS webserver to /build/Index.html.
Now, I started creating a Docker file, how do I point my nodeJS image to invoke /build/Index.html file
FROM node:carbon
WORKDIR /app
COPY /Core/* ./app
npm run build
EXPOSE 8080
CMD [ "node", ".app/build/index.html" ]
Please how can I run this app only on node v8.9.3 and
npm 5.6.0 ?
Any inputs please ?
You can specify the version of node specifically:
FROM node:8.9.3
Assumptions:
package.json is under Code directory.
npm run build will be running outside of the container and a build directory will be created in Code directory.
We will copy the whole Code/build directory under /app directory of the container.
We will copy package.json to /app folder and will run the website through scripts available in package.json file.
Solution:
I would say add a script named start in the package.json and call that script from Dockerfile's CMD command. The script would look like:
"scripts": {
"start": "node ./index.html",
},
And the Dockerfile would look like:
FROM node:8.9.3
# Make app directory in the container.
RUN MKDIR /app
# Copy whole code to app directory.
COPY Code/build/ /app
# Copy package.json app directory.
COPY package.json /app
# make app directory as the working directory.
WORKDIR /app
# Install dependencies.
RUN npm install -only=production
# Expose the port
EXPOSE 8080
# Start the process
CMD ["npm", "start"]

Docker + React App - Docker run command doesn't open up localhost

I'm having trouble with running Docker (I'm a total beginner at it) with my React app. This was given as a coding challenge so I was given these Docker commands:
docker build --tag app-name .
docker run -it -p 7357:80 app-name
Here is my dockerfile:
FROM node:6-wheezy
WORKDIR /srv/tenor-frontend-test/
COPY package.json package-lock.json ./
RUN npm install
COPY . .
EXPOSE 80
ENV NODE_JS_PORT=80
CMD [ "npm", "start" ]
My start script in package.json:
"start": "PORT=7357 react-scripts start"
Using "npm start" in my terminal opens up localhost fine but when I run the "docker run" command, it prints exactly what happens with "npm start" but no browser opens and localhost:7357 doesn't work.
I'm hoping this is just a dockerfile issue since "npm start" works fine?
Thanks in advance!

On server startup NPM find start script

I'm working with docker and I'm wondering how I can get the command npm start to locate the app.js file without me doing it via the command line.
my package.json (located: /srv/www) looks as so:
{
"name": "dist",
"version": "1.0.0",
"description": "",
"main": "app.js",
"scripts": {
"start": "forever start -c \"nodemon --harmony\" app.js --exitcrash"
},
"author": "",
"license": "ISC"
}
I'm currently invoking my docker image as so:
docker run -d -v /srv/docker/instantynode/srv:/srv -p 80:8080 myimg ???
I am hoping to replace the ??? with a command which will startup node and invoke npm start in the correct directory, any ideas?
I was thinking maybe of making a little startup script to fix this however I was wondering if npm can fix this on it's own?
Regarding node application, you can start with ONBUILD in Dockerfile, so you can try as below:
$ cat Dockerfile
FROM node:0.10-onbuild (or version 0.12, depend your request)
PORT 8000
the image node:0.10-onbuild has codes already as below:
WORKDIR /usr/src/app
ONBUILD COPY package.json /usr/src/app/
ONBUILD RUN npm install
ONBUILD COPY . /usr/src/app
CMD ["npm" "start"]
With ONBUILD, during docker build, your node.js (or angularjs) codes will be not copied to image and npm install doesn't run.
But when you run the container, it starts to copy the files to /usr/src/app and install npm package. Then start npm service.
So in your case, you should be fine to run your application directly without mount the volume every time.
# I guess file package.json is under /srv/docker/instantynode/srv
$ cd /srv/docker/instantynode/srv
$ cat Dockerfile
FROM node:0.10-onbuild
PORT 8000
$ docker build -t myimg .
$ docker run -d -p 8000:8000 myimg
That's all, you should be fine to access your application via port 8000 now.

Resources