Docker not installing packages, error says: sh: webpack: not found - docker

I am trying to run docker, im new to it, it is supposed to install the packages and then copy them to the docker instance.
But when it runs npm start, it complains that it doesnt find webpack(which is set as a dependency on package.json) which should have been installed.
This is the dockerfile:
FROM node:18.2.0-alpine as dependencies
WORKDIR /app
COPY ./app/package.json /app/package.json
COPY ./app/package-lock.json /app/package-lock.json
RUN if [ ! -d /app/node_modules ]; then npm install; else echo "skip install"; fi
FROM node:18.2.0-alpine as build
WORKDIR /app
COPY --from=dependencies /app/node_modules /app/node_modules
COPY app/ /app/
CMD ["npm", "start"]
And this is the folder structure:
Any ideas what could be the issue?

Related

Connection refused error to docker container from browser

I have a server.js file in javascript
it works OK
when I can create a image but I cannot connect to it from my web browser
(Cannot connect error)
Dockerfile
FROM node:16
WORKDIR /G/Docker/docker_node_1
# Copy to docker container
COPY package*.json ./
RUN npm install
# For production
#RUN npm ci --only=production
COPY . .
EXPOSE 8090
CMD ["node", "server.js"]
server.js file
FROM node:16
WORKDIR /G/Docker/docker_node_1
# Copy to docker container
COPY package*.json ./
RUN npm install
# For production
#RUN npm ci --only=production
COPY . .
EXPOSE 8090
CMD ["node", "server.js"]

how to alias npm with node? Cannot run npm without 'node npm'

I use multistage build with a busybox shell to copy node files in scratch.
I am able to run node, and npm with node npm command. However, I am unable to run node npm without being in the same path getting this error:
/ # node npm
node:internal/modules/cjs/loader:949
throw err;
^
Error: Cannot find module '/npm'
at Module._resolveFilename (node:internal/modules/cjs/loader:946:15)
at Module._load (node:internal/modules/cjs/loader:787:27)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:77:12)
at node:internal/main/run_main_module:17:47 {
code: 'MODULE_NOT_FOUND',
requireStack: []
}
Node.js v18.4.0
How do I correctly alias node npm so I can run npm without adding node infront?
FULL BUILD:
FROM amd64/ubuntu:20.04#sha256:b2339eee806d44d6a8adc0a790f824fb71f03366dd754d400316ae5a7e3ece3e as USER
# Create User in Ubuntu
RUN useradd -u 10001 node
RUN mkdir /home/node/.npm-global/bin -p
FROM node:alpine3.16 AS node
WORKDIR /app/
COPY ["package.json", "package-lock.json", "./"]
COPY ["src/", "./src/"]
COPY ["/public", "./public/"]
RUN ["npm", "install"]
RUN ["npm", "run", "build"]
FROM scratch
# Export Path
ENV PATH="/bin:/usr/local/bin:${PATH}"
# Copy User Files
COPY --from=USER /etc/passwd /etc/passwd
COPY --from=USER /etc/group /etc/group
COPY --chown=node:node --from=USER /home/node /home/node
# BusyBox Static Binary
COPY --from=ghcr.io/antyung88/scratch-sh:stable /lib /lib
COPY --from=ghcr.io/antyung88/scratch-sh:stable /bin /bin
# Node
COPY --from=node /usr/lib /usr/lib
COPY --from=node /usr/local/share /usr/local/share
COPY --from=node /usr/local/lib /usr/local/lib
COPY --from=node /usr/local/include /usr/local/include
COPY --from=node /usr/local/bin /usr/local/bin
# Copy Files
COPY --chown=node:node --from=node /app ./app
WORKDIR /app
USER node
RUN ln -s /usr/local/bin/npm .
ENTRYPOINT ["node", "npm", "start"]
EXPOSE 8080

How to create a Dockerfile for Nextjs and graphql?

