« Symfony Docker » : différence entre les versions
| Ligne 749 : | Ligne 749 : | ||
Le choix dépend de votre workflow, d’où le caractère optionnel ! | Le choix dépend de votre workflow, d’où le caractère optionnel ! | ||
== '''version complète optimisée avec un service Composer dédié''' == | |||
Voici une version complète optimisée avec '''un service Composer dédié''', tout en gardant la configuration pour vos bundles locaux et l’intégration avec PHPStorm. | |||
=== Structure finale recommandée === | |||
<pre>~/debian/projet/ | |||
├── app/ # Projet Symfony principal | |||
│ ├── src/ | |||
│ ├── vendor/ | |||
│ ├── public/ | |||
│ └── composer.json | |||
├── Bundles/ # Bundles locaux | |||
│ ├── bundlea/ # Bundle A | |||
│ └── bundleb/ # Bundle B | |||
├── docker/ | |||
│ ├── apache/ | |||
│ │ └── vhost.conf # Config Apache | |||
│ ├── php/ | |||
│ │ └── php.ini # Config PHP | |||
│ └── entrypoint.sh # Script d'initialisation | |||
├── .env # Variables d'environnement | |||
├── Dockerfile # Build PHP-FPM | |||
└── docker-compose.yml # Configuration Docker</pre> | |||
=== 1. Fichier <code>.env</code> (nouveau) === | |||
<syntaxhighlight lang="ini"># PostgreSQL | |||
POSTGRES_USER=symfony | |||
POSTGRES_PASSWORD=ChangeMe | |||
POSTGRES_DB=app | |||
# Symfony | |||
APP_ENV=dev | |||
DATABASE_URL="postgresql://${POSTGRES_USER}:${POSTGRES_PASSWORD}@postgres:5432/${POSTGRES_DB}?serverVersion=15&charset=utf8"</syntaxhighlight> | |||
=== 2. Dockerfile (optimisé) === | |||
<syntaxhighlight lang="dockerfile">FROM php:8.2-fpm | |||
# Dependencies + Extensions PHP | |||
RUN apt-get update && apt-get install -y \ | |||
git \ | |||
libpq-dev \ | |||
libzip-dev \ | |||
libevent-dev \ | |||
&& docker-php-ext-install pdo pdo_pgsql zip \ | |||
&& pecl install event \ | |||
&& docker-php-ext-enable event | |||
# Script d'entrée | |||
COPY docker/entrypoint.sh /usr/local/bin/ | |||
RUN chmod +x /usr/local/bin/entrypoint.sh | |||
WORKDIR /var/www | |||
ENTRYPOINT ["entrypoint.sh"]</syntaxhighlight> | |||
=== 3. docker-compose.yml (complet) === | |||
<syntaxhighlight lang="yaml">version: '3.8' | |||
services: | |||
php: | |||
build: . | |||
volumes: | |||
- ./app:/var/www | |||
- ./Bundles:/var/www/bundles | |||
env_file: | |||
- .env | |||
depends_on: | |||
- postgres | |||
apache: | |||
image: httpd:2.4 | |||
ports: | |||
- "8080:80" | |||
volumes: | |||
- ./app/public:/var/www/html | |||
- ./docker/apache/vhost.conf:/usr/local/apache2/conf/extra/httpd-vhosts.conf | |||
depends_on: | |||
- php | |||
postgres: | |||
image: postgres:15 | |||
env_file: | |||
- .env | |||
volumes: | |||
- postgres_data:/var/lib/postgresql/data | |||
composer: | |||
image: composer:2 | |||
volumes: | |||
- ./app:/var/www | |||
- ./Bundles:/var/www/bundles | |||
- composer_cache:/tmp | |||
working_dir: /var/www | |||
environment: | |||
- COMPOSER_CACHE_DIR=/tmp | |||
entrypoint: ["composer"] | |||
volumes: | |||
postgres_data: | |||
composer_cache:</syntaxhighlight> | |||
=== 4. docker/apache/vhost.conf === | |||
<syntaxhighlight lang="apache"><VirtualHost *:80> | |||
DocumentRoot /var/www/html | |||
ProxyPassMatch ^/(.*\.php(/.*)?)$ fcgi://php:9000/var/www/public/$1 | |||
<Directory "/var/www/html"> | |||
Options Indexes FollowSymLinks | |||
AllowOverride All | |||
Require all granted | |||
</Directory> | |||
</VirtualHost></syntaxhighlight> | |||
=== 5. docker/entrypoint.sh (optimisé) === | |||
<syntaxhighlight lang="bash">#!/bin/bash | |||
set -e | |||
# Attendre que PostgreSQL soit prêt | |||
while ! nc -z postgres 5432; do | |||
sleep 1 | |||
done | |||
# Lancer PHP-FPM | |||
exec php-fpm</syntaxhighlight> | |||
=== 6. Configuration Composer (app/composer.json) === | |||
<syntaxhighlight lang="json">{ | |||
"repositories": [ | |||
{ | |||
"type": "path", | |||
"url": "../Bundles/bundlea", | |||
"options": { | |||
"symlink": true | |||
} | |||
}, | |||
{ | |||
"type": "path", | |||
"url": "../Bundles/bundleb", | |||
"options": { | |||
"symlink": true | |||
} | |||
} | |||
] | |||
}</syntaxhighlight> | |||
=== 🔧 Workflow recommandé === | |||
<ol style="list-style-type: decimal;"> | |||
<li><p>'''Initialisation''' :</p> | |||
<syntaxhighlight lang="bash">docker-compose build | |||
docker-compose up -d</syntaxhighlight></li> | |||
<li><p>'''Installer les dépendances''' (via le service Composer) :</p> | |||
<syntaxhighlight lang="bash">docker-compose run --rm composer install</syntaxhighlight></li> | |||
<li><p>'''Mettre à jour un bundle local''' :</p> | |||
<syntaxhighlight lang="bash">docker-compose run --rm composer update vendor/bundlea</syntaxhighlight></li> | |||
<li><p>'''Accéder aux logs''' :</p> | |||
<syntaxhighlight lang="bash">docker-compose logs -f php</syntaxhighlight></li> | |||
<li><p>'''Arrêter l’environnement''' :</p> | |||
<syntaxhighlight lang="bash">docker-compose down</syntaxhighlight></li></ol> | |||
=== 🛠 Intégration PHPStorm === | |||
# '''Configurer les mappings''' : | |||
#* <code>/var/www</code> → <code>./app</code> | |||
#* <code>/var/www/bundles</code> → <code>./Bundles</code> | |||
# '''Interpréteur PHP''' : | |||
#* Utiliser le PHP du container (Settings > PHP) | |||
# '''Server Configuration''' : | |||
#* Nom : Docker Apache | |||
#* Host : localhost | |||
#* Port : 8080 | |||
#* Path mappings comme ci-dessus | |||
=== ✅ Points clés de cette configuration === | |||
* '''Service Composer isolé''' avec cache persistant (<code>composer_cache</code>) | |||
* '''Symlinks des bundles locaux''' fonctionnels grâce au volume partagé | |||
* '''Environnement reproductible''' avec PostgreSQL intégré | |||
* '''Optimisé pour PHPStorm''' avec les bons mappings de chemins | |||
* '''Entrypoint minimaliste''' qui attend juste que la DB soit prête | |||
Cette solution offre un équilibre parfait entre isolation des services et facilité de développement avec les bundles locaux. | |||
[[Catégorie:Symfony]] [[Catégorie:Docker]] | [[Catégorie:Symfony]] [[Catégorie:Docker]] | ||