Skip to content

Commit 4f1f252

Browse files
committed
Update README.md
1 parent e3f05af commit 4f1f252

File tree

1 file changed

+38
-12
lines changed

1 file changed

+38
-12
lines changed

README.md

Lines changed: 38 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -249,13 +249,9 @@ Update `nodejs/Dockerfile`:
249249
```Dockerfile
250250
FROM public.ecr.aws/lambda/nodejs:24
251251

252-
COPY ./package*.json ${LAMBDA_TASK_ROOT}
253-
254-
RUN npm ci
255-
256252
COPY ./ ${LAMBDA_TASK_ROOT}
257253

258-
RUN npm run build
254+
RUN npm ci && npm run build
259255

260256
CMD [ "build/index.handler" ]
261257
```
@@ -540,7 +536,35 @@ docker compose up --abort-on-container-exit --build
540536

541537
## 5. Optimisation
542538

543-
**Goal:** Prepare for production with multi-stage builds.
539+
**Goal:** Prepare for production with improved caching and multi-stage builds.
540+
541+
### Improved caching
542+
543+
Replace `nodejs/Dockerfile` with this cached optimised version:
544+
545+
```Dockerfile
546+
FROM public.ecr.aws/lambda/nodejs:24
547+
548+
COPY ./package*.json ${LAMBDA_TASK_ROOT}
549+
550+
RUN npm ci
551+
552+
COPY ./ ${LAMBDA_TASK_ROOT}
553+
554+
RUN npm run build
555+
556+
CMD [ "build/index.handler" ]
557+
```
558+
559+
Run the following command:
560+
561+
```shell
562+
docker compose up --abort-on-container-exit --build
563+
```
564+
565+
> [!TIP]
566+
> In this iteration, as `npm ci` and `npm run build` are two different layers, when one changes it doesn't impact the other.
567+
> For example, if we update our code without updating any packages, the `npm ci` can still use it's cached version where as the `npm run build` will get rebuilt.
544568
545569
### Multi-stage build
546570

@@ -570,14 +594,16 @@ COPY --from=builder ${LAMBDA_TASK_ROOT}/build ${LAMBDA_TASK_ROOT}/build
570594
CMD [ "build/index.handler" ]
571595
```
572596

573-
### Test the optimised build
574-
575-
Run the following command to ensure everything still works:
597+
Run the following command:
576598

577599
```shell
578-
docker compose up --build --abort-on-container-exit
600+
docker compose up --abort-on-container-exit --build
579601
```
580602

603+
> [!NOTE]
604+
> In this iteration, our built image only includes the files needed to actually be executed.
605+
> This means our Docker images has a reduced size but also any potential security risk of the development dependencies are removed.
606+
581607
---
582608

583609
## 6: Advanced integration
@@ -679,7 +705,7 @@ export const handler: Handler = async (event, context) => {
679705
Run the following command:
680706

681707
```shell
682-
docker compose up --build --abort-on-container-exit
708+
docker compose up --abort-on-container-exit --build
683709
```
684710

685711
---
@@ -724,7 +750,7 @@ services:
724750
### Run it
725751

726752
```shell
727-
docker compose up --build --abort-on-container-exit
753+
docker compose up --abort-on-container-exit --build
728754
```
729755

730756
> [!NOTE]

0 commit comments

Comments
 (0)