Skip to content

Docker Laravel 11 service with PHP FPM 8.3 served by Nginx 1.24 on Alpine 3.19 and MariaDB 10.11 service

License

Notifications You must be signed in to change notification settings

pabloripoll/docker-laravel-11-php-fpm-8

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

17 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Docker Laravel 11 with PHP FPM 8+

The objective of this repository is having a CaaS Containers as a Service to provide a start up application with the basic enviroment features to deploy a php service running with Nginx and PHP-FPM in a container for Laravel and another container with a MySQL database to follow the best practices on an easy scenario to understand and modify on development requirements.

The connection between container is as Host Network on eth0, thus both containers do not share networking or bridge configuration.

As client end user both services can be accessed through localhost:${PORT} but the connection between containers is through the ${HOSTNAME}:${PORT}.

Laravel Docker Container Service

Database Container Service

To connect this service to a SQL database, it can be used the following MariaDB 10.11 service:

Project objetives

  • Built on the lightweight and secure Alpine 3.19 2024 release Linux distribution
  • Multi-platform, supporting AMD4, ARMv6, ARMv7, ARM64
  • Very small Docker image size (+/-40MB)
  • Uses PHP 8.3 as default for the best performance, low CPU usage & memory footprint, but also can be downgraded till PHP 8.0
  • Optimized for 100 concurrent users
  • Optimized to only use resources when there's traffic (by using PHP-FPM's on-demand process manager)
  • The services Nginx, PHP-FPM and supervisord run under a project-privileged user to make it more secure
  • The logs of all the services are redirected to the output of the Docker container (visible with docker logs -f <container name>)
  • Follows the KISS principle (Keep It Simple, Stupid) to make it easy to understand and adjust the image to your needs
  • Services independency to connect the application to other database allocation

PHP config

To use a different PHP 8 version the following Dockerfile arguments and variable has to be modified:

ARG PHP_VERSION=8.3
ARG PHP_ALPINE=83
...
ENV PHP_V="php83"

Also, it has to be informed to Supervisor Config the PHP-FPM version to run.

...
[program:php-fpm]
command=php-fpm83 -F
...

Containers on Windows systems

This project has not been tested on Windows OS neither I can use it to test it. So, I cannot bring much support on it.

Anyway, using this repository you will needed to find out your PC IP by login as an administrator user to set connection between containers.

C:\WINDOWS\system32>ipconfig /all

Windows IP Configuration

 Host Name . . . . . . . . . . . . : 191.128.1.41
 Primary Dns Suffix. . . . . . . . : paul.ad.cmu.edu
 Node Type . . . . . . . . . . . . : Peer-Peer
 IP Routing Enabled. . . . . . . . : No
 WINS Proxy Enabled. . . . . . . . : No
 DNS Suffix Search List. . . . . . : scs.ad.cs.cmu.edu

Take the first ip listed. Wordpress container will connect with database container using that IP.

Containers on Unix based systems

Find out your IP on UNIX systems and take the first IP listed

$ hostname -I

191.128.1.41 172.17.0.1 172.20.0.1 172.21.0.1

Structure

Directories and main files on a tree architecture description

.
│
├── docker
│   └── nginx-php
│       ├── ...
│       ├── .env.example
│       └── docker-compose.yml
│
├── resources
│   ├── database
│   │   ├── laravel-init.sql
│   │   └── laravel-backup.sql
│   │
│   └── laravel
│       └── (any file or directory required for re-building the app...)
│
├── laravel
│   └── (application...)
│
├── .env
├── .env.example
└── Makefile

Automation with Makefile

Makefiles are often used to automate the process of building and compiling software on Unix-based systems as Linux and macOS.

On Windows - I recommend to use Makefile:
https://stackoverflow.com/questions/2532234/how-to-run-a-makefile-in-windows

Makefile recipies

$ make help
usage: make [target]

targets:
Makefile  help                     shows this Makefile help message
Makefile  hostname                 shows local machine ip
Makefile  fix-permission           sets project directory permission
Makefile  ports-check              shows this project ports availability on local machine
Makefile  laravel-ssh              enters the Laravel container shell
Makefile  laravel-set              sets the Laravel PHP enviroment file to build the container
Makefile  laravel-build            builds the Laravel PHP container from Docker image
Makefile  laravel-start            starts up the Laravel PHP container running
Makefile  laravel-stop             stops the Laravel PHP container but data will not be destroyed
Makefile  laravel-destroy          stops and removes the Laravel PHP container from Docker network destroying its data
Makefile  repo-flush               clears local git repository cache specially to update .gitignore

