Frontend container does not hot-reload for local development
Jibola opened this issue · 2 comments
Context
The frontend container does not support "hot reload" for local development within the container. This is because our Dockerfile does not have functionality for running a dev build. The solution would be to update the frontend/Dockerfile to have both dev and prod builds that would change based on the context it is being run from.
- If it is being run locally, use the local build which should support hot-reload and link to local files
- If run for production, build the production bundle without the need for hot-reload
Note
- The current stop-gap solution is to run the frontend outside of the docker container leveraging
npm install && npm run devoryarn install && yarn dev
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
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
...