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
Copy file name to clipboardExpand all lines: content/get-started/docker-concepts/building-images/writing-a-dockerfile.md
+1-8Lines changed: 1 addition & 8 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -147,21 +147,14 @@ Now that you have the project, you’re ready to create the `Dockerfile`.
147
147
> build cache, run as a non-root user, and multi-stage builds.
148
148
149
149
150
-
> **Containerize new projects quickly with `docker init`**
151
-
>
152
-
> The `docker init` command will analyze your project and quickly create
153
-
> a Dockerfile, a `compose.yaml`, and a `.dockerignore`, helping you get
154
-
> up and going. Since you're learning about Dockerfiles specifically here,
155
-
> you won't use it now. But, [learn more about it here](/reference/cli/docker/init/).
156
-
157
150
## Additional resources
158
151
159
152
To learn more about writing a Dockerfile, visit the following resources:
160
153
161
154
* [Dockerfile reference](/reference/dockerfile/)
162
155
* [Dockerfile best practices](/develop/develop-images/dockerfile_best-practices/)
163
156
* [Base images](/build/building/base-images/)
164
-
* [Getting started with Docker Init](/reference/cli/docker/init/)
157
+
* [Gordon](/ai/gordon/) — Docker's AI assistant can generate a Dockerfile for your project. Ask Gordon to analyze your code and suggest a Dockerfile optimized for your language and framework.
Docker provides an interactive CLI tool called `docker init` that helps scaffold the necessary configuration files for containerizing your application. This includes generating a `Dockerfile`, `.dockerignore`, `compose.yaml`, and `README.Docker.md`.
49
-
50
-
To begin, navigate to the root of your project directory:
51
-
52
-
```console
53
-
$ cd docker-angular-sample
54
-
```
55
-
56
-
Then run the following command:
57
-
58
-
```console
59
-
$ docker init
60
-
```
61
-
You’ll see output similar to:
62
-
63
-
```text
64
-
Welcome to the Docker Init CLI!
65
-
66
-
This utility will walk you through creating the following files with sensible defaults for your project:
67
-
- .dockerignore
68
-
- Dockerfile
69
-
- compose.yaml
70
-
- README.Docker.md
71
-
72
-
Let's get started!
73
-
```
74
-
75
-
The CLI will prompt you with a few questions about your app setup.
76
-
For consistency, please use the same responses shown in the example below when prompted:
| What application platform does your project use? | Node |
80
-
| What version of Node do you want to use? | 24.12.0-alpine |
81
-
| Which package manager do you want to use? | npm |
82
-
| Do you want to run "npm run build" before starting server? | yes |
83
-
| What directory is your build output to? | dist |
84
-
| What command do you want to use to start the app? | npm run start |
85
-
| What port does your server listen on? | 8080 |
86
-
87
-
After completion, your project directory will contain the following new files:
88
-
89
-
```text
90
-
├── docker-angular-sample/
91
-
│ ├── Dockerfile
92
-
│ ├── .dockerignore
93
-
│ ├── compose.yaml
94
-
│ └── README.Docker.md
95
-
```
96
-
97
-
---
98
-
99
46
## Build the Docker image
100
47
101
-
The default Dockerfile generated by `docker init` serves as a solid starting point for general Node.js applications. However, Angular is a front-end framework that compiles into static assets, so we need to tailor the Dockerfile to optimize for how Angular applications are built and served in a production environment.
102
-
103
-
### Step 1: Improve the generated Dockerfile and configuration
48
+
Angular is a front-end framework that compiles into static assets, so the Dockerfile uses a multi-stage build: one stage compiles the app with Node.js, and a second minimal stage serves the static output with Nginx.
104
49
105
-
In this step, you’ll improve the Dockerfile and configuration files by following best practices:
106
-
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
109
-
- Improve performance and security by only including what’s needed
110
-
111
-
These updates help ensure your app is easy to deploy, fast to load, and production-ready.
112
-
113
-
> [!NOTE]
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
-
> For full details, see the [Dockerfile reference](/reference/dockerfile/).
50
+
> [!TIP]
51
+
>
52
+
> [Gordon](/ai/gordon/), Docker's AI assistant, can generate Docker assets for your project. Ask Gordon to create a Dockerfile, Compose file, and `.dockerignore` tailored to your application.
116
53
117
-
### Step 2: Configure the Dockerfile
54
+
### Step 1: Create the Dockerfile
118
55
119
56
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
57
@@ -191,7 +128,7 @@ CMD ["-g", "daemon off;"]
191
128
{{< /tab >}}
192
129
{{< tab name="Using the Docker Official Image" >}}
193
130
194
-
Now you need to create a production-ready multi-stage Dockerfile. Replace the generated Dockerfile with the following optimized configuration:
131
+
Create a file named `Dockerfile`with the following contents:
195
132
196
133
```dockerfile
197
134
# =========================================
@@ -252,7 +189,20 @@ CMD ["-g", "daemon off;"]
252
189
{{< /tab >}}
253
190
{{< /tabs >}}
254
191
255
-
### Step 3: Configure the .dockerignore file
192
+
### Step 2: Create the compose.yaml file
193
+
194
+
Create a file named `compose.yaml` with the following contents:
195
+
196
+
```yaml {collapse=true,title=compose.yaml}
197
+
services:
198
+
server:
199
+
build:
200
+
context: .
201
+
ports:
202
+
- 8080:8080
203
+
```
204
+
205
+
### Step 3: Create the .dockerignore file
256
206
257
207
The `.dockerignore` file tells Docker which files and folders to exclude when building the image.
258
208
@@ -264,7 +214,7 @@ The `.dockerignore` file tells Docker which files and folders to exclude when bu
264
214
>
265
215
> To learn more, visit the [.dockerignore reference](/reference/dockerfile.md#dockerignore-file).
266
216
267
-
Copy and replace the contents of your existing `.dockerignore` with the configuration below:
217
+
Create a file named `.dockerignore` with the following contents:
268
218
269
219
```dockerignore
270
220
# ================================
@@ -434,8 +384,7 @@ After completing the previous steps, your project directory should now contain t
434
384
│ ├── Dockerfile
435
385
│ ├── .dockerignore
436
386
│ ├── compose.yaml
437
-
│ ├── nginx.conf
438
-
│ └── README.Docker.md
387
+
│ └── nginx.conf
439
388
```
440
389
441
390
Now that your Dockerfile is configured, you can build the Docker image for your Angular application.
@@ -455,7 +404,7 @@ What this command does:
455
404
- Tags the image as docker-angular-sample so you can reference it later
456
405
457
406
458
-
####Step 6: View local images
407
+
### Step 6: View local images
459
408
460
409
After building your Docker image, you can check which images are available on your local machine using either the Docker CLI or [Docker Desktop](/manuals/desktop/use-desktop/images.md). Since you're already working in the terminal, let's use the Docker CLI.
461
410
@@ -547,9 +496,8 @@ $ docker compose down
547
496
In this guide, you learned how to containerize, build, and run an Angular application using Docker. By following best practices, you created a secure, optimized, and production-ready setup.
548
497
549
498
What you accomplished:
550
-
- Initialized your project using `docker init` to scaffold essential Docker configuration files.
551
-
- Replaced the default `Dockerfile` with a multi-stage build that compiles the Angular application and serves the static files using Nginx.
552
-
- Replaced the default `.dockerignore` file to exclude unnecessary files and keep the image clean and efficient.
499
+
- Created a multi-stage `Dockerfile` that compiles the Angular application and serves the static files using Nginx.
500
+
- Created a `.dockerignore` file to exclude unnecessary files and keep the image clean and efficient.
553
501
- Built your Docker image using `docker build`.
554
502
- Ran the container using `docker compose up`, both in the foreground and in detached mode.
555
503
- Verified that the app was running by visiting [http://localhost:8080](http://localhost:8080).
@@ -566,7 +514,6 @@ Explore official references and best practices to sharpen your Docker workflow:
566
514
- [Multi-stage builds](/build/building/multi-stage/) – Learn how to separate build and runtime stages.
567
515
- [Best practices for writing Dockerfiles](/develop/develop-images/dockerfile_best-practices/) – Write efficient, maintainable, and secure Dockerfiles.
568
516
- [Build context in Docker](/build/concepts/context/) – Learn how context affects image builds.
0 commit comments