Skip to content

Commit 6049f5b

Browse files
kris-velkovkristiyan-velkov
authored andcommitted
[feat] initial commit for react.js application guide; added build-image documentation
[feat] imrpove build images section [feat] updated the containerize react.js guide [feat] added documentation for how to develop and test react.js application [feat] added files for deploy, ci/cd [feat] added page ci-cd [feat] added deployment documentation for React.js application [feat] improve the react.js guide [feat] improve react.hs documentation to follow the same strcuture
1 parent 877b29c commit 6049f5b

File tree

6 files changed

+1442
-0
lines changed

6 files changed

+1442
-0
lines changed

content/guides/reactjs/_index.md

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