Docker does not identify npm install command and throws error - node.js

My Dockerfile is as shown below:
From ubuntu:14.04
WORKDIR /app
#COPY package.json /app/package.json
COPY . /app
RUN npm install
EXPOSE 3000
CMD ["npm","start"]
Now, when I run command sudo docker -t my-app .. It gives me the following error:
Sending build context to Docker daemon 453.6 kB Sending build context
to Docker daemon Step 0 : FROM ubuntu:14.04 ---> 37a9c4a8276c Step 1
: WORKDIR /app ---> Using cache ---> a83d4ef27948 Step 2 : COPY .
/app ---> 1029f5d7d8a3 Removing intermediate container eb9e7ea7f7e6
Step 3 : RUN npm install ---> Running in 5d4f2c05d2d8 /bin/sh: 1:
npm: not found INFO[0000] The command [/bin/sh -c npm install]
returned a non-zero code: 127
Is there anything missing in my Dockerfile?

This is expected. Your image doesn't have node installed, since the base image is ubuntu. You should use the node image as a base image.
From node
WORKDIR /app
#COPY package.json /app/package.json
COPY . /app
RUN npm install
EXPOSE 3000
CMD ["npm","start"]

Your Dockerfile is building vanilla ubuntu FROM ubuntu:14.04 so if you want to use npm/node in your container you'll need to setup node yourself by adding RUN commands to install node, following the install instructions for ubuntu.
Instead of this, you probably want to simply use the official node image found at:
https://hub.docker.com/_/node/
FROM node
or use a specific version/distro like
FROM node:8.4.0-wheezy
(other tags/versions/distros are listed on the docker hub page)

Related

Docker - build fails with operating system is not supported and The command '/bin/sh -c npm install' returned a non-zero code

I try to build an image for a client app (nextjs app), but the build keeps failing.
This is the docker file:
FROM node:12.18.3
WORKDIR /app
ENV PATH /app/node_modules/.bin:$PATH
COPY package.json /app/
COPY package-lock.json /app/
RUN npm install
COPY . /app
RUN npm build
# start app
CMD [ "npm", "start" ]
It fails on the first step with this error:
Step 1/9 : FROM node:12.18.3
operating system is not supported
I followed this post https://stackoverflow.com/a/51071057/9608006 , changed the experimental settings to true, and it did pass the failing step.
but now it fails on the npm i step
npm notice
The command '/bin/sh -c npm install' returned a non-zero code: 4294967295: failed to shutdown container: container c425947f7f17ed39ed51ac0a67231f78ba7239ad199c7df979b3b442969a0a57 encountered an error during hcsshim::System::waitBackground: failure in a Windows system call: The virtual machine or container with the specified identifier is not running. (0xc0370110): subsequent terminate failed container c425947f7f17ed39ed51ac0a67231f78ba7239ad199c7df979b3b442969a0a57 encountered an error during hcsshim::System::waitBackground: failure in a Windows system call: The virtual machine or container with the specified identifier is not running. (0xc0370110)
I also get this warning in the start of this step:
Step 6/9 : RUN npm install
---> [Warning] The requested image's platform (linux/amd64) does not match the detected host platform (windows/amd64) and no specific platform was requested
I use windows 10,
docker v20.10.5
What is the issue ?
EDIT 1 - Folder structure
the following is the base folders layer of the client app
.next
.vercel
components
enums
hooks
node_modules
pages
pubilc
store
styles
utils
.dockerIgnore
.env.local
next.config.js
package.json
server.js
You are trying to build Linux based image under Windows.
It seems there is a problem in multiarch images of nodejs with tags version 12.
Try the answer under the post that you have tried:
Click on the docker icon in the tray and switch into Linux containers.
https://stackoverflow.com/a/57548944/3040844
If you are using docker desktop.. just change docker desktop option for windows containers bydefault to linux containers and run your dockerfile again.
I think that the problem related to your base image , I used this Dockerfile for nextjs app in my side and it's working correctly :
# Dockerfile
# base image
FROM node:alpine
# create & set working directory
RUN mkdir -p /app
WORKDIR /app
# copy source files
COPY . /app
# install dependencies
RUN npm install
# start app
RUN npm run build
EXPOSE 3000
CMD npm run start
I hope that can help you to resolve your issue .
According to your dockerfile
FROM node:12.18.3
WORKDIR /app
ENV PATH /app/node_modules/.bin:$PATH
COPY package.json /app/
COPY package-lock.json /app/
RUN npm install
COPY . /app
RUN npm build
# start app
CMD [ "npm", "start" ]
You missed the right image FROM node:12.18.3
Correct way to do this FROM node:alpine3.12 or FROM ubuntu:18.04
FROM: FROM directive is probably the most crucial amongst all others for Dockerfiles. It defines the base image to use to start the build process. It can be any image, including the ones you have created previously. If a FROM image is not found on the host, Docker will try to find it (and download) from the Docker Hub or other container repository. It needs to be the first command declared inside a Dockerfile
Simplest Dockerfile with Node Image
FROM node:alpine3.12
WORKDIR /usr/src/app
COPY package*.json ./
RUN npm install
COPY ..
RUN npm run build
EXPOSE 3000
CMD npm run start