I tried to deploy my Nextjs project to Heroku via Docker, so i follow docs on internet,then i get a Dockerfile like this
# Install dependencies only when needed
FROM node:alpine AS deps
# Check https://github.com/nodejs/docker-node/tree/b4117f9333da4138b03a546ec926ef50a31506c3#nodealpine to understand why libc6-compat might be needed.
RUN apk add --no-cache libc6-compat
WORKDIR /app
# COPY package.json yarn.lock ./
COPY package.json package-lock.json ./
# RUN yarn install --frozen-lockfile
RUN npm ci
# Rebuild the source code only when needed
FROM node:alpine AS builder
WORKDIR /app
COPY . .
COPY --from=deps /app/node_modules ./node_modules
# RUN yarn build && yarn install --production --ignore-scripts --prefer-offline
RUN npm run build && npm install --production --ignore-scripts --prefer-offline
# Production image, copy all the files and run next
FROM node:alpine AS runner
WORKDIR /app
ENV NODE_ENV production
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
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 ["yarn", "start"]
CMD ["npm", "run", "start"]
and .dockerignore
node_modules
.next
But i get this error when building
ERROR [runner 5/8] COPY --from=builder /app/public ./public
I think this error right because i don't see public folder in my project, maybe my project missing it
Then i go to learn basic about docker, i dont know so much , but i can create a simple Dockerfile like this
FROM node
RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app
COPY package.json ./
RUN npm install
COPY . .
# RUN npm run build
# ENV NODE_ENV=production
EXPOSE 3000
USER node
CMD [ "npm","run","dev"]
with .dockerignore above,I test it with dev enviroment, if it can run I'll edit to production, I run my project by command
docker run -it -p 3000:3000 --name run-my-client my-client
But i get this error
[Error: EACCES: permission denied, unlink '/usr/src/app/.next/build-manifest.json'] {
errno: -13,
code: 'EACCES',
syscall: 'unlink',
path: '/usr/src/app/.next/build-manifest.json'
}
I still see build-manifest.json file in .next folder, but i don't know why this error is appear.Am i missing something and How can i fix it?

Next JS App Dockerfile build locally but fails in Cloud Run when CI/CD

While trying to implement CI/CD in Cloud Run using a Dockerfile, it fails as if some fail were not found. I have built the same container locally with no issues. Here is my Dockerfile:
NextJS version: 12.0.1
Docker: 4.4.2
# Install dependencies only when needed
FROM node:16-alpine AS deps
# Check https://github.com/nodejs/docker-node/tree/b4117f9333da4138b03a546ec926ef50a31506c3#nodealpine to understand why libc6-compat might be needed.
RUN apk add --no-cache libc6-compat
WORKDIR /app
COPY package.json yarn.lock ./
RUN yarn install --frozen-lockfile
# Rebuild the source code only when needed
FROM node:16-alpine AS builder
WORKDIR /app
COPY . .
COPY --from=deps /app/node_modules ./node_modules
RUN yarn build
# Production image, copy all the files and run next
FROM node:16-alpine AS runner
WORKDIR /app
ENV NODE_ENV production
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
USER nextjs
EXPOSE 3000
ENV PORT 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 ["node", "server.js"]
The error displayed is the following:
enter image description here
Does anyone know what could be the problem here?
Are you on MacOS ? I had a similar issue a few weeks ago. The issue was casing : APFS is case insensitive by default. So everything worked on my local machine but failed when running on Linux.

Dockerizing Nextjs

I need to publish my Next js project on a server. For that, by request of admin, I need to dockerize it (because SWC compiler is not available on the server).
I set up Docker Desktop, created Dockerfile (content below) and docker-compose.yml (content below)
Then I successfully ran "docker-compose build"
and then "docker-compose up" - after that website is successfully work on my localhost:3000
What is my next steps? What should I provide to admin? I can push those 2 files on Github, but I guess, it's not enough. I can see in my docker app, that it created image of 723.88 Mb. Maybe I need to sent this one? But how and where it located?
I'm a noob in Docker, any advice is highly welcomed.
My Dockerfile:
# Install dependencies only when needed
FROM node:alpine AS deps
# Check https://github.com/nodejs/docker-node/tree/b4117f9333da4138b03a546ec926ef50a31506c3#nodealpine to understand why libc6-compat might be needed.
RUN apk add --no-cache libc6-compat
WORKDIR /app
COPY package.json ./
RUN yarn install --frozen-lockfile
# Rebuild the source code only when needed
FROM node:alpine AS builder
WORKDIR /app
COPY . .
COPY --from=deps /app/node_modules ./node_modules
RUN yarn build && yarn install --production --ignore-scripts --prefer-offline
# Production image, copy all the files and run next
FROM node:alpine AS runner
WORKDIR /app
RUN npm install --global pm2
ENV NODE_ENV production
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
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
# Run npm start script with PM2 when container starts
CMD [ "pm2-runtime", "npm", "--", "start" ]
My docker-compose.yml file:
version: '3'
services:
next:
build: ./frontend
image: dockerhubid/project-webui:latest
ports:
- '3000:3000'

Resources