Docker proper redirect link - node.js

I have Dockerfile:
FROM cloudron/base:0.10.0
ENV PATH /usr/local/node-6.9.5/bin:$PATH
WORKDIR /tmp
COPY package.json /tmp/
RUN npm config set registry http://registry.npmjs.org/ && npm install
WORKDIR /usr/src/app
COPY . /usr/src/app
RUN cp -a /tmp/node_modules /usr/src/app
RUN npm run build
EXPOSE 8000
CMD [ "npm", "run", "start:production" ]
if I run this by:
docker run -p 8000:8000 -t somename/appname
then
going to http://localhost:8000 works fine
but if I want go to http://localhost:8000/about I get error:
Cannot GET /about
What should I do to make it well configured ?
Of course my react app, works fine with such link using in development start.
EDIT:
"scripts": {
"clean": "rimraf dist",
"compile": "node build/scripts/compile",
"build": "npm run clean && cross-env NODE_ENV=production npm run compile",
"start": "cross-env NODE_ENV=development node build/scripts/start",
"start:production": "cross-env NODE_ENV=production node build/scripts/start",
"test": "cross-env NODE_ENV=test karma start build/karma.config",
"test:watch": "npm test -- --watch",
"lint": "eslint .",
"lint:fix": "npm run lint -- --fix"
},
looks like my start:production is not good..

Related

How do I build tsc file with docker?

I am trying to build a typescript project using docker, but there's an error with tsc.
This is my dockerfile
FROM node:alpine AS build
# Create app directory
RUN mkdir -p /app
WORKDIR /app
COPY package.json .
RUN npm install
COPY . .
RUN npm run build:tsc
# RUN npx tsc -p ./tsconfig.json
# Second stage: run things.
FROM node:12
WORKDIR /app
# Install the Javascript dependencies, only runtime libraries.
COPY package.json .
COPY --from=build /app/dist dist
CMD ["npm","run", "start:dev"]
package.json
"start": "node ./dist/server",
"start:dev": "nodemon ./dist/server | bunyan",
"watch:tsc": "tsc --watch -p ./tsconfig.json",
"build:tsc": "tsc -p ./tsconfig.json",
"heroku-postbuild": "npm run build:tsc",
The error I get

Nestjs docker run Error: Cannot find module '/usr/src/app/dist/main'

