Docker Compose setup for quick deployment of PHP applications. The configuration supports development and production modes, using lightweight Alpine images to reduce container size. Includes basic and optional services for convenient work.
- Flexibility: Easily configure services to suit the project needs.
- Isolation: Run multiple projects on one computer without conflicts.
- Production-ready: Built-in HTTPS support with Certbot.
- Convenience: Includes Composer, Redis, MariaDB, Ofelia and other useful tools.
- Included Services
- Optional Services (for local development)
- Project Structure and Configuration
- Basic Commands
- Service Configuration
- SSL Certificates
- Access to Sites by Domain Name
- Restricting Access to Services
- Additional Information
- Nginx
- PHP-FPM
- Composer
- Redis
- MariaDB
- Ofelia: Task scheduler (cron).
- Certbot: Automatic retrieval of SSL certificates (for production).
- Traefik: HTTP proxy for domains.
- Adminer: Web interface for MariaDB.
- MailHog: Mail testing.
-
Copy files: Copy
.docker/
,docker-compose.yml
andmakefile
from the project repository to the root of your project.makefile
contains the basic commands for working with Docker Compose. -
Select configuration: Copy
docker-compose.dev.yml
(for development) ordocker-compose.prod.yml
(for production) from the.docker/
directory todocker-compose.override.yml
, which should be located in the project root.cp .docker/docker-compose.dev.yml docker-compose.override.yml
-
Configure SSL (for development and production): Use the following command to copy the certificates. Ensure that the name of the destination folder matches the value of
APP_HOST
from.env
.cp -R .docker/certbot/conf/live/test-app.loc .docker/certbot/conf/live/my-app.loc
-
Remove unnecessary services: Edit
docker-compose.yml
anddocker-compose.override.yml
, removing unnecessary services. -
Configure environment: Edit the
.env
file.
- Run:
make up
ordocker compose up -d
- Stop:
make stop
ordocker compose stop
- Rebuild:
make rebuild
ordocker compose up -d --build
- PHP Console:
make shell
-
Nginx: Configured via
default.conf.template
(for production) anddefault.conf.dev.template
(for development), located in the.docker/nginx/
directory. -
PHP-FPM:
- PHP version is changed in
.docker/php-fpm/Dockerfile
. - General settings are in
.docker/php-fpm/app.ini
. - Configuration for prod version is in the file
.docker/php-fpm/app.prod.ini
. - Configuration for dev version is in the file
.docker/php-fpm/app.dev.ini
.
- PHP version is changed in
-
Ofelia: Configured in
docker-compose.override.yml
vialabels
.Example configuration for running the
cli.php
script every minute:ofelia.enabled: "true" ofelia.job-exec.php-cli.schedule: "@every 1m" ofelia.job-exec.php-cli.user: www-data ofelia.job-exec.php-cli.command: "php /app/public/cli.php"
-
Obtain: Use the following command to obtain SSL certificates. Replace
domain.com
andwww.domain.com
with your domain names.docker compose run --rm certbot certonly --webroot --webroot-path /var/www/certbot/ -d domain.com -d www.domain.com
-
Update: Use the following command to update expired SSL certificates.
docker compose run --rm certbot renew
-
Automate: Add the following task to cron for automatic certificate renewal. Replace
/home/user/project_path
with the path to your project.0 5 * * * cd /home/user/project_path && docker compose run --rm certbot renew
By default, your site will be accessible at localhost:port
, where port
is the port number specified in the .env
file in the NGINX_HTTP_PORT
and NGINX_HTTPS_PORT
variables.
To access the site by domain name, you can use Traefik as a proxy server.
The domain by which your local site will be accessible is specified in the .env
file in the APP_HOST
and NGINX_SERVER_NAME
variables.
In the NGINX_SERVER_NAME
variable, you can specify multiple domains separated by a space.
For the site to be accessible in the browser, you need to add a line with the domain and IP address to the hosts
file on your computer:
127.0.0.1 test-app.loc
Replace test-app.loc
with your domain specified in the .env
file.
More details on Traefik configuration can be found in the traefik directory.
If you do not want to use Traefik, you will need to remove the networks
section from docker-compose.override.yml
:
networks:
web:
name: traefik_default
external: true
By default, in production mode, external access to mariadb and redis containers is restricted. Depending on the network configuration, this restriction may not be necessary or may not work correctly.
In this case, remove 127.0.0.1: from docker-compose.override.yml for the respective services, but do not forget to restrict access to the ports, as this affects security.
- Ofelia Documentation: https://github.com/mcuadros/ofelia
- PHP Versions: https://hub.docker.com/_/php/tags