I am trying to deploy my Payload CMS app to Azure App Service as a container using Docker but I am facing issues with docker build command. I am trying to deploy the basic website template with no changes done to the code project except the required changes of the following:
- Adding
output: 'standalone'
in next.config.js:
const nextConfig = {
images: {
remotePatterns: [
...[NEXT_PUBLIC_SERVER_URL /* '' */].map((item) => {
const url = new URL(item)
return {
hostname: url.hostname,
protocol: url.protocol.replace(':', ''),
}
}),
],
},
reactStrictMode: true,
redirects,
output: 'standalone',
}
- For the Azure Cosmos DB integration I have done the following changes:
Added
transactionOptions: false
in buildConfig payload.config.ts:
db: mongooseAdapter({
url: process.env.DATABASE_URI || '',
transactionOptions: false,
}),
and added indexSortableFields: true
to the buildConfig as well.
Lastly, here is my Dockerfile:
# To use this Dockerfile, you have to set `output: 'standalone'` in your next.config.js file.
# From .js/blob/canary/examples/with-docker/Dockerfile
FROM node:22.12.0-alpine AS base
# Install dependencies only when needed
FROM base AS deps
# Check to understand why libc6-compat might be needed.
RUN apk add --no-cache libc6-compat
WORKDIR /app
# Install dependencies based on the preferred package manager
COPY package.json yarn.lock* package-lock.json* pnpm-lock.yaml* ./
RUN \
if [ -f yarn.lock ]; then yarn --frozen-lockfile; \
elif [ -f package-lock.json ]; then npm ci; \
elif [ -f pnpm-lock.yaml ]; then corepack enable pnpm && pnpm i --frozen-lockfile; \
else echo "Lockfile not found." && exit 1; \
fi
# Rebuild the source code only when needed
FROM base AS builder
WORKDIR /app
COPY --from=deps /app/node_modules ./node_modules
COPY . .
# Next.js collects completely anonymous telemetry data about general usage.
# Learn more here:
# Uncomment the following line in case you want to disable telemetry during the build.
# ENV NEXT_TELEMETRY_DISABLED 1
RUN \
if [ -f yarn.lock ]; then yarn run build; \
elif [ -f package-lock.json ]; then npm run build; \
elif [ -f pnpm-lock.yaml ]; then corepack enable pnpm && pnpm run build; \
else echo "Lockfile not found." && exit 1; \
fi
# Production image, copy all the files and run next
FROM base AS runner
WORKDIR /app
ENV NODE_ENV=production
# Uncomment the following line in case you want to disable telemetry during runtime.
# ENV NEXT_TELEMETRY_DISABLED 1
RUN addgroup --system --gid 1001 nodejs
RUN adduser --system --uid 1001 nextjs
# Remove this line if you do not have this folder
COPY --from=builder /app/public ./public
# Set the correct permission for prerender cache
RUN mkdir .next
RUN chown nextjs:nodejs .next
# Automatically leverage output traces to reduce image size
#
COPY --from=builder --chown=nextjs:nodejs /app/.next/standalone ./
COPY --from=builder --chown=nextjs:nodejs /app/.next/static ./.next/static
USER nextjs
EXPOSE 3000
ENV PORT=3000
# server.js is created by next build from the standalone output
#
# CMD HOSTNAME="0.0.0.0" node server.js
ENV HOSTNAME="0.0.0.0"
CMD ["node", "server.js"]
And here is my docker-compose.yml:
version: '3'
services:
payload:
image: node:18-alpine
ports:
- '3000:3000'
volumes:
- .:/home/node/app
- node_modules:/home/node/app/node_modules
working_dir: /home/node/app/
command: sh -c "yarn install && yarn dev"
depends_on:
- mongo
env_file:
- .env
mongo:
image: mongo:latest
ports:
- '27017:27017'
command:
- --storageEngine=wiredTiger
volumes:
- data:/data/db
logging:
driver: none
volumes:
data:
node_modules:
The commands I use to try build this docker image:
az login
az acr login --name <acr-name>
docker build -t <acr>/<build-name>:<version-tag> .
And lastly, this is the full error that I am getting:
[+] Building 491.8s (10/20) docker:desktop-linux
=> [internal] load build definition from Dockerfile 0.0s
=> => transferring dockerfile: 2.53kB 0.0s
=> [internal] load metadata for docker.io/library/node:22.12.0-alpine 0.0s
=> [internal] load .dockerignore 0.0s
=> => transferring context: 2B 0.0s
=> ERROR [internal] load build context 488.9s
=> => transferring context: 1.45GB 488.9s
=> CACHED [base 1/1] FROM docker.io/library/node:22.12.0-alpine@sha256:51eff88af6dff26f59316b6e356188ffa2c422bd3c3b76f2556a2e7e89d080bd => => transferring dockerfile: 2.53kB 0.0s fa2c422bd3c3b76f2556a2e7e89d080bd
=> [internal] load metadata for docker.io/library/node:22.12.0-alpine 0.0s
=> [internal] load .dockerignore 0.0s
2.7s
=> => resolve docker.io/library/node:22.12.0-alpine@sha256:51eff88af6dff26f59316b6e356188ffa2c422bd3c3b76f2556a2e7e89d080bd 2.7s
=> CACHED [builder 1/4] WORKDIR /app 0.0s
=> [runner 2/8] RUN addgroup --system --gid 1001 nodejs 0.8s
=> [deps 1/4] RUN apk add --no-cache libc6-compat 1.8s
=> [runner 3/8] RUN adduser --system --uid 1001 nextjs 0.7s
=> [deps 2/4] WORKDIR /app 0.0s
------
> [internal] load build context:
------
ERROR: failed to solve: archive/tar: unknown file mode ?rwxr-xr-x
Docker version: Docker version 27.5.1, build 9f9e405
I am trying to deploy my Payload CMS app to Azure App Service as a container using Docker but I am facing issues with docker build command. I am trying to deploy the basic website template with no changes done to the code project except the required changes of the following:
- Adding
output: 'standalone'
in next.config.js:
const nextConfig = {
images: {
remotePatterns: [
...[NEXT_PUBLIC_SERVER_URL /* 'https://example' */].map((item) => {
const url = new URL(item)
return {
hostname: url.hostname,
protocol: url.protocol.replace(':', ''),
}
}),
],
},
reactStrictMode: true,
redirects,
output: 'standalone',
}
- For the Azure Cosmos DB integration I have done the following changes:
Added
transactionOptions: false
in buildConfig payload.config.ts:
db: mongooseAdapter({
url: process.env.DATABASE_URI || '',
transactionOptions: false,
}),
and added indexSortableFields: true
to the buildConfig as well.
Lastly, here is my Dockerfile:
# To use this Dockerfile, you have to set `output: 'standalone'` in your next.config.js file.
# From https://github/vercel/next.js/blob/canary/examples/with-docker/Dockerfile
FROM node:22.12.0-alpine AS base
# Install dependencies only when needed
FROM base AS deps
# Check https://github/nodejs/docker-node/tree/b4117f9333da4138b03a546ec926ef50a31506c3#nodealpine to understand why libc6-compat might be needed.
RUN apk add --no-cache libc6-compat
WORKDIR /app
# Install dependencies based on the preferred package manager
COPY package.json yarn.lock* package-lock.json* pnpm-lock.yaml* ./
RUN \
if [ -f yarn.lock ]; then yarn --frozen-lockfile; \
elif [ -f package-lock.json ]; then npm ci; \
elif [ -f pnpm-lock.yaml ]; then corepack enable pnpm && pnpm i --frozen-lockfile; \
else echo "Lockfile not found." && exit 1; \
fi
# Rebuild the source code only when needed
FROM base AS builder
WORKDIR /app
COPY --from=deps /app/node_modules ./node_modules
COPY . .
# Next.js collects completely anonymous telemetry data about general usage.
# Learn more here: https://nextjs./telemetry
# Uncomment the following line in case you want to disable telemetry during the build.
# ENV NEXT_TELEMETRY_DISABLED 1
RUN \
if [ -f yarn.lock ]; then yarn run build; \
elif [ -f package-lock.json ]; then npm run build; \
elif [ -f pnpm-lock.yaml ]; then corepack enable pnpm && pnpm run build; \
else echo "Lockfile not found." && exit 1; \
fi
# Production image, copy all the files and run next
FROM base AS runner
WORKDIR /app
ENV NODE_ENV=production
# Uncomment the following line in case you want to disable telemetry during runtime.
# ENV NEXT_TELEMETRY_DISABLED 1
RUN addgroup --system --gid 1001 nodejs
RUN adduser --system --uid 1001 nextjs
# Remove this line if you do not have this folder
COPY --from=builder /app/public ./public
# Set the correct permission for prerender cache
RUN mkdir .next
RUN chown nextjs:nodejs .next
# Automatically leverage output traces to reduce image size
# https://nextjs./docs/advanced-features/output-file-tracing
COPY --from=builder --chown=nextjs:nodejs /app/.next/standalone ./
COPY --from=builder --chown=nextjs:nodejs /app/.next/static ./.next/static
USER nextjs
EXPOSE 3000
ENV PORT=3000
# server.js is created by next build from the standalone output
# https://nextjs./docs/pages/api-reference/next-config-js/output
# CMD HOSTNAME="0.0.0.0" node server.js
ENV HOSTNAME="0.0.0.0"
CMD ["node", "server.js"]
And here is my docker-compose.yml:
version: '3'
services:
payload:
image: node:18-alpine
ports:
- '3000:3000'
volumes:
- .:/home/node/app
- node_modules:/home/node/app/node_modules
working_dir: /home/node/app/
command: sh -c "yarn install && yarn dev"
depends_on:
- mongo
env_file:
- .env
mongo:
image: mongo:latest
ports:
- '27017:27017'
command:
- --storageEngine=wiredTiger
volumes:
- data:/data/db
logging:
driver: none
volumes:
data:
node_modules:
The commands I use to try build this docker image:
az login
az acr login --name <acr-name>
docker build -t <acr>/<build-name>:<version-tag> .
And lastly, this is the full error that I am getting:
[+] Building 491.8s (10/20) docker:desktop-linux
=> [internal] load build definition from Dockerfile 0.0s
=> => transferring dockerfile: 2.53kB 0.0s
=> [internal] load metadata for docker.io/library/node:22.12.0-alpine 0.0s
=> [internal] load .dockerignore 0.0s
=> => transferring context: 2B 0.0s
=> ERROR [internal] load build context 488.9s
=> => transferring context: 1.45GB 488.9s
=> CACHED [base 1/1] FROM docker.io/library/node:22.12.0-alpine@sha256:51eff88af6dff26f59316b6e356188ffa2c422bd3c3b76f2556a2e7e89d080bd => => transferring dockerfile: 2.53kB 0.0s fa2c422bd3c3b76f2556a2e7e89d080bd
=> [internal] load metadata for docker.io/library/node:22.12.0-alpine 0.0s
=> [internal] load .dockerignore 0.0s
2.7s
=> => resolve docker.io/library/node:22.12.0-alpine@sha256:51eff88af6dff26f59316b6e356188ffa2c422bd3c3b76f2556a2e7e89d080bd 2.7s
=> CACHED [builder 1/4] WORKDIR /app 0.0s
=> [runner 2/8] RUN addgroup --system --gid 1001 nodejs 0.8s
=> [deps 1/4] RUN apk add --no-cache libc6-compat 1.8s
=> [runner 3/8] RUN adduser --system --uid 1001 nextjs 0.7s
=> [deps 2/4] WORKDIR /app 0.0s
------
> [internal] load build context:
------
ERROR: failed to solve: archive/tar: unknown file mode ?rwxr-xr-x
Docker version: Docker version 27.5.1, build 9f9e405
1 Answer
Reset to default 0Resolved this by adding node_modules in .dockerignore file and using npm to install pnpm