Installing fresh react app with dockerfile - node.js

I'm trying to run a fresh installation of React using a Dockerfile.
FROM node:12.14-alpine
RUN mkdir /app
WORKDIR /app
RUN npm install -g nodemon \
npx create-react-app .
COPY package.json package.json
RUN npm install --silent
COPY . .
CMD [ "node", "start" ]
My root folder is empty so no package.json file exists etc. When I run docker-compose up --build I get the following error which indicates that's failing on npx create-react-app .
Step 5/9 : RUN npm install -g nodemon npx create-react-app .
---> Running in c4878af5f94d
npm ERR! code ENOLOCAL
npm ERR! Could not install from "" as it does not contain a package.json file.
npm ERR! A complete log of this run can be found in:
npm ERR! /root/.npm/_logs/2020-02-18T12_55_14_899Z-debug.log
ERROR: Service 'react' failed to build: The command '/bin/sh -c npm install -g nodemon npx create-react-app .' returned a non-zero code: 1
Any ideas what I'm doing wrong? I'm not having node or npm installed nativelly so all needs t run through docker

You need to delete the . at the end of the RUN :
RUN npm install -g nodemon \
npx \
create-react-app

You forgot "&&" at the beginning line 7
FROM node:12.14-alpine
RUN mkdir /app
WORKDIR /app
RUN npm install -g nodemon \
&& npx create-react-app .
COPY package.json package.json
RUN npm install --silent
...
CMD [ "node", "start" ]

Related

Node.js service Timeout issues

The Node.js API service hosted on AWS times out at intervals and crashes the server. When I checked Cloudwatch logs, I saw this error come up often.
npm ERR! path /app
npm ERR! command failed
npm ERR! signal SIGTERM
npm ERR! command sh -c -- npm run build && node dist/server.js
npm ERR! A complete log of this run can be found in:
npm ERR! /root/.npm/_logs/2022-11-11T11_54_06_397Z-debug-0.log
Here's my docker file
#!/bin/sh
FROM node:16-bullseye-slim
RUN apt-get update; \
apt-get install -y python3; \
apt-get install -y python2 make; \
apt-get install -y g++; \
ln -s /usr/bin/python2.7 /usr/bin/python;
ARG app_env
ARG DOPPLER_TOKEN
ARG DOPPLER_PROJECT
ARG DOPPLER_CONFIG
ENV NODE_ENV=${app_env}
ENV DOPPLER_TOKEN=${DOPPLER_TOKEN}
ENV DOPPLER_PROJECT=${DOPPLER_PROJECT}
ENV DOPPLER_CONFIG=${DOPPLER_CONFIG}
WORKDIR /app
COPY package*.json ./
RUN npm install
RUN npm ci --only=production
COPY . .
CMD ["npm", "run", "start:server"]
EXPOSE 3000
My package.json file start script
"start:server": "npm run build && node dist/server.js"
I'm using Node version: v16.13.2
Npm version: 8.19.0
Not sure the cause of this issue.

Docker: an error happened: ffmpeg exited with code 127

As I mentioned above, when I run the container, I get an error. When I run it normally, it works, there is no problem.
my container run code: docker run --rm -i -t -p 8080:4444 9fa71c8ca9ba
the error i got;
an error happened: ffmpeg exited with code 127
APP.JS
https://pastebin.ubuntu.com/p/znCRWz2rgx/
Dockerfile:
FROM node:10
COPY . /app
WORKDIR /app
RUN npm install
RUN npm install fluent-ffmpeg
RUN npm install os
RUN npm install express
RUN npm install --save #google-cloud/text-to-speech
RUN npm install --save #google-cloud/translate
RUN export GOOGLE_APPLICATION_CREDENTIALS="dogwood-cipher-307314-4074350f02b8.json"
CMD ["node", "app.js"]

Docker node Error: 'darwin-x64' binaries cannot be used on the 'linux-x64' platform

I am trying to run a container based on my app image by using docker and i getting the next error:
/app/node_modules/sharp/lib/libvips.js:68
throw new Error('${vendorPlatformId}' binaries cannot be used on the '${currentPlatformId}' platform. Please remove the 'node_modules/sharp' directory and run 'npm install' on the '${currentPlatformId}' platform.);
Error: 'darwin-x64' binaries cannot be used on the 'linux-x64' platform. Please remove the 'node_modules/sharp' directory and run 'npm install' on the 'linux-x64' platform.
My Dockerfile:
FROM node:10.20.1
WORKDIR /app
COPY package.json .
RUN npm install -g node-gyp
RUN npm install
EXPOSE 8080
CMD [ "npm", "start" ]
I tryied to add the next Commands to my Dockerfile but i am getting the same error:
Second attempt Dockerfile:
FROM node:10.20.1
WORKDIR /app
COPY package.json .
RUN npm install -g node-gyp
RUN rm -rf node_modules/sharp
RUN npm install --arch=x64 --platform=linux --target=8.10.0 sharp
EXPOSE 8080
CMD [ "npm", "start" ]
The docker command I using locally on my mac in order to run that container is:
docker run -d -p 8080:8080 --env-file ./.env --name server-dev-1 server:1
Any ideas?

