Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
/symfony-dockerPublic template

Symfony 6 setup with Docker and Traefik

License

NotificationsYou must be signed in to change notification settings

ToshY/symfony-docker

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

82 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Code styleMess detectorStatic analysisUnit testsSecurity

A webapp starting template forSymfony 6 withDocker Compose.

📜 Introduction

This repository acts as a template to set up basic Symfony webapp with docker compose and Traefik.

🧰 Prerequisites

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.

🎬 Get Started

Update hosts file

Addwebapp.local to your hosts files, e.g./etc/hosts (Unix).

Initialise dotenv

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.

Start application services

task up

Visit the application

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.

📚 Additional services

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.

Mail

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 ###

Usage

You can now access Mailcrab at localhost:1080.

Redis

Want to cache in-memory? UseRedis.

Compose configuration

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

Environment variables

.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 ###

Usage

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.

Messenger & RabbitMQ

Want to handle data asynchronously? UseRabbitMQ.

Dependencies

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 configuration

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

Environment variables

.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 ###

Usage

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

🛠️ Contribute

Install dependencies

task app:install:dev

Enable GrumPHP

task grum:init

Manual test run

task contribute

🧰 DIY

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).

❕ Licence

This repository comes with aMIT license.


[8]ページ先頭

©2009-2025 Movatter.jp