can't start node server from CMD - node.js

I am building a docker image with these Docker file:
FROM node:14.18.1-alpine as projectbuild
WORKDIR /app
ENV PATH /app/node_modules/.bin:$PATH
COPY . .
ARG NODE_ENV
RUN yarn
RUN yarn build
# production environment
FROM nginx:stable-alpine
WORKDIR /app
COPY --from=projectbuild /app/build /usr/share/nginx/html
COPY --from=projectbuild /app/node_modules /app/node_modules
COPY --from=projectbuild /app/server.js /app
COPY --from=projectbuild /app/package.json /app
RUN rm -rf /etc/nginx/conf.d
RUN apk add --update nodejs npm libc6-compat libstdc++
ADD entrypoint.sh /app
RUN chmod +x /app/entrypoint.sh
COPY conf /etc/nginx
EXPOSE 80
CMD ["sh", "-c", "nginx -g \"daemon off;\" ; npm run server"]
Unfortunately when I run it , only nginx seems to come alive , as it ignore the npm run server command.
I would appreciate any insights into what I am doing wrong.

Related

I have error with docker-compose (exited with code 243)

I'm have dockers error(exited with code 243), after run my docker-compose file
My docker file:
FROM node:16
ARG stage
RUN mkdir -p /app
WORKDIR /app
COPY ./env/${stage}.env .
COPY ./env/test.env .
EXPOSE 3000
CMD npm run start --prefix ./
I change node version from node:16 to node:16.14.2
It's work for me
FROM node:16.14.2
ARG stage
RUN mkdir -p /app
WORKDIR /app
COPY ./env/${stage}.env .
COPY ./env/test.env .
EXPOSE 3000
CMD npm run start --prefix ./

Running Node with dumb-init inside a Docker Container

I am trying to use dumb-init in my docker container but the container OS cannot find the executable. My file is
FROM node:16 AS builder
RUN apt update
RUN apt install dumb-init
WORKDIR /app
COPY package.json .
RUN yarn install
COPY . .
RUN yarn run build
FROM node:16 AS production
WORKDIR /app
COPY --from=builder /app/package.json ./package.json
COPY --from=builder /app/yarn.lock ./yarn.lock
COPY --from=builder /app/dist ./dist
COPY --from=builder /app/node_modules ./node_modules
ENTRYPOINT ["/usr/bin/dumb-init", "--"]
CMD ["node", "dist/main"]
and when I run it
docker: Error response from daemon: failed to create shim: OCI runtime create failed: container_linux.go:380: starting container process caused: exec: "/usr/bin/dumb-init": stat /usr/bin/dumb-init: no such file or directory: unknown.
You final nodejs:16 image is a debian based image, you however need to install dumb-init on it.
RUN apt-get install dumb-init
on debian / ubuntu based images
RUN apk add dumb-init
on Alpine based images
I think a better approach here is to just use the docker image for dumb-init.
For example..
FROM building5/dumb-init:1.2.1 as init
FROM node:16.15.0 as build
COPY package*.json ./
RUN npm install
COPY . .
FROM node:16.15.0 as prod
COPY --from=init /dumb-init /usr/local/bin/
COPY --from=build /usr/src/app/package*.json ./
COPY --from=build /usr/src/app/node_modules ./node_modules
EXPOSE 8080
ENTRYPOINT ["/usr/local/bin/dumb-init", "--"]
CMD ["node", "./server.js"]

RUN yarn doesn't create node_modules inside docker image

