Docker Starter
A docker-based infrastructure wrapped in an easy-to-use command line, oriented for PHP projects.
This repository contains a collection of Dockerfile and docker-compose configurations
for your PHP projects with built-in support for HTTPS, custom domain, databases, workers...
and is used as a foundation for our projects at JoliCode.
Warning
You are reading the README of version 4 that uses Castor.
Project configuration
Before executing any command, you need to configure a few parameters in the
castor.php file, in the create_default_variables() function:
-
project_name (required): This will be used to prefix all docker objects
(network, images, containers);
-
root_domain (optional, default: project_name + '.test'): This is the root
domain where the application will be available;
-
extra_domains (optional): This contains extra domains where the application
will be available;
-
php_version (optional, default: 8.5): This is the PHP version installed inside the containers.
For example:
function create_default_variables(): array
{
$projectName = 'app';
$tld = 'test';
return [
'project_name' => $projectName,
'root_domain' => "{$projectName}.{$tld}",
'extra_domains' => [
"www.{$projectName}.{$tld}",
"admin.{$projectName}.{$tld}",
"api.{$projectName}.{$tld}",
],
'php_version' => 8.5,
];
)
Will give you https://app.test, https://www.app.test,
https://api.app.test and https://admin.app.test pointing at your
application/ directory.
Note
Some castor tasks have been added for DX purposes. Checkout and adapt
the tasks install, migrate and cache_clear to your project.
Git worktree support
When running castor commands inside a git worktree,
the stack is automatically isolated from other worktrees:
- Docker Compose project name, volumes, and networks are suffixed with
-wt-<worktree-name>
- Ports are derived from the worktree name (use
castor docker:ports to display them)
This allows running multiple feature branches in parallel without conflicts.
Usage documentation
We provide a README.dist.md to bootstrap your project
documentation, with everything you need to know to start and interact with the
infrastructure.
If you want to install a Symfony project, you can run (before castor init):
castor symfony [--web-app]
To replace this README with the dist, and remove all unnecessary files, you can
run:
Note
This command can be run only once
The GitHub Actions workflows of .github/ are kept as the CI of your project:
castor init only removes their "docker-starter only" blocks, which test
docker-starter itself (PHP versions matrix, demo application...).
Also, in order to improve your usage of castor scripts, you can install console
autocompletion script.
If you are using bash:
castor completion | sudo tee /etc/bash_completion.d/castor
If you are using something else, please refer to your shell documentation. You
may need to use castor completion > /to/somewhere.
Castor supports completion for bash, zsh & fish shells.
Cookbooks
How to install third party tools with Composer
Read the cookbook
If you want to install some third party tools with Composer, it is recommended to install them in their dedicated directory.
PHPStan and PHP-CS-Fixer are already installed in the tools directory.
We suggest to:
-
create a composer.json which requires only this tool in tools/<tool name>/composer.json;
-
create an executable symbolic link to the tool from the root directory of the project: ln -s ../<tool name>/vendor/bin/<tool bin> tools/bin/<tool bin>;
[!NOTE]
Relative symlinks works here, because the first part of the command is relative to the second part, not to the current directory.
Since tools/bin path is appended to the $PATH, tools will be available globally in the builder container.
How to change the layout of the project
Read the cookbook
If you want to rename the application directory, or even move its content to
the root directory, you have to edit each reference to it. Theses references
represent each application entry point, whether it be over HTTP or CLI.
Usually, there is three places where you need to do it:
- In Nginx configuration file (shared by the dev and production images):
infrastructure/docker/services/php/nginx/conf.d/default.conf. You need
to update server.root option to the new path. For example:
- root /var/www/application/public;
+ root /var/www/public;
- In all workers configuration file:
infrastructure/docker/docker-compose.worker.yml:
- command: php -d memory_limit=1G /var/www/application/bin/console messenger:consume async --memory-limit=128M
+ command: php -d memory_limit=1G /var/www/bin/console messenger:consume async --memory-limit=128M
- In the builder, to land in the right directory directly:
infrastructure/docker/services/php/Dockerfile:
- WORKDIR /var/www/application
+ WORKDIR /var/www
- In the production images: the
app build context in
infrastructure/docker/docker-compose.prod.yml and the
/var/www/application paths of the "Production stages" of
infrastructure/docker/services/php/Dockerfile.
How to use MariaDB instead of PostgreSQL
Read the cookbook
In order to use MariaDB, you will need to apply this patch:
diff --git a/infrastructure/docker/docker-compose.yml b/infrastructure/docker/docker-compose.yml
index e891ada..a49a99d 100644
--- a/infrastructure/docker/docker-compose.yml
+++ b/infrastructure/docker/docker-compose.yml
@@ -8,7 +8,7 @@ x-templates:
environment:
- APP_ENV
depends_on:
- postgres:
+ mariadb:
condition: service_healthy
volumes:
- "../..:/var/www:cached"
@@ -16,23 +16,25 @@ x-templates:
- worker
volumes:
- postgres-data: {}
+ mariadb-data: {}
# # Needed if $XDG_ env vars have been overridden
# builder-yarn-data: {}
services:
- postgres:
- image: postgres:18
+ mariadb:
+ image: mariadb:12
environment:
- - POSTGRES_USER=app
- - POSTGRES_PASSWORD=app
- volumes:
- - postgres-data:/var/lib/postgresql
+ - MARIADB_ALLOW_EMPTY_ROOT_PASSWORD=1
+ - MARIADB_USER=app
+ - MARIADB_PASSWORD=app
+ - MARIADB_DATABASE=app
healthcheck:
- test: ["CMD-SHELL", "pg_isready -U postgres"]
+ test: "mariadb-admin ping -h localhost"
interval: 5s
timeout: 5s
- retries: 5
+ retries: 10
+ volumes:
+ - mariadb-data:/var/lib/mysql
profiles:
- default
@@ -49,7 +51,7 @@ services:
- "../..:/var/www:cached"
- "../../.home:/home/app:cached"
depends_on:
- postgres:
+ mariadb:
condition: service_healthy
profiles:
- default
@@ -90,6 +92,6 @@ services:
# of $XDG_DATA_HOME
# - "builder-yarn-data:/data/yarn"
depends_on:
- - postgres
+ - mariadb
profiles:
- builder
diff --git a/infrastructure/docker/services/php/Dockerfile b/infrastructure/docker/services/php/Dockerfile
index e9e95fd..8f00745 100644
--- a/infrastructure/docker/services/php/Dockerfile
+++ b/infrastructure/docker/services/php/Dockerfile
@@ -35,7 +35,7 @@ RUN apt-get update \
"php${PHP_VERSION}-iconv" \
"php${PHP_VERSION}-intl" \
"php${PHP_VERSION}-mbstring" \
- "php${PHP_VERSION}-pgsql" \
+ "php${PHP_VERSION}-mysql" \
"php${PHP_VERSION}-uuid" \
"php${PHP_VERSION}-xml" \
"php${PHP_VERSION}-zip" \
How to use MySQL instead of PostgreSQL
Read the cookbook
In order to use MySQL, you will need to follow the same steps as for MariaDB, but with the following configuration for the database service:
mysql:
image: mysql:8
environment:
- MYSQL_ALLOW_EMPTY_PASSWORD=1
healthcheck:
test: "mysqladmin ping -h localhost"
interval: 5s
timeout: 5s
retries: 10
volumes:
- mysql-data:/var/lib/mysql
How to use with Webpack Encore
Read the cookbook
[!NOTE]
this cookbook documents the integration of webpack 5+. For older version
of webpack, use previous version of the docker starter.
If you want to use Webpack Encore in a Symfony project,
-
Follow instructions on symfony.com to install webpack encore.
You will need to follow these instructions too.
-
Create a new service for encore:
Add the following content to the docker-compose.yml file:
services:
encore:
build:
context: services/php
target: builder
volumes:
- "../..:/var/www:cached"
command: >
yarn run dev-server
--hot
--host 0.0.0.0
--public https://encore.${PROJECT_ROOT_DOMAIN}
--allowed-hosts ${PROJECT_ROOT_DOMAIN}
--allowed-hosts encore.${PROJECT_ROOT_DOMAIN}
--client-web-socket-url-hostname encore.${PROJECT_ROOT_DOMAIN}
--client-web-socket-url-port 443
--client-web-socket-url-protocol wss
--server-type http
labels:
- "project-name=${PROJECT_NAME}"
- "traefik.enable=true"
- "traefik.http.routers.${PROJECT_NAME}-encore.rule=Host(`encore.${PROJECT_ROOT_DOMAIN}`)"
- "traefik.http.routers.${PROJECT_NAME}-encore.tls=true"
- "traefik.http.services.encore.loadbalancer.server.port=8000"
healthcheck:
test: ["CMD", "curl", "-f", "http://localhost:8000/build/app.css"]
profiles:
- default
If the assets are not reachable, you may accept self-signed certificate. To do so, open a new tab
at https://encore.app.test and click on accept.