Skip to content

Commit ffcbd75

Browse files
[feat] added next.js documentation -sample guide based on the next.js documentation (#24402)
## Description Adds a **Next.js language-specific guide** under `content/guides/nextjs/`: - **_index.md** — Overview, prerequisites, what you’ll learn - **containerize.md** — `docker init`, multi-stage Dockerfile, standalone/export, production image - **develop.md** — Compose dev/prod setup, Compose Watch, hot reload - **run-tests.md** — Vitest/Jest and ESLint in containers - **configure-github-actions.md** — CI/CD with GitHub Actions → Docker Hub - **deploy.md** — Deploy to local Kubernetes (Docker Desktop) Uses sample app: [kristiyan-velkov/docker-nextjs-sample](https://github.com/kristiyan-velkov/docker-nextjs-sample). ## Related issues or tickets _None_ ## Reviews - [x ] Technical review - [x] Editorial review - [ ] Product review --------- Co-authored-by: David Karlsson <35727626+dvdksn@users.noreply.github.com>
1 parent 3c87de0 commit ffcbd75

File tree

6 files changed

+2075
-0
lines changed

6 files changed

+2075
-0
lines changed

content/guides/nextjs/_index.md

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
---
2+
title: Containerize a Next.js application
3+
linkTitle: Next.js
4+
description: Containerize, develop, test, and deploy Next.js apps with Docker and Kubernetes
5+
keywords: getting started, Next.js, next.js, docker, language, Dockerfile, CI/CD, Kubernetes
6+
summary: |
7+
This guide explains how to containerize Next.js applications, set up
8+
development and testing in containers, automate builds with GitHub Actions,
9+
and deploy to Kubernetes.
10+
toc_min: 1
11+
toc_max: 2
12+
languages: [js]
13+
tags: [frameworks]
14+
params:
15+
time: 20 minutes
16+
---
17+
18+
This guide shows you how to containerize a Next.js application using Docker, following best practices for creating efficient, production-ready containers.
19+
20+
[Next.js](https://nextjs.org/) is a React framework that enables server-side
21+
rendering, static site generation, and full-stack capabilities. Docker
22+
provides a consistent containerized environment from development to
23+
production.
24+
25+
> **Acknowledgment**
26+
>
27+
> Docker extends its sincere gratitude to [Kristiyan Velkov](https://www.linkedin.com/in/kristiyan-velkov-763130b3/) for authoring this guide and contributing the official [Next.js Docker examples](https://github.com/vercel/next.js/tree/canary/examples/with-docker) to the Vercel Next.js repository, including the standalone and export output examples. As a Docker Captain and experienced engineer, his expertise in Docker, DevOps, and modern web development has made this resource invaluable for the community, helping developers navigate and optimize their Docker workflows.
28+
29+
---
30+
31+
## What will you learn?
32+
33+
In this guide, you will learn how to:
34+
35+
- Containerize and run a Next.js application using Docker.
36+
- Set up a local development environment for Next.js inside a container.
37+
- Run tests for your Next.js application within a Docker container.
38+
- Configure a CI/CD pipeline using GitHub Actions for your containerized app.
39+
- Deploy the containerized Next.js application to a local Kubernetes cluster for testing and debugging.
40+
41+
To begin, you'll start by containerizing an existing Next.js application.
42+
43+
---
44+
45+
## Prerequisites
46+
47+
Before you begin, make sure you're familiar with the following:
48+
49+
- Basic understanding of [JavaScript](https://developer.mozilla.org/en-US/docs/Web/JavaScript) or [TypeScript](https://www.typescriptlang.org/).
50+
- Basic knowledge of [Node.js](https://nodejs.org/en) and [npm](https://docs.npmjs.com/about-npm) for managing dependencies and running scripts.
51+
- Familiarity with [React](https://react.dev/) and [Next.js](https://nextjs.org/) fundamentals.
52+
- Understanding of Docker concepts such as images, containers, and Dockerfiles. If you're new to Docker, start with the [Docker basics](/get-started/docker-concepts/the-basics/what-is-a-container.md) guide.
53+
54+
Once you've completed the Next.js getting started modules, you'll be ready to containerize your own Next.js application using the examples and instructions provided in this guide.
Lines changed: 332 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,332 @@
1+
---
2+
title: Automate your builds with GitHub Actions
3+
linkTitle: Automate your builds with GitHub Actions
4+
weight: 60
5+
keywords: CI/CD, GitHub Actions, Next.js
6+
description: Learn how to configure CI/CD using GitHub Actions for your Next.js application.
7+
8+
---
9+
10+
## Prerequisites
11+
12+
Complete all the previous sections of this guide, starting with [Containerize Next.js application](containerize.md).
13+
14+
You must also have:
15+
- A [GitHub](https://github.com/signup) account.
16+
- A verified [Docker Hub](https://hub.docker.com/signup) account.
17+
18+
---
19+
20+
## Overview
21+
22+
In this section, you'll set up a **CI/CD pipeline** using [GitHub Actions](https://docs.github.com/en/actions) to automatically:
23+
24+
- Build your Next.js application inside a Docker container.
25+
- Run tests in a consistent environment.
26+
- Push the production-ready image to [Docker Hub](https://hub.docker.com).
27+
28+
---
29+
30+
## Integrate GitHub and Docker Hub
31+
32+
To enable GitHub Actions to build and push Docker images, you'll securely
33+
store your Docker Hub credentials in your new GitHub repository.
34+
35+
### Step 1: Connect your GitHub repository to Docker Hub
36+
37+
1. Create a Personal Access Token (PAT) from [Docker Hub](https://hub.docker.com)
38+
1. Go to your **Docker Hub account → Account Settings → Security**.
39+
2. Generate a new Access Token with **Read/Write** permissions.
40+
3. Name it something like `nextjs-sample`.
41+
4. Copy and save the token — you'll need it in Step 4.
42+
43+
2. Create a repository in [Docker Hub](https://hub.docker.com/repositories/)
44+
1. Go to your **Docker Hub account → Create a repository**.
45+
2. For the Repository Name, use something descriptive — for example: `nextjs-sample`.
46+
3. Once created, copy and save the repository name — you'll need it in Step 4.
47+
48+
3. Create a new [GitHub repository](https://github.com/new) for your Next.js project
49+
50+
4. Add Docker Hub credentials as GitHub repository secrets
51+
52+
In your newly created GitHub repository:
53+
54+
1. Navigate to:
55+
**Settings → Secrets and variables → Actions → New repository secret**.
56+
57+
2. Add the following secrets:
58+
59+
| Name | Value |
60+
|-------------------|--------------------------------|
61+
| `DOCKER_USERNAME` | Your Docker Hub username |
62+
| `DOCKERHUB_TOKEN` | Your Docker Hub access token (created in Step 1) |
63+
| `DOCKERHUB_PROJECT_NAME` | Your Docker Project Name (created in Step 2) |
64+
65+
These secrets let GitHub Actions authenticate securely with Docker Hub
66+
during automated workflows.
67+
68+
5. Connect Your Local Project to GitHub
69+
70+
Link your local project to the GitHub repository you just created by running the following command from your project root:
71+
72+
```console
73+
$ git remote set-url origin https://github.com/{your-username}/{your-repository-name}.git
74+
```
75+
76+
>[!IMPORTANT]
77+
>Replace `{your-username}` and `{your-repository}` with your actual GitHub username and repository name.
78+
79+
To confirm that your local project is correctly connected to the remote GitHub repository, run:
80+
81+
```console
82+
$ git remote -v
83+
```
84+
85+
You should see output similar to:
86+
87+
```console
88+
origin https://github.com/{your-username}/{your-repository-name}.git (fetch)
89+
origin https://github.com/{your-username}/{your-repository-name}.git (push)
90+
```
91+
92+
This confirms that your local repository is properly linked and ready to push your source code to GitHub.
93+
94+
6. Push Your Source Code to GitHub
95+
96+
Follow these steps to commit and push your local project to your GitHub repository:
97+
98+
1. Stage all files for commit.
99+
100+
```console
101+
$ git add -A
102+
```
103+
This command stages all changes — including new, modified, and deleted files — preparing them for commit.
104+
105+
106+
2. Commit your changes.
107+
108+
```console
109+
$ git commit -m "Initial commit"
110+
```
111+
This command creates a commit that snapshots the staged changes with a descriptive message.
112+
113+
3. Push the code to the `main` branch.
114+
115+
```console
116+
$ git push -u origin main
117+
```
118+
This command pushes your local commits to the `main` branch of the remote GitHub repository and sets the upstream branch.
119+
120+
Once completed, your code will be available on GitHub, and any GitHub Actions workflow you've configured will run automatically.
121+
122+
> [!NOTE]
123+
> Learn more about the Git commands used in this step:
124+
> - [Git add](https://git-scm.com/docs/git-add) – Stage changes (new, modified, deleted) for commit
125+
> - [Git commit](https://git-scm.com/docs/git-commit) – Save a snapshot of your staged changes
126+
> - [Git push](https://git-scm.com/docs/git-push) – Upload local commits to your GitHub repository
127+
> - [Git remote](https://git-scm.com/docs/git-remote) – View and manage remote repository URLs
128+
129+
---
130+
131+
### Step 2: Set up the workflow
132+
133+
Now you'll create a GitHub Actions workflow that builds your Docker image, runs tests, and pushes the image to Docker Hub.
134+
135+
1. Go to your repository on GitHub and select the **Actions** tab in the top menu.
136+
137+
2. Select **Set up a workflow yourself** when prompted.
138+
139+
This opens an inline editor to create a new workflow file. By default, it will be saved to:
140+
`.github/workflows/main.yml`
141+
142+
143+
3. Add the following workflow configuration to the new file:
144+
145+
```yaml
146+
# CI/CD – Next.js Application with Docker
147+
# Builds the app, runs tests in a container, and pushes the production image to Docker Hub.
148+
149+
name: CI/CD – Next.js Application with Docker
150+
151+
on:
152+
push:
153+
branches: [main]
154+
pull_request:
155+
branches: [main]
156+
types: [opened, synchronize, reopened]
157+
158+
jobs:
159+
build-test-push:
160+
name: Build, Test and Push Docker Image
161+
runs-on: ubuntu-latest
162+
163+
steps:
164+
# 1. Checkout source code
165+
- name: Checkout source code
166+
uses: actions/checkout@v5
167+
with:
168+
fetch-depth: 0
169+
170+
# 2. Set up Docker Buildx
171+
- name: Set up Docker Buildx
172+
uses: docker/setup-buildx-action@v4
173+
174+
# 3. Cache Docker layers
175+
- name: Cache Docker layers
176+
uses: actions/cache@v5
177+
with:
178+
path: /tmp/.buildx-cache
179+
key: ${{ runner.os }}-buildx-${{ github.sha }}
180+
restore-keys: ${{ runner.os }}-buildx-
181+
182+
# 4. Cache pnpm dependencies
183+
- name: Cache pnpm dependencies
184+
uses: actions/cache@v4
185+
with:
186+
path: ~/.local/share/pnpm/store
187+
key: ${{ runner.os }}-pnpm-${{ hashFiles('**/pnpm-lock.yaml') }}
188+
restore-keys: ${{ runner.os }}-pnpm-
189+
190+
# 5. Extract metadata
191+
- name: Extract metadata
192+
id: meta
193+
run: |
194+
echo "REPO_NAME=${GITHUB_REPOSITORY##*/}" >> "$GITHUB_OUTPUT"
195+
echo "SHORT_SHA=${GITHUB_SHA::7}" >> "$GITHUB_OUTPUT"
196+
197+
# 6. Build dev Docker image (for running tests)
198+
- name: Build Docker image for tests
199+
uses: docker/build-push-action@v6
200+
with:
201+
context: .
202+
file: Dockerfile.dev
203+
tags: ${{ steps.meta.outputs.REPO_NAME }}-dev:latest
204+
load: true
205+
cache-from: type=local,src=/tmp/.buildx-cache
206+
cache-to: type=local,dest=/tmp/.buildx-cache,mode=max
207+
208+
# 7. Run Vitest tests inside the container
209+
# Use same package-manager detection as Dockerfile (no corepack at runtime; node user can't write to /usr/local/bin)
210+
- name: Run tests
211+
run: |
212+
docker run --rm \
213+
--workdir /app \
214+
--entrypoint "" \
215+
-e CI=true \
216+
${{ steps.meta.outputs.REPO_NAME }}-dev:latest \
217+
sh -c "if [ -f package-lock.json ]; then npm run test:run; elif [ -f yarn.lock ]; then yarn test:run; elif [ -f pnpm-lock.yaml ]; then pnpm run test:run; else npm run test:run; fi"
218+
env:
219+
CI: true
220+
NODE_ENV: test
221+
timeout-minutes: 10
222+
223+
# 8. Log in to Docker Hub (only needed for push)
224+
- name: Log in to Docker Hub
225+
if: github.event_name == 'push' && github.ref == 'refs/heads/main'
226+
uses: docker/login-action@v3
227+
with:
228+
username: ${{ secrets.DOCKER_USERNAME }}
229+
password: ${{ secrets.DOCKERHUB_TOKEN }}
230+
231+
# 9. Build and push production image (only on push to main)
232+
- name: Build and push production image
233+
if: github.event_name == 'push' && github.ref == 'refs/heads/main'
234+
uses: docker/build-push-action@v6
235+
with:
236+
context: .
237+
file: Dockerfile
238+
push: true
239+
platforms: linux/amd64,linux/arm64
240+
tags: |
241+
${{ secrets.DOCKER_USERNAME }}/${{ secrets.DOCKERHUB_PROJECT_NAME }}:latest
242+
${{ secrets.DOCKER_USERNAME }}/${{ secrets.DOCKERHUB_PROJECT_NAME }}:${{ steps.meta.outputs.SHORT_SHA }}
243+
cache-from: type=local,src=/tmp/.buildx-cache
244+
cache-to: type=local,dest=/tmp/.buildx-cache,mode=max
245+
246+
```
247+
248+
This workflow performs the following tasks for your Next.js application:
249+
- Triggers on every `push` or `pull request` targeting the `main` branch.
250+
- Builds a development Docker image using `Dockerfile.dev`, optimized for testing.
251+
- Executes unit tests using Jest inside a clean, containerized environment to ensure consistency.
252+
- Halts the workflow immediately if any test fails — enforcing code quality.
253+
- Caches both Docker build layers and npm dependencies for faster CI runs.
254+
- Authenticates securely with Docker Hub using GitHub repository secrets.
255+
- Builds a production-ready image using the `Dockerfile`.
256+
- Tags and pushes the final image to Docker Hub with both `latest` and short SHA tags for traceability.
257+
258+
> [!NOTE]
259+
> For more information about `docker/build-push-action`, refer to the [GitHub Action README](https://github.com/docker/build-push-action/blob/master/README.md).
260+
261+
---
262+
263+
### Step 3: Run the workflow
264+
265+
After you've added your workflow file, it's time to trigger and observe the CI/CD process in action.
266+
267+
1. Commit and push your workflow file
268+
269+
Select "Commit changes…" in the GitHub editor.
270+
271+
- This push will automatically trigger the GitHub Actions pipeline.
272+
273+
2. Monitor the workflow execution
274+
275+
1. Go to the Actions tab in your GitHub repository.
276+
2. Click into the workflow run to follow each step: **build**, **test**, and (if successful) **push**.
277+
278+
3. Verify the Docker image on Docker Hub
279+
280+
- After a successful workflow run, visit your [Docker Hub repositories](https://hub.docker.com/repositories).
281+
- You should see a new image under your repository with:
282+
- Repository name: `${your-repository-name}`
283+
- Tags include:
284+
- `latest` – represents the most recent successful build; ideal for quick testing or deployment.
285+
- `<short-sha>` – a unique identifier based on the commit hash, useful for version tracking, rollbacks, and traceability.
286+
287+
> [!TIP] Protect your main branch
288+
> To maintain code quality and prevent accidental direct pushes, enable branch protection rules:
289+
> - Navigate to your **GitHub repo → Settings → Branches**.
290+
> - Under Branch protection rules, select **Add rule**.
291+
> - Specify `main` as the branch name.
292+
> - Enable options like:
293+
> - *Require a pull request before merging*.
294+
> - *Require status checks to pass before merging*.
295+
>
296+
> This ensures that only tested and reviewed code is merged into `main` branch.
297+
---
298+
299+
## Summary
300+
301+
In this section, you set up a complete CI/CD pipeline for your containerized Next.js application using GitHub Actions.
302+
303+
Here's what you accomplished:
304+
305+
- Created a new GitHub repository specifically for your project.
306+
- Generated a secure Docker Hub access token and added it to GitHub as a secret.
307+
- Defined a GitHub Actions workflow to:
308+
- Build your application inside a Docker container.
309+
- Run tests in a consistent, containerized environment.
310+
- Push a production-ready image to Docker Hub if tests pass.
311+
- Triggered and verified the workflow execution through GitHub Actions.
312+
- Confirmed that your image was successfully published to Docker Hub.
313+
314+
With this setup, your Next.js application is now ready for automated testing and deployment across environments — increasing confidence, consistency, and team productivity.
315+
316+
---
317+
318+
## Related resources
319+
320+
Deepen your understanding of automation and best practices for containerized apps:
321+
322+
- [Introduction to GitHub Actions](/guides/gha.md) – Learn how GitHub Actions automate your workflows
323+
- [Docker Build GitHub Actions](/manuals/build/ci/github-actions/_index.md) – Set up container builds with GitHub Actions
324+
- [Workflow syntax for GitHub Actions](https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions) – Full reference for writing GitHub workflows
325+
- [Compose file reference](/compose/compose-file/) – Full configuration reference for `compose.yaml`
326+
- [Best practices for writing Dockerfiles](/develop/develop-images/dockerfile_best-practices/) – Optimize your image for performance and security
327+
328+
---
329+
330+
## Next steps
331+
332+
Next, learn how you can locally test and debug your Next.js workloads on Kubernetes before deploying. This helps you ensure your application behaves as expected in a production-like environment, reducing surprises during deployment.

0 commit comments

Comments
 (0)