102 lines
4.4 KiB
Markdown
102 lines
4.4 KiB
Markdown
Environnement de développement Docker pour démarrer un nouveau projet Symfony5
|
|
==================================
|
|
|
|
# Présentation
|
|
|
|
L'objectif de ce dépôt est de proposer et de faire évoluer une configuration de départ pour démarrer très simplement un nouveau projet Symfony dans des conteneurs Docker.
|
|
|
|
En suivant pas-à-pas les instructions à partir du chapitre "Installation", on peut démarrer en quelques minutes un nouveau projet.
|
|
|
|
Le dépôt utilise plusieurs branches qui peuvent être utilisée selon le point de départ recherché:
|
|
1. la branche `docker_config` fournit juste le docker-compose.yml et les Dockerfile qui permettent de construire les conteneurs ;
|
|
2. la branche `master` fournit la structure de départ du projet Symfony ;
|
|
3. la branche `with_vujs` permet de démarrer le projet en complémentarité du framework Javascript VueJS ;
|
|
|
|
|
|
# Installation
|
|
|
|
* Build containers: `docker-compose build`
|
|
* Run containers: `docker-compose up -d`
|
|
* Manually change owner of app dir (temp): `sudo chown 1000:1000 app/`
|
|
* Enter in php container `docker-compose exec --user 1000 php bash`
|
|
* Install Symfony: `composer create-project symfony/skeleton symfony`
|
|
* Move project in parent (app): `mv symfony/* . && mv symfony/.* . ; rmdir symfony`
|
|
* Add Symfony requirements:
|
|
```
|
|
composer require annotations
|
|
composer require --dev profiler
|
|
composer require twig
|
|
composer require orm
|
|
composer require form
|
|
composer require form validator
|
|
composer require maker-bundle
|
|
composer require security-csrf
|
|
```
|
|
* Stop containers: `docker-compose down`
|
|
* Adapt querystring in `./app/.env`:
|
|
```
|
|
--- DATABASE_URL="postgresql://db_user:db_password@127.0.0.1:5432/db_name?serverVersion=13&charset=utf8"
|
|
+++ DATABASE_URL="postgresql://postgres:secret@db:5432/postgres?serverVersion=12&charset=utf8"
|
|
```
|
|
* Run containers: `docker-compose up -d`
|
|
* Enter in php container `docker-compose exec --user 1000 php bash`
|
|
* Test psql connection: `psql --host db --user postgres --password postgres`
|
|
* Create database schema: `bin/console doctrine:schema:create`
|
|
|
|
## C'est parti !
|
|
|
|
* Créons un premier objet:
|
|
* Create entity: `bin/console make:entity Beer`
|
|
* Create CRUD on entity: `bin/console make:crud Beer`
|
|
* See sql: `bin/console doctrine:schema:update --dump-sql`
|
|
* Control migrations: `bin/console doctrine:migrations:status`
|
|
* Generate migration file: `bin/console doctrine:migrations:diff`
|
|
* See migration file: `cat migrations/Version20210218160541.php `
|
|
* Migrate: `bin/console doctrine:migrations:migrate`
|
|
* Control migrations: `bin/console doctrine:migrations:status`
|
|
* ...
|
|
|
|
## Yarn
|
|
|
|
* installation de webpack-encore: `composer require symfony/webpack-encore-bundle`
|
|
* pour finir il faut installer et lancer yarn:
|
|
* un script bash `docker-node.sh` lancera node dans un conteneur quand on a besoin de yarn:
|
|
* au premier lancement du container node:14, j'initialise la mise en place:
|
|
* `npm install yarn`
|
|
* `npm install --force`
|
|
* puis je lance (ce qu'il faudra faire à chaque fois):
|
|
* `yarn encore dev`
|
|
|
|
## Sass
|
|
|
|
* dans le container node, installation de sass: `npm install sass-loader@^11.0.0 sass --save-dev`
|
|
|
|
|
|
---
|
|
|
|
# Docker compose cheatsheet
|
|
|
|
**Note:** you need to cd first to where your docker-compose.yml file lives.
|
|
|
|
* Start containers in the background: `docker-compose up -d`
|
|
* Start containers on the foreground: `docker-compose up`. You will see a stream of logs for every container running.
|
|
* Stop containers: `docker-compose stop`
|
|
* Kill containers: `docker-compose kill`
|
|
* View container logs: `docker-compose logs`
|
|
* Execute command inside of container: `docker-compose exec SERVICE_NAME COMMAND` where `COMMAND` is whatever you want to run. Examples:
|
|
* Shell into the PHP container, `docker-compose exec --user 1000 php bash`
|
|
* Run symfony console, `docker-compose exec --user 1000 php bin/console`
|
|
* Open a postgresql shell, `docker-compose exec --user postgres db psql`
|
|
* or if user is not postgres: `docker-compose exec db psql --user admin postgres`
|
|
|
|
# Docker general cheatsheet
|
|
|
|
**Note:** these are global commands and you can run them from anywhere.
|
|
|
|
* To clear containers: `docker rm -f $(docker ps -a -q)`
|
|
* To clear images: `docker rmi -f $(docker images -a -q)`
|
|
* To clear volumes: `docker volume rm $(docker volume ls -q)`
|
|
* To clear networks: `docker network rm $(docker network ls | tail -n+2 | awk '{if($2 !~ /bridge|none|host/){ print $1 }}')`
|
|
|
|
|