I built nest app and apply docker for deploy to server
Nest app work as well in local dev environment
I wrote Dockerfile by refer docker tutorial video/article
Dockerfile code below :
# Dockerfile
From node:16
# Specify app directory
WORKDIR /usr/src/app
# Copy package.json and package-lock.json
COPY package*.json ./
# Install dependencies
RUN npm install
# Paste source
COPY . .
# Build
RUN npm run build
# Set port
EXPOSE 3000
# Run app
CMD [ "node", "dist/main"]
# CMD [ "npm", "run", "start:prod"] # for testing
I guess scripts has not changed but write it down in case you don't know.
# package.json (scripts)
"scripts": {
"prebuild": "rimraf dist",
"build": "nest build",
"format": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\"",
"start": "nest start",
"start:dev": "nest start --watch",
"start:debug": "nest start --debug --watch",
"start:prod": "node dist/main",
"lint": "eslint \"{src,apps,libs,test}/**/*.ts\" --fix",
"test": "jest",
"test:watch": "jest --watch",
"test:cov": "jest --coverage",
"test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand",
"test:e2e": "jest --config ./test/jest-e2e.json",
"typeorm": "ts-node ./node_modules/typeorm/cli.js"
},
I understand docker build process the above comments in Dockerfile and write like that
And run docker build ./ -t <name>, docker run <name>
But error occurred docker build and run that Error: Cannot find module '/usr/src/app/main'
Error message below:
% docker run <name>
node:internal/modules/cjs/loader:936
throw err;
^
Error: Cannot find module '/usr/src/app/dist/main'
at Function.Module._resolveFilename (node:internal/modules/cjs/loader:933:15)
at Function.Module._load (node:internal/modules/cjs/loader:778:27)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)
at node:internal/main/run_main_module:17:47 {
code: 'MODULE_NOT_FOUND',
requireStack: []
}
My question/curious is
Why WORKDIR command has not create /usr/src/app directory
Why RUN npm run build has not create bundled file(main.js) in dist directory (I confirmed run npm run build command directly in dev environment, that create main.js in dist directory)
And I tried
Change WORKDIR <path> (access, permissions ..)
Remove node_module, package-lock.json and npm install (re-install dependencies)
Comment out RUN npm run build, CMD ["node", "dist/main"] and add command `CMD ["npm", "run", "start:prod"]
And parameter changes ..
Please advice to me
thanks

Unable to run node.js app through Docker container

I have a node.js application which I am trying to run in docker. Here is the package.json file snippet.
"main": "lib/server.js",
"scripts": {
"clean": "cross-env rm -rf dist",
"build": "cross-env babel lib -d dist",
"start": "npm run clean && npm run build && npm run serve",
"serve": "node dist/index.js",
"dev": "cross-env NODE_ENV=development nodemon lib/server.js --exec babel-node",
"test": "echo \"Error: no test specified\" && exit 1",
"lint": "eslint --ext .js lib/ scripts/ --ignore-pattern node_modules/"
}
And here is the Dockerfile to build the image
#---- Base Node------
FROM node:10.15.1-alpine
RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app
COPY package*.json ./
COPY lib ./lib
RUN npm install --only=production && npm run build
EXPOSE 4201
CMD ["npm", "run", "serve"]
I get the following error when I try to run the image. Here is the screenshot of the error.
The node and npm version are the same on my machine and the base image that I am using to build my image.
When I run the same command locally in my machine it works
Here is the exact command that I am running on my machine and it works.
npm install --only=production
npm run build
npm run serve
Am I doing something wrong here ? Any help is appreciated.
You are using ES6 imports
import x from package
In order to do that, you should have Babel package installed, otherwise it won't know how to import that file

Node Application Heroku Application Error

I am using heroku/nodejs build pack.
this is how my scripts in package.json look like:
"scripts": {
"build": "webpack --config webpack.prod.js",
"server": "http-server public -p 3000 -a localhost -c 0",
"prod": "npm run build && npm run server",
"dev": "webpack-dev-server --config webpack.dev.js",
"lint": "eslint . --fix"
},
In procfile I have only one line of code:
web: npm run prod
Any ideas why am I seeing application error?
In regards to the error, I think that you should be using the environment variable $PORT i.e. http-server public -p $PORT -c 0 Make sure you have http-server in you deps and not your devDeps.
This might not be causing the error but you shouldn't do your build on run. Instead add the build step to a postinstall NPM script like below.
"scripts": {
"build": "webpack --config webpack.prod.js",
"postinstall": "npm run build",
"server": "http-server public -p $PORT -c 0",
"prod": "npm run server",
"dev": "webpack-dev-server --config webpack.dev.js",
"lint": "eslint . --fix"
},
This will ensure that your build gets run on deployment and not every time the node process starts.

Allocation failed - process out of memory

I am going to compile web-pack i have got issue in that i.e Allocation failed - process out of memory .I know i can increase the space size using this
--max_old_space_size=8192
i tried many ways to fix this but nothing is working.
here is my package.json
"scripts": {
"karma": "karma",
"test": "karma start",
"rimraf": "rimraf",
"tslint": "tslint",
"typedoc": "typedoc",
"webpack": "webpack --progress --profile --bail",
"webpack-dev-server": "webpack-dev-server",
"webdriver-manager": "webdriver-manager",
"clean": "npm cache clean && npm run rimraf -- node_modules doc coverage dist compiled dll",
"clean:dist": "npm run rimraf -- dist",
"clean:dll": "npm run rimraf -- dll",
"clean:electron": "npm run rimraf -- build",
"preclean:install": "npm run clean",
"clean:install": "npm set progress=false && npm install",
"preclean:start": "npm run clean",
"clean:start": "npm start",
"watch": "npm run watch:dev",
"watch:dev": "npm run build:dev -- --watch",
"watch:dev:hmr": "npm run watch:dev -- --hot",
"watch:prod": "npm run build:prod -- --watch",
"build": "npm run build:dev",
"prebuild:dev": "npm run clean:dist",
"build:dev": " npm run clean:dist && npm run webpack -- --config config/webpack.dev.js",
"prebuild:prod": "npm run clean:dist",
"build:prod": "npm run clean:dist && webpack --config config/webpack.prod.js --progress --profile",
"server": "npm run server:dev",
"server:dev": "webpack-dev-server --config config/webpack.dev.js --progress --profile --watch --content-base src/",
"server:dev:hmr": "npm run server:dev -- --inline --hot",
"server:prod": "http-server dist -c-1 --cors",
"server:test": "http-server dist-demo -c-1 --cors",
"server:prod:ci": "http-server dist -p 3000 -c-1 --cors",
"webdriver:update": "npm run webdriver-manager update",
"webdriver:start": "npm run webdriver-manager start",
"lint": "npm run tslint \"src/**/*.ts\" --force",
"ngc": "./node_modules/.bin/ngc-w -p tsconfig.webpack.json",
"pree2e": "npm run webdriver:update -- --standalone",
"pretest": "npm run lint",
"docs": "npm run typedoc -- --options typedoc.json --exclude '**/*.spec.ts' ./src/",
"gh-pages": "wintersmith build -C docs && gh-pages -d docs/build",
"start": "npm run server:dev",
"start:hmr": "npm run server:dev:hmr",
"version": "npm run build",
"postversion": "git push && git push --tags",
"build:electron": "npm run build:electron.full",
"prebuild:electron.full": "npm run clean:electron",
"build:electron.full": "npm run build:electron.renderer && npm run build:electron.main",
"postbuild:electron.full": "npm run electron:start",
"build:electron.renderer": "npm run webpack -- --config config/electron/webpack.renderer.prod.js",
"build:electron.main": "npm run webpack -- --config config/electron/webpack.electron.prod.js",
"electron:start": "electron build",
"build:aot:prod": "npm run clean:dist && npm run clean:aot && webpack --config config/webpack.prod.js --progress --profile --bail",
"build:aot": "npm run build:aot:prod",
"clean:aot": "npm run rimraf -- compiled",
"build:ci": "npm run build:prod && npm run build:aot"
},
and the command for running my package.json is
npm server
please help my out how i increase size.
I had similar issue. I was using node version 8.x. Downgrading it to 6.11.0 resolved my issue.

Resources