Skip to content

Commit 29891f4

Browse files
committed
feat(examples): add Docker health check to all Docker examples
1 parent 5118c41 commit 29891f4

File tree

9 files changed

+57
-4
lines changed

9 files changed

+57
-4
lines changed

examples/with-docker-compose/next-app/prod-without-multistage.Dockerfile

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
FROM node:20-alpine
44

5+
# Install curl for health check
6+
RUN apk add --no-cache curl
7+
58
WORKDIR /app
69

710
# Install dependencies based on the preferred package manager
@@ -41,6 +44,10 @@ RUN \
4144
else npm run build; \
4245
fi
4346

47+
# Health check for container orchestration (Docker, Kubernetes, etc.)
48+
HEALTHCHECK --interval=30s --timeout=5s --start-period=30s --retries=3 \
49+
CMD curl -f http://localhost:3000/api/health || exit 1
50+
4451
# Start Next.js based on the preferred package manager
4552
CMD \
4653
if [ -f yarn.lock ]; then yarn start; \

examples/with-docker-compose/next-app/prod.Dockerfile

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
FROM node:20-alpine AS base
44

5+
# Install curl for health check
6+
RUN apk add --no-cache curl
7+
58
# Step 1. Rebuild the source code only when needed
69
FROM base AS builder
710

@@ -72,4 +75,8 @@ ENV NEXT_PUBLIC_ENV_VARIABLE=${NEXT_PUBLIC_ENV_VARIABLE}
7275

7376
# Note: Don't expose ports here, Compose will handle that for us
7477

78+
# Health check for container orchestration (Docker, Kubernetes, etc.)
79+
HEALTHCHECK --interval=30s --timeout=5s --start-period=30s --retries=3 \
80+
CMD curl -f http://localhost:3000/api/health || exit 1
81+
7582
CMD ["node", "server.js"]
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// Health check endpoint for container orchestration (Docker, Kubernetes, etc.)
2+
// https://nextjs.org/docs/api-routes/introduction
3+
4+
export default function health(req, res) {
5+
res.status(200).json({ status: "ok" });
6+
}

examples/with-docker-multi-env/docker/development/Dockerfile

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ FROM node:20-alpine AS base
55
# 1. Install dependencies only when needed
66
FROM base AS deps
77
# Check https://github.com/nodejs/docker-node/tree/b4117f9333da4138b03a546ec926ef50a31506c3#nodealpine to understand why libc6-compat might be needed.
8-
RUN apk add --no-cache libc6-compat
8+
# curl is required for the health check in the runner stage.
9+
RUN apk add --no-cache libc6-compat curl
910

1011
WORKDIR /app
1112

@@ -50,4 +51,8 @@ EXPOSE 3000
5051

5152
ENV PORT=3000
5253

54+
# Health check for container orchestration (Docker, Kubernetes, etc.)
55+
HEALTHCHECK --interval=30s --timeout=5s --start-period=30s --retries=3 \
56+
CMD curl -f http://localhost:3000/api/health || exit 1
57+
5358
CMD HOSTNAME="0.0.0.0" node server.js

examples/with-docker-multi-env/docker/production/Dockerfile

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ FROM node:20-alpine AS base
55
# 1. Install dependencies only when needed
66
FROM base AS deps
77
# Check https://github.com/nodejs/docker-node/tree/b4117f9333da4138b03a546ec926ef50a31506c3#nodealpine to understand why libc6-compat might be needed.
8-
RUN apk add --no-cache libc6-compat
8+
# curl is required for the health check in the runner stage.
9+
RUN apk add --no-cache libc6-compat curl
910

1011
WORKDIR /app
1112

@@ -51,4 +52,8 @@ EXPOSE 3000
5152

5253
ENV PORT=3000
5354

55+
# Health check for container orchestration (Docker, Kubernetes, etc.)
56+
HEALTHCHECK --interval=30s --timeout=5s --start-period=30s --retries=3 \
57+
CMD curl -f http://localhost:3000/api/health || exit 1
58+
5459
CMD HOSTNAME="0.0.0.0" node server.js

examples/with-docker-multi-env/docker/staging/Dockerfile

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ FROM node:20-alpine AS base
55
# 1. Install dependencies only when needed
66
FROM base AS deps
77
# Check https://github.com/nodejs/docker-node/tree/b4117f9333da4138b03a546ec926ef50a31506c3#nodealpine to understand why libc6-compat might be needed.
8-
RUN apk add --no-cache libc6-compat
8+
# curl is required for the health check in the runner stage.
9+
RUN apk add --no-cache libc6-compat curl
910

1011
WORKDIR /app
1112

@@ -51,4 +52,8 @@ EXPOSE 3000
5152

5253
ENV PORT=3000
5354

55+
# Health check for container orchestration (Docker, Kubernetes, etc.)
56+
HEALTHCHECK --interval=30s --timeout=5s --start-period=30s --retries=3 \
57+
CMD curl -f http://localhost:3000/api/health || exit 1
58+
5459
CMD HOSTNAME="0.0.0.0" node server.js
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// Health check endpoint for container orchestration (Docker, Kubernetes, etc.)
2+
// https://nextjs.org/docs/api-routes/introduction
3+
4+
export default function health(req, res) {
5+
res.status(200).json({ status: "ok" });
6+
}

examples/with-docker/Dockerfile

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ FROM node:20-alpine AS base
55
# Install dependencies only when needed
66
FROM base AS deps
77
# Check https://github.com/nodejs/docker-node/tree/b4117f9333da4138b03a546ec926ef50a31506c3#nodealpine to understand why libc6-compat might be needed.
8-
RUN apk add --no-cache libc6-compat
8+
# curl is required for the health check in the runner stage.
9+
RUN apk add --no-cache libc6-compat curl
910
WORKDIR /app
1011

1112
# Install dependencies based on the preferred package manager
@@ -63,4 +64,9 @@ ENV PORT=3000
6364
# server.js is created by next build from the standalone output
6465
# https://nextjs.org/docs/pages/api-reference/config/next-config-js/output
6566
ENV HOSTNAME="0.0.0.0"
67+
68+
# Health check for container orchestration (Docker, Kubernetes, etc.)
69+
HEALTHCHECK --interval=30s --timeout=5s --start-period=30s --retries=3 \
70+
CMD curl -f http://localhost:3000/api/health || exit 1
71+
6672
CMD ["node", "server.js"]
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// Health check endpoint for container orchestration (Docker, Kubernetes, etc.)
2+
// https://nextjs.org/docs/api-routes/introduction
3+
4+
export default function health(req, res) {
5+
res.status(200).json({ status: "ok" });
6+
}

0 commit comments

Comments
 (0)