Copy output of npm install to docker container

I dockerized node.js and all works fine
Dockerfile:
FROM node:alpine
WORKDIR '/app'
COPY package.json .
RUN npm install
COPY . .
EXPOSE 9000
CMD ["npm", "run", "dev"]
I'm trying to run npm install outside Dockerfile and to copy content of npm install to docker container
On docker host i ran
npm install --prefix /opt/npm/ -g
Folder /opt/npm/lib/node_modules/ui is created. In that folder there are bunch json files and folder node_modules.Dockerfile is in that foler.Now, in Dockerfile i skipped npm install and just copied content of /opt/npm/lib/node_modules/ui to docker container.
Modified Dockerfile
FROM node:alpine
WORKDIR '/app'
COPY . .
EXPOSE 9000
Built image from Dockerfile sucessfully, but when trying to run container from that image
docker run -p 9000:4200 pm
> ui#0.0.0 dev /app
> ng serve --host 0.0.0.0 --proxy-config src/proxy.conf.json
sh: ng: not found
npm ERR! code ELIFECYCLE
npm ERR! syscall spawn
npm ERR! file sh
npm ERR! errno ENOENT
npm ERR! ui#0.0.0 dev: `ng serve --host 0.0.0.0 --proxy-config src/proxy.conf.json`
npm ERR! spawn ENOENT
npm ERR!
npm ERR! Failed at the ui#0.0.0 dev script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
npm WARN Local package.json exists, but node_modules missing, did you mean to install?
Is it possible to run npm install outside docker container ?
When dockerizing any application you should always compile and install dependencies in the docker container.
Your Docker file start form node:alpine. this means that when you install an npm package that needs compilation outside (your OS) the alpine OS won't be able to use this.
Best practice is to always build your application on the same OS. That's way docker introduce build container.
# Dockerfile
FROM node:12.13-alpine As build
WORKDIR /usr/src/app
COPY package*.json ./
RUN npm install --only=production
FROM node:12.13-alpine as production
WORKDIR /usr/src/app
COPY ./ ./ # copy static files
COPY --from=build /usr/src/app/node_modules ./. # Copy node_modules from build container
EXPOSE 3000
CMD ["node", "main.js"]
# .dockerignore
node_modules
Dockerfile
Try to fit this to your environment

npm link package name must have a name field to be linked

Hi I am trying to create a docker image.
Below is a part of my docker file which is causing problem.I think the problem is due to nodejs package.
The current softlink is:
ls -la "/usr/bin/npm"
lrwxrwxrwx 1 root root 25 Mar 2 01:50 /usr/bin/npm -> /usr/lib/node_modules/npm
Installation is does by a nonroot user
Docker File
#Install node js and npm
RUN curl -sL https://deb.nodesource.com/setup_6.x | sudo -E bash -
RUN apt-get -y install nodejs
RUN npm install -g bower gulp
RUN npm install -g node-gyp#latest --save
RUN npm init -y
RUN npm install -g n
RUN npm install zmq --save
RUN ldconfig
RUN npm config set prefix ~/npm
#RUN npm install -g stf t
#RUN npm install
#RUN rm -rf /usr/bin/npm
#RUN ln -s /usr/lib/node_modules/npm/ /usr/bin/npm
RUN npm link
Below is the docker build output:
Step 35/42 : RUN npm init -y
---> Running in abf773c34e35
Wrote to /package.json:
{
"name": "",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC"
}
Step 38/42 : RUN ldconfig
---> Running in 1562e282d324
Removing intermediate container 1562e282d324
---> bdf746069a90
Step 39/42 : RUN npm config set prefix ~/npm
---> Running in 400512f792d0
Removing intermediate container 400512f792d0
---> 0f3eeb03910c
Step 40/42 : RUN npm link
---> Running in 40ae29419291
npm ERR! Linux 4.4.0-98-generic
npm ERR! argv "/usr/bin/node" "/usr/bin/npm" "link"
npm ERR! node v6.13.0
npm ERR! npm v3.10.10
npm ERR! Package must have a name field to be linked
npm ERR!
npm ERR! If you need help, you may report this error at:
npm ERR! <https://github.com/npm/npm/issues>
npm ERR! Please include the following file with any support request:
npm ERR! /npm-debug.log
The command '/bin/sh -c npm link' returned a non-zero code: 1
Any help would be appreciated.Thanks
The problem is that the generated package.json does not have the name property set "name": "",.
As documented in npm-link:
Note that package-name is taken from package.json, not from directory name
The reason why name is empty in package.json is that you are running npm init in the root / of the container.
You have to create a directory and run npm init in there.
...
WORKDIR /app
RUN npm init -y
...

Resources