The command '/bin/sh -c npm install --silent' returned a non-zero code: 254

When m trying to craete image of react app with dockerenter image description herefile its saying...
The command '/bin/sh -c npm install --silent' returned a non-zero code: 254
Here is my dockerfile
# Pull official base image
FROM node:13.12.0-alpine
# Set working directory
WORKDIR /app
# Add `/app/node_modules/.bin` to $PATH
ENV PATH /app/node_modules/.bin:$PATH
# install app dependencies
COPY package.json ./
COPY package-lock.json ./
RUN npm install --silent
RUN npm install react-scripts#3.4.1 -g --silent
# add app
COPY . ./
# start app
CMD ["npm", "start"]
Make sure that npm is reading package.json from the place you're copying it to.
This basically happens when something is broken while docker tries to install JS packages. As the error code "254" stands for all types of installation failures, the only way to get the specific issue is by running the "install" or "ci" command without --silent.

Jenkins,Docker,Kubernetes,AWS EKS: RUN npm install either hangs or produces EAI_AGAIN

First off, I am totally new to deploying CICD builds.
I started with a successful setup of Jenkins X on an AWS EKS Cluster via this
guide.
I am able to run the pipeline via GitHub and builds successfully on a normal jx quickstart.
Problems arose when I started pushing my node express application.
On an alpine node base, my dockerfile looked like this:
FROM node:10.15.3-alpine
RUN mkdir -p /app/node_modules && chown -R node:node /app
WORKDIR /app
COPY package*.json ./
RUN npm ci --prod
FROM alpine:3.7
COPY --from=0 /usr/bin/node /usr/bin/
COPY --from=0 /usr/lib/libgcc* /usr/lib/libstdc* /usr/lib/
WORKDIR /app
COPY --from=0 /app .
EXPOSE 3000
CMD ["node", "server.js"]
And it terminated with an error:
Step 5/14 : RUN npm ci --prod
---> Running in c7f038a80dcc
[91mnpm[0m[91m ERR! code EAI_AGAIN
[0m[91mnpm ERR! errno EAI_AGAIN
[0m[91mnpm ERR![0m[91m request to https://registry.npmjs.org/express/-/express-4.16.4.tgz failed, reason: getaddrinfo EAI_AGAIN registry.npmjs.org registry.npmjs.org:443
[0mtime="2019-03-28T08:26:00Z" level=fatal msg="build failed: building [community]: build artifact: The command '/bin/sh -c npm ci --prod' returned a non-zero code: 1"
I tried using a non alpine base and this was how it looked:
FROM node:10-slim
WORKDIR /usr/src/app
COPY package*.json ./
RUN npm install
COPY . .
ENV PORT 3000
EXPOSE 3000
CMD ["npm", "start"]
But then, the problem was the build hangs (or is taking very long) when it hits the RUN npm install step.
I have scoured for possible answers and duplicate questions but to no avail. So I last resorted into asking it here.
I don't have an idea of what's going on honestly.
I managed to solve this issue by enabling docker bridge network when bootstrapping EKS worker nodes.
#!/bin/bash
set -o xtrace
/etc/eks/bootstrap.sh --enable-docker-bridge true 'your-cluster-name'
More detail in this Github issue: https://github.com/awslabs/amazon-eks-ami/issues/183

npm install not being running on building container

I have a simple node app with the following Dockerfile:
FROM node:8-alpine
WORKDIR /home/my-app
COPY package.json .
COPY ./app ./app
COPY ./server.js ./
RUN rm -rf node_modules
RUN npm install \
npm run build
EXPOSE 3000
When I build the image with: docker build -t my-app:latest ., I attempt to run the app and it complains that some modules are missing.
When I go into the container via docker run -i -t my-app:latest /bin/sh I can see that the packages have not been installed. After manually running npm install in the container, it seems to work.
I can only conclude that from this RUN npm install not being executed correctly inside the container.

Building Docker image for Node application using Yarn dependency

I am trying to build a docker image for a node application that uses yarn to install dependencies. My Dockerfile looks like this:
FROM node:7
WORKDIR /app
COPY package.json /app
RUN yarn install
COPY . /app
CMD npm run develop
EXPOSE 8000
Every thing runs well when I run yarn install on my local machine but when I do a docker build, I get this error that blocks for ever.
**docker build -t rs .**
Sending build context to Docker daemon 219.1MB
Step 1/7 : FROM node:7
---> d9aed20b68a4
Step 2/7 : WORKDIR /reason
---> Using cache
---> fe51a1860989
Step 3/7 : COPY package.json /reason
---> Using cache
---> b0e136ee6eeb
Step 4/7 : RUN yarn install
---> Running in e273f8cf1f3e
yarn install v0.24.4
info No lockfile found.
[1/4] Resolving packages...
Couldn't find any versions for "glamor" that matches "next"
? Please choose a version of "glamor" from this list: (Use arrow keys)
❯ 2.20.40
2.20.39
2.20.38
2.20.37
2.20.36
2.20.35
2.20.34
(Move up and down to reveal more choices)warning glamor#3.0.0-3: abandoned, please use v2 instead
warning gatsby-plugin-glamor > glamor-inline#1.0.5: use glamor/inline instead
warning gatsby-plugin-glamor > glamor-react > glamor#3.0.0-3: abandoned, please use v2 instead
warning gatsby-plugin-glamor > glamor-server > glamor#3.0.0-3: abandoned, please use v2 instead
warning gatsby > babel-preset-es2015#6.24.1: 🙌 Thanks for using Babel: we recommend using babel-preset-env now:
please read babeljs.io/env to update!
The console remains in this stage for ever. How can I fix this please.
You should first run yarn install to generate a yarn lockfile (yarn.lock) before building the image. Then make sure to copy it along with the package.json. Your dockerfile should look like this :
FROM node:7
WORKDIR /app
COPY package.json /app
COPY yarn.lock /app
RUN yarn install
COPY . /app
CMD npm run develop
EXPOSE 8000
With this all dependencies should install successfully when building your image
Dockerfile
FROM node:6.9.5-alpine
RUN mkdir -p /code
WORKDIR /code
ADD . /code
RUN npm install -g -s --no-progress yarn && \
yarn && \
yarn run build && \
yarn cache clean
CMD [ "npm", "start" ]
EXPOSE 8080
docker-compose.yml
version: '2'
services:
sample-app:
image: sample-node-yarn-app
ports:
- "8080:8080"
Create docker image
docker build -t sample-node-app .
RUN
docker-compose up -d
You can simplify the above answers by using a predefined yarn docker image. We are assuming here this image is only for development purpose. For production mode, you should only consider the minimum binaries, such as node.
FROM gmolaire/yarn:1.22.4_12.18.3-alpine3.12
WORKDIR /usr/local/app
ADD . .
RUN yarn install && \
yarn build
EXPOSE 8080
CMD [ "yarn", "run", "develop" ]

Resources