Skip to content

Commit e8b6c3b

Browse files
committed
updating the structure of development folder, added LICENSE file and adjusted main README file
1 parent a73166e commit e8b6c3b

File tree

7 files changed

+170
-103
lines changed

7 files changed

+170
-103
lines changed

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) [2024] [Sergei Shitikov]
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 133 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -1,131 +1,181 @@
1-
# Laravel Dockerize Project
2-
## Overview
3-
4-
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.
8-
- Polish production example to optimize build
9-
- Polish docker compose for production
10-
- Setup Dockerignore
11-
- Check env replacement. Add env encryption example.
12-
- Add debugger for devel environment.
13-
- Examples with Laravel Octane.
1+
# Laravel Docker Examples Project
142

153
## Table of Contents
164

175
- [Overview](#overview)
186
- [Project Structure](#project-structure)
19-
- [Development Environment](#development-environment)
20-
- [Key Features](#key-features)
7+
- [Example App](#example-app)
8+
- [Development Environment](#development-environment)
9+
- [Production Environment](#production-environment)
2110
- [Getting Started](#getting-started)
11+
- [Clone the Repository](#clone-the-repository)
12+
- [Setting Up the Development Environment](#setting-up-the-development-environment)
2213
- [Usage](#usage)
23-
- [Production Environment](#production-environment)
24-
- [Building the Production Image](#building-the-production-image)
25-
- [Deploying](#deploying)
14+
- [Production Environment](#production-environment-1)
15+
- [Building and Running the Production Environment](#building-and-running-the-production-environment)
2616
- [Technical Details](#technical-details)
2717
- [Contributing](#contributing)
18+
- [How to Contribute](#how-to-contribute)
2819
- [License](#license)
2920

30-
## Project Structure
3121

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
3423

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.
3625

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.
3826

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.
4034

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
4236

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.
4438

4539
### Development Environment
4640

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.
5147

5248
### Production Environment
5349

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+
5757

5858
## Getting Started
5959

60-
To get started with this project, follow these steps:
60+
Follow these steps to set up and run the Laravel Docker Examples Project:
6161

62-
1. Clone the Repository:
62+
### Clone the Repository
6363

6464
```bash
65-
git clone https://github.com/yourusername/laravel-dockerize.git
66-
cd laravel-dockerize
65+
git clone https://github.com/rw4lll/laravel-docker-examples.git
66+
cd laravel-docker-examples
6767
```
6868

69-
2. Start the Development Environment:
69+
### Setting Up the Development Environment
70+
71+
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:
7084

7185
```bash
72-
cd development/
73-
docker-compose up -d
86+
docker compose up -d
7487
```
7588

76-
3. Install Laravel Dependencies:
89+
4. Install Laravel Dependencies:
7790

7891
```bash
79-
docker-compose exec workspace bash
92+
docker compose exec workspace bash
8093
composer install
8194
npm install
8295
npm run dev
8396
```
8497

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:
86105

87106
Open your browser and navigate to [http://localhost](http://localhost).
88107

89108
## Usage
90109

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.
92115

93116
```bash
94-
docker-compose exec workspace bash
117+
docker compose exec workspace bash
95118
```
96119

97120
### Run Artisan Commands:
98121

99122
```bash
100-
docker-compose exec workspace php artisan migrate
123+
docker compose exec workspace php artisan migrate
101124
```
102125

103126
### Rebuild Containers:
104127

105128
```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
107148
```
108149

109150
## Production Environment
110151

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.
112159

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.
116160

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+
```
118168

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:
120170

121171
```bash
122-
docker build -f Dockerfile.prod -t laravel-app:latest .
172+
cp .env.example .env
123173
```
124174

125-
2. Run the Container:
175+
3. Start the Docker Compose Services:
126176

127177
```bash
128-
docker run -d -p 9000:9000 laravel-app:latest
178+
docker compose up -d
129179
```
130180

131181
### Deploying
@@ -134,31 +184,42 @@ The production image can be deployed to any Docker-compatible hosting environmen
134184

135185
## Technical Details
136186

137-
- PHP: Version 8.3 FPM is used for optimal performance in production.
138-
- Node.js: Version 18.x is used in development for building frontend assets with Vite.
139-
- MySQL/PostgreSQL: Both databases are supported, allowing flexibility depending on your application's requirements.
140-
- Redis: Integrated for caching and session management.
187+
- **PHP**: Version **8.3 FPM** is used for optimal performance in both development and production environments.
188+
- **Node.js**: Version **22.x** is used in the development environment for building frontend assets with Vite.
189+
- **PostgreSQL**: Version **16** is used as the database in the examples, but you can adjust the configuration to use MySQL if preferred.
190+
- **Redis**: Used for caching and session management, integrated into both development and production environments.
191+
- **Nginx**: Used as the web server to serve the Laravel application and handle HTTP requests.
192+
- **Docker Compose**: Orchestrates the services, simplifying the process of starting and stopping the environment.
193+
- **Health Checks**: Implemented in the Docker Compose configurations and Laravel application to ensure all services are operational.
194+
141195

142196
## Contributing
143197

144-
Contributions are welcome! Please follow these steps to contribute:
198+
Contributions are welcome! Whether you find a bug, have an idea for improvement, or want to add a new feature, your input is valuable.
199+
200+
### How to Contribute
145201

146-
1. Fork the Repository: Click the "Fork" button at the top right of this page.
147-
2. Clone Your Fork:
202+
1. **Fork the Repository:**
203+
204+
Click the "Fork" button at the top right of this page to create your own copy of the repository.
205+
206+
2. **Clone Your Fork:**
148207

149208
```bash
150-
git clone https://github.com/yourusername/laravel-dockerize.git
151-
cd laravel-dockerize
209+
git clone https://github.com/your-user-name/laravel-docker-examples.git
210+
cd laravel-docker-examples
152211
```
153212

154213
3. Create a Branch:
155214

156215
```bash
157-
git checkout -b feature-branch
216+
git checkout -b your-feature-branch
158217
```
159218

160219
4. Make Your Changes.
161220

221+
Implement your changes or additions.
222+
162223
5. Commit Your Changes:
163224

164225
```bash
@@ -168,11 +229,14 @@ git commit -m "Description of changes"
168229
6. Push to Your Fork:
169230

170231
```bash
171-
git push origin feature-branch
232+
git push origin feature-branch
172233
```
173234

174-
7. Create a Pull Request: Navigate to the original repository and create a new pull request.
235+
7. Submit a Pull Request:
236+
- Go to the original repository.
237+
- Click on "Pull Requests" and then "New Pull Request."
238+
- Select your fork and branch, and submit your pull request.
175239

176240
## License
177241

178-
This project is licensed under the MIT License. See the LICENSE file for more details.
242+
This project is licensed under the MIT License. See the [LICENSE]LICENSE file for more details.

development/.env.example renamed to development/nginx-fpm/.env.example

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
COMPOSE_PROJECT_NAME=laravel-example-development-nginx-fpm
2+
13
# Sync user ID and group ID to avoid permission issues
24
UID=1000 # Replace with your user ID
35
GID=1000 # Replace with your group ID
@@ -14,10 +16,13 @@ NGINX_PORT=80
1416

1517
# PostgreSQL database configuration
1618
# The name of the database to be used by your Laravel application.
17-
DB_DATABASE=laravel
19+
POSTGRES_DATABASE=laravel
1820

1921
# The username for accessing the PostgreSQL database.
20-
DB_USERNAME=laravel
22+
POSTGRES_USERNAME=laravel
2123

2224
# The password for accessing the PostgreSQL database.
23-
DB_PASSWORD=secret
25+
POSTGRES_PASSWORD=secret
26+
27+
# The port on which the PostgreSQL database will be exposed.
28+
POSTGRES_PORT=5432

development/compose.yaml renamed to development/nginx-fpm/compose.yaml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ services:
44
networks:
55
- laravel
66
volumes:
7-
- ./nginx/nginx.conf:/etc/nginx/nginx.conf:ro # Mount the nginx.conf file directly
7+
- ${LARAVEL_APP_PATH}/nginx.conf:/etc/nginx/nginx.conf:ro # Mount the nginx.conf file directly
88
- ${LARAVEL_APP_PATH}:/var/www
99
ports:
1010
- "${NGINX_PORT}:80"
@@ -19,6 +19,8 @@ services:
1919
args:
2020
UID: ${UID}
2121
GID: ${GID}
22+
env_file:
23+
- ${LARAVEL_APP_PATH}/.env
2224
volumes:
2325
- ${LARAVEL_APP_PATH}:/var/www
2426
networks:
@@ -34,6 +36,8 @@ services:
3436
GID: ${GID}
3537
tty: true # Keep the terminal open
3638
stdin_open: true # Keep stdin open to attach to the container
39+
env_file:
40+
- ${LARAVEL_APP_PATH}/.env
3741
volumes:
3842
- ${LARAVEL_APP_PATH}:/var/www
3943
networks:

development/workspace/Dockerfile renamed to development/nginx-fpm/workspace/Dockerfile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,14 @@ RUN apt-get update && apt-get install -y \
1111
curl \
1212
git \
1313
sudo \
14+
unzip \
1415
libpq-dev \
1516
libonig-dev \
1617
libssl-dev \
1718
libxml2-dev \
1819
libcurl4-openssl-dev \
20+
libicu-dev \
21+
libzip-dev \
1922
&& curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer \
2023
&& curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.40.0/install.sh | bash \
2124
&& apt-get clean && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*

0 commit comments

Comments
 (0)