I’m currently having fun with k8s AKS on azure with DevOps etc. and I’m creating a NextJs app or rather two that are connected via their zone system where you in next.config.js
can add rewrite and it automatically sets proxy op to run on the other service.
But I have run into the problem is that I cant apply the hostname or call the hostname in the NextJs config from the pod / container env.
I am running node server for NextJs and not standalone, which I have read it serialised the config on build rather than load them on start.
I have tried checking by executing a kubectl
command to printenv
and I can see they are there even from my own secrets I have created and applied to the deployment.
So question is – why doesn’t it want to take the hostname from k8s have given the pod so the multiple containers can talk together.
Is there a better way to do this – should I split them up into multiple pods on the deploy meant and do some port service or ?
Deploy-definition
apiVersion: apps/v1
kind: Deployment
metadata:
name: project-home-dev-deploy
namespace: project-dev
labels:
environment: development
name: project-home-dev-deploy
app: project-home-dev-deploy
spec:
replicas: 1
selector:
matchLabels:
app: project-home-dev-pod
template:
metadata:
name: project-home-dev-pod
namespace: project-dev
labels:
environment: development
app: project-home-dev-pod
name: project-home-dev-pod
spec:
nodeSelector:
kubernetes.io/os: linux
containers:
- name: project-home-dev-container
image: Contain registry where img is stored.
envFrom:
- secretRef:
name: project-dev-secrets-env
ports:
- containerPort: 3000
- name: project-admin-dev-container
image: Contain registry where img is stored.
envFrom:
- secretRef:
name: project-dev-secrets-env
ports:
- containerPort: 3001
DockerFile
FROM node:22.3-alpine AS base
FROM base AS builder
RUN apk update
RUN apk add --no-cache libc6-compat
WORKDIR /app
RUN npm -g i turbo
COPY . .
RUN turbo prune project-home --docker
FROM base AS installer
RUN apk update
RUN apk add --no-cache libc6-compat
WORKDIR /app
COPY .gitignore .gitignore
COPY --from=builder /app/out/json/ .
COPY --from=builder /app/out/package-lock.json ./package-lock.json
RUN npm install
COPY --from=builder /app/out/full/ .
COPY turbo.json turbo.json
RUN npm run build
FROM base AS runner
WORKDIR /app
RUN addgroup --system --gid 1001 nodejs
RUN adduser --system --uid 1001 nextjs
USER nextjs
COPY --from=installer /app/apps/home/next.config.js .
COPY --from=installer /app/apps/home/package.json .
COPY --from=installer --chown=nextjs:nodejs /app/apps/home/src ./src
COPY --from=installer --chown=nextjs:nodejs /app/apps/home/.next ./.next
COPY --from=installer --chown=nextjs:nodejs /app/apps/home/publi[c] ./public
COPY --from=installer --chown=nextjs:nodejs /app/node_modules ./node_modules
EXPOSE 3000
ENV PORT 3000
CMD ["npm", "run", "start"]
next.config.js
const path = require('path')
const hostname = process.env.HOSTNAME || 'localhost';
const adminPort = process.env.ADMIN_PORT || '3001';
const envAdminUrl = `http://${hostname}:${adminPort}`;
/** @type {import('next').NextConfig} */
module.exports = {
transpilePackages: ['@repo/database'],
eslint: {
ignoreDuringBuilds: true
},
experimental: {
outputFileTracingRoot: path.join(__dirname, '../../'),
},
poweredByHeader: false,
async rewrites() {
return [
{
source: "/:path*",
destination: `/:path*`,
},
{
source: "/admin",
destination: `${envAdminUrl}/admin`,
},
{
source: "/admin/:path*",
destination: `${envAdminUrl}/admin/:path*`,
},
];
},
};