I have a Vue application which I serve with docker, currently I want to use environment variables to target specific project repo. But when I am setting env variables, it's not picking up in dockerfile,
What am I doing wrong in my setup ?
NPM scripts.
scripts: {
"build:project1": "vue-cli-service build src/project1/main.js",
"deploy:project1": "cross-env PROJECT_REPO=project1 bash build_deploy.sh"
}
build_deploy.sh
#!/bin/bash
docker build -t ${PROJECT_REPO} .
Dockerfile
# build stage
FROM node:lts-alpine as build-stage
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
RUN npm run build:${PROJECT_REPO}
# production stage
FROM nginx:stable-alpine as production-stage
COPY --from=build-stage /app/dist /usr/share/nginx/html
EXPOSE 80
CMD ["nginx", "-g", "daemon off;"]
Aha. Found it. I was missing ARG in docker file
# build stage
ARG PROJECT_REPO
FROM node:lts-alpine as build-stage
ARG PROJECT_REPO
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
RUN npm run build:${PROJECT_REPO}
# production stage
FROM nginx:stable-alpine as production-stage
COPY --from=build-stage /app/dist /usr/share/nginx/html
EXPOSE 80
CMD ["nginx", "-g", "daemon off;"]
Reference taken from docs: https://docs.docker.com/engine/reference/builder/#understand-how-arg-and-from-interact
Related
I failed to execute docker built -t "my_image_namev0.1" .
With Error Massage : ERROR [build 4/4] RUN cp .env && yarn && yarn generate
Here is my Dockerfile script :
# Stage 1: Build
FROM node:16-alpine as build
WORKDIR /app
COPY . .
RUN cp .env && yarn && yarn generate
# Stage 2: Serve
FROM nginx:stable-alpine
COPY --from=build /app/dist /usr/share/nginx/html/mdp
COPY --from=build /app/nginx/conf.d/default.conf /etc/nginx/conf.d/default.conf
EXPOSE 80
CMD ["nginx", "-g", "daemon off;"]
I know the error because of the yarn failed to run from the docker.
I have tried to remove all the node_modules and insall it again but still facing the same error.
Maybe someone have this similar problem before, and I really need your help..
Thank you
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"]
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.
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 am having issues with running my mean stack application in staging environment. View page source still only has empty tags. It is working great in dev environment (probably because im not using docker in dev environment).
Here is my dockerfile:
#################
# Build the app #
#################
FROM node:14.5-alpine as build
WORKDIR /app
COPY package.json package-lock.json ./
RUN npm install
COPY . .
RUN npm install -g #angular/cli
RUN ng build --configuration=staging && ng run bananas:server:staging
################
# Run in NGINX #
################
FROM nginx:alpine
COPY --from=build /app/dist/bananas/browser /usr/share/nginx/html
RUN rm /etc/nginx/conf.d/default.conf
COPY nginx/nginx.conf /etc/nginx/conf.d
EXPOSE 80
CMD ["nginx", "-g", "daemon off;"]