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
Related
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?
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?
I am having an issue where the docker buildx tool doesn't properly copy files to the production container. For example:
FROM --platform=$BUILDPLATFORM node:16.14.0-alpine as build
WORKDIR /app
ENV PATH /app/node_modules/.bin:$PATH
COPY package.json ./
COPY yarn.lock ./
RUN apk update && apk add python3 g++ make && rm -rf /var/cache/apk/*
RUN yarn install
COPY . ./
RUN yarn run build
# production environment
FROM nginx:stable-alpine
COPY --from=build /app/build /usr/share/nginx/html/
COPY ./nginx/nginx.conf /etc/nginx/conf.d/default.conf
# Default port exposure
EXPOSE 80
# Copy .env file and shell script to container
COPY ./env.sh /usr/share/nginx/html/env.sh
# Start Nginx server
CMD ["/bin/sh", "-c", "/usr/share/nginx/html/env.sh && nginx -g \"daemon off;\""]
All of the files from build will copy properly. However, the nginx.conf and env.sh will report as not found. Any ideas?
P.S: This seems to be an issue with nginx:alpine. No clue
So I am trying to run a NextJS app inside a docker-compose.
In order to have a NextJS boilerplate + a Docker image to build the container, I followed the steps provided in the docker example of the NextJS official repo.
Here is the provided Dockerfile :
# 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 --from=deps /app/node_modules ./node_modules
COPY . .
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 /app/package.json ./package.json
# Automatically leverage output traces to reduce image size
# https://nextjs.org/docs/advanced-features/output-file-tracing
COPY --from=builder --chown=nextjs:nodejs /app/.next/standalone ./
COPY --from=builder --chown=nextjs:nodejs /app/.next/static ./.next/static
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"]
From there, I created a docker-compose.yml file at the root of the app, and try to write it myself so that it starts the NextJS app :
version: "3"
services:
web:
build:
context: .
dockerfile: Dockerfile
container_name: web
restart: always
volumes:
- ./:/app
- /app/node_modules
- /app/.next
ports:
- 3004:3000
However, when running sudo docker-compose up --build, I get the following error :
node:internal/modules/cjs/loader:936
throw err;
^
Error: Cannot find module '/app/server.js'
at Function.Module._resolveFilename (node:internal/modules/cjs/loader:933:15)
at Function.Module._load (node:internal/modules/cjs/loader:778:27)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)
at node:internal/main/run_main_module:17:47 {
code: 'MODULE_NOT_FOUND',
requireStack: []
}
Why is it not able to find the node modules? What am I doing wrong exactly?
Here is my docker file from the same boiler plate which is working for me.
# 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 ./
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 export NODE_OPTIONS=--openssl-legacy-provider && 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
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
ENV PORT=80
EXPOSE 80
# 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"]
I have commented # USER nextjs as nextjs user was not working.
And this is my dockerignore file
**/.classpath
**/.dockerignore
**/.env.development
**/.git
**/.github
**/.husky
**/.idea
**/.next
**/.gitignore
**/.project
**/.settings
**/.toolstarget
**/.vs
**/.vscode
**/*.*proj.user
**/*.dbmdl
**/*.jfm
**/azds.yaml
**/bin
**/charts
**/docker-compose*
**/Dockerfile*
**/node_modules
**/npm-d
I’m trying to automate build on dockercloud to build a docker container for my front-end.
I have
web.dockerfile
### STAGE 1: Build ###
FROM node:9.3.0-alpine as builder
COPY package.json ./
RUN npm set progress=false && npm config set depth 0 && npm cache clean --force
## Storing node modules on a separate layer will prevent unnecessary npm installs at each build
RUN npm i
RUN mkdir /web
RUN cp -R ./node_modules ./web
WORKDIR /web
COPY . .
## Build the angular app in production mode and store the artifacts in dist folder
RUN $(npm bin)/ng build --prod --build-optimizer
### STAGE 2: Setup ###
FROM nginx:1.13.8-alpine
COPY nginx.conf /etc/nginx/nginx.conf
COPY site.conf /etc/nginx/conf.d/default.conf
RUN rm -rf /usr/share/nginx/html/*
COPY dist /usr/share/nginx/html/
RUN touch /var/run/nginx.pid && \
chown -R nginx:nginx /var/run/nginx.pid && \
chown -R nginx:nginx /var/cache/nginx && \
chown -R nginx:nginx /usr/share/nginx/html
USER nginx
as you see I already did this line :
RUN $(npm bin)/ng build --prod --build-optimizer
Which it should generate the dist/ folder for me.
I got
This long logs in my docker-cloud
What I don't understand is, it is working fine, when I used that Dockerfile locally.
Should I look into my webpack settings or my Dockerfile syntax ?
Use the --from argument to COPY to set the source location to a previous build stage.
COPY --from=builder /web/dist /usr/share/nginx/html/
For your local build you probably have a dist/ folder for your local development which is then included in the build context sent to Docker. The local copy is what ends up in the image.