You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
[feat] update angular sample guide to use dhi example (#23820)
## Description
This PR adds a DHI-based Dockerfile example demonstrating how to build
an Angular application using the dhi-node image and serve it through the
dhi-nginx image.
## Reviews
- [x] Technical review
| What application platform does your project use? | Node |
80
-
| What version of Node do you want to use? |23.11.0-alpine |
80
+
| What version of Node do you want to use? |24.12.0-alpine |
81
81
| Which package manager do you want to use? | npm |
82
82
| Do you want to run "npm run build" before starting server? | yes |
83
83
| What directory is your build output to? | dist |
@@ -105,7 +105,7 @@ The default Dockerfile generated by `docker init` serves as a solid starting poi
105
105
In this step, you’ll improve the Dockerfile and configuration files by following best practices:
106
106
107
107
- Use multi-stage builds to keep the final image clean and small
108
-
- Serve the app using NGINX, a fast and secure web server
108
+
- Serve the app using Nginx, a fast and secure web server
109
109
- Improve performance and security by only including what’s needed
110
110
111
111
These updates help ensure your app is easy to deploy, fast to load, and production-ready.
@@ -114,19 +114,90 @@ These updates help ensure your app is easy to deploy, fast to load, and producti
114
114
> A `Dockerfile` is a plain text file that contains step-by-step instructions to build a Docker image. It automates packaging your application along with its dependencies and runtime environment.
115
115
> For full details, see the [Dockerfile reference](/reference/dockerfile/).
116
116
117
-
118
117
### Step 2: Configure the Dockerfile
119
118
120
-
Copy and replace the contents of your existing `Dockerfile` with the configuration below:
119
+
Before creating a Dockerfile, you need to choose a base image. You can either use the [Node.js Official Image](https://hub.docker.com/_/node) or a Docker Hardened Image (DHI) from the [Hardened Image catalog](https://hub.docker.com/hardened-images/catalog).
120
+
121
+
Choosing DHI offers the advantage of a production-ready image that is lightweight and secure. For more information, see [Docker Hardened Images](https://docs.docker.com/dhi/).
122
+
123
+
> [!IMPORTANT]
124
+
> This guide uses a stable Node.js LTS image tag that is considered secure when the guide is written. Because new releases and security patches are published regularly, the tag shown here may no longer be the safest option when you follow the guide. Always review the latest available image tags and select a secure, up-to-date version before building or deploying your application.
125
+
>
126
+
> Official Node.js Docker Images: https://hub.docker.com/_/node
127
+
128
+
{{< tabs >}}
129
+
{{< tab name="Using Docker Hardened Images" >}}
130
+
Docker Hardened Images (DHIs) are available for Node.js in the [Docker Hardened Images catalog](https://hub.docker.com/hardened-images/catalog/dhi/node). Docker Hardened Images are freely available to everyone with no subscription required. You can pull and use them like any other Docker image after signing in to the DHI registry. For more information, see the [DHI quickstart](/dhi/get-started/) guide.
131
+
132
+
1. Sign in to the DHI registry:
133
+
```console
134
+
$ docker login dhi.io
135
+
```
136
+
137
+
2. Pull the Node.js DHI (check the catalog for available versions):
138
+
```console
139
+
$ docker pull dhi.io/node:24-alpine3.22-dev
140
+
```
141
+
142
+
In the following Dockerfile, the `FROM` instruction uses `dhi.io/node:24-alpine3.22-dev` as the base image.
121
143
122
144
```dockerfile
123
145
# =========================================
124
146
# Stage 1: Build the Angular Application
125
147
# =========================================
148
+
149
+
# Use a lightweight DHI Node.js image for building
150
+
FROM dhi.io/node:24-alpine3.22-dev AS builder
151
+
152
+
# Set the working directory inside the container
153
+
WORKDIR /app
154
+
155
+
# Copy package-related files first to leverage Docker's caching mechanism
156
+
COPY package.json package-lock.json* ./
157
+
158
+
# Install project dependencies using npm ci (ensures a clean, reproducible install)
159
+
RUN --mount=type=cache,target=/root/.npm npm ci
160
+
161
+
# Copy the rest of the application source code into the container
162
+
COPY . .
163
+
164
+
# Build the Angular application
165
+
RUN npm run build
166
+
167
+
# =========================================
168
+
# Stage 2: Prepare Nginx to Serve Static Files
169
+
# =========================================
170
+
171
+
FROM dhi.io/nginx:1.28.0-alpine3.21-dev AS runner
172
+
173
+
# Copy custom Nginx config
174
+
COPY nginx.conf /etc/nginx/nginx.conf
175
+
176
+
# Copy the static build output from the build stage to Nginx's default HTML serving directory
> We are using nginx-unprivileged instead of the standard NGINX image to follow security best practices.
246
+
> We are using nginx-unprivileged instead of the standard Nginx image to follow security best practices.
177
247
> Running as a non-root user in the final image:
178
248
>- Reduces the attack surface
179
249
>- Aligns with Docker’s recommendations for container hardening
180
250
>- Helps comply with stricter security policies in production environments
181
251
252
+
{{< /tab >}}
253
+
{{< /tabs >}}
254
+
182
255
### Step 3: Configure the .dockerignore file
183
256
184
257
The `.dockerignore` file tells Docker which files and folders to exclude when building the image.
@@ -259,12 +332,12 @@ docker-compose*.yml
259
332
260
333
### Step 4: Create the `nginx.conf` file
261
334
262
-
To serve your Angular application efficiently inside the container, you’ll configure NGINX with a custom setup. This configuration is optimized for performance, browser caching, gzip compression, and support for client-side routing.
335
+
To serve your Angular application efficiently inside the container, you’ll configure Nginx with a custom setup. This configuration is optimized for performance, browser caching, gzip compression, and support for client-side routing.
263
336
264
337
Create a file named `nginx.conf` in the root of your project directory, and add the following content:
265
338
266
339
> [!NOTE]
267
-
> To learn more about configuring NGINX, see the [official NGINX documentation](https://nginx.org/en/docs/).
340
+
> To learn more about configuring Nginx, see the [official Nginx documentation](https://nginx.org/en/docs/).
268
341
269
342
270
343
```nginx
@@ -351,7 +424,7 @@ With your custom configuration in place, you're now ready to build the Docker im
351
424
352
425
The updated setup includes:
353
426
354
-
- The updated setup includes a clean, production-ready NGINX configuration tailored specifically for Angular.
427
+
- The updated setup includes a clean, production-ready Nginx configuration tailored specifically for Angular.
355
428
- Efficient multi-stage Docker build, ensuring a small and secure final image.
356
429
357
430
After completing the previous steps, your project directory should now contain the following files:
0 commit comments