- Notifications
You must be signed in to change notification settings - Fork0
Symfony 6 setup with Docker and Traefik
License
ToshY/symfony-docker
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
This repository acts as a template to set up basic Symfony webapp with docker compose and Traefik.
- Docker Compose (v2.21.0+)
- Task
- Reverse proxy | Traefik (Optional)
- It is assumed that the user has a working development setup forTraefik.
Tip
You can switch out Traefik for any other reverse proxy of your choice (or not use a reverse proxy at all), althoughthis requires additional tweaking of labels (or exposing ports) in the docker compose configuration.
Addwebapp.local
to your hosts files, e.g./etc/hosts
(Unix).
For first time setup, initialise the.env.local
from the.env
.
task init
You can now tweak the values in the.env.local
if needed.
task up
If the reverse proxy is configured correctly, you should be able to visitwebapp.local
in your browser and begreeted by Symfony's default landing page.
Note
You can disregard the SSL certificate warnings for development usages.
You can add additional services to eithercompose.yaml
orcompose.override.yaml
(specifically for your currentenvironment).
The following services are commonly used in a Symfony web application and serve as examples on how you can use additional docker containers in your stack.
Important
Make sure you runtask docker:up
(ortask docker:recreate
) after adding new services.
Want to test if your mails are send and displayed correctly in development? UseMailcrab.
compose.override.yaml
services:mailcrab:image:marlonb/mailcrab:${MAILCRAB_IMAGE_VERSION}ports: -"1080:1080"expose: -1025networks: -webapp -proxynetworks:webapp:driver:bridgeproxy:external:true
.env.local
###> IMAGE VERSIONS ###MAILCRAB_IMAGE_VERSION="v1.4.0"###< IMAGE VERSIONS ######> symfony/mailjet-mailer ###MAILER_DSN="smtp://mailcrab:1025"###< symfony/mailjet-mailer ###
You can now access Mailcrab at localhost:1080.
Want to cache in-memory? UseRedis.
compose.yaml
x-restart-always:&restart-alwaysrestart:alwaysservices:redis:image:redis:${REDIS_IMAGE_VERSION}command:redis-server --include /usr/local/etc/redis/redis.conf --requirepass ${REDIS_PASSWORD}<<:[*restart-always]environment:REDISCLI_AUTH:${REDIS_PASSWORD}expose: -6379volumes: -redis_data:/data -./.docker/redis/redis.conf:/usr/local/etc/redis/redis.confnetworks: -webappvolumes:redis_data:driver:localnetworks:webapp:driver:bridge
.env
###> IMAGE VERSIONS ###REDIS_IMAGE_VERSION="7.0-alpine"###< IMAGE VERSIONS ######> redis ###REDIS_USER="default"REDIS_PASSWORD="password"REDIS_DSN="redis:${REDIS_USER}:${REDIS_PASSWORD}@?host[redis:6379]&dbindex=1"###< redis ###
You can either runtask
commands to runredis-cli
commands inside the Redis container (see.tasks/redis/Taskfile.yml
), or you canexpose the port to the host and use external tools to access the Redis container throughlocalhost
.
For the latter case, this would require additional entry to thecompose.override.yaml
(create if not exists).
compose.override.yaml
services:redis:ports: -"6379:6379"
You can now access Redis at localhost:6379.
Tip
UseRedis Insight (v2) to view the data in your Redis container.
Want to handle data asynchronously? UseRabbitMQ.
Install the following PHP dependencies to use AMQP messenger with Symfony:
task composer:require -- symfony/messenger:^6.4.*task composer:require -- symfony/amqp-messenger:^6.4.*
compose.yaml
x-restart-always:&restart-alwaysrestart:alwaysx-restart-unless-stopped:&restart-unless-stoppedrestart:unless-stoppedservices:messenger:extends:file:compose.common.yamlservice:phpdepends_on:phpfpm:condition:service_startedrabbitmq:condition:service_healthy<<:[*restart-unless-stopped]command:bin/console messenger:consume --time-limit=300 --quiet asynclogging:options:max-size:"25mb"max-file:"10"rabbitmq:image:rabbitmq:${RABBITMQ_IMAGE_VERSION}hostname:rabbitmqsecurity_opt: -no-new-privileges:true<<:[*restart-always]environment:RABBITMQ_DEFAULT_USER:${MESSENGER_TRANSPORT_USER}RABBITMQ_DEFAULT_PASS:${MESSENGER_TRANSPORT_SECRET}healthcheck:test:["CMD-SHELL", "if rabbitmqctl status; then \nexit 0 \nfi \nexit 1"]interval:5stimeout:5sretries:5expose: -5672 -15672volumes: -./.docker/rabbitmq/enabled_plugins:/etc/rabbitmq/enabled_plugins -./.docker/rabbitmq/rabbitmq.conf:/etc/rabbitmq/rabbitmq.conf -rabbitmq_data:/var/lib/rabbitmqlabels:traefik.enable:truetraefik.docker.network:proxytraefik.http.routers.webapp-rabbitmq.tls:truetraefik.http.routers.webapp-rabbitmq.entrypoints:websecuretraefik.http.routers.webapp-rabbitmq.rule:Host(`rabbitmq.${APP_DOMAIN}`)traefik.http.routers.webapp-rabbitmq.service:webapp-rabbitmqtraefik.http.services.webapp-rabbitmq.loadbalancer.server.port:15672networks: -webapp -proxyvolumes:rabbitmq_data:driver:localnetworks:webapp:driver:bridgeproxy:external:true
.env
###> IMAGE VERSIONS ###RABBITMQ_IMAGE_VERSION="3-management"###< IMAGE VERSIONS ######> symfony/messenger ###MESSENGER_TRANSPORT_USER="default"MESSENGER_TRANSPORT_SECRET="password"MESSENGER_TRANSPORT_DSN="amqp://${MESSENGER_TRANSPORT_USER}:${MESSENGER_TRANSPORT_SECRET}@rabbitmq:5672/%2f/messages"###< symfony/messenger ###
You can now access the RabbitMQ panel atrabbitmq.webapp.local.
Tip
You can scale themessenger
service to use multiple containers to handle messages even faster!
Scalemessenger
service with5
containers:
docker compose up -d messenger --scale messenger=5
task app:install:dev
task grum:init
task contribute
If you want to create a Symfony project from scratch yourself, with the essential dependencies, youcan do the following:
# Substitute "dev.example.com" with desired project directory namedocker run --rm -it -v$(pwd):/app composer:2 create-project symfony/skeleton:6.4.* dev.example.comdocker run --rm -it -v$(pwd)/dev.example.com:/app composer:2 require webapp -nsudo chown -R$(id -u):$(id -g)$(pwd)/dev.example.com
This will give you a basic Symfony webapp environment (without Docker).
This repository comes with aMIT license.
About
Symfony 6 setup with Docker and Traefik