Next.js Discord

Discord Forum

Next.JS does NOT support Docker Swarm.

Unanswered
Sander posted this in #help-forum
Open in Discord
Avatar
Nextjs can not be reached from the host when deployed on docker swarm, but docker compose works totally fine.
docker service create and docker stack deploy both do launch the nodes and services but going to localhost:3000 results in nothing.

After a docker swarm init you can run docker service create -p 80:80 --replicas 1 --name nginx nginx and then visit localhost:80 (make sure to disable apache in wsl running sudo systemctl stop apache2.service in wsl terminal) you can see the nginx welcome page.

Using curl to go to localhost:3000 you get this:
curl: (52) Empty reply from server

4 Replies

Avatar
# Dockerfile
FROM node:20-alpine AS base
ENV PNPM_HOME="/pnpm"
ENV PATH="$PNPM_HOME:$PATH"
RUN corepack enable

RUN apk update
RUN apk add --no-cache ca-certificates libc6-compat

FROM base AS pruner
WORKDIR /app

COPY . .

RUN pnpm dlx turbo prune codehouse --docker

FROM base AS builder
WORKDIR /app

COPY --from=pruner /app/out/json/ .

RUN --mount=type=cache,id=pnpm,target=/pnpm/store pnpm fetch --frozen-lockfile --prefer-frozen-lockfile
RUN pnpm install --offline

ARG ANALYZE_BUNDLE=disabled
ENV ANALYZE_BUNDLE=$ANALYZE_BUNDLE

ARG CI=disabled
ENV CI=$CI

# Required for docker
ENV NEXT_OUTPUT=standalone

ARG API_URL
ENV API_URL=$API_URL

ARG NEXT_PUBLIC_ENV
ENV NEXT_PUBLIC_ENV=$NEXT_PUBLIC_ENV

ARG NEXT_PUBLIC_SENTRY=disabled
ENV NEXT_PUBLIC_SENTRY=$NEXT_PUBLIC_SENTRY

ARG TURBO_TEAM
ENV TURBO_TEAM=$TURBO_TEAM

ARG TURBO_TOKEN
ENV TURBO_TOKEN=$TURBO_TOKEN

COPY --from=pruner /app/out/pnpm-lock.yaml /app/out/full/ ./
RUN pnpm dlx turbo build

FROM base AS runner
WORKDIR /app

RUN addgroup --system --gid 1001 nodejs
RUN adduser --system --uid 1001 nextjs
USER nextjs

COPY --from=builder --chown=nextjs:nodejs /app/apps/codehouse/.next/standalone ./
COPY --from=builder --chown=nextjs:nodejs /app/apps/codehouse/.next/static ./apps/codehouse/.next/static
COPY --from=builder --chown=nextjs:nodejs /app/apps/codehouse/public ./apps/codehouse/public

EXPOSE 3000

CMD ["node","apps/codehouse/server.js"]
# docker-compose.yml
services:
  codehouse:
    depends_on:
      - api
      - db
      - redis
    ports:
      - 3000:3000
    image: codehouse
    deploy:
      replicas: 1
      restart_policy:
        condition: on-failure
        max_attempts: 3
        delay: 30s
    restart: on-failure

    environment:
      HUSKY: 0 # Disable husky hooks on CI
    build:
      dockerfile: apps/codehouse/Dockerfile
      context: ../../
      args:
        # $ variables are loaded from .env file to keep them secret
        SENTRY_AUTH_TOKEN: $SENTRY_AUTH_TOKEN
        API_URL: $API_URL
        NEXT_PUBLIC_ENV: $NEXT_PUBLIC_ENV
        NEXT_PUBLIC_SENTRY: $NEXT_PUBLIC_SENTRY
        ANALYZE_BUNDLE: $ANALYZE_BUNDLE
        TURBO_TEAM: $TURBO_TEAM
        TURBO_TOKEN: $TURBO_TOKEN
        CI: disabled
# Top level docker-compose.yml
include:
  - ./apps/nginx/docker-compose.yml
  - ./apps/codehouse/docker-compose.yml
  - ./apps/api/docker-compose.yml
Avatar
bump