From 8e1c6490e33b478ddf4b4171b4a7ab3974678d06 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Camille=C2=A0Bouvat?= Date: Mon, 27 Nov 2023 16:05:43 +0100 Subject: [PATCH] =?UTF-8?q?feat:=20=E2=9C=A8=20Big=20update=20(with=20PHP?= =?UTF-8?q?=208.2,=20PostgreSQL=2016,=20Node=2020,=20Mailpit),=20new=20Mak?= =?UTF-8?q?efile=20...?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 3 + Makefile | 131 +++++++++--------- README.md | 80 ++++------- docker-compose.dev.yml | 50 +++++++ ...-production.yml => docker-compose.prod.yml | 40 +++--- docker-compose.yml | 70 ---------- docker/nginx/nginx.conf | 3 +- docker/php/{Dockerfile => dev.Dockerfile} | 30 ++-- docker/php/{php.ini => php.dev.ini} | 1 + .../php/{php-production.ini => php.prod.ini} | 2 +- ...{Dockerfile-production => prod.Dockerfile} | 29 ++-- 11 files changed, 189 insertions(+), 250 deletions(-) create mode 100644 docker-compose.dev.yml rename docker-compose-production.yml => docker-compose.prod.yml (60%) delete mode 100644 docker-compose.yml rename docker/php/{Dockerfile => dev.Dockerfile} (56%) rename docker/php/{php.ini => php.dev.ini} (90%) rename docker/php/{php-production.ini => php.prod.ini} (79%) rename docker/php/{Dockerfile-production => prod.Dockerfile} (53%) diff --git a/.gitignore b/.gitignore index b4d8a9e..f22e0b1 100644 --- a/.gitignore +++ b/.gitignore @@ -19,3 +19,6 @@ supervisord.pid _ide_helper_models.php _ide_helper.php .phpstorm.meta.php +/docker-compose.yml +/docker/pgsql/datas +/docker/mariadb/datas diff --git a/Makefile b/Makefile index 6106c94..ed75c8e 100644 --- a/Makefile +++ b/Makefile @@ -1,96 +1,91 @@ -build: - docker-compose -f docker-compose-production.yml build +default: help ## Display help -build-dev: - docker-compose build +artisan: ## Laravel Artisan command add arg="command" to run a specific command (ex: make artisan arg="migrate") + docker compose exec php php artisan $(arg) -composer-install: - docker-compose exec php composer install --optimize-autoloader --no-suggest --no-dev +artisan-migrate: ## Laravel Artisan migrate + docker compose exec php php artisan migrate -composer-install-dev: - docker-compose run --rm php composer install +artisan-migrate-fresh: ## Laravel Artisan migrate:fresh + docker compose exec php php artisan migrate:fresh -composer-update: - docker-compose run --rm php composer update +artisan-migrate-seed: ## Laravel Artisan migrate:fresh --seed + docker compose exec php php artisan migrate:fresh --seed -database-migrate: - docker-compose run --rm php php artisan migrate --force +artisan-key-generate: ## Laravel Artisan key:generate + docker compose exec php php artisan key:generate -database-seed: - docker-compose run --rm php php artisan migrate:fresh --seed - -down: - docker-compose exec php php artisan down --refresh=15 - -ide-helper: - docker-compose run --rm php php artisan ide-helper:eloquent - docker-compose run --rm php php artisan ide-helper:generate - docker-compose run --rm php php artisan ide-helper:meta - docker-compose run --rm php php artisan ide-helper:models --nowrite +artisan-storage: + docker-compose run --rm php php artisan storage:link -git-pull: - git reset --hard - git pull origin main +build: ## Build containers + docker compose build -install: build composer-install laravel-storage +composer: ## Composer command add arg="command" to run a specific command (ex: make composer arg="require laravel/ui") + docker compose run --rm php composer $(arg) -install-dev: build-dev composer-install-dev laravel-storage +composer-install-dev: ## Composer install dev + docker compose run --rm php composer install -laravel-install: - docker-compose run --rm php composer create-project laravel/laravel laravel --prefer-dist - mv README.md README-docker.md - -mv -f ./laravel/* ./laravel/.* ./ - -rm -rf ./laravel +composer-install-prod: ## Composer install prod + docker compose run --rm php composer install --optimize-autoloader --no-suggest --no-dev -laravel-cache: - docker-compose exec php php artisan cache:clear - docker-compose exec php php artisan config:cache - docker-compose exec php php artisan event:cache - docker-compose exec php php artisan route:cache - docker-compose exec php php artisan view:cache +composer-update: ## Composer update + docker compose run --rm php composer update -laravel-storage: - docker-compose run --rm php php artisan storage:link +copy-env: ## Copy .env.example to .env + cp .env.example .env -pull: - docker-compose -f docker-compose-production.yml pull +copy-docker-compose-dev: ## Copy docker-compose.dev.yml to docker-compose.yml + cp docker-compose.dev.yml docker-compose.yml -pull-dev: - docker-compose pull +down: ## Stop and remove all containers + docker compose down --remove-orphans + @echo "🛑 Project are stopped and removed" -restart: stop start +eslint: ## Run eslint + docker compose run --rm node npm run eslint -restart-dev: stop-dev start-dev +install: copy-docker-compose-dev upgrade laravel-install copy-env up npm-install artisan-key-generate artisan-storage ## Install Project + @echo "✅ Project is installed and running, edit .env" -start: - docker-compose -f docker-compose-production.yml up -d +git-pull: + git reset --hard + git pull origin --no-rebase -start-dev: - docker-compose up -d +help: ## Display this help + @echo "📖 Project help" + @echo "✍️ Usage: make [command]" + @echo "👉 Available commands open Makefile to see all commands" -stop: - docker-compose -f docker-compose-production.yml down --remove-orphans +laravel-install: ## Install Laravel + docker-compose run --rm php composer create-project laravel/laravel laravel --prefer-dist + mv README.md README-docker.md + -mv -f ./laravel/* ./laravel/.* ./ + -rm -rf ./laravel -stop-dev: - docker-compose down --remove-orphans +npm: ## Npm command add arg="command" to run a specific command (ex: make npm arg="install") + docker compose run --rm node npm $(arg) -tinker: - docker-compose exec php php artisan tinker +npm-install: ## Npm install + docker compose run --rm node npm install -test: - docker-compose exec php php artisan config:clear - docker-compose exec php php artisan test +npm-update: ## Npm update + docker compose run --rm node npm update -test-create: - docker-compose run --rm php php artisan make:test $(name) +up: ## Create and start all containers + docker compose up -d + @echo "✅ Project is up and running" -up: - docker-compose exec php php artisan up +upgrade: pull build ## Upgrade containers (pull and build) -update: down git-pull composer-install laravel-cache database-migrate up +upgrade-dev: down copy-docker-compose-dev upgrade up composer-install-dev npm-install ## Upgrade Project with docker-compose.dev.yml -update-dev: down git-pull-dev composer-install-dev database-migrate up +pint: ## Run Laravel Pint + docker compose run --rm php ./vendor/bin/pint -upgrade: stop git-pull pull build composer-install laravel-cache database-migrate start +pull: ## Pull all containers + docker compose pull -upgrade-dev: stop-dev git-pull pull-dev build-dev composer-install-dev database-migrate start-dev +setup-dev: copy-docker-compose-dev copy-env upgrade composer-install-dev npm-install artisan-key-generate artisan-migrate + @echo "✅ Project is installed, edit .env and you can now run 'make up' to start containers" diff --git a/README.md b/README.md index 83c3aa3..798f18d 100644 --- a/README.md +++ b/README.md @@ -1,24 +1,24 @@ # Docker Laravel -## 🚫 Requirement +## Requirement -- Docker engine 19.03.0+ -- Docker Compose 1.27+ +- Docker engine +- Docker Compose - Make -## 🛠 Build with +## Build with -- HTTP server Nginx 1.20 -- PHP 8.1 -- Node 16 -- Database PostgreSQL 14 +- HTTP server Nginx 1.25 +- PHP 8.2 +- Node 20 +- PostgreSQL 16 or MariaDB 11 -## 📖 Install Laravel +## Install Laravel -Many commands are in `Makefile` file. Example `start`, `stop`, `build` and [many more 👇](#-list-of-commands). +Many commands are in `Makefile` file. Example `up`, `down`, `upgrade` and [many more 👇](#list-of-commands). -- It is not recommended to clone or fork the repository, download a copy of this Git repository -- Run this command `make laravel-install` for install Laravel with Composer +- It is not recommended to clone or fork the repository, download a copy of this Git repository and extract it. +- Run this command `make install` for install Laravel with Composer - **⚠️ Modify** environnement file `.env` create by Laravel installer with this : ```bash @@ -42,15 +42,15 @@ MAIL_FROM_NAME="${APP_NAME}" - Launch the environment with `make start-dev` - And enjoy 😊 ! -## 🧰 Development environment +## Development environment This command build container, install composer dependencies. -`make install-dev` +`make install` Start environment. -`make start-dev` +`make up` ### Development tools @@ -75,11 +75,14 @@ For Visual Studio Code, use this configuration in `.vscode/launch.json` file. } ``` -#### Database management tool +#### Database -Adminer equivalent to PHPmyAdmin. All credentials are in `.env` file. +The database is accessible on port `5432` with this parameters : -`http://localhost:8080` +- Host : `localhost` +- Port : `5432` + +Use for example [DBeaver](https://dbeaver.io/) for connect to the database. #### Mail capture @@ -87,43 +90,14 @@ MailHog capture all mails form the application. `http://localhost:8025` -## 💼 Production environment +## Production environment ### First launch - **Modify** ⚠️ environnement file `.env` with production parameters - `make install` -- `make start` - -## 📚 List of commands - -- `build` : Build docker container -- `build-dev` : Build docker development container -- `composer-install` : Install PHP dependencies with Composer for production -- `composer-install-dev` : Install PHP dependencies with Composer for devellopement -- `composer-update` : Update PHP dependencies -- `database-migrate` : Laravel database migration command -- `database-seed` : Laravel database seeding command -- `down` : Set in maintenance Laravel application -- `ide-helper` : Generate helpers file for IDE (PHPStorm, VS Code ...) -- `git-pull` : Reset stage and git pull -- `install` : Run `build`, `composer-install` `laravel-storage` -- `install-dev` : Run `build-dev`, `composer-install-dev` `laravel-storage` -- `laravel-install` : Install Laravel with Composer -- `laravel-cache` : Generate all cache for Laravel -- `laravel-storage` : Create store link for Laravel -- `pull` : Pull the lastest Docker containers -- `restart` : Restart Docker containers -- `restart-dev` : Restart Docker development containers -- `start` : Start Docker containers -- `start-dev` : Start Docker development containers -- `stop` : Stop Docker containers -- `stop-dev` : Stop Docker development containers -- `tinker` : Run Laravel Tinker -- `test` : Run Laravel unit test -- `test-create name=TestName` : Create run Laravel unit test -- `up` : Set online Laravel application -- `update` : Run `down` `git-pull` `composer-install` `laravel-cache` `database-migrate` `up` -- `update-dev` : Run `down` `git-pull` `composer-install-dev` `database-migrate` `up` -- `upgrade` : Run `stop` `git-pull` `pull` `build` `composer-install` `laravel-cache` `database-migrate` `start` -- `upgrade-dev` : Run `stop` `git-pull` `pull-dev` `build-dev` `composer-install-dev` `database-migrate` `start` +- `make up` + +## List of commands + +All commands are in `Makefile` file with description. Open this file for more informations. \ No newline at end of file diff --git a/docker-compose.dev.yml b/docker-compose.dev.yml new file mode 100644 index 0000000..2a2c781 --- /dev/null +++ b/docker-compose.dev.yml @@ -0,0 +1,50 @@ +services: + php: + build: + context: ./docker/php/ + dockerfile: dev.Dockerfile + ports: + - 80:80 + volumes: + - ./:/app/ + extra_hosts: + - 'host.docker.internal:host-gateway' + depends_on: + - pgsql + node: + image: node:20 + ports: + - 5173:5173 + volumes: + - ./:/app/ + working_dir: /app/ + command: "npm run dev" + # mariadb: + # image: mariadb:11 + # ports: + # - 3306:3306 + # environment: + # MARIADB_ROOT_PASSWORD: "${DB_ROOT_PASSWORD}" + # MARIADB_DATABASE: "${DB_DATABASE}" + # MARIADB_USER: "${DB_USERNAME}" + # MARIADB_PASSWORD: "${DB_PASSWORD}" + # volumes: + # - ./docker/mariadb/datas:/var/lib/mysql + pgsql: + image: postgres:16 + ports: + - '${FORWARD_DB_PORT:-5432}:5432' + environment: + PGPASSWORD: '${DB_PASSWORD:-secret}' + POSTGRES_DB: '${DB_DATABASE:-laravel}' + POSTGRES_USER: '${DB_USERNAME:-laravel}' + POSTGRES_PASSWORD: '${DB_PASSWORD:-secret}' + volumes: + - './docker/pgsql/datas:/var/lib/postgresql/data' + healthcheck: + test: [ "CMD", "pg_isready", "-q", "-d", "${DB_DATABASE:-laravel}", "-U", "${DB_USERNAME:-laravel}" ] + mailpit: + image: axllent/mailpit:latest + ports: + - 1025:1025 + - 8025:8025 diff --git a/docker-compose-production.yml b/docker-compose.prod.yml similarity index 60% rename from docker-compose-production.yml rename to docker-compose.prod.yml index e8a828c..9e02d18 100644 --- a/docker-compose-production.yml +++ b/docker-compose.prod.yml @@ -1,6 +1,6 @@ services: http: - image: nginx:1.20 + image: nginx:1.25 restart: always ports: - '80:80' @@ -9,27 +9,33 @@ services: - './docker/nginx/nginx.conf:/etc/nginx/nginx.conf' depends_on: - php - networks: - - laravel php: build: context: ./docker/php/ - dockerfile: Dockerfile-production - args: - WWWUSER: 'laravel' - WWWUID: '1000' + dockerfile: prod.Dockerfile restart: always volumes: - - '.:/app/' - networks: - - laravel + - ./:/app/ + working_dir: /app/ depends_on: - pgsql node: - image: node:16 + image: node:20 volumes: - - './:/app/' - working_dir: '/app/' + - ./:/app/ + working_dir: /app/ + command: "npm run build" + # mariadb: + # image: mariadb:11 + # ports: + # - 3306:3306 + # environment: + # MARIADB_ROOT_PASSWORD: "${DB_ROOT_PASSWORD}" + # MARIADB_DATABASE: "${DB_DATABASE}" + # MARIADB_USER: "${DB_USERNAME}" + # MARIADB_PASSWORD: "${DB_PASSWORD}" + # volumes: + # - ./docker/mariadb/datas:/var/lib/mysql pgsql: image: postgres:14 restart: always @@ -42,13 +48,5 @@ services: POSTGRES_PASSWORD: '${DB_PASSWORD:-secret}' volumes: - 'pgsql:/var/lib/postgresql/data' - networks: - - laravel healthcheck: test: [ "CMD", "pg_isready", "-q", "-d", "${DB_DATABASE:-laravel}", "-U", "${DB_USERNAME:-laravel}" ] -networks: - laravel: - driver: bridge -volumes: - pgsql: - driver: local diff --git a/docker-compose.yml b/docker-compose.yml deleted file mode 100644 index 5ebe419..0000000 --- a/docker-compose.yml +++ /dev/null @@ -1,70 +0,0 @@ -services: - http: - image: nginx:1.20 - ports: - - '80:80' - volumes: - - './:/app/' - - './docker/nginx/nginx.conf:/etc/nginx/nginx.conf' - depends_on: - - php - networks: - - laravel - php: - build: - context: ./docker/php/ - args: - WWWUSER: 'laravel' - WWWUID: '1000' - volumes: - - '.:/app/' - extra_hosts: - - 'host.docker.internal:host-gateway' - networks: - - laravel - depends_on: - - pgsql - adminer: - image: adminer - ports: - - 8080:8080 - networks: - - laravel - environment: - ADMINER_DEFAULT_SERVER: 'pgsql' - ADMINER_DEFAULT_TYPE: 'postgresql' - depends_on: - - pgsql - node: - image: node:16 - volumes: - - './:/app/' - working_dir: '/app/' - pgsql: - image: postgres:14 - ports: - - '${FORWARD_DB_PORT:-5432}:5432' - environment: - PGPASSWORD: '${DB_PASSWORD:-secret}' - POSTGRES_DB: '${DB_DATABASE:-laravel}' - POSTGRES_USER: '${DB_USERNAME:-laravel}' - POSTGRES_PASSWORD: '${DB_PASSWORD:-secret}' - volumes: - - 'pgsql:/var/lib/postgresql/data' - networks: - - laravel - healthcheck: - test: [ "CMD", "pg_isready", "-q", "-d", "${DB_DATABASE:-laravel}", "-U", "${DB_USERNAME:-laravel}" ] - mailhog: - image: 'mailhog/mailhog:latest' - ports: - - '${FORWARD_MAILHOG_PORT:-1025}:1025' - - '${FORWARD_MAILHOG_DASHBOARD_PORT:-8025}:8025' - networks: - - laravel -networks: - laravel: - driver: bridge -volumes: - pgsql: - driver: local diff --git a/docker/nginx/nginx.conf b/docker/nginx/nginx.conf index 49dd1b9..9642a3b 100644 --- a/docker/nginx/nginx.conf +++ b/docker/nginx/nginx.conf @@ -26,12 +26,11 @@ http { access_log /app/storage/logs/access.log; error_log /app/storage/logs/error.log warn; - # domain.local server { listen 80 default_server; listen [::]:80 default_server; - server_name domain.local; + server_name _; set $base /app; root $base/public; diff --git a/docker/php/Dockerfile b/docker/php/dev.Dockerfile similarity index 56% rename from docker/php/Dockerfile rename to docker/php/dev.Dockerfile index dbe51ac..52f33d2 100644 --- a/docker/php/Dockerfile +++ b/docker/php/dev.Dockerfile @@ -1,18 +1,15 @@ -FROM php:8.1-fpm - -# Arguments defined in docker-compose.yml -ARG WWWUSER -ARG WWWUID +FROM php:8.2-cli # Set working directory WORKDIR /app/ +# Set environment variables ENV DEBIAN_FRONTEND noninteractive ENV TZ=UTC # Install system dependencies and clear cache -RUN apt-get -y update \ - && apt-get install -y \ +RUN apt -y update \ + && apt install -y \ libfreetype6-dev \ libpq-dev \ libicu-dev \ @@ -23,14 +20,13 @@ RUN apt-get -y update \ libzip-dev \ zip \ unzip \ - curl \ - git \ - && apt-get clean \ + mariadb-client \ + && apt clean \ && rm -rf /var/lib/apt/lists/* # Install PHP extensions RUN docker-php-ext-configure gd --with-freetype --with-jpeg -RUN docker-php-ext-install -j$(nproc) iconv gd mysqli pdo_pgsql pdo_mysql soap bcmath gmp intl opcache zip +RUN docker-php-ext-install -j$(nproc) iconv gd mysqli pdo_pgsql pdo_mysql soap bcmath gmp intl opcache zip exif # Install Xdebug RUN pecl install xdebug \ @@ -39,11 +35,11 @@ RUN pecl install xdebug \ # Get latest Composer COPY --from=composer:latest /usr/bin/composer /usr/bin/composer -# Create system user to run Composer and Artisan Commands -RUN useradd -G www-data,root -u $WWWUID -d /home/$WWWUSER $WWWUSER -RUN mkdir -p /home/$WWWUSER/.composer && \ - chown -R $WWWUSER:$WWWUSER /home/$WWWUSER +# Copy PHP config +COPY php.dev.ini /usr/local/etc/php/conf.d/zz-custom.ini -COPY php.ini /usr/local/etc/php/conf.d/zz-custom.ini +# Lauch Laravel PHP server on port 80 +CMD php artisan serve --host=0.0.0.0 --port=80 -USER $WWWUSER +# Expose port 80 +EXPOSE 80 diff --git a/docker/php/php.ini b/docker/php/php.dev.ini similarity index 90% rename from docker/php/php.ini rename to docker/php/php.dev.ini index e5c6bcc..837a4d8 100644 --- a/docker/php/php.ini +++ b/docker/php/php.dev.ini @@ -3,6 +3,7 @@ post_max_size = 100M upload_max_filesize = 100M variables_order = EGPCS error_reporting = E_ALL +memory_limit = 512M [xdebug] xdebug.mode = debug diff --git a/docker/php/php-production.ini b/docker/php/php.prod.ini similarity index 79% rename from docker/php/php-production.ini rename to docker/php/php.prod.ini index 2a57768..b1d0eb7 100644 --- a/docker/php/php-production.ini +++ b/docker/php/php.prod.ini @@ -2,4 +2,4 @@ post_max_size = 100M upload_max_filesize = 100M variables_order = EGPCS -error_reporting = 0 +memory_limit = 512M diff --git a/docker/php/Dockerfile-production b/docker/php/prod.Dockerfile similarity index 53% rename from docker/php/Dockerfile-production rename to docker/php/prod.Dockerfile index 9ab6323..cd3cbdb 100644 --- a/docker/php/Dockerfile-production +++ b/docker/php/prod.Dockerfile @@ -1,18 +1,18 @@ -FROM php:8.0-fpm +FROM php:8.2-fpm -# Arguments defined in docker-compose.yml -ARG WWWUSER -ARG WWWUID +# Use the default production configuration +RUN mv "$PHP_INI_DIR/php.ini-production" "$PHP_INI_DIR/php.ini" # Set working directory WORKDIR /app/ +# Set environment variables ENV DEBIAN_FRONTEND noninteractive ENV TZ=UTC # Install system dependencies and clear cache -RUN apt-get -y update \ - && apt-get install -y \ +RUN apt -y update \ + && apt install -y \ libfreetype6-dev \ libpq-dev \ libicu-dev \ @@ -23,23 +23,16 @@ RUN apt-get -y update \ libzip-dev \ zip \ unzip \ - curl \ - git \ - && apt-get clean \ + mariadb-client \ + && apt clean \ && rm -rf /var/lib/apt/lists/* # Install PHP extensions RUN docker-php-ext-configure gd --with-freetype --with-jpeg -RUN docker-php-ext-install -j$(nproc) iconv gd mysqli pdo_pgsql pdo_mysql soap bcmath gmp intl opcache zip +RUN docker-php-ext-install -j$(nproc) iconv gd mysqli pdo_pgsql pdo_mysql soap bcmath gmp intl opcache zip exif # Get latest Composer COPY --from=composer:latest /usr/bin/composer /usr/bin/composer -# Create system user to run Composer and Artisan Commands -RUN useradd -G www-data,root -u $WWWUID -d /home/$WWWUSER $WWWUSER -RUN mkdir -p /home/$WWWUSER/.composer && \ - chown -R $WWWUSER:$WWWUSER /home/$WWWUSER - -COPY php-production.ini /etc/php/8.0/fpm/conf.d/xy-custom.ini - -USER $WWWUSER +# Copy PHP config +COPY php.prod.ini /usr/local/etc/php/conf.d/zz-custom.ini