Skip to content

Commit e507194

Browse files
authored
Merge pull request #23 from YuriiDorosh/features/redis
Features/redis
2 parents af4b2b8 + 150df3b commit e507194

5 files changed

Lines changed: 257 additions & 4 deletions

File tree

Makefile

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,46 @@ docker_run_win:
2323
docker_stop_win:
2424
docker-compose down
2525

26+
# Production Docker Management Commands
27+
#-------------------------------------------------
28+
29+
# Start the production Docker environment
30+
prod_up:
31+
docker-compose -f docker-compose.prod.yml up -d
32+
33+
# Stop the production Docker environment
34+
prod_down:
35+
docker-compose -f docker-compose.prod.yml down
36+
37+
# Restart the production Docker environment
38+
prod_restart:
39+
make prod_down
40+
make prod_up
41+
42+
# View logs for the production Docker environment
43+
prod_logs:
44+
docker-compose -f docker-compose.prod.yml logs
45+
46+
# Rebuild and start the production Docker environment
47+
prod_rebuild:
48+
docker-compose -f docker-compose.prod.yml up -d --build
49+
50+
# Stop and remove all containers, networks, and volumes
51+
prod_clean:
52+
docker-compose -f docker-compose.prod.yml down -v
53+
54+
# Execute production migrations (adjust command as necessary for your project setup)
55+
prod_migrate:
56+
docker-compose -f docker-compose.prod.yml exec app php spark migrate
57+
58+
# Execute production seeders (adjust command as necessary for your project setup)
59+
prod_seed:
60+
docker-compose -f docker-compose.prod.yml exec app php spark db:seed
61+
62+
# Clear the Redis cache in the production environment
63+
prod_clear_cache:
64+
docker-compose -f docker-compose.prod.yml exec redis redis-cli FLUSHALL
65+
2666
# Database migrations
2767
#-------------------------------------------------
2868

docker-compose.prod.yml

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
# Production Docker Configuration for CodeIgniter 4 Application
2+
# =============================================================
3+
#
4+
# This Docker Compose configuration is designed for production deployment of a
5+
# CodeIgniter 4 application. It defines four main services: the application itself (`app`),
6+
# an Nginx web server (`nginx`), a MySQL database (`mysql`), and a Redis cache (`redis`).
7+
# Each service is configured with production in mind, using Alpine images where available
8+
# for their smaller footprint and setting environment variables for secure and efficient operation.
9+
#
10+
# Usage:
11+
# ------
12+
# Before running, ensure you have set the environment variables `MYSQL_PROD_DATABASE`,
13+
# `MYSQL_PROD_USER`, and `MYSQL_PROD_PASSWORD` for the MySQL service. These can be set
14+
# in an `.env` file located in the same directory as this docker-compose file or exported
15+
# directly in your shell.
16+
#
17+
# To start all services in detached mode, use:
18+
# `docker-compose -f docker-compose.prod.yml up -d`
19+
#
20+
# To stop all services and remove containers, networks, and volumes created by `up`, use:
21+
# `docker-compose -f docker-compose.prod.yml down`
22+
#
23+
# Services:
24+
# ---------
25+
# app: The main application service built from a Dockerfile located in `./docker/ci4`.
26+
# It's configured to run in a `production` environment. The entire application directory
27+
# is mounted into the container to facilitate easy updates, but consider using COPY
28+
# in Dockerfile for a more secure, immutable deployment.
29+
#
30+
# nginx: Serves as the web server, using the lightweight Alpine Linux version of Nginx.
31+
# It serves static files directly and proxies PHP requests to the `app` service.
32+
# The production Nginx configuration is mounted from `./docker/nginx/nginx.prod.conf`.
33+
#
34+
# mysql: The MySQL database service, crucial for data persistence. It's configured through
35+
# environment variables for the database name, user, and password, which should be
36+
# securely managed. Data is persisted in a Docker volume named `mysql-data`.
37+
#
38+
# redis: Used for caching and session storage to enhance application performance.
39+
# Like Nginx, it uses an Alpine Linux image for a smaller footprint.
40+
#
41+
# Networks:
42+
# ---------
43+
# app-network: A custom bridge network that facilitates communication between services.
44+
# All services are attached to this network.
45+
#
46+
# Volumes:
47+
# --------
48+
# mysql-data: A Docker-managed volume that ensures the persistence of MySQL data across
49+
# container restarts and deployments.
50+
#
51+
# Notes:
52+
# ------
53+
# - This configuration is optimized for production use, but security and performance
54+
# tuning is an ongoing process. Always keep your images up to date and monitor
55+
# for any potential security vulnerabilities.
56+
# - Ensure SSL/TLS configuration for Nginx if exposing services directly to the internet.
57+
# Consider using a service like Let's Encrypt for free SSL certificates.
58+
#
59+
version: '3.8'
60+
services:
61+
app:
62+
build:
63+
context: ./docker/ci4
64+
volumes:
65+
- .:/var/www/html
66+
environment:
67+
CI_ENVIRONMENT: production
68+
networks:
69+
- app-network
70+
71+
nginx:
72+
image: nginx:alpine # Using the alpine version for smaller size
73+
volumes:
74+
- ./public:/var/www/html/public
75+
- ./docker/nginx/nginx.prod.conf:/etc/nginx/conf.d/default.conf
76+
networks:
77+
- app-network
78+
depends_on:
79+
- app
80+
81+
mysql:
82+
image: mysql:5.7
83+
environment:
84+
MYSQL_DATABASE: ${MYSQL_PROD_DATABASE}
85+
MYSQL_USER: ${MYSQL_PROD_USER}
86+
MYSQL_PASSWORD: ${MYSQL_PROD_PASSWORD}
87+
networks:
88+
- app-network
89+
90+
redis:
91+
image: redis:alpine # Using the alpine version for smaller size
92+
networks:
93+
- app-network
94+
95+
networks:
96+
app-network:
97+
driver: bridge
98+
99+
volumes:
100+
mysql-data:

