When deploy github action "npm ERR! command sh -c" error raise - docker

I am using github actions, dockerfile to deploy container to AWS ecs.
But npm ERR is always raised after deploy task-definition to ECR.
I checked ECS container on EC2 instance with ssh. And I can API call in a few seconds but container stop soon.
I watched cloudwatch and find logs below.
I tried installing ts-node global, removing node_modules and npm cache... but error not disappear.
How to solve this? And where is the 2021-11-10T13_02_58_148Z-debug.log? I can not find this on container...
npm ERR! path /test
2021-11-10T22:02:58.147+09:00 npm ERR! command failed
2021-11-10T22:02:58.147+09:00 npm ERR! signal SIGTERM
2021-11-10T22:02:58.147+09:00 npm ERR! command sh -c NODE_ENV=prod ts-node app.ts
2021-11-10T22:02:58.156+09:00 npm ERR! A complete log of this run can be found in:
2021-11-10T22:02:58.156+09:00 npm ERR! /root/.npm/_logs/2021-11-10T13_02_58_148Z-debug.log
package.json
{
"name": "test-package",
"version": "1.0.0",
"description": "",
"dependencies": {
"#types/express": "^4.17.13",
"#types/node": "^16.11.7",
"ajv": "^6.12.6",
"aws-sdk": "^2.1015.0",
"dotenv": "^10.0.0",
"express": "^4.17.1",
"hardhat": "^2.6.6",
"power-di": "^2.4.14",
"tslint": "^6.1.3",
"typescript": "^4.4.4"
},
"scripts": {
"start:prod": "NODE_ENV=prod ts-node app.ts",
},
"devDependencies": {
"#types/chai": "^4.2.22",
"#types/mocha": "^9.0.0",
"#types/supertest": "^2.0.11",
"chai": "^4.3.4",
"mocha": "^9.1.3",
"supertest": "^6.1.6"
}
}
FROM node:16-alpine
RUN mkdir /test
WORKDIR /test
COPY . /test
RUN npm update
RUN npm i
RUN npm i -g ganache-cli
RUN npm i -g ts-node
EXPOSE 3000
ENTRYPOINT ["npm", "run", "start:prod"]

Related

gitlab ci runner doesnt know mocha

I have a gitlab.ci with this jobs:
build_image:
stage: build
tags:
- BUILD
script:
# recuperation de la derniere image
- docker pull ${CI_REGISTRY_IMAGE}:${TAG} || true
# build ap artir de la derniere image taggé
- docker build --cache-from ${CI_REGISTRY_IMAGE}:${TAG}
--rm
--pull
--tag ${CI_REGISTRY_IMAGE}:${CI_COMMIT_SHA}
-f cicd/Dockerfile
.
- docker push ${CI_REGISTRY_IMAGE}:${CI_COMMIT_SHA}
only:
- master
- develop
test_api_image:
image: ${CI_REGISTRY_IMAGE}:${CI_COMMIT_SHA}
stage: testbuild
variables:
#on force la connexion au service local
MONGODB_URL: mongodb://mongo:27017/BDD
script:
- npm run cicdtest
tags:
- TEST
only:
- master
- develop
First, in the build job, we build our image with a Dockerfile and we push it on nexus.
In the next job the gitlab runner pull this image, and launch mocha test with "npm run cicdtest"
and we catch this error.
$ npm run cicdtest
> api#0.1.0 cicdtest /builds/data/api
> mocha test/api/**/tests.js --file test/helper --reporter list --exit
sh: mocha: command not found
npm ERR! code ELIFECYCLE
npm ERR! syscall spawn
npm ERR! file sh
npm ERR! errno ENOENT
On my desktop, i pull the same image for test in local and i run and enter the container. When i execute "npm run cicdtest" i have no problems.
Any idea?
For information this is my Dockerfile:
FROM centos:latest
RUN mkdir -p /var/www/myapp
WORKDIR /var/www/myapp
RUN yum update -y \
&& yum install -y gcc gcc-c++ make \
&& curl -sL https://rpm.nodesource.com/setup_14.x | bash - \
&& yum install -y nodejs
COPY ["package.json", "package-lock.json*", "./"]
RUN npm install
COPY . .
EXPOSE 3000
CMD [ "node" , "./src/app.js"]
And my package.json contains:
"dependencies": {
"body-parser": "^1.19.0",
"cors": "^2.8.5",
"crypto-js": "^4.0.0",
"dotenv": "^8.2.0",
"express": "^4.17.1",
"mongoose": "^5.10.13",
"mustache": "^4.2.0",
"nodemailer": "^6.6.0",
"nodemon": "^2.0.6",
"restify": "^8.5.1",
"web-push": "^3.4.4"
},
"devDependencies": {
"chai": "^4.2.0",
"chai-http": "^4.3.0",
"eslint": "^7.26.0",
"eslint-plugin-chai-expect": "^2.2.0",
"husky": "^6.0.0",
"mocha": "^8.3.0",
"supertest": "^6.1.3"
}
Add this line before RUN npm install:
RUN npm install --global mocha
The mocha command works on your local because you installed it globally in the past.
The solution was to add GIT_STRATEGY None because we are testing a built image with code source so we don't need git to checkout source one more time.
test_api_image:
image: ${CI_REGISTRY_IMAGE}:${CI_COMMIT_SHA}
stage: testbuild
variables:
# We do not need GitLab to clone the source code.
GIT_STRATEGY: none
script:
- npm run cicdtest
tags:
- TEST
only:
- master
- develop

