90 lines
2.8 KiB
Markdown
90 lines
2.8 KiB
Markdown
# Note sul Dockerfile
|
|
|
|
## Protezione del Codice Sorgente
|
|
|
|
Questo Dockerfile è configurato per **proteggere il codice sorgente Python** rimuovendo i file `.py` e mantenendo solo il bytecode compilato (`.pyc`).
|
|
|
|
### Processo di Build
|
|
|
|
1. **Copia dei sorgenti**: I file Python vengono copiati nell'immagine
|
|
2. **Installazione dipendenze**: Usa `uv` per installare le dipendenze Python
|
|
3. **Compilazione bytecode**: Compila tutti i file con `python -OO -m compileall`
|
|
4. **Rimozione sorgenti**: Elimina tutti i file `.py` lasciando solo i `.pyc`
|
|
|
|
### Ottimizzazione `-OO`
|
|
|
|
L'opzione `-OO` di Python:
|
|
- Rimuove le istruzioni `assert`
|
|
- Rimuove le docstring (`__doc__`)
|
|
- Rimuove il flag `__debug__`
|
|
- Produce file `.opt-2.pyc` invece di `.pyc`
|
|
|
|
### Variabili Ambiente
|
|
|
|
- `PYTHONUNBUFFERED=1`: Output immediato dei log (no buffering)
|
|
- `PYTHONDONTWRITEBYTECODE=1`: Non crea nuovi `.pyc` a runtime (già compilati)
|
|
- `PYTHONPATH=/app`: Permette import diretti da `/app`
|
|
|
|
### Verifica Immagine
|
|
|
|
Per verificare che l'immagine sia stata buildata correttamente:
|
|
|
|
```bash
|
|
# Build locale
|
|
docker build -t orchestrator-app:test .
|
|
|
|
# Test immagine
|
|
../scripts/test-pyc-image.sh orchestrator-app:test
|
|
|
|
# Verifica manualmente
|
|
docker run --rm orchestrator-app:test find /app/src -name "*.py" # Deve restituire nulla
|
|
docker run --rm orchestrator-app:test find /app/src -name "*.pyc" # Deve mostrare i .pyc
|
|
```
|
|
|
|
### Limitazioni
|
|
|
|
⚠️ **Debug**: I traceback non mostrano il codice sorgente
|
|
⚠️ **Sviluppo**: Non usare questa configurazione per sviluppo locale
|
|
⚠️ **Repository**: Mantieni sempre i sorgenti in git
|
|
|
|
### Alternative
|
|
|
|
Se vuoi **disabilitare** la rimozione dei sorgenti (per debug), commenta queste righe nel Dockerfile:
|
|
|
|
```dockerfile
|
|
# Commenta queste righe per mantenere i file .py
|
|
# RUN python -OO -m compileall /app/src /app/env || true
|
|
# RUN find /app/src -type f -name "*.py" -delete && \
|
|
# find /app/env -type f -name "*.py" -delete || true
|
|
```
|
|
|
|
### Build con Sorgenti (Debug)
|
|
|
|
Per build temporanee con codice sorgente visibile:
|
|
|
|
```dockerfile
|
|
# Dockerfile.debug
|
|
FROM python:3.12-slim
|
|
COPY --from=ghcr.io/astral-sh/uv:latest /uv /usr/local/bin/uv
|
|
WORKDIR /app
|
|
COPY pyproject.toml ./
|
|
COPY src/ ./src/
|
|
COPY env/ ./env/
|
|
COPY certs/ ./certs/
|
|
COPY matlab_func/ ./matlab_func/
|
|
RUN uv pip install --system -e .
|
|
# NO COMPILATION - NO DELETION
|
|
RUN mkdir -p /app/logs /app/aseftp/csvfs /app/certs /app/matlab_runtime /app/matlab_func
|
|
ENV PYTHONUNBUFFERED=1
|
|
ENV PYTHONPATH=/app
|
|
CMD ["python", "-m", "src.elab_orchestrator"]
|
|
```
|
|
|
|
## Best Practices
|
|
|
|
1. **Versioning**: Usa tag semantici per le immagini di produzione
|
|
2. **Registry**: Pusha le immagini compilate su un registry privato
|
|
3. **Backup**: Tieni sempre i sorgenti in un repository git sicuro
|
|
4. **Testing**: Testa l'immagine con `test-pyc-image.sh` prima del deploy
|
|
5. **Security**: Combina con autenticazione registry per massima sicurezza
|