Skip to content

Commit 4ef1d75

Browse files
feat: added readme, env vars for migrations and separate scheduler container
1 parent a3de0e3 commit 4ef1d75

File tree

3 files changed

+145
-22
lines changed

3 files changed

+145
-22
lines changed

.docker/Dockerfile

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,5 +72,10 @@ RUN chmod -R 775 /var/www/html/storage /var/www/html/bootstrap/cache
7272
# Switch back to the non-privileged user
7373
USER www-data
7474

75+
# Set environment variables, available in the README
76+
ENV AUTORUN_ENABLED=true
77+
ENV AUTORUN_LARAVEL_MIGRATION=true
78+
79+
7580
# Expose port 8080 and start php-fpm
7681
EXPOSE 8080

.docker/README.md

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
# Laravel Docker Environment
2+
3+
This project provides a production-ready Docker setup for a Laravel application using `serversideup/php` images. It includes a multi-stage `Dockerfile` for optimized builds and a `docker-compose.yml` file to orchestrate the application, database, and caching services.
4+
5+
## Prerequisites
6+
7+
- Docker
8+
- Docker Compose
9+
10+
## Setup & Installation
11+
12+
1. **Clone the Repository:**
13+
Clone this project to your local machine.
14+
15+
2. **Configure Environment:**
16+
Create a `.env` file in the project root by copying the example file:
17+
```bash
18+
cp .env.example .env
19+
```
20+
Update the `.env` file with your specific application and database settings. The default database configuration is set up to work with the `docker-compose.yml` file provided.
21+
22+
3. **Generate Application Key:**
23+
If this is a new project, you'll need to generate a Laravel application key. First, start the containers, then run the command.
24+
```bash
25+
# Start containers in the background
26+
docker-compose up -d --build
27+
28+
# Generate the key
29+
docker-compose exec app php artisan key:generate
30+
```
31+
32+
## Usage
33+
34+
- **Build and Start Containers:**
35+
```bash
36+
docker-compose up -d --build
37+
```
38+
Your application will be available at `http://localhost:8080`.
39+
40+
- **Stop Containers:**
41+
```bash
42+
docker-compose down
43+
```
44+
45+
- **Stop Containers and Remove Volumes:** (Use this to start fresh)
46+
```bash
47+
docker-compose down -v
48+
```
49+
50+
## Scheduled Tasks (Cron)
51+
52+
To run Laravel's scheduled tasks, this setup includes a dedicated `scheduler` service in the `docker-compose.yml` file.
53+
54+
This container uses the same application image but runs a loop that executes `php artisan schedule:run` every minute. This is the standard way to handle cron jobs in a containerized Laravel application.
55+
56+
All of your scheduled tasks are defined in the `app/Console/Kernel.php` file as usual. The `scheduler` service will automatically pick up and execute any tasks you define there.
57+
58+
## Laravel Automations (`serversideup/php`)
59+
60+
The `serversideup/php` image provides a powerful automation system that can run common Laravel commands for you when the container starts. This is perfect for production deployments.
61+
62+
### Master Switch
63+
64+
All automations are controlled by a single master switch. They will **only run** if this is enabled.
65+
66+
- **`AUTORUN_ENABLED`**: Set to `"true"` to enable the automation scripts. Defaults to `false`.
67+
68+
### Automation Flags
69+
70+
If `AUTORUN_ENABLED` is set to `"true"`, you can then toggle individual automations.
71+
72+
| Environment Variable | Default | Command Executed | Description |
73+
| ---------------------------------- | ------- | ----------------------------------- | ------------------------------------------------------------------------------------------------------- |
74+
| `AUTORUN_LARAVEL_MIGRATION` | `true` | `php artisan migrate --force` | Automatically runs your database migrations. |
75+
| `AUTORUN_LARAVEL_CONFIG_CACHE` | `true` | `php artisan config:cache` | Caches your configuration files for a significant performance boost. |
76+
| `AUTORUN_LARAVEL_ROUTE_CACHE` | `true` | `php artisan route:cache` | Caches your route definitions. |
77+
| `AUTORUN_LARAVEL_VIEW_CACHE` | `true` | `php artisan view:cache` | Compiles and caches your Blade templates. |
78+
| `AUTORUN_LARAVEL_EVENT_CACHE` | `true` | `php artisan event:cache` | Caches your application's events and listeners. |
79+
| `AUTORUN_LARAVEL_STORAGE_LINK` | `true` | `php artisan storage:link` | Creates the symbolic link from `public/storage` to `storage/app/public`. |
80+
| `AUTORUN_LARAVEL_MIGRATION_ISOLATION` | `false` | `php artisan migrate --force --isolated` | Ensures only one container runs migrations at a time. Requires Laravel 9.38+ and a locking database. |
81+
| `AUTORUN_LARAVEL_MIGRATION_TIMEOUT` | `30` | N/A | The number of seconds the script will wait for the database to be available before migrating. |
82+
83+
## Manual Artisan Commands
84+
85+
You can run any `artisan` command manually by executing it inside the `app` container.
86+
87+
- **Run Migrations:**
88+
```bash
89+
docker-compose exec app php artisan migrate
90+
```
91+
92+
- **Access Tinker:**
93+
```bash
94+
docker-compose exec app php artisan tinker
95+
```
96+
97+
- **Clear Cache:**
98+
```bash
99+
docker-compose exec app php artisan optimize:clear
100+
```

