Code Monkey home page Code Monkey logo

Comments (2)

Mrostgaard avatar Mrostgaard commented on June 3, 2024

How does this work in practice?
Do I currently need to run
docker-compose build --no-cache
Every time i update the frontend and backend to get the updates working?

If it is being run locally, use the local build which should support hot-reload and link to local files

is it ./frontend/Dockerfile you are talking about in this quote? I can't see that it has npm install && npm run dev capabilities

from full-stack-fastapi-mongodb.

Mrostgaard avatar Mrostgaard commented on June 3, 2024

I have updated ./frontend/Dockerfile to accept npm run dev
It currently works okay. You'll need to run a docker-compose down then a docker-compose up --build
After this it can be ran normally like docker-compose up -d

I've had some issue where the first time after docker-compose up -d it doesn't really update, but after a refresh it seems to working OK

The ./frontend/Dockerfile should look like this:

# Base stage with Node.js on Alpine Linux
FROM node:18-alpine AS base
WORKDIR /frontend

# Dependency installation stage
FROM base AS deps
# Install necessary system libraries
RUN apk add --no-cache libc6-compat
# Set the working directory
WORKDIR /frontend
# Copy package management files into the image
COPY package.json yarn.lock* package-lock.json* pnpm-lock.yaml* ./
# Install dependencies based on the detected lock file
RUN \
  if [ -f yarn.lock ]; then yarn --frozen-lockfile; \
  elif [ -f package-lock.json ]; then npm ci; \
  elif [ -f pnpm-lock.yaml ]; then yarn global add pnpm && pnpm i --frozen-lockfile; \
  else npm install && npm ci; \
  fi

# Build stage for production
FROM base AS builder
WORKDIR /frontend
# Copy installed node_modules from deps stage and all source files
COPY --from=deps /frontend/node_modules ./node_modules
COPY . .
# Disable telemetry for production builds
ENV NEXT_TELEMETRY_DISABLED 1
RUN npm run build

# Production stage setup
FROM base AS runner
WORKDIR /frontend
ENV NODE_ENV production
# Further disable telemetry
ENV NEXT_TELEMETRY_DISABLED 1
# Add a system group and user for running the application
RUN addgroup --system --gid 1001 nodejs
RUN adduser --system --uid 1001 nextjs
# Copy public assets and the built application from the builder stage
COPY --from=builder /frontend/public ./public
RUN mkdir .next
RUN chown nextjs:nodejs .next
COPY --from=builder --chown=nextjs:nodejs /frontend/.next/standalone ./
COPY --from=builder --chown=nextjs:nodejs /frontend/.next/static ./.next/static
USER nextjs
EXPOSE 3000
ENV PORT 3000
ENV HOSTNAME "0.0.0.0"
CMD ["node", "server.js"]

# Development stage setup
FROM base AS development
WORKDIR /frontend
COPY --from=deps /frontend/node_modules ./node_modules
COPY . .
ENV NODE_ENV=development
EXPOSE 3000
CMD ["npm", "run", "dev"]

And then update ./docker-compose.override.yml like so:

...
  frontend:
    ports:
      - "3000:3000"
    build:
      context: ./frontend
      target: development
    volumes:
      - ./frontend:/frontend  # Mount the source code directory to enable hot reloading
      - /frontend/node_modules  # Use a volume to persist installed node_modules
    environment:
      - NODE_ENV=development
    labels:
      - traefik.enable=true
      - traefik.constraint-label-stack=${TRAEFIK_TAG?Variable not set}
      - traefik.http.routers.${STACK_NAME?Variable not set}-frontend-http.rule=PathPrefix(`/`)
      - traefik.http.services.${STACK_NAME?Variable not set}-frontend.loadbalancer.server.port=80
...

from full-stack-fastapi-mongodb.

Related Issues (14)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.