Next.js Discord

Discord Forum

Hello, need an advice on deployment with docker.

Answered
Spectacled bear posted this in #help-forum
Open in Discord
Spectacled bearOP
Have been following this example for setting up production docker setup for my application: https://github.com/vercel/next.js/tree/canary/examples/with-docker-compose

Have a question regarding source code updates. Should we rebuild docker images and relaunching docker containers each time we have updates?
Answered by Sun bear
Sure, we should rebuild docker image because there can be newly installed dependencies with updates of codebase.
View full answer

35 Replies

@joulev what updates? code updates: yes, else you can't get the new code in the deployed images. data updates: it depends on how you fetch that data in your app.
Spectacled bearOP
Code updates. Whenever there's a merge to main branch I have a setup pipeline that connects to the remote server, pulls the changes, builds new image and restarts the container. Could this cause any problems? How to deal with old dangling images?

Thank you for the response.
@Spectacled bear Code updates. Whenever there's a merge to main branch I have a setup pipeline that connects to the remote server, pulls the changes, builds new image and restarts the container. Could this cause any problems? How to deal with old dangling images? Thank you for the response.
i must be honest here, i have never managed the entire workflow by myself so don't have the experience to back my answer haha.

but i think you can build, then up the images, then remove unused images.
Sun bear
I'm not sure why nextjs + docker? Vercel is the best for next.js app
@joulev i must be honest here, i have never managed the entire workflow by myself so don't have the experience to back my answer haha. but i think you can build, then up the images, then remove unused images.
Spectacled bearOP
Ahh no worries, all help appreciated. The only other option I can see is to opt-out from "output: standalone" and add .next folder into volumes and track updates this way without the need of rebuilding images
@Sun bear I'm not sure why nextjs + docker? Vercel is the best for next.js app
Spectacled bearOP
I'm afraid that's not for me to decide on this matter
Sun bear
Ah.. then we can setup it using docker easily.
@Sun bear Ah.. then we can setup it using docker easily.
Spectacled bearOP
Yeah, we have whole setup already done, just wanted to double check if that would be the best solution for production with CD
Sun bear
Ah... sure
@Sun bear Then, can I check it?
Spectacled bearOP
Would you like to see Dockerfile, docker-compose and gitlab-ci files?
Sun bear
no gitlab-ci, only docker-compose and Dockerfile.
@Sun bear no gitlab-ci, only docker-compose and Dockerfile.
Spectacled bearOP
FROM node:22-alpine AS base

# Install dependencies
FROM base AS deps

WORKDIR /webapp

COPY package.json package-lock.json ./

RUN npm ci

# Build stage
FROM base AS builder

WORKDIR /webapp

ENV NEXT_TELEMETRY_DISABLED=1
ENV NODE_ENV=production

COPY --from=deps /webapp/node_modules ./node_modules

COPY . .

RUN npm run build

# Run stage
FROM base AS runner

WORKDIR /webapp

ENV NEXT_TELEMETRY_DISABLED=1
ENV NODE_ENV=production
ENV HOSTNAME=0.0.0.0
ENV PORT=3000

RUN apk --no-cache add openssh g++ make python3 git

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

COPY --from=builder /webapp/public ./public

RUN mkdir .next
RUN chown nextjs:nodejs .next

COPY --from=builder --chown=nextjs:nodejs /webapp/.next/standalone ./
COPY --from=builder --chown=nextjs:nodejs /webapp/.next/static ./.next/static


services:
  test-app:
    container_name: "test-app"
    build:
      context: .
      dockerfile: dockerfiles/prod.Dockerfile
    labels:
      - traefik.enable=true
      - traefik.docker.network=test
      - traefik.http.routers.test-app.rule=Host(`${APP_HOST:-app.test.test}`)
      - traefik.http.routers.test-app.entrypoints=https
      - traefik.http.routers.test-app.tls=true
      - traefik.http.routers.test-app.service=test-app@docker
      - traefik.http.services.test-app.loadbalancer.server.port=3000
    command: [ sh, -c, 'node server.js' ]
    networks:
      - test
    restart: unless-stopped

networks:
  test:
    external: true

volumes:
  test-app:
    external: true
Sun bear
thanks, then, can you let me know which cloud you would deploy this app?
and I think your docker settings are a bit complicated.
With several environments ( Dev, Stage, Prod ), we can easily setup docker env, and setup service for "always running app"
@Sun bear thanks, then, can you let me know which cloud you would deploy this app?
Spectacled bearOP
Currently we have a plain ubuntu server on hostinger
@Sun bear and I think your docker settings are a bit complicated.
Spectacled bearOP
Why do you think so?
Sun bear
Sorry but, please let me know you are Developer or DevOps Engineer.
Sun bear
Ah... Then, you used Nextjs as server & client both?
@Sun bear Ah... Then, you used Nextjs as server & client both?
Spectacled bearOP
No, we have a separate backend application. I've send the docker configurations only for the front facing application
Sun bear
Ah... if you seperated backend, it'd be nice to deploy it on vercel... but you want to deploy on hostinger, and it's okay.
In simple terms, you just install the dependencies, build the app, and then use a static hosting service like nginx. Simple.
@Sun bear In simple terms, you just install the dependencies, build the app, and then use a static hosting service like nginx. Simple.
Spectacled bearOP
Everything if fine on this part. I had a bit different question. Whenever we have an updates coming in to our code base in the CD pipeline should we rebuild docker images and reload the container? And is there a way to avoid that?
Sun bear
Sure, we should rebuild docker image because there can be newly installed dependencies with updates of codebase.
Answer
Sun bear
You! 😃
@Sun bear You! 😃
Spectacled bearOP
Well we couldv'e tracked changes with volumes, but I suppose that's not the best idea right? 😅
Sun bear
Yup, right.
@Sun bear Yup, right.
Spectacled bearOP
Great, thank you for the help!
🫰
Sun bear
👍
Can you show me your app?
@Sun bear Can you show me your app?
Spectacled bearOP
Sorry, it's still in dev/testing stage and we can't disclose it to the public. But I can share it whenever we release it!
Sun bear
Ah... Great! Exactly, I'm developer too, and I'm working as Generative AI Engineer.