feat: Alpine Dockerfile version
All checks were successful
ci/woodpecker/push/workflow Pipeline was successful
All checks were successful
ci/woodpecker/push/workflow Pipeline was successful
This commit is contained in:
parent
1703be6a0e
commit
0ebade2b63
4 changed files with 89 additions and 6 deletions
|
@ -21,6 +21,5 @@ steps:
|
||||||
- /var/run/docker.sock:/var/run/docker.sock
|
- /var/run/docker.sock:/var/run/docker.sock
|
||||||
commands:
|
commands:
|
||||||
- docker login forgejo.transprot.eu -u $DOCKER_USERNAME -p $DOCKER_PASSWORD
|
- docker login forgejo.transprot.eu -u $DOCKER_USERNAME -p $DOCKER_PASSWORD
|
||||||
- docker build -t 'forgejo.transprot.eu/public/react-multistage:${CI_PIPELINE_NUMBER}' -t 'forgejo.transprot.eu/public/react-multistage:latest' .
|
- docker build -t 'forgejo.transprot.eu/public/react-multistage-alpine:latest' .
|
||||||
- docker push forgejo.transprot.eu/public/react-multistage:${CI_PIPELINE_NUMBER}
|
- docker push forgejo.transprot.eu/public/react-multistage-alpine:latest
|
||||||
- docker push forgejo.transprot.eu/public/react-multistage:latest
|
|
|
@ -1,5 +1,5 @@
|
||||||
# Build stage of the application
|
# Build stage of the application
|
||||||
FROM node:18 AS build
|
FROM node:18-alpine AS build
|
||||||
WORKDIR /app
|
WORKDIR /app
|
||||||
COPY package*.json ./
|
COPY package*.json ./
|
||||||
RUN npm install
|
RUN npm install
|
||||||
|
@ -7,7 +7,7 @@ COPY . .
|
||||||
RUN npm run build
|
RUN npm run build
|
||||||
|
|
||||||
# Production stage to run the application
|
# Production stage to run the application
|
||||||
FROM node:18 AS production
|
FROM node:18-alpine AS production
|
||||||
COPY --from=build /app/build /prod
|
COPY --from=build /app/build /prod
|
||||||
RUN npm install --global serve
|
RUN npm install --global serve
|
||||||
EXPOSE 3000
|
EXPOSE 3000
|
||||||
|
|
86
README.md
86
README.md
|
@ -89,6 +89,48 @@ I am still usig Nushell on my end to have a pretty print and filter on the image
|
||||||
|
|
||||||
The image now weighs `44 MB` less, which is 1.27 times smaller, and a whopping 21.67% reduction in size!
|
The image now weighs `44 MB` less, which is 1.27 times smaller, and a whopping 21.67% reduction in size!
|
||||||
|
|
||||||
|
### Second optimisation: using alpine images
|
||||||
|
|
||||||
|
Alpine images are based on the Alpine Linux distro, which is a minimalistic and light weight version of Linux. It's realle stupid and simple, but if your images are built using a small base image, your resulting image will also be small !
|
||||||
|
|
||||||
|
It's as easy as adding `-alpine` on your images in your Dockerfile.
|
||||||
|
Here's the new version of the Dockerfile using an alpine image of node:
|
||||||
|
```Dockerfile
|
||||||
|
# Build stage of the application
|
||||||
|
FROM node:18-alpine AS build
|
||||||
|
WORKDIR /app
|
||||||
|
COPY package*.json ./
|
||||||
|
RUN npm install
|
||||||
|
COPY . .
|
||||||
|
RUN npm run build
|
||||||
|
|
||||||
|
# Production stage to run the application
|
||||||
|
FROM node:18-alpine AS production
|
||||||
|
COPY --from=build /app/build /prod
|
||||||
|
RUN npm install --global serve
|
||||||
|
EXPOSE 3000
|
||||||
|
CMD ["serve", "prod/"]
|
||||||
|
```
|
||||||
|
|
||||||
|
You can rebuild the image by running the following command:
|
||||||
|
```shell
|
||||||
|
docker build -t 'react-multistage-alpine' .
|
||||||
|
```
|
||||||
|
|
||||||
|
You can also verify the image sizes by running the following command:
|
||||||
|
```shell
|
||||||
|
docker image ls
|
||||||
|
```
|
||||||
|
|
||||||
|
And as always, I'm using Nushell to have a prettier output, which gives me the following results which are incredibly impressive:
|
||||||
|