docker-compose.yml

Lines changed: 40 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
# docker-compose.yml for a Laravel application
1+
# docker-compose.yml for a Laravel application with PostgreSQL
22

33
services:
4-
# Application Service
4+
# Application Service (with built-in Nginx)
55
app:
66
build:
77
context: .
@@ -11,43 +11,61 @@ services:
1111
restart: unless-stopped
1212
tty: true
1313
environment:
14+
# These environment variables are used by the serversideup/php image
15+
# to configure the built-in Nginx and PHP-FPM.
1416
SERVICE_NAME: app
1517
SERVICE_TAGS: dev
18+
19+
# --- Laravel Automations ---
20+
# Master switch for all automations. Set to "true" to enable.
21+
AUTORUN_ENABLED: "true"
22+
23+
# Individual toggles for each automation.
24+
# These only work if AUTORUN_ENABLED is "true".
25+
AUTORUN_LARAVEL_MIGRATION: "true"
26+
AUTORUN_LARAVEL_CONFIG_CACHE: "true"
27+
AUTORUN_LARAVEL_ROUTE_CACHE: "true"
28+
AUTORUN_LARAVEL_VIEW_CACHE: "true"
29+
AUTORUN_LARAVEL_EVENT_CACHE: "true"
30+
AUTORUN_LARAVEL_STORAGE_LINK: "true"
31+
1632
working_dir: /var/www/html
1733
volumes:
1834
- ./:/var/www/html
35+
ports:
36+
# Expose the port from the Nginx running inside this container
37+
- "8080:8080"
1938
networks:
2039
- laravel
40+
depends_on:
41+
- pgsql
2142

22-
# Nginx Service
23-
nginx:
24-
image: nginx:alpine
25-
container_name: laravel-nginx
43+
# Scheduler Service (Cron)
44+
scheduler:
45+
image: your-laravel-app
46+
container_name: laravel-scheduler
2647
restart: unless-stopped
27-
ports:
28-
- "8080:80"
48+
working_dir: /var/www/html
2949
volumes:
3050
- ./:/var/www/html
31-
- ./docker/nginx/conf.d/:/etc/nginx/conf.d/
51+
command: >
52+
sh -c "while true; do php artisan schedule:run >> /dev/null 2>&1; sleep 60; done"
3253
networks:
3354
- laravel
3455

35-
# Database Service (MySQL)
36-
db:
37-
image: mysql:8.0
38-
container_name: laravel-db
56+
# Database Service (PostgreSQL)
57+
pgsql:
58+
image: postgres:15-alpine
59+
container_name: laravel-pgsql
3960
restart: unless-stopped
4061
environment:
41-
MYSQL_DATABASE: ${DB_DATABASE}
42-
MYSQL_ROOT_PASSWORD: ${DB_PASSWORD}
43-
MYSQL_PASSWORD: ${DB_PASSWORD}
44-
MYSQL_USER: ${DB_USERNAME}
45-
SERVICE_TAGS: dev
46-
SERVICE_NAME: mysql
62+
POSTGRES_DB: laravel
63+
POSTGRES_USER: sail
64+
POSTGRES_PASSWORD: password
4765
volumes:
48-
- dbdata:/var/lib/mysql
66+
- pgdata:/var/lib/postgresql/data
4967
ports:
50-
- "3306:3306"
68+
- "5432:5432"
5169
networks:
5270
- laravel
5371

@@ -68,5 +86,5 @@ networks:
6886

6987
# Volumes
7088
volumes:
71-
dbdata:
89+
pgdata:
7290
driver: local

0 commit comments

Comments
 (0)