Laravel Sail setup

This commit is contained in:
2025-10-30 11:10:32 -07:00
parent e7c90db220
commit 283595485c
7 changed files with 74 additions and 112 deletions

View File

@@ -1,30 +0,0 @@
FROM php:8.3-apache
# Install dependencies and PHP extensions
RUN apt-get update && apt-get install -y \
libpq-dev \
curl \
git \
libzip-dev \
&& curl -fsSL https://deb.nodesource.com/setup_lts.x | bash - \
&& apt-get install -y nodejs \
&& docker-php-ext-install pdo pdo_pgsql pcntl zip
# Enable Apache mod_rewrite
RUN a2enmod rewrite
# Set working directory
WORKDIR /var/www/html
# Copy only package files, install node deps (faster caches)
COPY package*.json ./
RUN npm ci --unsafe-perm
# Make git accept repo path (prevents dubious ownership)
RUN git config --global --add safe.directory /var/www/html
# Now copy app code
COPY . .
# Install composer
COPY --from=composer:latest /usr/bin/composer /usr/bin/composer

View File

@@ -4,7 +4,7 @@
Clone the repo. Clone the repo.
### 1a. Docker (Recommended) ### 1a. Laravel Sail / Docker (Recommended)
It's recommended to use Docker to set up the development environment. It's literally one command: It's recommended to use Docker to set up the development environment. It's literally one command:
@@ -12,48 +12,38 @@ It's recommended to use Docker to set up the development environment. It's liter
docker compose up -d docker compose up -d
``` ```
From this point onward, run all shell commands inside the `ewaste-backend-laravel-1` container. The easiest way to do this is through your IDE's Docker extension, but the manual way is: From this point onward, run all shell commands with the `./vendor/bin/sail` prefix so they get run inside the container. You might want to set up a shell alias in `~/.bashrc` to remove the relative path:
```bash ```bash
docker exec -it ewaste-backend-laravel-1 /bin/bash alias sail='./vendor/bin/sail '
``` ```
### 1b. Manual ### 1b. Manual
Alternatively, you can install PHP 8 (with the relevant extensions), PHP Composer, and Postgres on your system, then go into this directory and run: Alternatively, you can install PHP 8 (with the [relevant extensions](https://stackoverflow.com/questions/40815984/how-to-install-all-required-php-extensions-for-laravel)), PHP Composer, and Postgres on your system, then go into this directory and run:
```bash ```bash
composer install composer install
``` ```
Don't use the `sail` prefix if you install it this way.
### 2. Migrations ### 2. Migrations
Apply all migrations to the database: Apply all migrations to the database:
```bash ```bash
php artisan migrate sail php artisan migrate
``` ```
**Note:** this step has to be redone every time a new database migration is written. **Note:** this step has to be redone every time a new database migration is written.
### 3. Generate key ### 3. Generate key
Your .env should look something like this prior to running the command: Copy the `.env.example` file to `.env`. Generate the application encryption key:
```
DB_CONNECTION=pgsql
DB_HOST=db
DB_PORT=5432
DB_DATABASE=laravel
DB_USERNAME=laravel
DB_PASSWORD=secret
APP_KEY=
```
Generate Artisan key necessary for accessing endpoint:
```bash ```bash
exec laravel php artisan key:generate sail php artisan key:generate
``` ```
## Development ## Development
@@ -61,23 +51,17 @@ exec laravel php artisan key:generate
### Dev Server ### Dev Server
To start the Laravel dev server, run this *inside* the Docker container: Start the Laravel dev server with this command:
```bash ```bash
composer run dev sail php artisan serve --host=0.0.0.0
``` ```
Then you can go to http://localhost:8000 in your browser, like it says. The site will live update any time you make changes. Then you can go to http://localhost/ in your browser (if using Sail) or http://localhost:8000/ (for manual installs). The site will live update any time you make changes.
### Changing Docker Configs ### Changing Docker Configs
If you change the Dockerfile, run this *outside* the Docker container: If you change the compose.yml file, run this in your shell (not with Sail):
```bash
docker compose up --build -d
```
If you change the docker-compose.yml file, run this *outside* the Docker container:
```bash ```bash
docker compose restart docker compose restart

56
compose.yaml Normal file
View File

@@ -0,0 +1,56 @@
services:
laravel.test:
build:
context: ./vendor/laravel/sail/runtimes/8.4
dockerfile: Dockerfile
args:
WWWGROUP: '${WWWGROUP}'
image: sail-8.4/app
extra_hosts:
- 'host.docker.internal:host-gateway'
ports:
- '${APP_PORT:-80}:80'
- '${VITE_PORT:-5173}:${VITE_PORT:-5173}'
environment:
WWWUSER: '${WWWUSER}'
LARAVEL_SAIL: 1
XDEBUG_MODE: '${SAIL_XDEBUG_MODE:-off}'
XDEBUG_CONFIG: '${SAIL_XDEBUG_CONFIG:-client_host=host.docker.internal}'
IGNITION_LOCAL_SITES_PATH: '${PWD}'
volumes:
- '.:/var/www/html'
networks:
- sail
depends_on:
- pgsql
pgsql:
image: 'postgres:18-alpine'
ports:
- '${FORWARD_DB_PORT:-5432}:5432'
environment:
PGPASSWORD: '${DB_PASSWORD:-secret}'
POSTGRES_DB: '${DB_DATABASE}'
POSTGRES_USER: '${DB_USERNAME}'
POSTGRES_PASSWORD: '${DB_PASSWORD:-secret}'
volumes:
- 'sail-pgsql:/var/lib/postgresql/data'
- './vendor/laravel/sail/database/pgsql/create-testing-database.sql:/docker-entrypoint-initdb.d/10-create-testing-database.sql'
networks:
- sail
healthcheck:
test:
- CMD
- pg_isready
- '-q'
- '-d'
- '${DB_DATABASE}'
- '-U'
- '${DB_USERNAME}'
retries: 3
timeout: 5s
networks:
sail:
driver: bridge
volumes:
sail-pgsql:
driver: local

View File

@@ -18,7 +18,7 @@
"laravel/boost": "^1.6", "laravel/boost": "^1.6",
"laravel/pail": "^1.2.2", "laravel/pail": "^1.2.2",
"laravel/pint": "^1.24", "laravel/pint": "^1.24",
"laravel/sail": "^1.41", "laravel/sail": "*",
"mockery/mockery": "^1.6", "mockery/mockery": "^1.6",
"nunomaduro/collision": "^8.6", "nunomaduro/collision": "^8.6",
"pestphp/pest": "^4.1", "pestphp/pest": "^4.1",

2
composer.lock generated
View File

@@ -4,7 +4,7 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically" "This file is @generated automatically"
], ],
"content-hash": "f57f42baa39a474eb39dad5682f0748c", "content-hash": "ada8f90ada660db7576ae0f26a2847a7",
"packages": [ "packages": [
{ {
"name": "brick/math", "name": "brick/math",

View File

@@ -1,47 +0,0 @@
version: '3.8'
services:
laravel:
build:
context: .
dockerfile: Dockerfile
volumes:
- .:/var/www/html
environment:
DB_CONNECTION: pgsql
DB_HOST: db
DB_PORT: 5432
DB_DATABASE: laravel
DB_USERNAME: laravel
DB_PASSWORD: secret
ports:
- "5173:5173"
- "8000:8000"
networks:
- laravel-network
depends_on:
db:
condition: service_healthy
db:
image: postgres:16
volumes:
- postgres_data:/var/lib/postgresql/data
environment:
POSTGRES_USER: laravel
POSTGRES_PASSWORD: secret
POSTGRES_DB: laravel
healthcheck:
test: ["CMD-SHELL", "pg_isready -U $$POSTGRES_USER -d $$POSTGRES_DB"]
interval: 5s
timeout: 5s
retries: 5
networks:
- laravel-network
volumes:
postgres_data:
networks:
laravel-network:
driver: bridge

View File

@@ -23,8 +23,7 @@
<env name="BCRYPT_ROUNDS" value="4"/> <env name="BCRYPT_ROUNDS" value="4"/>
<env name="BROADCAST_CONNECTION" value="null"/> <env name="BROADCAST_CONNECTION" value="null"/>
<env name="CACHE_STORE" value="array"/> <env name="CACHE_STORE" value="array"/>
<env name="DB_CONNECTION" value="pgsql"/> <env name="DB_DATABASE" value="testing"/>
<env name="DB_DATABASE" value=":memory:"/>
<env name="MAIL_MAILER" value="array"/> <env name="MAIL_MAILER" value="array"/>
<env name="QUEUE_CONNECTION" value="sync"/> <env name="QUEUE_CONNECTION" value="sync"/>
<env name="SESSION_DRIVER" value="array"/> <env name="SESSION_DRIVER" value="array"/>