Docker container with nestjs app exits without any error - node.js

I am trying to dockerize nestjs application. I have to use approach of our devops, so I can't give all details of configuration.
Scripts in package.json typical for any nestjs application.
I have Dockerfile.backend:
FROM some.registry.net/docker/node16 as builder
WORKDIR /opt/app
COPY --chown=app:app ./nestjs/nest-project .
RUN yarn install --non-interactive --production --frozen-lockfile
FROM some.registry.net/docker/node16 as serve
WORKDIR /opt/app
ENV NODE_ENV=production
ENV APP_CONFIG=/opt/app/config/config.yaml
COPY --chown=app:app ./build/Procfile /opt/startup/Procfile
COPY --chown=app:app ./build/config.yaml ./config/config.yaml
COPY --chown=app:app --from=builder /opt/app/ ./
COPY --chown=app:app --from=builder /opt/app/node_modules ./node_modules
USER root
##RUN npm install pm2 -g
##RUN npm install -g nodemon
RUN npm run build
CMD ["/opt/startup/entrypoint.sh"]
And I have docker-compose.yml file:
version: "2"
services:
backend:
build:
context: .
dockerfile: ./build/Dockerfile.backend
command: npm run start
##volumes:
##- ./nestjs/nest-project:/app
##- /app/node_modules
ports:
- 4001:4001
- 9229:9229
environment:
- NODE_ENV=development
- PORT=4001
- REACT_APP_PROD=0
- REACT_APP_BACKEND_URL=http://127.0.0.1:4001
- FRONTEND_URL=http://localhost:4000
- APP_CONFIG=/opt/app/config/config.yaml
frontend:
build: ./frontend
command: npm start
##volumes:
##- ./frontend:/app
##- /app/node_modules
environment:
- NODE_ENV=development
- DISABLE_ESLINT_PLUGIN=true
- REACT_APP_BACKEND_URL=http://127.0.0.1:4001/backend
- PORT=4000
- REACT_APP_PROD=0
ports:
- 4000:4000
So the most interesting point lies in backend command part. I am able to start container only with npm run start:dev command. When I enter there npm start or npm run start:prod
container executes (I see in logs that nestjs app starts, successfully connects to database) and exits without any errors. I tried node dist/main and got the same result. I tried nodemon, with nodemon dist/main nodemon, even with verbose flag shows red line something like app crashed... and gives no more information. I tried pm2 with this command pm2 --name nestjs start npm -- start pm2 successfully starts and container exits without any information.
So far I see problem lies somewhere in my configuration, but I have no clue where to seek. Thanks in advance.

Related

Dev dependencies not getting installed in docker container

I am new to docker. I am trying to create a container for react and express and run both the containers on same network using docker compose.
Below is my dockerfile for frontend:
FROM node:alpine
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
EXPOSE 3000
CMD ["npm","run","start"]
Below is my dockerfile for backend
FROM node:alpine
WORKDIR /app
COPY package*.json ./
RUN NODE_ENV=development npm install
COPY . .
EXPOSE 5000
CMD ["npm","run","server"]
Below is my docker-compose.yml
version: '3'
services:
client:
build:
context: './frontend'
dockerfile: Dockerfile
ports:
- 3000:3000
container_name: react_cont
environment:
- WATCHPACK_POLLING=true
networks:
- mern
volumes:
- ./frontend:/app
depends_on:
- server
server:
build:
context: './backend'
dockerfile: Dockerfile
ports:
- 5000:5000
container_name: express_cont
networks:
- mern
volumes:
- ./backend:/app
networks:
mern:
react container is getting is created and running successfully but the express container is not getting created with an error
sh: nodemon: not found
I had installed nodemon as my dev dependency.
Any help is appreciated. Thanks in advance.
Answering my own Question.
I had installed nodemon globally in my machine but forgot to install it as a dependency for my current project.
Since nodemon was installed globally in my machine i was not getting any errors while i was trying to run my server.js using nodemon. nodemon server.js in scripts did not throw any error while i was in developing my project locally prior moving it to docker container.
But since neither my package.json had nodemon as my dependency and i had not installed it separately in my container nodemon did not get installed and it gave me error.
You can try to delete node_modules folder in your source code and add flag --production=false explicitly to the npm install command. I think it's caching problem.
You may need to install nodemon package globally in your Docker:
RUN NODE_ENV=development npm install && npm --global install nodemon

Unable to enable hot reload for React on docker

I'm new to docker so I'm sure I'm missing something.
I'm trying to create a container with a react app. I'm using docker on a Windows 10 machine.
This is my docker file
FROM node:latest
EXPOSE 3000
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
COPY . /app
WORKDIR /app
CMD ["npm","run", "start"]
and this is my docker compose
version: '3.7'
services:
sample:
container_name: prova-react1
build:
context: .
dockerfile: Dockerfile
volumes:
- '.:/app'
- '/app/node_modules'
ports:
- 3000:3000
environment:
- CHOKIDAR_USEPOLLING=true
- COMPOSE_CONVERT_WINDOWS_PATHS=1
When i start the container if I go on the browser everything is working fine but when i go back to Visual Studio Code and I make a modification to the files and save nothing occurs to the container and even to the website

How do I make a react app in docker-compose? Container is exiting after build steps are complete