How to Dockerize VueJS app for production and skip installation of Dev dependencies

I have the following Dockerfile
FROM node:lts-alpine
WORKDIR /app
COPY package*.json ./
RUN export NODE_ENV=production
RUN npm config set strict-ssl false
RUN npm install --only=prod
RUN npm i #vue/cli-service
COPY . ./
RUN npm run build:prod
RUN npm install -g http-server
EXPOSE 8080
CMD ["http-server" "dist"]
My dev environment includes Cypress for E2E testing, but when the npm i #vue/cli-service command runs, it fails with the following error
Cypress Version: 3.8.3
npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents#^1.2.7 (node_modules/webpack-dev-server/node_modules/chokidar/node_modules/fsevents):
npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for fsevents#1.2.13: wanted {"os":"darwin","arch":"any"} (current: {"os":"linux","arch":"x64"})
npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents#1.2.13 (node_modules/watchpack-chokidar2/node_modules/fsevents):
npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for fsevents#1.2.13: wanted {"os":"darwin","arch":"any"} (current: {"os":"linux","arch":"x64"})
npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents#1.2.13 (node_modules/mochapack/node_modules/fsevents):
npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for fsevents#1.2.13: wanted {"os":"darwin","arch":"any"} (current: {"os":"linux","arch":"x64"})
npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents#2.3.2 (node_modules/fsevents):
npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for fsevents#2.3.2: wanted {"os":"darwin","arch":"any"} (current: {"os":"linux","arch":"x64"})
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! cypress#3.8.3 postinstall: `node index.js --exec install`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the cypress#3.8.3 postinstall script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
npm ERR! A complete log of this run can be found in:
npm ERR! /root/.npm/_logs/2021-03-27T11_53_21_406Z-debug.log
The command '/bin/sh -c npm i #vue/cli-service' returned a non-zero code: 1
ERROR: Job failed: exit status 1
here is my package.json file
{
"name": "todo-app",
"version": "0.1.0",
"private": true,
"scripts": {
"serve": "vue-cli-service serve",
"build:dev": "vue-cli-service build --mode development",
"build:prod": "vue-cli-service build --mode production",
"test:unit": "vue-cli-service test:unit",
"test:e2e": "vue-cli-service test:e2e",
"test:e2e:ci": "vue-cli-service test:e2e --headless",
"lint": "vue-cli-service lint"
},
"dependencies": {
"core-js": "^3.6.5",
"vue": "^2.6.11",
"vue-router": "^3.2.0",
"vuex": "^3.4.0"
},
"devDependencies": {
"#vue/cli-plugin-babel": "~4.5.0",
"#vue/cli-plugin-e2e-cypress": "~4.5.0",
"#vue/cli-plugin-eslint": "~4.5.0",
"#vue/cli-plugin-router": "~4.5.0",
"#vue/cli-plugin-unit-mocha": "~4.5.0",
"#vue/cli-plugin-vuex": "~4.5.0",
"#vue/cli-service": "~4.5.0",
"#vue/eslint-config-prettier": "^6.0.0",
"#vue/test-utils": "^1.0.3",
"babel-eslint": "^10.1.0",
"chai": "^4.1.2",
"eslint": "^6.7.2",
"eslint-plugin-prettier": "^3.3.1",
"eslint-plugin-vue": "^6.2.2",
"node-sass": "^4.12.0",
"prettier": "^2.2.1",
"sass-loader": "^8.0.2",
"vue-template-compiler": "^2.6.11"
}
}
I seem to be in a catch 22. I can't build my VueJS App for production with out having the vue-cli-service, but vue-cli-service needs the dev dependencies installed and I can only install the dev dependencies if I include a lot of extra dependencies for testing etc in my Production Docker container that I don't want or need.
I am building my containers in a GitLab CI Runner.
How do other people get around this issue?
GitLab CI was able to help me solve my problem.
What I did was to build the app in the same stage that I run the tests (The build only runs if all the tests pass). I then made the dist folder available as an artifact that I could use in the docker_build_dev stage, which is then available as a folder I can simply copy into the Docker image file.
You can read more about GitLab Artifacts here https://docs.gitlab.com/ee/ci/pipelines/job_artifacts.html
This has not only speeded up my test and build but also made it simpler.
Here is my .gitlab-ci.yml file
stages:
- test
- build
test:
stage: test
tags:
- docker-test
script:
- npm config set strict-ssl false
- export CYPRESS_INSTALL_BINARY=/cypress/cypress.zip
- npm install
- npm run test:e2e:ci
- npm run test:unit
- npm run build:prod
artifacts:
paths:
- tests/e2e/videos/
- dist
expire_in: 20 minutes
docker_build_dev:
only:
- development
stage: build
tags:
- docker
dependencies:
- test
script:
- docker login -u gitlab-ci-token -p $CI_JOB_TOKEN $CI_REGISTRY
- docker build --tag $CI_REGISTRY_IMAGE:DEV .
- docker push $CI_REGISTRY_IMAGE:DEV
needs: ["test"]
Here is my simplified Dockerfile (I switched to Nginx, but that has nothing todo with the solution).
FROM nginx:stable-alpine
COPY ./dist/ /usr/share/nginx/html/
EXPOSE 80
CMD ["nginx", "-g", "daemon off;"]