I have a dockerfile that should build an image of a node. In the build phase, it builds right, however, in the production phase, the node_modules folder does not appear inside the image.
I'm not mapping any volumes, just trying to build the image. Could anyone help me with this? I don't understand why this is happening.
FROM node:16.14-alpine3.15 as builder
ENV NODE_ENV=development
WORKDIR /home/node/app
COPY package*.json .
COPY tsconfig.json .
RUN yarn install
COPY . .
RUN yarn build
FROM node:16.14-alpine3.15 as production
ENV NODE_ENV=production
RUN mkdir -p /usr/src/app
VOLUME /usr/src/app
WORKDIR /usr/src/app
RUN mkdir logs
COPY package*.json .
COPY yarn.lock .
RUN yarn install --production
RUN ls -la
RUN ls -la node_modules
COPY --from=builder /home/node/app/dist /usr/src/app/dist
EXPOSE 3333
CMD ["yarn", "start"]

How to correctly build optimized (faster builds) for Nextjs production image using Docker

I have struggled to find good examples of images in Nextjs for docker images. The images I have found have not suited my needs. Below is the current image that I am using currently. I am trying to speed it up, I think a way in where I dont have to install twice would be more ideal.
# Install dependencies only when needed
FROM node:14.8.0-alpine3.12 AS deps
RUN apk add --no-cache libc6-compat
WORKDIR /app
COPY package.json ./
RUN apk add git
RUN npm install
RUN mkdir /app/.next
# Rebuild the source code only when needed
FROM node:14.8.0-alpine3.12 AS builder
WORKDIR /app
COPY . .
COPY --from=deps /app/node_modules ./node_modules
RUN npm run build && npm install --production --ignore-scripts --prefer-offline
# Production image, copy all the files and run next
FROM node:14.8.0-alpine3.12 AS runner
WORKDIR /app
RUN addgroup -g 1001 -S nodejs
RUN adduser -S nextjs -u 1001
# You only need to copy next.config.js if you are NOT using the default configuration
COPY --from=builder /app/next.config.js ./
COPY --from=builder /app/public ./public
COPY --from=builder --chown=nextjs:nodejs /app/.next ./.next
COPY --from=builder /app/node_modules ./node_modules
COPY --from=builder /app/package.json ./package.json
COPY --from=builder /app/.env.production ./
USER nextjs
EXPOSE 3000
# Next.js collects completely anonymous telemetry data about general usage.
# Learn more here: https://nextjs.org/telemetry
# Uncomment the following line in case you want to disable telemetry.
# ENV NEXT_TELEMETRY_DISABLED 1
CMD ["npm", "start"]

NPM install in docker container fails and returns "The command '/bin/sh -c npm install' returned a non-zero code: 1"

I'm trying to deploy my dockerized MERN stack web app to my vps with gitlab CI/CD. In my react app dockerfile, when I try to install npm packages, I get this error:
Service 'nowfront' failed to build: The command '/bin/sh -c npm install' returned a non-zero code: 1
This is my Dockerfile:
# pull official base image
FROM node as builder
# make directory
RUN mkdir -p /app
RUN chmod -R 777 /app
# 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 .
RUN npm install
# add app
COPY . .
COPY .env .
RUN ls -li
#RUN yarn build
#CMD ["npm","run","build"]
RUN ls
FROM nginx:stable-alpine
COPY --from=builder /app/build /usr/share/nginx/html
RUN rm /etc/nginx/conf.d/default.conf
COPY nginx.conf /etc/nginx/conf.d/default.conf
EXPOSE 80
CMD ["nginx", "-g", "daemon off;"]
My Ubuntu version is 20.04.1 LTS.
Is there any solution to pass this?
I updated my Dockerfile and added line 14 and it works now.
# pull official base image
FROM node as builder
# make directory
RUN mkdir -p /app
RUN chmod -R 777 /app
# 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
# add app
COPY . .
COPY .env .
RUN ls -li
#RUN yarn build
#CMD ["npm","run","build"]
RUN ls
FROM nginx:stable-alpine
COPY --from=builder /app/build /usr/share/nginx/html
RUN rm /etc/nginx/conf.d/default.conf
COPY nginx.conf /etc/nginx/conf.d/default.conf
EXPOSE 80
CMD ["nginx", "-g", "daemon off;"]

Resources