├── .gitignore ├── Makefile ├── README.md ├── docker-compose.yml.dist └── docker ├── database ├── Dockerfile └── testing.sql ├── nginx ├── Dockerfile └── default.conf └── php ├── Dockerfile └── xdebug.ini /.gitignore: -------------------------------------------------------------------------------- 1 | ###> Other files and folders ### 2 | .idea 3 | .vscode 4 | docker-compose.yml 5 | ###< Other files and folders ### 6 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | OS = $(shell uname) 4 | UID = $(shell id -u) 5 | DOCKER_BE = docker-dev-env-for-symfony-be 6 | 7 | help: ## Show this help message 8 | @echo 'usage: make [target]' 9 | @echo 10 | @echo 'targets:' 11 | @egrep '^(.+)\:\ ##\ (.+)' ${MAKEFILE_LIST} | column -t -c 2 -s ':#' 12 | 13 | start: ## Start the containers 14 | docker network create docker-dev-env-for-symfony-network || true 15 | cp -n docker-compose.yml.dist docker-compose.yml || true 16 | cp -n .env.dist .env || true 17 | U_ID=${UID} docker-compose up -d 18 | 19 | stop: ## Stop the containers 20 | U_ID=${UID} docker-compose stop 21 | 22 | restart: ## Restart the containers 23 | $(MAKE) stop && $(MAKE) start 24 | 25 | build: ## Rebuilds all the containers 26 | docker network create docker-dev-env-for-symfony-network || true 27 | cp -n docker-compose.yml.dist docker-compose.yml || true 28 | cp -n .env.dist .env || true 29 | U_ID=${UID} docker-compose build 30 | 31 | prepare: ## Runs backend commands 32 | $(MAKE) composer-install 33 | $(MAKE) migrations 34 | 35 | # Backend commands 36 | composer-install: ## Installs composer dependencies 37 | U_ID=${UID} docker exec --user ${UID} ${DOCKER_BE} composer install --no-interaction 38 | 39 | migrations: ## Installs composer dependencies 40 | U_ID=${UID} docker exec --user ${UID} ${DOCKER_BE} bin/console doctrine:migration:migrate -n --allow-no-migration 41 | 42 | be-logs: ## Tails the Symfony dev log 43 | U_ID=${UID} docker exec --user ${UID} ${DOCKER_BE} tail -f var/log/dev.log 44 | # End backend commands 45 | 46 | ssh-be: ## bash into the be container 47 | U_ID=${UID} docker exec -it --user ${UID} ${DOCKER_BE} bash 48 | 49 | code-style: ## Runs php-cs to fix code styling following Symfony rules 50 | U_ID=${UID} docker exec --user ${UID} ${DOCKER_BE} php-cs-fixer fix src --rules=@Symfony 51 | 52 | .PHONY: migrations 53 | 54 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # docker-dev-env-for-symfony 2 | 3 | This repository contains the basic configuration for a complete local environment for Symfony projects 4 | 5 | ### Content: 6 | - NGINX 1.19 container to handle HTTP requests 7 | - PHP 8.0.1 container to host your Symfony application 8 | - MySQL 8.0 container to store databases 9 | 10 | (feel free to update any version in `Dockerfiles` and ports in `docker-compose.yml`) 11 | 12 | ### Installation: 13 | - Run `make build` to create all containers 14 | - Run `make start` to spin up containers 15 | - Enter the PHP container with `make ssh-be` 16 | - Install your favourite Symfony version with `composer create-project symfony/skeleton project [version (e.g. 5.2.*)]` 17 | - Move the content to the root folder with `mv project/* . && mv project/.env .`. This is necessary since Composer won't install the project if the folder already contains data. 18 | - Copy the content from `project/.gitignore` and paste it in the root's folder `.gitignore` 19 | - Remove `project` folder (not needed anymore) 20 | - Navigate to `localhost:1000` so you can see the Symfony welcome page :) 21 | 22 | Happy coding! 23 | -------------------------------------------------------------------------------- /docker-compose.yml.dist: -------------------------------------------------------------------------------- 1 | version: '3.5' 2 | 3 | services: 4 | docker-dev-env-for-symfony-web: 5 | container_name: docker-dev-env-for-symfony-web 6 | build: 7 | context: ./docker/nginx 8 | args: 9 | UID: $U_ID 10 | ports: 11 | - 1000:80 12 | volumes: 13 | - ./public:/appdata/www/public 14 | depends_on: 15 | - docker-dev-env-for-symfony-be 16 | networks: 17 | - docker-dev-env-for-symfony-network 18 | 19 | docker-dev-env-for-symfony-be: 20 | container_name: docker-dev-env-for-symfony-be 21 | build: 22 | context: docker/php 23 | args: 24 | UID: $U_ID 25 | volumes: 26 | - ./:/appdata/www 27 | ###> XDEBUG 3 ### 28 | # Use your client IP here 29 | # Linux: run "ip a | grep docker0" 30 | # Windows (with WSL2) and Mac: host.docker.internal 31 | environment: 32 | XDEBUG_CLIENT_HOST: 172.17.0.1 33 | XDEBUG_CLIENT_PORT: 9003 34 | PHP_IDE_CONFIG: serverName=docker-dev-env-for-symfony 35 | networks: 36 | - docker-dev-env-for-symfony-network 37 | 38 | docker-dev-env-for-symfony-db: 39 | container_name: docker-dev-env-for-symfony-db 40 | build: 41 | context: ./docker/database 42 | ports: 43 | - 10000:3306 44 | environment: 45 | MYSQL_DATABASE: database 46 | MYSQL_ROOT_PASSWORD: root 47 | volumes: 48 | - docker-dev-env-for-symfony-db-data:/var/lib/mysql 49 | networks: 50 | - docker-dev-env-for-symfony-network 51 | command: ['mysqld', '--character-set-server=utf8mb4', '--collation-server=utf8mb4_unicode_ci'] 52 | 53 | volumes: 54 | docker-dev-env-for-symfony-db-data: 55 | 56 | networks: 57 | docker-dev-env-for-symfony-network: 58 | external: true 59 | -------------------------------------------------------------------------------- /docker/database/Dockerfile: -------------------------------------------------------------------------------- 1 | FROM mysql:8.0 2 | 3 | COPY ./testing.sql /docker-entrypoint-initdb.d/testing.sql 4 | -------------------------------------------------------------------------------- /docker/database/testing.sql: -------------------------------------------------------------------------------- 1 | CREATE DATABASE IF NOT EXISTS database_test CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci; 2 | -------------------------------------------------------------------------------- /docker/nginx/Dockerfile: -------------------------------------------------------------------------------- 1 | FROM nginx:1.19 2 | 3 | ARG UID 4 | 5 | RUN adduser -u ${UID} --disabled-password --gecos "" appuser 6 | 7 | COPY default.conf /etc/nginx/conf.d/ 8 | -------------------------------------------------------------------------------- /docker/nginx/default.conf: -------------------------------------------------------------------------------- 1 | server { 2 | listen 80; 3 | server_name localhost; 4 | root /appdata/www/public; 5 | 6 | location / { 7 | try_files $uri @rewriteapp; 8 | } 9 | 10 | location @rewriteapp { 11 | rewrite ^(.*)$ /index.php/$1 last; 12 | } 13 | 14 | location ~ ^/index\.php(/|$) { 15 | fastcgi_pass docker-dev-env-for-symfony-be:9000; 16 | fastcgi_split_path_info ^(.+\.php)(/.*)$; 17 | include fastcgi_params; 18 | fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; 19 | fastcgi_param HTTPS off; 20 | } 21 | 22 | error_log /var/log/nginx/register_service_error.log; 23 | access_log /var/log/nginx/register_service_access.log; 24 | } 25 | -------------------------------------------------------------------------------- /docker/php/Dockerfile: -------------------------------------------------------------------------------- 1 | FROM php:8.0.1-fpm 2 | 3 | ARG UID 4 | 5 | # Create user and some useful stuff 6 | RUN adduser -u ${UID} --disabled-password --gecos "" appuser 7 | RUN mkdir /home/appuser/.ssh 8 | RUN chown -R appuser:appuser /home/appuser/ 9 | RUN echo "StrictHostKeyChecking no" >> /home/appuser/.ssh/config 10 | RUN echo "export COLUMNS=300" >> /home/appuser/.bashrc 11 | RUN echo "alias sf=/appdata/www/bin/console" >> /home/appuser/.bashrc 12 | 13 | # Install packages and PHP extensions 14 | RUN apt update \ 15 | && apt install -y git acl openssl openssh-client wget zip vim librabbitmq-dev libssh-dev \ 16 | && apt install -y libpng-dev zlib1g-dev libzip-dev libxml2-dev libicu-dev \ 17 | && docker-php-ext-install intl pdo pdo_mysql zip soap bcmath sockets \ 18 | && pecl install xdebug \ 19 | && docker-php-ext-enable --ini-name 05-opcache.ini opcache xdebug 20 | 21 | # Install and update composer 22 | RUN curl https://getcomposer.org/composer.phar -o /usr/bin/composer && chmod +x /usr/bin/composer 23 | RUN composer self-update 24 | 25 | # Install PHP-CS-FIXER 26 | RUN wget https://cs.symfony.com/download/php-cs-fixer-v2.phar -O php-cs-fixer 27 | RUN chmod a+x php-cs-fixer 28 | RUN mv php-cs-fixer /usr/local/bin/php-cs-fixer 29 | 30 | RUN mkdir -p /appdata/www 31 | 32 | # Config XDEBUG 33 | COPY ./xdebug.ini /usr/local/etc/php/conf.d/xdebug.ini 34 | 35 | WORKDIR /appdata/www 36 | -------------------------------------------------------------------------------- /docker/php/xdebug.ini: -------------------------------------------------------------------------------- 1 | xdebug.mode=debug 2 | xdebug.start_with_request=yes 3 | xdebug.client_host=${XDEBUG_CLIENT_HOST} 4 | xdebug.client_port=${XDEBUG_CLIENT_PORT} 5 | --------------------------------------------------------------------------------