Running Node with dumb-init inside a Docker Container - node.js

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"]

Related

can't start node server from CMD

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.

Updating a nodejs image inside of a Dockerfile

I am trying to update the node version that we are using inside the Dockerfile of my application. Currently it is set as v12.21.0. I would like to update it to v16.13.0 as that is the current node LTS version. Please see my dockerfile code below.
So I turned this:
FROM node:12.21.0-alpine as build
RUN apk --update add bash
WORKDIR /usr/src/app
COPY . .
RUN yarn
RUN yarn build
FROM opsline/tools:alpine AS tools
FROM node:12.21.0-alpine
RUN apk --update --no-cache add ca-certificates
WORKDIR /usr/src/app
COPY --from=tools /usr/local/bin/chalk /usr/local/bin/
COPY --from=tools /usr/local/bin/gosu /usr/local/bin/
COPY --from=build /usr/src/app/build ./build
COPY --from=build /usr/src/app/node_modules ./node_modules
COPY --from=build /usr/src/app/server ./server
COPY --from=build /usr/src/app/package.json .
COPY --from=build /usr/src/app/scripts/pre-start.js .
COPY ./docker/entrypoint.sh /entrypoint.sh
RUN chmod +x /entrypoint.sh
ENTRYPOINT ["/entrypoint.sh"]
To This:
FROM node:16.13.0-alpine as build
RUN apk --update add bash
WORKDIR /usr/src/app
COPY . .
RUN yarn
RUN yarn build
FROM opsline/tools:alpine AS tools
FROM node:16.13.0-alpine
RUN apk --update --no-cache add ca-certificates
WORKDIR /usr/src/app
COPY --from=tools /usr/local/bin/chalk /usr/local/bin/
COPY --from=tools /usr/local/bin/gosu /usr/local/bin/
COPY --from=build /usr/src/app/build ./build
COPY --from=build /usr/src/app/node_modules ./node_modules
COPY --from=build /usr/src/app/server ./server
COPY --from=build /usr/src/app/package.json .
COPY --from=build /usr/src/app/scripts/pre-start.js .
COPY ./docker/entrypoint.sh /entrypoint.sh
RUN chmod +x /entrypoint.sh
ENTRYPOINT ["/entrypoint.sh"]
Is it true that all I would have to do is change the node version manually in the code and the rest should fall into place? Any advice is appreciated. Thanks
Changing version will upgrade NodeJS if you are installing
but here your are using it as Base Image so you need to check whether such version is available in docker hub
If it is there we can use but 16.13.0-alpine is not there So I think you need to change version number to any version available at https://hub.docker.com/_/node

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