Checkout local machine ports availability

$ make ports-check

Checking configuration for Laravel container:
Laravel > port:8888 is free to use.

Checkout local machine IP to set connection between containers using the following makefile recipe

$ make hostname

192.168.1.41

Build the project

$ make project-build

LARAVEL docker-compose.yml .env file has been set.

[+] Building 49.7s (25/25)                                             docker:default
 => [wordpress internal] load build definition from Dockerfile         0.0s
 => => transferring dockerfile: 2.47kB
...
=> => naming to docker.io/library/lara-app:php-8.3                     0.0s
[+] Running 1/2
 ⠇ Network lara-app_default  Created                                   0.8s
 ✔ Container lara-app        Started

Running the project

$ make project-start

[+] Running 1/0
 ✔ Container lara-app  Running                      0.0s

Now, Laravel should be available on local machine by visiting http://localhost:8888/

Docker Info

Docker container

$ sudo docker ps -a
CONTAINER ID   IMAGE      COMMAND    CREATED      STATUS      PORTS                                             NAMES
ecd27aeae010   lara...    "docker-php-entrypoi…"   3 mins...   9000/tcp, 0.0.0.0:8888->80/tcp, :::8888->80/tcp  laravel-app

Docker image

$ sudo docker images
REPOSITORY   TAG           IMAGE ID       CREATED         SIZE
laravel-app  lara...       373f6967199b   5 minutes ago   200MB

Docker stats

$ sudo docker system df
TYPE            TOTAL     ACTIVE    SIZE      RECLAIMABLE
Images          1         1         532.2MB   0B (0%)
Containers      1         1         25.03kB   0B (0%)
Local Volumes   1         0         117.9MB   117.9MB (100%)
Build Cache     39        0         10.21kB   10.21kB

Check Laravel status

Visiting http://localhost:8888/ should display Laravel's welcome page.

Use an API platform (Postman, Firefox RESTClient, etc..) to check connection with Laravel

GET: http://localhost:8888/api/v1/health

{
    "status": true
}

Check Laravel database connection

Check connection to database through a test endpoint. If conenction params are not set already will response as follows

GET: http://localhost:8888/api/v1/health/db

{
    "status": false,
    "message": "Connect to database failed - Check connection params.",
    "error": {
        "errorInfo": [
            "HY000",
            2002,
            "Host is unreachable"
        ]
    }
}

Complete the MySQL database connection params. Use local hostname IP $ make hostname to set DB_HOST variable

DB_CONNECTION=mysql
DB_HOST=192.168.1.41
DB_PORT=8889
DB_DATABASE=laravel
DB_USERNAME=laravel
DB_PASSWORD=123456

Checking the connection to database once is set correctly will response as follows

GET: http://localhost:8888/api/v1/health/db

{
    "status": true
}

Stop Containers

Using the following Makefile recipe stops application and database containers, keeping database persistance and application files binded without any loss

$ make laravel-stop

[+] Killing 1/1
 ✔ Container laravel-app  Killed              0.5s
Going to remove laravel-app
[+] Removing 1/0
 ✔ Container laravel-app  Removed             0.0s

Remove Containers

To stop and remove both application and database containers from docker network use the following Makefile recipe

$ make laravel-destroy

[+] Killing 1/1
 ✔ Container laravel-app  Killed                   0.4s
Going to remove laravel-app
[+] Removing 1/0
 ✔ Container laravel-app  Removed                  0.0s
[+] Running 1/1
 ✔ Network laravel-app_default  Removed

Prune Docker system cache

$ sudo docker system prune

...
Total reclaimed space: 423.4MB

Prune Docker volume cache

$ sudo docker system prune

...
Total reclaimed space: 50.7MB

About

Docker Laravel 11 service with PHP FPM 8.3 served by Nginx 1.24 on Alpine 3.19 and MariaDB 10.11 service

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published