|
||||||
|
|
||||||
|
A size reduction of `1.82 GB`, which is an image that is 9.8 smaller than the first one and is equivalent to a size reduction of 89.8% !!!!
|
||||||
|
|
||||||
|
And the best in all of this ? Not only is the image smaller, but it is way more performant as it only includes what is necessary to run the application on the alpine version of node.
|
||||||
|
|
||||||
|
But we can go even further beyond in optimizing this image.
|
||||||
|
|
||||||
# Français
|
# Français
|
||||||
|
|
||||||
## Comment réduire la taille de vos images 💿 docker 🐳 ?
|
## Comment réduire la taille de vos images 💿 docker 🐳 ?
|
||||||
|
@ -178,4 +220,46 @@ docker image ls
|
||||||
Pour ma part j'utilise toujours Nushell pour avoir cette belle présentation des résultats filtré:
|
Pour ma part j'utilise toujours Nushell pour avoir cette belle présentation des résultats filtré:
|
||||||

|

|
||||||
|
|
||||||
L'image pèse maintenant `44 MB` de moins, on a une image 1.27 fois plus petite, soit une diminution de la taille de 21.67% !
|
L'image pèse maintenant `44 MB` de moins, on a une image 1.27 fois plus petite, soit une diminution de la taille de 21.67% !
|
||||||
|
|
||||||
|
### Deuxième optimisation: utilisation d'images alpines
|
||||||
|
|
||||||
|
Les images alpine sont basé sur la distro Alpine Linux qui est une version minimale et peu volumineuse de Linux. C'est tout bête, mais si l'image de base de votre image est petite, votre image finale le sera aussi !
|
||||||
|
|
||||||
|
C'est tout aussi simple que de rajouter `-alpine` sur vos image docker dans le Dockerfile.
|
||||||
|
Voici la nouvelle version du Dockerfile:
|
||||||
|
```Dockerfile
|
||||||
|
# Build stage of the application
|
||||||
|
FROM node:18-alpine AS build
|
||||||
|
WORKDIR /app
|
||||||
|
COPY package*.json ./
|
||||||
|
RUN npm install
|
||||||
|
COPY . .
|
||||||
|
RUN npm run build
|
||||||
|
|
||||||
|
# Production stage to run the application
|
||||||
|
FROM node:18-alpine AS production
|
||||||
|
COPY --from=build /app/build /prod
|
||||||
|
RUN npm install --global serve
|
||||||
|
EXPOSE 3000
|
||||||
|
CMD ["serve", "prod/"]
|
||||||
|
```
|
||||||
|
|
||||||
|
Puis vous pouvez reconstruire l'image en lançant la commande suivante:
|
||||||
|
```shell
|
||||||
|
docker build -t 'react-multistage-alpine' .
|
||||||
|
```
|
||||||
|
|
||||||
|
Vous pouvez maintenant lancer la commande suivante vérifier la taille de votre image:
|
||||||
|
```shell
|
||||||
|
docker image ls
|
||||||
|
```
|
||||||
|
|
||||||
|
Et comme d'habitude, j'utilise Nushell pour avoir un meilleur rendu visuelle, et voici les résultats qui sont impréssionnants:
|
||||||
|

|
||||||
|
|
||||||
|
On a une réduction de `1.82 GB`, soit une image 9.8 fois plus petite et une réduction de 89.8% !!!!
|
||||||
|
|
||||||
|
Et le meilleur dans tout ça ? Non seulement l'image est plus petite, mais elle est plus performante car elle ne contient que le strict minimum nécessaire pour faire tourner node via la version alpine.
|
||||||
|
|
||||||
|
Mais on peut encore aller plus loins dans l'optimisation.
|
BIN
assets/react-multistage-alpine-size.png
Normal file
BIN
assets/react-multistage-alpine-size.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 16 KiB |
Loading…
Reference in a new issue