@@ -0,0 +1,11 @@
|
||||
FROM nginx:1.27-alpine
|
||||
|
||||
ARG NGINX_CONF=web.conf
|
||||
|
||||
RUN rm -f /etc/nginx/conf.d/default.conf \
|
||||
&& mkdir -p /usr/share/nginx/html
|
||||
|
||||
COPY docker/nginx/${NGINX_CONF} /etc/nginx/conf.d/app.conf
|
||||
COPY docker/nginx/snippets /etc/nginx/snippets
|
||||
|
||||
EXPOSE 80
|
||||
@@ -0,0 +1,41 @@
|
||||
# Image des services API Node. Un seul Dockerfile pour Cloud et CRM :
|
||||
# APP_DIR choisit quelle application on construit, donc les deux deploiements
|
||||
# ne peuvent pas diverger dans leur methode de construction.
|
||||
FROM node:22-slim AS builder
|
||||
|
||||
ARG APP_DIR
|
||||
|
||||
WORKDIR /repo
|
||||
|
||||
COPY package.json package-lock.json tsconfig.json tsconfig.base.json ./
|
||||
COPY frontend ./frontend
|
||||
COPY backend ./backend
|
||||
|
||||
RUN npm ci --no-audit --no-fund --fetch-retries=5
|
||||
RUN npm run build --workspace=backend/shared
|
||||
RUN npm run build --workspace=${APP_DIR}
|
||||
|
||||
# On ne embarque pas les devDependencies dans l'image de production.
|
||||
RUN npm prune --omit=dev && npm cache clean --force
|
||||
|
||||
FROM node:22-alpine AS export
|
||||
|
||||
ARG APP_DIR
|
||||
|
||||
ENV NODE_ENV=production
|
||||
|
||||
WORKDIR /repo
|
||||
|
||||
COPY --from=builder /repo/package.json ./package.json
|
||||
COPY --from=builder /repo/package-lock.json ./package-lock.json
|
||||
COPY --from=builder /repo/node_modules ./node_modules
|
||||
# backend/shared est le paquet @cohaise/server : les liens de workspace
|
||||
# de node_modules pointent dessus, il doit donc voyager avec l'application.
|
||||
COPY --from=builder /repo/backend/shared ./backend/shared
|
||||
COPY --from=builder /repo/${APP_DIR} ./${APP_DIR}
|
||||
|
||||
WORKDIR /repo/${APP_DIR}
|
||||
|
||||
EXPOSE 8080
|
||||
|
||||
CMD ["node", "dist/index.js"]
|
||||
@@ -0,0 +1,19 @@
|
||||
FROM node:22-slim AS builder
|
||||
|
||||
WORKDIR /repo
|
||||
|
||||
ARG APP_DIR
|
||||
|
||||
COPY package.json package-lock.json tsconfig.json tsconfig.base.json ./
|
||||
COPY frontend ./frontend
|
||||
COPY backend ./backend
|
||||
|
||||
RUN npm ci --no-audit --no-fund --fetch-retries=5
|
||||
RUN npm run build --workspace=frontend/shared
|
||||
RUN npm run build --workspace=${APP_DIR}
|
||||
|
||||
FROM alpine:3.21 AS export
|
||||
|
||||
ARG APP_DIR
|
||||
|
||||
COPY --from=builder /repo/${APP_DIR}/dist /app/dist
|
||||
@@ -0,0 +1,78 @@
|
||||
# docker/ — images de la vitrine
|
||||
|
||||
Contient **tout ce qui touche à l'image** : build du front, image nginx,
|
||||
configuration et script de publication. Ce dépôt ne construit qu'une seule app
|
||||
(la vitrine) mais trois images, d'où ce dossier dédié plutôt que des `Dockerfile`
|
||||
à la racine.
|
||||
|
||||
Le fonctionnement du service (compose, ports, healthchecks) est décrit dans
|
||||
[`../docs/deployment.md`](../docs/deployment.md) ; la sécurité des en-têtes, dans
|
||||
[`../docs/security.md`](../docs/security.md).
|
||||
|
||||
## Fichiers
|
||||
|
||||
| Fichier | Image / rôle | Utilisé par compose ? |
|
||||
| --- | --- | --- |
|
||||
| `Dockerfile` | `nginx:1.27-alpine` + `nginx/web.conf` — le serveur | oui → service `nginx` |
|
||||
| `Dockerfile.app` | build Vite dans `node:22-alpine`, export `dist/` | oui → service `app-build` |
|
||||
| `Dockerfile.api` | build de `backend/shared` + API | **non** (reliquat du monorepo) |
|
||||
| `nginx/web.conf` | serveur : racine `current`, `/healthz`, gzip, caches | copié dans l'image |
|
||||
| `nginx/snippets/security-headers.conf` | `nosniff`, `X-Frame-Options`, `Referrer-Policy` | inclus partout |
|
||||
| `nginx/snippets/cache-immutable.conf` | `public, immutable` (assets) | `location /assets/` |
|
||||
| `nginx/snippets/cache-no-cache.conf` | `no-cache` (`index.html`) | `location /` |
|
||||
| `publish.sh` | publication atomique dans le volume | monté dans `app-build` |
|
||||
|
||||
`Dockerfile.api` n'est référencé nulle part : `npm run build` le rend obsolète
|
||||
pour la vitrine. Il n'est conservé que parce que `backend/shared` appartient
|
||||
toujours au workspace.
|
||||
|
||||
## Images
|
||||
|
||||
```sh
|
||||
docker compose build # les deux images
|
||||
docker compose build app-build # seulement le build du front
|
||||
```
|
||||
|
||||
### `Dockerfile` (nginx)
|
||||
|
||||
Supprime le `default.conf` par défaut, copie `web.conf` et les snippets,
|
||||
`EXPOSE 80`. Rien d'autre : pas de module ajouté, pas d'utilisateur dédié.
|
||||
|
||||
### `Dockerfile.app` (front)
|
||||
|
||||
- `ARG APP_DIR` (`frontend/web`, passé par compose).
|
||||
- Stage `builder` : `npm ci` avec cache npm, puis
|
||||
`npm run build --workspace=frontend/shared` **puis**
|
||||
`--workspace=${APP_DIR}` (l'ordre compte : la vitrine importe le design system).
|
||||
- Stage `export` : recopie uniquement `dist/` dans `/app/dist`, sans les
|
||||
sources ni `node_modules`.
|
||||
|
||||
### Publication
|
||||
|
||||
Le service `app-build` exécute `sh /publish.sh` avec `app_data` monté :
|
||||
|
||||
1. copie de `dist/` vers `releases/<UTC>-<pid>` ;
|
||||
2. bascule atomique du lien `current` (`ln -sfn` puis `mv -T`) ;
|
||||
3. purge des releases autres que la courante, **après 5 minutes**.
|
||||
|
||||
Pendant la copie, nginx sert encore l'ancienne version. Aucun reload nécessaire.
|
||||
|
||||
## Aperçu de la conf nginx
|
||||
|
||||
```nginx
|
||||
root /usr/share/nginx/html/current;
|
||||
location = /healthz { return 200 "ok\n"; access_log off; }
|
||||
location /assets/ { include snippets/cache-immutable.conf; expires 1y; try_files $uri =404; }
|
||||
location / { include snippets/cache-no-cache.conf; try_files $uri $uri/ @spa; }
|
||||
location @spa { try_files /index.html =404; }
|
||||
```
|
||||
|
||||
Les en-têtes de sécurité sont inclus **au niveau serveur et dans chaque
|
||||
location** : les réponses d'erreur et de santé sont couvertes aussi.
|
||||
|
||||
## Modifier la configuration
|
||||
|
||||
1. Éditer `nginx/web.conf` ou un snippet.
|
||||
2. `docker compose build nginx && docker compose up -d nginx`
|
||||
(ou `make reload` si le fichier est monté — il ne l'est pas : il est copié
|
||||
dans l'image, donc une reconstruction est obligatoire).
|
||||
@@ -0,0 +1,55 @@
|
||||
server {
|
||||
listen 80 default_server;
|
||||
listen [::]:80 default_server;
|
||||
server_name _;
|
||||
|
||||
root /usr/share/nginx/html/current;
|
||||
index index.html;
|
||||
|
||||
include /etc/nginx/snippets/security-headers.conf;
|
||||
|
||||
gzip on;
|
||||
gzip_vary on;
|
||||
gzip_min_length 1024;
|
||||
gzip_types text/plain text/css application/javascript application/json image/svg+xml application/manifest+json;
|
||||
|
||||
location = /healthz {
|
||||
access_log off;
|
||||
default_type text/plain;
|
||||
return 200 "ok\n";
|
||||
}
|
||||
|
||||
# L'API. Le resolver Docker est declare pour que le conteneur soit resolu
|
||||
# a la demande : nginx peut demarrer avant l'API, et le front reste servi
|
||||
# pendant un redemarrage de celle-ci.
|
||||
location /api/ {
|
||||
include /etc/nginx/snippets/security-headers.conf;
|
||||
resolver 127.0.0.11 valid=30s ipv6=off;
|
||||
set $cohaise_api http://api:8080;
|
||||
proxy_pass $cohaise_api$request_uri;
|
||||
proxy_http_version 1.1;
|
||||
proxy_set_header Host $host;
|
||||
proxy_set_header X-Real-IP $remote_addr;
|
||||
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
||||
proxy_set_header X-Forwarded-Proto $scheme;
|
||||
proxy_read_timeout 60s;
|
||||
client_max_body_size 1m;
|
||||
}
|
||||
|
||||
location /assets/ {
|
||||
include /etc/nginx/snippets/security-headers.conf;
|
||||
include /etc/nginx/snippets/cache-immutable.conf;
|
||||
expires 1y;
|
||||
try_files $uri =404;
|
||||
}
|
||||
|
||||
location / {
|
||||
include /etc/nginx/snippets/security-headers.conf;
|
||||
include /etc/nginx/snippets/cache-no-cache.conf;
|
||||
try_files $uri $uri/ @spa;
|
||||
}
|
||||
|
||||
location @spa {
|
||||
try_files /index.html =404;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
add_header Cache-Control "public, immutable" always;
|
||||
@@ -0,0 +1 @@
|
||||
add_header Cache-Control "no-cache" always;
|
||||
@@ -0,0 +1,3 @@
|
||||
add_header X-Content-Type-Options "nosniff" always;
|
||||
add_header X-Frame-Options "DENY" always;
|
||||
add_header Referrer-Policy "strict-origin-when-cross-origin" always;
|
||||
@@ -0,0 +1,38 @@
|
||||
server {
|
||||
listen 80 default_server;
|
||||
listen [::]:80 default_server;
|
||||
server_name _;
|
||||
|
||||
root /usr/share/nginx/html/current;
|
||||
index index.html;
|
||||
|
||||
include /etc/nginx/snippets/security-headers.conf;
|
||||
|
||||
gzip on;
|
||||
gzip_vary on;
|
||||
gzip_min_length 1024;
|
||||
gzip_types text/plain text/css application/javascript application/json image/svg+xml application/manifest+json;
|
||||
|
||||
location = /healthz {
|
||||
access_log off;
|
||||
default_type text/plain;
|
||||
return 200 "ok\n";
|
||||
}
|
||||
|
||||
location /assets/ {
|
||||
include /etc/nginx/snippets/security-headers.conf;
|
||||
include /etc/nginx/snippets/cache-immutable.conf;
|
||||
expires 1y;
|
||||
try_files $uri =404;
|
||||
}
|
||||
|
||||
location / {
|
||||
include /etc/nginx/snippets/security-headers.conf;
|
||||
include /etc/nginx/snippets/cache-no-cache.conf;
|
||||
try_files $uri $uri/ @spa;
|
||||
}
|
||||
|
||||
location @spa {
|
||||
try_files /index.html =404;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
#!/bin/sh
|
||||
set -eu
|
||||
|
||||
SRC="${1:-/app/dist}"
|
||||
DEST="${2:-/output}"
|
||||
|
||||
REL="releases/$(date -u +%Y%m%dT%H%M%SZ)-$$"
|
||||
KEEP=$(basename "$REL")
|
||||
|
||||
mkdir -p "$DEST/$REL"
|
||||
cp -a "$SRC/." "$DEST/$REL/"
|
||||
touch "$DEST/$REL"
|
||||
|
||||
ln -sfn "$REL" "$DEST/.current.tmp"
|
||||
mv -T "$DEST/.current.tmp" "$DEST/current"
|
||||
|
||||
find "$DEST/releases" -mindepth 1 -maxdepth 1 -type d ! -name "$KEEP" -mmin +5 -exec rm -rf {} + 2>/dev/null || true
|
||||
Reference in New Issue
Block a user