App Engine DEADLINE_EXCEEDED (NestJS server) - google-app-engine

When running gcloud app deploy, everything seems to be fine, except for the fact the running server (trigger by npm run start) prevents the deployment to finish.
That should explain why I have the following error:
I'm not sure what I'm doing wrong here, the server is successfully up and running but I don't understand what I should do to make the deployment successful.
app.yaml:
runtime: custom
env: flex
Dockerfile
FROM node:14
RUN apt-get update && apt-get install -y postgresql-client
WORKDIR /usr/src/app
COPY package.json /usr/src/app
RUN npm install
COPY . /usr/src/app
COPY prisma ./prisma/
EXPOSE 3000
RUN npm run prisma:generate
RUN npm run build
RUN npm run start:prod
Package.json
{
"name": "mango3-api",
"version": "0.0.1",
"description": "",
"author": "",
"private": true,
"license": "UNLICENSED",
"main": "./dist/src/main.js",
"scripts": {
"build": "nest build",
"start": "node dist/main",
},
"prisma": {
"seed": "ts-node prisma/seed.ts"
}
}
Thanks for your help guys

Related

Dockerizing react for production not working

I have created a Dockerfile and Docker-compose.yml for containerzing my create-react-app to production. My problem of understanding is how should I create the production build if the scripts that are needed to run the command are dev-dependencies?
Also I can`t find anything on deployment docs.
> [builder 7/7] RUN yarn build:
#13 0.507 yarn run v1.22.19
#13 0.556 $ react-scripts build
#13 0.579 /bin/sh: react-scripts: not found
#13 0.590 error Command failed with exit code 127.
#13 0.590 info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
------
executor failed running [/bin/sh -c yarn build]: exit code: 127
ERROR: Service 'app' failed to build : Build failed
`docker-compose` process finished with exit code 1
package.json
{
"version": "0.1.0",
"private": true,
"dependencies": {
"react": "^18.1.0",
"react-dom": "^18.1.0",
"react-i18next": "^11.17.2",
"react-router-dom": "^6.3.0",
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build", <--- THIS WILL BE USED FOR PRODUCTION
"test": "react-scripts test",
"eject": "react-scripts eject"
},
"devDependencies": {
"react-scripts": "5.0.1", <--- THIS WILL NOT BE INSTALLED WITH --production FLAG
"tailwindcss": "^3.0.24",
"typescript": "^4.6.3"
}
}
Dockerfile
FROM node:18-alpine AS builder
ENV NODE_ENV production
# Add a work directory
WORKDIR /app
# Cache and Install dependencies
COPY package.json .
COPY yarn.lock .
RUN yarn install --production <---- THIS DOES NOT INSTALL DEV DEPENENCIES
# Copy app files
COPY . .
# Build the app
RUN yarn build <---- THIS CAUSES THE CRASHING
# ---------------------------------------------------
# Bundle static assets with nginx
FROM nginx:1.23.1-alpine AS production
ENV NODE_ENV production
# Copy built assets from builder
COPY --from=builder /app/build /usr/share/nginx/html
# Add your nginx.conf
COPY nginx.conf /etc/nginx/conf.d/default.conf
# Expose port
EXPOSE 80
# Start nginx
CMD ["nginx", "-g", "daemon off;"]
docker-compose.yml
version: "3.8"
services:
app:
container_name: my_app
build:
context: .
target: production
dockerfile: Dockerfile
In the final image stage, you're only COPY --from=builder the /app/build directory but not any of the node_modules tree. It doesn't matter in the initial stage if you have development or production dependencies, since they're not going to be in the final image.
Since you do need the devDependencies at this point, I'd remove the yarn install --production option. If it makes a difference to your build environment you can set NODE_ENV=production after you install the dependencies.
RUN yarn install # not --production
COPY . .
# ENV NODE_ENV=production
RUN yarn build

ReactJS + Docker-Compose ERROR | "Could not find a required file"

I have a ReactJS application which has been dockerized. The application is built into a docker image and run on a container for development also using docker-compose tool. This works well when I use it with Docker for Windows. Below is the package.json, it is a very old reactjs scaffolding.
{
"name": "client",
"version": "0.1.0",
"private": true,
"dependencies": {
"axios": "^0.18.0",
"react": "^16.8.0",
"react-dom": "^16.8.0",
"react-router-dom": "^4.3.1",
"react-scripts": "2.1.2",
"react-listbox":"^1.2.5"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
"eslintConfig": {
"extends": "react-app"
},
"browserslist": [
">0.2%",
"not dead",
"not ie <= 11",
"not op_mini all"
],
"devDependencies": {
"enzyme": "^3.8.0",
"enzyme-adapter-react-16": "^1.7.1"
}
}
Folder Structure of the Client Code (Approximate Idea) -
client
-- public
- index.html
-- src
- App.js
package.json
Dockerfile
# base image
FROM node:11.6.0-alpine
# set working directory
WORKDIR /usr/src/app
# add `/usr/src/app/node_modules/.bin` to $PATH
ENV PATH /usr/src/app/node_modules/.bin:$PATH
# install and cache app dependencies
COPY package.json /usr/src/app/package.json
RUN npm install
# start app
CMD ["npm", "start"]
docker-compose-dev.yml
version: '3.7'
services:
client:
build:
context: ./services/client
dockerfile: Dockerfile
volumes:
- './services/client:/usr/src/app' #(map) outside: inside the docker
- '/usr/src/app/node_modules'
ports:
- 3007:3000
environment:
- NODE_ENV=development
But when I try to push this docker image to a repository and then use it to deploy into a cloud container (possibly, a unix based system), the deployment fails with NPM ERR (as described below).
STDERR
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! client#0.1.0 start: `react-scripts start`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the client#0.1.0 start 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/2020-03-11T03_53_28_432Z-debug.log
STDOUT
> client#0.1.0 start /usr/src/app
> react-scripts start
Could not find a required file.
Name: index.html
Searched in: /usr/src/app/public
It fails saying the index.html is not found when I am quite sure, the file is under /public/ which gets mounted to the dockerized image.
NOTE - I know the fact that this is not the way the reactjs code should be deployed on server. It should be through a web server like nginx. But the legacy team had little knowledge about the front end development. Please help regardless.
You are mounting client folder as a volume in the docker image. So, where ever you deploy the docker image, it will expect the volume to be mounted. Instead, you can copy the folder to docker image and make it self contained.
FROM node:11.6.0-alpine
# set working directory
WORKDIR /usr/src/app
# add `/usr/src/app/node_modules/.bin` to $PATH
ENV PATH /usr/src/app/node_modules/.bin:$PATH
# install and cache app dependencies
COPY public /usr/src/app/public
COPY src /usr/src/app/src
COPY package.json /usr/src/app/package.json
RUN npm install
# start app
CMD ["npm", "start"]
your docker-compose-dev.yml will be:
version: '3.7'
services:
client:
build:
context: ./services/client
dockerfile: Dockerfile
ports:
- 3007:3000
environment:
- NODE_ENV=development

Docker stuck at npm install

I have a project that I want to dockerize. I run npm install and npm build with no problem on my computer, but it has some problems when I build with Docker.
Docker output:
Sending build context to Docker daemon 56.96MB
Step 1/7 : FROM node:12.2.0-alpine
---> f391dabf9dce
Step 2/7 : WORKDIR /app
---> Using cache
---> b50a8efbf074
Step 3/7 : ENV PATH /app/node_modules/.bin:$PATH
---> Using cache
---> 3358967a13ab
Step 4/7 : COPY package.json /app/package.json
---> Using cache
---> 851ac31a0adb
Step 5/7 : RUN npm install
---> Running in 8cc36a435cec
npm WARN deprecated core-js#1.2.7: core-js#<2.6.8 is no longer maintained. Please, upgrade to core-js#3 or at least to actual version of core-js#2.
It is stuck in this here:
Dockerfile:
# base image
FROM node:12.2.0-alpine
# set working directory
WORKDIR /app
# add `/app/node_modules/.bin` to $PATH
ENV PATH /app/node_modules/.bin:$PATH
# install and cache app dependencies
COPY package.json /app/package.json
RUN npm install
RUN npm install react-scripts#3.0.1 -g --silent
# start app
CMD ["npm", "start"]
I have done this with other dockerfiles, but the result was the same.
package.json:
{
"name": "front",
"version": "0.1.0",
"private": true,
"dependencies": {
"axios": "^0.18.0",
"bootstrap": "^4.3.1",
"express": "^4.17.0",
"react": "^16.8.6",
"react-dom": "^16.8.6",
"react-router": "^5.0.0",
"react-router-dom": "^5.0.0",
"react-scripts": "2.1.8"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
"eslintConfig": {
"extends": "react-app"
},
"browserslist": [
">0.2%",
"not dead",
"not ie <= 11",
"not op_mini all"
]
}
So, this might be a temporary solution till this is properly addressed, but you can use
npm config set registry http://registry.npmjs.org/
I have used it for docker environment and it worked fine.
when ever you run docker run my-image it will run in a new container and previous container remain unused,
we can use docker build location-of-the-Dockerfile --no-cache switch to not using cached images but it didn't work for me
in my case, removing unused containers and images fixed the problem ,
remove all containers > docker rm $(docker ps -aq)
remove all images if you want >> docker image rm $(docker images -q)
I had the same issue. I just waited for 1 hour and it went to the next step.
Edit :
While building the npm install downloads all the packages and it takes time to do that, it depends on the size of your application how long will it take.

How to setup new reactjs project on local environment or system?

I started to learn reactjs for front end in web development. For learning purpose I install node and npm on my local system below are the versions of node and npm
node v8.11.3
npm v5.6.0
and I have run bellow command
npm init
and create pakage.json following file using command
{
"name": "loginpage",
"version": "0.1.0",
"private": true,
"dependencies": {
"react": "^15.4.2",
"react-dom": "^15.4.2"
},
"devDependencies": {
"react-scripts": "0.9.3"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test --env=jsdom",
"eject": "react-scripts eject"
},
"main": "index.js",
"author": "rizwan",
"license": "ISC",
"description": ""
}
in next step i run following command install node and react dependencies
npm install
and then I run create-react-app hello-world commands but I did't find any change in my current local project folder exception pakage.json file I don't know that where I am doing mistake. Guide in right direction.
Create-react-app creates everything from scratch, you don't need to create a folder or npm init, anything like that.
Just run create-react-app my-project and you'll have everything you need in the my-project folder.
To create react app
first, install create-react-app globally
then, run these commands
create-react-app app-name
cd app-name
npm run start
I prefer to use Yarn instead of npm

understanding npm script (webpack-dev-server)

I have a question about npm run.
('npm run dev' is from https://github.com/vuejs/vue-hackernews/blob/gh-pages/package.json)
{
"name": "vue-hackernews",
"version": "1.0.0",
"description": "HN clone with Vue.js using HN API",
"scripts": {
"dev": "webpack-dev-server --inline --hot --no-info",
"build": "cross-env NODE_ENV=production webpack --progress --hide-modules"
....
},
why does 'npm run dev' go well,
but does other commands like 'npm run webpack-dev-server'
or just 'webpack-dev-server' throw errors?
what does 'npm run' do? not just executing the value of the property of "scripts"?
( which I was thinking 'command exactly same thing')
thank you!
just 'webpack-dev-server' throw errors?
because in order for it to work, webpack-dev-server has to be added to the PATH environment variable. If you use npm run script-name, then:
In addition to the shell's pre-existing PATH, npm run adds
node_modules/.bin to the PATH provided to scripts.
check node_modules/.bin folder, you'll see webpack-dev-server there, and this executable runs the js package like this:
node "$basedir/../webpack-dev-server/bin/webpack-dev-server.js" "$#"
Another alternative is this if you're on Unix based env:
$(npm bin)/webpack-dev-server'

Resources