npm ERR! in docker-compose

I'm new to docker and i'm trying to build containers from a docker-compose.yml file
version: '3.0'
volumes:
mongodb:
esdb:
driver: local
services:
app:
build: ./
command: npm run dev
container_name: beez-backend
networks:
- custom
volumes:
- ./:/beez-backend/
ports:
- "3030:3030"
- "9229:9229"
external_links:
- mongo
depends_on:
- mongo
env_file:
./dev.env
# MongoDB image config - this container will exposed as external so other container will use it
mongo:
image: mongo
container_name: beez-backend-mongo
ports:
- "27036:27017"
volumes:
- mongodb:/data/db
networks:
- custom
networks:
custom:
external: true
And my Dockerfile
FROM node:dubnium-alpine
LABEL maintainer="XXX"
RUN mkdir -p /beez-backend
RUN npm install nodemon -g
WORKDIR /beez-backend
COPY package.json /beez-backend
RUN npm install
COPY . /beez-backend
EXPOSE 3030
EXPOSE 9229
CMD ["npm","start"]
package.json dependencies -
{
"name": "XXX",
"description": "XXX",
"version": "0.0.0",
"homepage": "",
"main": "src",
"keywords": [
"feathers",
"beez"
],
"author": {
"name": "X",
"email": "X"
},
"contributors": [],
"bugs": {},
"directories": {
"lib": "src",
"test": "test/"
},
"engines": {
"node": "^11.0.0",
"npm": ">= 3.0.0"
},
"scripts": {
"test": "npm run eslint && NODE_ENV= npm run mocha",
"eslint": "eslint src/. test/. --config .eslintrc.json",
"dev": "nodemon -L --delay 2 --inspect=0.0.0.0:9229 src/",
"start": "node src/",
"mocha": "mocha test/ --recursive --exit"
},
"dependencies": {
"#feathersjs/authentication": "^4.5.2",
"#feathersjs/authentication-local": "^4.5.2",
"#feathersjs/authentication-oauth": "^4.5.2",
"#feathersjs/configuration": "^4.5.2",
"#feathersjs/errors": "^4.5.2",
"#feathersjs/express": "^4.5.2",
"#feathersjs/feathers": "^4.5.2",
"#feathersjs/socketio": "^4.5.2",
"axios": "^0.19.2",
"compression": "^1.7.4",
"cors": "^2.8.5",
"cron": "^1.7.2",
"exceljs": "^3.8.1",
"feathers-authentication-hooks": "^1.0.0",
"feathers-authentication-management": "^2.0.1",
"feathers-hooks-common": "^4.20.7",
"feathers-mailer": "^3.0.1",
"feathers-mongoose": "^8.1.0",
"feathers-populate-hook": "^0.5.3",
"feathers-swagger": "^1.1.1",
"firebase-admin": "^8.6.0",
"form-data": "^2.5.1",
"helmet": "^3.21.3",
"lodash": "^4.17.15",
"moment": "^2.24.0",
"mongodb-core": "^3.2.7",
"mongoosastic": "^4.5.1",
"mongoose": "^5.9.3",
"multer": "^1.4.2",
"multer-google-storage": "^1.3.0",
"nodemailer-sendgrid": "^1.0.3",
"nodemailer-sendgrid-transport": "^0.2.0",
"nodemailer-smtp-transport": "^2.7.4",
"npm": "^6.14.2",
"pug": "^2.0.4",
"serve-favicon": "^2.5.0",
"validator": "^11.1.0",
"winston": "^3.2.1"
},
"devDependencies": {
"eslint": "^6.5.1",
"mocha": "^6.2.1",
"nodemon": "^1.19.4",
"request": "^2.88.2",
"request-promise": "^4.2.5"
}
}
It goes through the steps properly until step 7/11 which is RUN npm install. Here is gives the error:
npm ERR! Unexpected end of JSON input while parsing near /..UyIRBcPKVK9LxycWnlezO'
npm ERR! A complete log of this run can be found in:
npm ERR! /root/.npm/_logs/2020-05-21T17_48_59_909Z-debug.log
ERROR: Service 'app' failed to build:
The command '/bin/sh -c npm install' returned a non-zero code: 1
I tried the npm cache clean --force and running docker-compose up but it didn't change. I also tried adding the line RUN npm cache clean right before the RUN npm install in the Dockerfile but it still didn't work. Both the Dockerfile and docker-compose.yml file are on the same directory.
Try below steps :
$ npm cache clean --force
or
$ npm cache verify
$ npm install -g #angular/cli#latest
$ npm install --save-dev #angular/cli#latest
Delete node_modules
Run $ npm install

