You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This project serves as an educational guide for Laravel developers looking to create modern, efficient, and flexible Docker environments for both development and production. It demonstrates best practices in building Docker images tailored for Laravel applications, providing a solid foundation for developers to understand and extend Docker functionalities in their own projects.
5
-
6
-
## Current roadmap
7
-
- Adjust example app to provide simple page with demonstrating phpinfo(), as well as health check of redis and database.
-[Building and Running the Production Environment](#building-and-running-the-production-environment)
26
16
-[Technical Details](#technical-details)
27
17
-[Contributing](#contributing)
18
+
-[How to Contribute](#how-to-contribute)
28
19
-[License](#license)
29
20
30
-
## Project Structure
31
21
32
-
The project is divided into three main parts: example Laravel 11 app used to test docker environments,
33
-
development environment and production environment.
22
+
## Overview
34
23
35
-
### Development Environment
24
+
The **Laravel Docker Examples Project** provides practical examples and comprehensive guidance for Laravel developers to create modern, efficient, and flexible Docker environments for both development and production. By demonstrating best practices and offering detailed configurations, this project helps you build Docker images tailored for Laravel applications. Whether you're new to Docker or looking to optimize your deployment workflow, these examples serve as a solid foundation to understand and extend Docker functionalities in your Laravel projects.
36
25
37
-
Focuses on providing a flexible and independent environment that includes all necessary tools for typical Laravel development, such as Redis, MariaDB/PostgreSQL, and Node.js for building frontend assets. This environment supports rapid development and testing.
38
26
39
-
### Production Environment
27
+
## Project Structure
28
+
29
+
The project is organized into the following main directories:
30
+
31
+
-**example-app**: Contains a sample Laravel 11 application used to test and demonstrate Docker environments.
32
+
-**development**: Includes Docker configurations and Compose files for setting up a complete development environment.
33
+
-**production**: Contains Dockerfiles and Compose files optimized for a production environment.
40
34
41
-
Emphasizes building a slim, secure, and efficient Docker image suitable for deploying a Laravel application in production. The image is optimized for performance, with pre-built assets and only essential dependencies.
35
+
### example-app
42
36
43
-
## Key Features
37
+
The `example-app` directory holds the sample Laravel application. It includes basic routes, a health check endpoint to test database and Redis connections, and demonstrates how to integrate Laravel with Docker. This serves as a practical example to illustrate the concepts discussed in this project.
44
38
45
39
### Development Environment
46
40
47
-
- Complete Development Stack: Includes PHP, MySQL/PostgreSQL, Redis, and Node.js.
48
-
- Hot-Reloading: Supports live reloading of code changes for rapid development.
49
-
- Service Flexibility: Easily swap out or add services (e.g., switching between MySQL and PostgreSQL).
50
-
- Persistent Volumes: Code changes are reflected immediately without rebuilding containers.
41
+
Located in the `development` directory, this environment focuses on providing a flexible and independent setup that includes all necessary tools for typical Laravel development:
42
+
43
+
-**Services**: PHP-FPM, Nginx, Node.js, Redis, and PostgreSQL.
44
+
-**Hot Reloading**: Supports live reloading of code changes without rebuilding containers.
45
+
-**Custom Dockerfiles**: Allows customization of images to include extensions and tools needed for development.
46
+
-**Docker Compose**: Orchestrates the various services, making it easy to start and stop the entire environment.
51
47
52
48
### Production Environment
53
49
54
-
- Optimized Image: The production image is slim, containing only the necessary dependencies for running a Laravel application.
55
-
- Pre-Built Assets: Assets are built during the image creation process, ensuring that the production environment is ready to serve content immediately.
56
-
- Security Best Practices: The image is built with security in mind, minimizing the attack surface by excluding unnecessary packages and users.
50
+
The `production` directory emphasizes building a slim, secure, and efficient Docker image suitable for deploying a Laravel application in production:
51
+
52
+
-**Optimized Images**: Uses multi-stage builds to keep the final image size small.
53
+
-**Pre-Built Assets**: Assets are compiled during the build process to ensure the container is ready to serve content immediately.
54
+
-**Security Best Practices**: Minimizes the attack surface by excluding unnecessary packages and users.
55
+
-**Docker Compose for Production**: Provides a Compose file tailored for deploying the application with services like Nginx, PHP-FPM, Redis, and PostgreSQL.
56
+
57
57
58
58
## Getting Started
59
59
60
-
To get started with this project, follow these steps:
60
+
Follow these steps to set up and run the Laravel Docker Examples Project:
1. Navigate to the specific example in Development Directory:
72
+
73
+
```bash
74
+
cd development/nginx-fpm
75
+
```
76
+
77
+
2. Copy the .env.example file to .env and adjust any necessary environment variables:
78
+
79
+
```bash
80
+
cp .env.example .env
81
+
```
82
+
83
+
3. Start the Docker Compose Services:
70
84
71
85
```bash
72
-
cd development/
73
-
docker-compose up -d
86
+
docker compose up -d
74
87
```
75
88
76
-
3. Install Laravel Dependencies:
89
+
4. Install Laravel Dependencies:
77
90
78
91
```bash
79
-
docker-compose exec workspace bash
92
+
dockercompose exec workspace bash
80
93
composer install
81
94
npm install
82
95
npm run dev
83
96
```
84
97
85
-
4. Access the Application:
98
+
5. Run Migrations:
99
+
100
+
```bash
101
+
docker compose exec workspace php artisan migrate
102
+
```
103
+
104
+
6. Access the Application:
86
105
87
106
Open your browser and navigate to [http://localhost](http://localhost).
88
107
89
108
## Usage
90
109
91
-
### Access the Workspace Container:
110
+
Here are some common commands and tips for using the development environment:
111
+
112
+
### Accessing the Workspace Container
113
+
114
+
The workspace container includes Composer, Node.js, NPM, and other tools necessary for development.
92
115
93
116
```bash
94
-
docker-compose exec workspace bash
117
+
dockercompose exec workspace bash
95
118
```
96
119
97
120
### Run Artisan Commands:
98
121
99
122
```bash
100
-
docker-compose exec workspace php artisan migrate
123
+
dockercompose exec workspace php artisan migrate
101
124
```
102
125
103
126
### Rebuild Containers:
104
127
105
128
```bash
106
-
docker-compose up -d --build
129
+
docker compose up -d --build
130
+
```
131
+
132
+
### Stop Containers:
133
+
134
+
```bash
135
+
docker compose down
136
+
```
137
+
138
+
### View Logs:
139
+
140
+
```bash
141
+
docker compose logs -f
142
+
```
143
+
144
+
For specific services, you can use:
145
+
146
+
```bash
147
+
docker compose logs -f web
107
148
```
108
149
109
150
## Production Environment
110
151
111
-
### Key Features
152
+
The production environment is designed with security and efficiency in mind:
153
+
154
+
-**Optimized Docker Images**: Uses multi-stage builds to minimize the final image size, reducing the attack surface.
155
+
-**Environment Variables Management**: Sensitive data such as passwords and API keys are managed carefully to prevent exposure.
156
+
-**User Permissions**: Containers run under non-root users where possible to follow the principle of least privilege.
157
+
-**Health Checks**: Implemented to monitor the status of services and ensure they are functioning correctly.
158
+
-**HTTPS Setup**: While not included in this example, it's recommended to configure SSL certificates and use HTTPS in a production environment.
112
159
113
-
- Optimized Image: The production image is slim, containing only the necessary dependencies for running a Laravel application.
114
-
- Pre-Built Assets: Assets are built during the image creation process, ensuring that the production environment is ready to serve content immediately.
115
-
- Security Best Practices: The image is built with security in mind, minimizing the attack surface by excluding unnecessary packages and users.
116
160
117
-
### Building the Production Image
161
+
### Building and Running the Production Environment
162
+
163
+
1.**Navigate to the specific example in Production Directory :**
164
+
165
+
```bash
166
+
cd production/nginx-fpm/
167
+
```
118
168
119
-
1. Build the Image:
169
+
2. Copy the .env.example file to .env (compose env, not the laravel env file) and adjust any necessary environment variables:
0 commit comments