1aea91da55
The deployment was unreachable because the Next.js server was binding to localhost:3000 (127.0.0.1) instead of 0.0.0.0, making it only accessible inside the Docker container. - Added HOSTNAME=0.0.0.0 to Dockerfile build and runtime stages - Added HOSTNAME=0.0.0.0 to docker-compose.yml for nextjs service This allows the server to accept connections from external hosts.
46 lines
1.2 KiB
Docker
46 lines
1.2 KiB
Docker
# Multi-stage production Dockerfile for Next.js standalone
|
|
|
|
# Stage 1: Dependencies
|
|
FROM node:22-alpine AS deps
|
|
RUN npm install -g bun
|
|
WORKDIR /app
|
|
COPY package.json bun.lock ./
|
|
RUN bun install --frozen-lockfile
|
|
|
|
# Stage 2: Build
|
|
FROM node:22-alpine AS build
|
|
RUN npm install -g bun
|
|
WORKDIR /app
|
|
COPY --from=deps /app/node_modules ./node_modules
|
|
COPY . .
|
|
# Set environment for build to ensure server binds to all interfaces
|
|
ENV HOSTNAME="0.0.0.0"
|
|
ENV PORT="3000"
|
|
RUN bun run build
|
|
|
|
# Stage 3: Runtime
|
|
FROM node:22-alpine AS runner
|
|
WORKDIR /app
|
|
|
|
# Create non-root user
|
|
RUN adduser --system --uid 1001 nextjs
|
|
|
|
# Copy standalone build output
|
|
COPY --from=build --chown=nextjs:nodejs /app/.next/standalone ./
|
|
COPY --from=build --chown=nextjs:nodejs /app/.next/static ./.next/static
|
|
COPY --from=build --chown=nextjs:nodejs /app/public ./public
|
|
|
|
# Switch to non-root user
|
|
USER nextjs
|
|
|
|
EXPOSE 3000
|
|
|
|
# Health check
|
|
HEALTHCHECK --interval=30s --timeout=3s --start-period=5s --retries=3 \
|
|
CMD node -e "require('http').get('http://localhost:3000', (r) => {if (r.statusCode !== 200) throw new Error(r.statusCode)})"
|
|
|
|
# Start standalone server - bind to all interfaces (0.0.0.0) for external access
|
|
ENV HOSTNAME="0.0.0.0"
|
|
ENV PORT="3000"
|
|
CMD ["node", "server.js"]
|