I am trying to make a simple react app in docker-compose. I am using this reference What I have done is run npx create-react-app frontend to generate the default react app. Then I added the Dockerfile. This is all in a directory frontend.
#Dockerfile
FROM node:14.9
# 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"]
In the directory above, I have my docker-compose
#docker-compose.yml
version: '3.7'
services:
frontend:
container_name: frontend
build: ./frontend
volumes:
- './frontend:/app'
- '/app/node_modules'
ports:
- 3000:3000
environment:
- CHOKIDAR_USEPOLLING=true
I run docker-compose up --build and after the normal build process I get this message.
docker-compose up --build
Building frontend
Step 1/9 : FROM node:14.9
---> 1b2c72215052
Step 2/9 : WORKDIR /app
---> Using cache
---> 2bab04404275
Step 3/9 : ENV PATH /app/node_modules/.bin:$PATH
---> Using cache
---> ff4ce5f5ec47
Step 4/9 : COPY package.json ./
---> Using cache
---> b1d25c0b6c05
Step 5/9 : COPY package-lock.json ./
---> Using cache
---> 5b829feaf00d
Step 6/9 : RUN npm install --silent
---> Using cache
---> 835367c47253
Step 7/9 : RUN npm install react-scripts#3.4.1 -g --silent
---> Using cache
---> 015ccb2db237
Step 8/9 : COPY . ./
---> Using cache
---> e4a5285339b5
Step 9/9 : CMD ["npm", "start"]
---> Using cache
---> 3f91b16d34d6
Successfully built 3f91b16d34d6
Successfully tagged projectname_frontend:latest
Recreating frontend ... done
Attaching to frontend
frontend |
frontend | > frontend#0.1.0 start /app
frontend | > react-scripts start
frontend |
frontend | ℹ 「wds」: Project is running at http://172.29.0.2/
frontend | ℹ 「wds」: webpack output is served from
frontend | ℹ 「wds」: Content not from webpack is served from /app/public
frontend | ℹ 「wds」: 404s will fallback to /
frontend | Starting the development server...
frontend |
frontend exited with code 0
It seems the container just exits without an error message. I have no idea what is causing this.
Docker version 19.03.12, build 48a66213fe
It is worth noting that I have been able to build my react apps successfully without the solutions below for the past few months. The need for the commands given in the solution has only recently become an issue for me.
I think I solved the issue. Adding stdin_open: true to the docker-compose.yml was half the solution. I also added command: npm start. After this, the container stopped exiting. I thought that the command in the Dockerfile would be sufficient. It seems to be working now.
docker-compose.yml
version: '3.7'
services:
frontend:
container_name: frontend
build: ./frontend
volumes:
- './:/app'
- '/app/node_modules'
ports:
- 3000:3000
stdin_open: true
environment:
- CHOKIDAR_USEPOLLING=true
command: npm start
Dockerfile
FROM node:14.9
WORKDIR /usr/src/app
COPY package*.json ./
RUN npm install
COPY . .
EXPOSE 3000
CMD [ "npm", "start" ]
This simple Dockerfile always works for me:
FROM node:10
WORKDIR /usr/src/app
COPY package*.json ./
RUN npm install
COPY . .
EXPOSE 8083
CMD [ "npm", "start" ]
I also like to add a stdin_open: true to my container in docker-compose.yml.
version: "3.8"
services:
frontend:
build: .
container_name: your-container-name
ports: 4300:4300
volumes:
- ./src:/app/src
- ./public/assets:/app/public/assets
- /app/node_modules
stdin_open: true
tty: true
env-file:
- location/to/your/env-file
I can't comment to Seth Faulkner's solution because of lacking reputation.
I added the stdin_open: true to the docker-compose at first and it still gives the exit with code 0 issue.
However, adding the command: yarn run start in the docker-compose solve the problem for me
stdin_open: true
command: yarn run start

Dockerizing react app

The build folder is ready to be deployed.
You may serve it with a static server:
serve -s build
---> b252a9088991
Removing intermediate container cb5c1e2629c9
Step 16/16 : RUN serve -s build
---> Running in c27b54b31108
serve: Running on port 5000
created and dockerized react application using react-app and getting output like above on "docker-compose up" command
but nothing is showing on http://0.0.0.0:5000/ or http://localhost:5000/
version: '3'
services:
web:
build: .
image: react-cli
container_name: react-cli
volumes:
- .:/app
ports:
- '3000:3000'
above my docker-compose.yml file
FROM scratch
FROM mhart/alpine-node:6.12.0
RUN npm install -g npm --prefix=/usr/local
RUN ln -s -f /usr/local/bin/npm /usr/bin/npm
CMD [ "/bin/sh" ]
ENV NPM_CONFIG_LOGLEVEL warn
RUN npm install -g serve
CMD serve -s build
EXPOSE 3000
COPY package.json package.json
COPY semantic.json semantic.json
COPY npm-shrinkwrap.json npm-shrinkwrap.json
RUN npm install gulp-header --save-dev
RUN npm install --no-optional
COPY . .
RUN npm run build --production
RUN serve -s build
and this is my Dockerfile
Most likely you are not exposing container port on host. I don't see how your compose file looks, but you probably need to add
ports:
- "5000:5000"
To your container definition in compose file.

docker compose run with command error

From this documentation, it seems like that I can execute a single command from a service like this:
docker-compose run SERVICE CMD
But when I run
docker-compose up pwa npm test
I get the error
ERROR: No such service: npm
From my configurations, it will execute npm start, but I'd like to know how to execute other commands.
Files
Dockerfile:
From node:8
WORKDIR /app
copy package.json /app/
RUN npm install --quiet
CMD npm start
docker-compose.yml:
version: '3'
services:
pwa:
build: .
ports:
- '3000:3000'
volumes:
- ./src:/app/src
- ./public:/app/public
Versions
Docker version: 17.03
Docker compose version: 1.11.2
As docs say, the command is docker-compose run, not docker-compose up. The later expects all service names.
Do as this:
docker-compose run pwa npm test

Resources