Updating a nodejs image inside of a Dockerfile - node.js

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

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.

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

how do a dockerfile for React JS, Next js , npm project

I'm making an app with React JS, Next Js, npm and also I would have to change the .npmrc for it to run.
I don't know how I could make a DockerFile for these technologies and at the same time this dockerfile has to change the .npmrc
my docker file
FROM node:lts as dependencies
WORKDIR /emercore-arg-manager
COPY package.json yarn.lock ./
RUN echo "#lala-lalal:registry=https://npm.pkg.github.com/" >> ~/.npmrc
RUN echo "//npm.pkg.github.com/:_authToken=asdasdasdasdasdsad" >> ~/.npmrc
RUN echo "//registry.npmjs.org/:_authToken=assdasdasdasdsaasd" >> ~/.npmrc
RUN yarn install --frozen-lockfile
FROM node:lts as builder
WORKDIR /emercore-arg-manager
COPY . .
COPY --from=dependencies /emercore-arg-manager/node_modules ./node_modules
RUN yarn build
FROM node:lts as runner
WORKDIR /emercore-arg-manager
ENV NODE_ENV production
# If you are using a custom next.config.js file, uncomment this line.
# COPY --from=builder /my-project/next.config.js ./
COPY --from=builder /emercore-arg-manager/public ./public
COPY --from=builder /emercore-arg-manager/.next ./.next
COPY --from=builder /emercore-arg-manager/node_modules ./node_modules
COPY --from=builder /emercore-arg-manager/package.json ./package.json
EXPOSE 3000
CMD ["yarn", "start:dev"]
It does not work for me, and I think it is a lot of content for a dockerfile with these technologies, could someone help me to put together a shorter one and make it work?
The command that i use in my deskpot is yarn install and yarn start:dev (and its working)

Resources