lerna bootstrap command does not install the local yarn packages

I am trying to install my local npm packages (using yarn workspaces) in my docker container but it does not seem to be installing the local packages. Though it does install the global packages successfully.
Also, when I bash into the container and run "lerna bootstrap", that installs my local packages successfully. I have been searching all over the internet why is this happening.
My docker file's content looks like
FROM node:12.4.0-alpine
RUN apk add --no-cache bash
RUN apk add --no-cache yarn
WORKDIR "/app"
RUN yarn global add lerna
COPY . .
RUN yarn install
RUN lerna bootstrap
CMD ["npm", "run", "dev"]
My root package.json file looks like
{
"name": "my-test",
"version": "1.0.0",
"description": "Test",
"main": "app.js",
"private": true,
"workspaces": [
"packages/**"
],
"scripts": {
"start": "node app.js",
"dev": "nodemon -L app.js"
},
"author": "Phantom",
"license": "ISC",
"dependencies": {
"config": "3.0.1",
"dotenv": "7.0.0",
"express": "4.16.4",
"node-locale": "2.0.0",
},
"devDependencies": {
"lerna": "^3.15.0",
"#lerna/bootstrap": "3.8.5",
"nodemon": "1.18.10",
}
}
My lerna.json file looks like
{
"version": "1.0.0",
"npmClient": "yarn",
"useWorkspaces": true,
"packages": ["packages/*"]
}
As a workaround, I am runing the following docker command once the container is up.
docker exec -w /app <my-container-name> lerna bootstrap
I know this is not a proper solution, so can someone please help me out?

npm ERR! missing script: dev

I have a Jenkins container.
With node v7.2.1 and npm 5.6.0, installed by the command:
apk add nodejs-current
Out of the container, I can run the npm run dev command directly in the folder containing the package.json. It has node v8.11.1 and npm 5.6.0 and build sucess.
Returning to container jenkins
In Jenkins Build, it runs shell ...
command in the same package.json folder:
npm run dev
My package.json:
"scripts": {
"dev": "webpack-dev-server --progress --colors --inline --hot",
"production": "webpack --progress -p"
}
npm ERR! missing script: dev
Build Failed
I saw some solutions here, but I did not succeed.I believe my error is due to the version of nodejs.
If anyone knows how to upgrade nodejs to 8.11.1 or reinstall version 8.11.1 inside the jenkins container. Try nvm, n... fail
Sorry for my english. Google translate save me xD

Resources