Why tanstack app is not accessible in dockerized monorepo? #21260
-
|
I am using monorepo along with Below is the folder structure for more context. (N.B - This works for single/non-monorepo vite project and using windows) The // monorepo/apps/web/package.json
{
"name": "web",
"scripts": {
"build": "vite build",
"start": "vite preview --host 0.0.0.0 --port 4173",
},
"dependencies": {
"@repo/data": "workspace:^",
"@repo/env": "workspace:^",
},
"devDependencies": {
...
}
}After building the image with I have looked for reason and a lot of sources says that vite is not binding with My question is why it is not binding with I can confirm container is accessible at This is my Dockerfile. (un-necessary folders are ignored with // monorepo/apps/web/Dockerfile
# ---------------------------
# 1. Builder Image
# ---------------------------
FROM node:22-alpine AS builder
# Install PNPM
RUN npm install -g pnpm
# Set working directory
WORKDIR /builder
# Copy whole monorepo to resolve workspace packages
COPY . .
# Install all dependencies (including workspaces)
RUN pnpm install --frozen-lockfile
ARG DATABASE_NAME
ARG DATABASE_URL
ARG REDIS_URL
ARG NODE_ENV
ENV DATABASE_NAME=${DATABASE_NAME}
ENV DATABASE_URL=${DATABASE_URL}
ENV REDIS_URL=${REDIS_URL}
ENV NODE_ENV=${NODE_ENV}
# Build only the web workspace
RUN pnpm build --filter web
# ---------------------------
# 2. Workspace Package Image
# ---------------------------
FROM node:22-alpine AS packager
# Set working directory
WORKDIR /packager
# Move required production ready packages for lower image size
# @packages/env
COPY --from=builder /builder/packages/env/dist ./packages/env/dist
COPY --from=builder /builder/packages/env/package.json ./packages/env/package.json
# @packages/data
COPY --from=builder /builder/packages/data/dist ./packages/data/dist
COPY --from=builder /builder/packages/data/package.json ./packages/data/package.json
# web
COPY --from=builder /builder/apps/web/dist ./apps/web/dist
COPY --from=builder /builder/apps/web/package.json ./apps/web/package.json
# ---------------------------
# 3. Runtime Image
# ---------------------------
FROM node:22-alpine AS runner
# Install PNPM
RUN npm install -g pnpm
# Set working directory
WORKDIR /portfolio
# Copy package.json and lockfiles
COPY --from=builder /builder/package.json ./package.json
COPY --from=builder /builder/pnpm-lock.yaml ./pnpm-lock.yaml
COPY --from=builder /builder/pnpm-workspace.yaml ./pnpm-workspace.yaml
# Bring dependent packages
COPY --from=packager /packager/packages ./packages
COPY --from=packager /packager/apps ./apps
# Set environment for Vite
ENV HOST=0.0.0.0
ENV PORT=4173
# Install production dependencies
RUN pnpm i --prod --frozen-lockfile
# Set working directory
WORKDIR /portfolio/apps/web
# Expose port
EXPOSE 4173
ARG DATABASE_NAME
ARG DATABASE_URL
ARG REDIS_URL
ARG NODE_ENV
ENV DATABASE_NAME=${DATABASE_NAME}
ENV DATABASE_URL=${DATABASE_URL}
ENV REDIS_URL=${REDIS_URL}
ENV NODE_ENV=${NODE_ENV}
# Start the app in production mode
CMD ["pnpm","exec","vite","preview","--host","0.0.0.0","--port","4173"] |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
|
The issue is in your runner stage - you're running The fix: Option 1: Install vite globally in the runner stage # In runner stage, before CMD
RUN npm install -g viteOption 2: Move vite to dependencies (not recommended for a library, but fine for an app) "dependencies": {
"vite": "^6.0.0"
}Option 3: Copy vite from builder stage # In runner stage
COPY --from=builder /builder/node_modules/vite ./node_modules/vite
COPY --from=builder /builder/node_modules/.bin/vite ./node_modules/.bin/viteBetter approach - serve static files directly: Since # Runtime Image
FROM node:22-alpine AS runner
RUN npm install -g serve
WORKDIR /app
COPY --from=builder /builder/apps/web/dist ./dist
EXPOSE 4173
CMD ["serve", "-s", "dist", "-l", "4173"]Or even better, use nginx: FROM nginx:alpine AS runner
COPY --from=builder /builder/apps/web/dist /usr/share/nginx/html
EXPOSE 80This makes your production image smaller and faster. |
Beta Was this translation helpful? Give feedback.
I was using tanstack and I figured out the solution couple of days ago.
The way @tt-a1i suggested
is not working because the generated file
dist/server/server.jsis a handler and not an actual server, so simplenode dist/server/server.jswill execute the file and process will complete.Insted we have to create a server on our own to handle that.