️ 一个骨架,用于使用 PHP、Docker 和 Castor 启动新的 Web 项目
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.
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,migrateandcache_clearto your project.
When running castor commands inside a git worktree,
the stack is automatically isolated from other worktrees:
-wt-castor docker:ports to display them)This allows running multiple feature branches in parallel without conflicts.
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:
castor init[!NOTE] This command can be run only once
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/castorIf 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.
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//composer.json;
create an executable symbolic link to the tool from the root directory of the project: ln -s ..//vendor/bin/ tools/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.
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:
infrastructure/docker/services/php/frontend/etc/nginx/nginx.conf. You need
to update http.server.root option to the new path. For example:- root /var/www/application/public;
+ root /var/www/public;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=128Minfrastructure/docker/services/php/Dockerfile:- WORKDIR /var/www/application
+ WORKDIR /var/wwwRead the cookbook
In order to use MariaDB, you will need to apply this patch:
…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/mysqlRead 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:
…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.
Read the cookbook
Follow instructions on symfony.com to install AssetMapper.
Remove this block in the
infrastructure/docker/services/php/frontend/etc/nginx/nginx.conf file:
location ~* \.(jpg|jpeg|png|gif|ico|css|js|svg)$ {
access_log off;
add_header Cache-Control "no-cache";
}Remove these lines in the infrastructure/docker/services/php/Dockerfile file:
…Read the cookbook
In order to use Elasticsearch and Kibana, you should add the following content
to the docker-compose.yml file:
…Then, you will be able to browse:
https://kibana.https://elasticsearch.In your application, you can use the following configuration:
http;elasticsearch;9200.Read the cookbook
If you want to install a Sylius project, you can run (before castor init):
castor syliusRead the cookbook
In order to use RabbitMQ and its dashboard, you should add a new service:
# services/rabbitmq/Dockerfile
FROM rabbitmq:3-management-alpine
COPY etc/. /etc/And you can add specific RabbitMQ configuration in the services/rabbitmq/etc/rabbitmq/rabbitmq.conf file:
# services/rabbitmq/etc/rabbitmq/rabbitmq.conf
vm_memory_high_watermark.absolute = 1GBFinally, add the following content to the docker-compose.yml file:
…In order to publish and consume messages with PHP, you need to install the
php${PHP_VERSION}-amqp in the php image.
Then, you will be able to browse:
https://rabbitmq. (username: guest, password: guest)In your application, you can use the following configuration:
rabbitmq;guest;guest;rabbitmq.For example in Symfony you can use: MESSENGER_TRANSPORT_DSN=amqp://guest:guest@rabbitmq:5672/%2f/messages.
Read the cookbook
In order to use Redis and its dashboard, you should add the following content to
the docker-compose.yml file:
…In order to communicate with Redis, you need to install the
php${PHP_VERSION}-redis in the php image.
Then, you will be able to browse:
https://redis.In your application, you can use the following configuration:
redis;6379.Read the cookbook
In order to use Mailpit and its dashboard, you should add the following content
to the docker-compose.yml file:
services:
mail:
image: axllent/mailpit
environment:
- MP_SMTP_BIND_ADDR=0.0.0.0:25
labels:
- "traefik.enable=true"
- "project-name=${PROJECT_NAME}"
- "traefik.http.routers.${PROJECT_NAME}-mail.rule=Host(`mail.${PROJECT_ROOT_DOMAIN}`)"
- "traefik.http.routers.${PROJECT_NAME}-mail.tls=true"
- "traefik.http.services.mail.loadbalancer.server.port=8025"
profiles:
- defaultThen, you will be able to browse:
https://mail.In your application, you can use the following configuration:
smtp;mail;25.For example in Symfony you can use: MAILER_DSN=smtp://mail:25.
Read the cookbook
In order to use Mercure, you should add the following content to the
docker-compose.yml file:
services:
mercure:
image: dunglas/mercure
environment:
- "MERCURE_PUBLISHER_JWT_KEY=password"
- "MERCURE_SUBSCRIBER_JWT_KEY=password"
- "ALLOW_ANONYMOUS=1"
- "CORS_ALLOWED_ORIGINS=*"
labels:
- "traefik.enable=true"
- "project-name=${PROJECT_NAME}"
- "traefik.http.routers.${PROJECT_NAME}-mercure.rule=Host(`mercure.${PROJECT_ROOT_DOMAIN}`)"
- "traefik.http.routers.${PROJECT_NAME}-mercure.tls=true"
profiles:
- defaultIf you are using Symfony, you must put the following configuration in the .env file:
MERCURE_PUBLISH_URL=http://mercure/.well-known/mercure
MERCURE_JWT_TOKEN=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJtZXJjdXJlIjp7InN1YnNjcmliZSI6W10sInB1Ymxpc2giOltdfX0.t9ZVMwTzmyjVs0u9s6MI7-oiXP-ywdihbAfPlghTBeQRead the cookbook
In order to use redirection.io, you should add the following content to the
docker-compose.yml file to run the agent:
services:
redirectionio-agent:
build: services/redirectionio-agentAdd the following file infrastructure/docker/services/redirectionio-agent/Dockerfile:
…Add infrastructure/docker/services/redirectionio-agent/etc/redirectionio/agent.yml:
instance_name: "my-instance-dev" ### You may want to change this
listen: 0.0.0.0:10301Then you'll need wget. In
infrastructure/docker/services/php/Dockerfile, in stage frontend:
RUN apt-get update \
&& apt-get install -y --no-install-recommends \
wget \
&& apt-get clean \暂无开放 Issues,或尚未同步最近议题。