docker-compose.yml

Lines changed: 58 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,61 @@
1+
# CodeIgniter 4 Project Docker Configuration
2+
# ==========================================
3+
#
4+
# This Docker Compose file orchestrates a Docker environment suitable for developing
5+
# and running a CodeIgniter 4 application. It sets up the following services:
6+
#
7+
# 1. App - The main application service running PHP and the CodeIgniter framework.
8+
# 2. Nginx - A web server to serve the CodeIgniter application.
9+
# 3. MySQL - A database server for the application's data persistence.
10+
# 4. Redis - A key-value store used for caching and session storage to enhance performance.
11+
# 5. Adminer - A database management tool accessible via a web interface for managing the MySQL database.
12+
#
13+
# Each service is configured to meet the development needs of a typical CodeIgniter 4 application,
14+
# ensuring that developers can work on the application in an environment that mirrors
15+
# production settings as closely as possible. This setup also facilitates easy sharing of
16+
# the development environment among team members, ensuring consistency across development setups.
17+
#
18+
# Usage:
19+
# ------
20+
# To start the entire stack, run: `docker-compose up -d`
21+
# This will build and start all the defined services in detached mode.
22+
#
23+
# To stop the services, run: `docker-compose down`
24+
# This command stops and removes all running containers defined in this file.
25+
#
26+
# Service Details:
27+
# ----------------
28+
# - `app`: This service uses a custom Dockerfile located in `./docker/ci4`. It's set up to run PHP applications,
29+
# particularly tailored for the CodeIgniter framework. The project directory is mounted into the container
30+
# to allow live editing of the application code.
31+
#
32+
# - `nginx`: Configured as the web server for the application. It uses the official Nginx image and forwards
33+
# requests to the PHP application running in the `app` service. Custom Nginx configurations can be applied
34+
# by modifying `./docker/nginx/nginx.conf`.
35+
#
36+
# - `mysql`: This service runs a MySQL database server, version 5.7. It's used for the application's data storage.
37+
# The service is configured with environment variables to set the root password and create a default database
38+
# upon initialization.
39+
#
40+
# - `redis`: Utilizes the latest Redis image to provide caching and session storage capabilities. It's accessible
41+
# by the application through the network, enhancing performance by storing session and cache data in memory.
42+
#
43+
# - `adminer`: A lightweight database management tool that provides a web interface for managing MySQL databases.
44+
# It's useful for development and debugging purposes.
45+
#
46+
# Network Configuration:
47+
# ----------------------
48+
# All services are connected via a custom bridge network named `app-network`. This setup ensures that services
49+
# can communicate with each other using service names as hostnames, thereby simplifying configuration and connectivity.
50+
#
51+
# Volumes:
52+
# --------
53+
# - `mysql-data`: A named volume for persisting MySQL data. This ensures that the database data remains
54+
# intact across container restarts and rebuilds.
55+
#
56+
# Note: Modify the service configurations as per your project requirements. Ensure that any sensitive or
57+
# environment-specific variables are appropriately managed, ideally through a `.env` file or Docker secrets.
58+
#
159
version: '3.8'
260
services:
361
app:
@@ -9,10 +67,6 @@ services:
967
- mysql
1068
environment:
1169
CI_ENVIRONMENT: development
12-
DB_HOST: mysql
13-
DB_DATABASE: ci4
14-
DB_USERNAME: root
15-
DB_PASSWORD: root
1670
networks:
1771
- app-network
1872

