5 premiers cours
This commit is contained in:
392
05_networks_dockerfile.md
Normal file
392
05_networks_dockerfile.md
Normal file
@@ -0,0 +1,392 @@
|
||||
---
|
||||
marp: true
|
||||
theme: default
|
||||
paginate: true
|
||||
backgroundColor: #fff
|
||||
footer: 'Conteneurisation - Partie 2 - 2025'
|
||||
style: |
|
||||
section {
|
||||
font-family: 'Arial', sans-serif;
|
||||
}
|
||||
h1 {
|
||||
color: #2496ed;
|
||||
}
|
||||
h2 {
|
||||
color: #384c54;
|
||||
}
|
||||
---
|
||||
|
||||
# Docker Networks & Dockerfile
|
||||
### IUT Sénart-Fontainebleau
|
||||
#### 2025
|
||||
|
||||
---
|
||||
|
||||
# Plan du cours
|
||||
1. Docker Networks
|
||||
- Types de réseaux
|
||||
- Communication entre conteneurs
|
||||
- Use cases et bonnes pratiques
|
||||
- Exercices pratiques
|
||||
|
||||
2. Dockerfile
|
||||
- Construction d'images
|
||||
- Multi-stage builds
|
||||
- Optimisation et sécurité
|
||||
- Cas pratiques
|
||||
|
||||
---
|
||||
|
||||
# 1. Docker Networks
|
||||
|
||||
## Pourquoi les réseaux Docker ?
|
||||
- Isolation des conteneurs
|
||||
- Communication sécurisée
|
||||
- Simulation d'infrastructure
|
||||
- Environnements multi-conteneurs
|
||||
- Séparation des préoccupations
|
||||
|
||||
---
|
||||
|
||||
# Types de réseaux Docker
|
||||
|
||||
## Bridge (default)
|
||||
- Réseau privé interne
|
||||
- Communication entre conteneurs
|
||||
- Port mapping pour accès externe
|
||||
```bash
|
||||
# Création d'un réseau bridge personnalisé
|
||||
docker network create --driver bridge my-network
|
||||
|
||||
# Lancement d'un conteneur sur ce réseau
|
||||
docker run --network my-network --name web1 nginx
|
||||
|
||||
# Inspection du réseau
|
||||
docker network inspect my-network
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
# Types de réseaux Docker
|
||||
|
||||
## Host
|
||||
- Utilise la stack réseau de l'hôte
|
||||
- Performance maximale
|
||||
- Moins sécurisé
|
||||
- Cas d'usage : haute performance nécessaire
|
||||
```bash
|
||||
# Lancement en mode host
|
||||
docker run --network host nginx
|
||||
|
||||
# Vérification des ports utilisés
|
||||
ss -tunlp
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
# Types de réseaux Docker
|
||||
|
||||
## None
|
||||
- Aucune connectivité réseau
|
||||
- Isolation totale
|
||||
- Pour les traitements isolés
|
||||
- Cas d'usage : scripts de traitement batch
|
||||
```bash
|
||||
# Isolation complète
|
||||
docker run --network none nginx
|
||||
|
||||
# Vérification de l'isolation
|
||||
docker exec [container-id] ping 8.8.8.8
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
# Communication entre conteneurs
|
||||
|
||||
## Même réseau bridge
|
||||
```bash
|
||||
# Création du réseau
|
||||
docker network create app-network
|
||||
|
||||
# Déploiement d'une stack web
|
||||
docker run -d --network app-network --name frontend nginx
|
||||
docker run -d --network app-network --name backend api
|
||||
docker run -d --network app-network --name db postgres
|
||||
|
||||
# Test de communication
|
||||
docker exec frontend ping backend
|
||||
docker exec backend ping db
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
# DNS dans Docker
|
||||
|
||||
## Service Discovery
|
||||
- Résolution automatique des noms
|
||||
- Service discovery intégré
|
||||
- Facilite la mise à l'échelle
|
||||
|
||||
```bash
|
||||
# Communication par nom de service
|
||||
http://backend:8080/api
|
||||
http://db:5432
|
||||
|
||||
# Test de résolution DNS
|
||||
docker exec frontend nslookup backend
|
||||
docker exec frontend wget -qO- http://backend/health
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
# Inspection et Debug Réseau
|
||||
|
||||
## Outils disponibles
|
||||
```bash
|
||||
# Voir les réseaux
|
||||
docker network ls
|
||||
|
||||
# Détails d'un réseau
|
||||
docker network inspect bridge
|
||||
|
||||
# Connecter/Déconnecter à chaud
|
||||
docker network connect app-network container1
|
||||
docker network disconnect app-network container1
|
||||
|
||||
# Traces réseau
|
||||
docker logs --follow container1
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
# 2. Pourquoi des Dockerfiles ?
|
||||
|
||||
## Problèmes résolus
|
||||
- "Ça marche sur ma machine"
|
||||
- Dépendances manquantes
|
||||
- Versions différentes
|
||||
- Configuration manuelle
|
||||
|
||||
## Avantages
|
||||
- Infrastructure as Code (IaC)
|
||||
- Reproductibilité garantie
|
||||
- Déploiement automatisé
|
||||
- Tests cohérents
|
||||
- Documentation vivante
|
||||
|
||||
---
|
||||
|
||||
# Structure d'un Dockerfile Avancé
|
||||
|
||||
```dockerfile
|
||||
# Base image selection
|
||||
FROM alpine:3.14 AS builder
|
||||
|
||||
# Métadonnées détaillées
|
||||
LABEL maintainer="name@example.com" \
|
||||
version="1.0" \
|
||||
description="Application web" \
|
||||
environment="production"
|
||||
|
||||
# Variables d'environnement
|
||||
ENV APP_HOME=/app \
|
||||
APP_USER=webuser \
|
||||
APP_ENV=prod
|
||||
```
|
||||
|
||||
---
|
||||
```dockerfile
|
||||
# Création utilisateur dédié
|
||||
RUN adduser -D -h $APP_HOME $APP_USER
|
||||
|
||||
# Installation des dépendances
|
||||
RUN apk add --no-cache \
|
||||
nodejs \
|
||||
npm \
|
||||
git
|
||||
|
||||
# Configuration du workdir
|
||||
WORKDIR $APP_HOME
|
||||
|
||||
# Copie et permissions
|
||||
COPY --chown=$APP_USER:$APP_USER . .
|
||||
|
||||
# Switch utilisateur
|
||||
USER $APP_USER
|
||||
|
||||
# Healthcheck
|
||||
HEALTHCHECK --interval=30s --timeout=3s \
|
||||
CMD wget -q localhost:3000/health || exit 1
|
||||
|
||||
# Commande de démarrage
|
||||
CMD ["node", "app.js"]
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
# Multi-stage builds
|
||||
|
||||
## Exemple Complet
|
||||
```dockerfile
|
||||
# Stage 1: Build
|
||||
FROM node:16 AS builder
|
||||
WORKDIR /build
|
||||
COPY package*.json ./
|
||||
RUN npm install
|
||||
COPY . .
|
||||
RUN npm run build
|
||||
|
||||
# Stage 2: Test
|
||||
FROM builder AS tester
|
||||
RUN npm run test
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
# Multi-stage builds
|
||||
|
||||
## Exemple Complet
|
||||
|
||||
```dockerfile
|
||||
# Stage 3: Production
|
||||
FROM nginx:alpine
|
||||
COPY --from=builder /build/dist /usr/share/nginx/html
|
||||
COPY nginx.conf /etc/nginx/conf.d/default.conf
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
# Optimisation des images
|
||||
|
||||
## Techniques avancées
|
||||
1. Utilisation de .dockerignore
|
||||
```dockerignore
|
||||
node_modules
|
||||
*.log
|
||||
.git
|
||||
tests
|
||||
docs
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
# Optimisation des images
|
||||
|
||||
2. Ordonnancement des layers
|
||||
```dockerfile
|
||||
# Bon ordre (moins de rebuilds)
|
||||
COPY package.json .
|
||||
RUN npm install
|
||||
COPY . .
|
||||
|
||||
# Mauvais ordre (rebuilds fréquents)
|
||||
COPY . .
|
||||
RUN npm install
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
# Optimisation des images
|
||||
|
||||
3. Nettoyage des caches
|
||||
```dockerfile
|
||||
RUN apt-get update && \
|
||||
apt-get install -y package && \
|
||||
rm -rf /var/lib/apt/lists/*
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
# Variables d'environnement et Secrets
|
||||
|
||||
## Bonnes pratiques
|
||||
```dockerfile
|
||||
# Variables build-time
|
||||
ARG VERSION=1.0
|
||||
|
||||
# Variables runtime
|
||||
ENV APP_VERSION=${VERSION} \
|
||||
NODE_ENV=production
|
||||
|
||||
# Usage avec arguments
|
||||
docker build --build-arg VERSION=2.0 .
|
||||
```
|
||||
|
||||
|
||||
---
|
||||
|
||||
# Variables d'environnement et Secrets
|
||||
|
||||
## Secrets en production
|
||||
```bash
|
||||
# Utilisation de docker secrets
|
||||
docker secret create db_password password.txt
|
||||
docker service create --secret db_password ...
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
# Healthcheck Détaillé
|
||||
|
||||
## Implémentation robuste
|
||||
```dockerfile
|
||||
HEALTHCHECK --interval=30s \
|
||||
--timeout=3s \
|
||||
--start-period=60s \
|
||||
--retries=3 \
|
||||
CMD wget -q --spider \
|
||||
http://localhost:80/health || exit 1
|
||||
```
|
||||
|
||||
## Monitoring
|
||||
```bash
|
||||
# Vérification status
|
||||
docker inspect --format='{{.State.Health.Status}}' container
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
# Sécurité et Bonnes Pratiques
|
||||
|
||||
## Sécurisation des conteneurs
|
||||
- Utilisateur non-root
|
||||
- Images officielles ou vérifiées
|
||||
- Scan des vulnérabilités
|
||||
- Mise à jour régulière des bases
|
||||
- Principe du moindre privilège
|
||||
|
||||
```dockerfile
|
||||
# Exemple sécurisé
|
||||
FROM alpine:3.14
|
||||
RUN adduser -D appuser
|
||||
USER appuser
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
# Déploiement et Tests
|
||||
|
||||
## Workflow complet
|
||||
```bash
|
||||
# Build avec tag
|
||||
docker build -t app:latest .
|
||||
|
||||
# Test local
|
||||
docker run --rm app:latest
|
||||
|
||||
# Publication
|
||||
docker tag app:latest registry/app:latest
|
||||
docker push registry/app:latest
|
||||
|
||||
# Déploiement
|
||||
docker pull registry/app:latest
|
||||
docker run -d app:latest
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
# Questions & Travaux Pratiques
|
||||
|
||||
Explication de quelques notions pour le TP
|
||||
|
||||
Place aux exercices pratiques !
|
||||
Reference in New Issue
Block a user