docker/nginx/nginx.prod.conf

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
server {
2+
listen 80;
3+
# Strongly recommended to also configure listening on 443 (SSL) for HTTPS
4+
# listen 443 ssl;
5+
# ssl_certificate /path/to/your_certificate.pem;
6+
# ssl_certificate_key /path/to/your_private.key;
7+
8+
server_name example.com; # Change this to your domain
9+
10+
root /var/www/html/public;
11+
index index.php index.html index.htm;
12+
13+
# Serve static files directly without passing to PHP
14+
location ~* \.(jpg|jpeg|gif|css|png|js|ico|html)$ {
15+
access_log off;
16+
expires max;
17+
}
18+
19+
# Deny access to sensitive files
20+
location ~ /\.ht {
21+
deny all;
22+
}
23+
24+
# URL rewrites and forwarding to index.php
25+
location / {
26+
try_files $uri $uri/ /index.php?$query_string;
27+
}
28+
29+
# PHP FPM configuration
30+
location ~ \.php$ {
31+
try_files $uri /index.php =404;
32+
fastcgi_pass app:9000;
33+
fastcgi_index index.php;
34+
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
35+
include fastcgi_params;
36+
}
37+
38+
# Recommended: security headers
39+
add_header X-Frame-Options "SAMEORIGIN" always;
40+
add_header X-Content-Type-Options "nosniff" always;
41+
add_header X-XSS-Protection "1; mode=block" always;
42+
43+
# Further optimizations and security settings can be added here
44+
}

env

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515
#--------------------------------------------------------------------
1616

1717
# CI_ENVIRONMENT = production
18+
#MYSQL_PROD_USER=
19+
#MYSQL_PROD_DATABASE=
20+
#MYSQL_PROD_PASSWORD=
1821

1922
#--------------------------------------------------------------------
2023
# APP
@@ -30,12 +33,24 @@
3033
# DATABASE
3134
#--------------------------------------------------------------------
3235

36+
# DEV
37+
3338
database.default.hostname = mysql
3439
database.default.database = ci4
3540
database.default.username = root
3641
database.default.password = root
3742
database.default.DBDriver = MySQLi
3843

44+
# PROD
45+
46+
# database.default.hostname = mysql
47+
# database.default.database = YOUR_PROD_DATABASE
48+
# database.default.username = YOUR_PROD_USERNAME
49+
# database.default.password = YOUR_PROD_PASSWORD
50+
# database.default.DBDriver = MySQLi
51+
52+
# TESTS
53+
3954
# database.tests.hostname = localhost
4055
# database.tests.database = ci4_test
4156
# database.tests.username = root

0 commit comments

Comments
 (0)