├── app ├── public │ └── index.php ├── .gitignore ├── composer.json └── phpunit.xml ├── .env ├── .env.local ├── nginx ├── Dockerfile └── conf.d │ └── default.conf ├── bin └── dev-mode.sh ├── php ├── conf.d │ └── xdebug.ini └── Dockerfile ├── docker-compose.dev.yaml ├── .github └── workflows │ └── deploy.yaml ├── README.md ├── docker-compose.yaml ├── sql └── docker-php.sql └── .gitignore /app/public/index.php: -------------------------------------------------------------------------------- 1 | 2 | 13 | 14 | 15 | tests 16 | 17 | 18 | 19 | 21 | 22 | src 23 | 24 | 25 | 26 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Docker - PHP 2 | 3 | [Docker PHP][1] is a **Docker and PHP** repository which accompanies [a YouTube tutorial][2]. 4 | 5 | Setup 6 | ------------ 7 | 8 | * For a standard build / setup, simply run 9 | ```docker compose up -d ``` 10 | * For a development build which exposes DB ports and includes Xdebug, you can run the dev-mode shell script like so 11 | ```sh ./bin/dev-mode.sh -d``` 12 | * To run with Xdebug enabled, run 13 | ```XDEBUG_MODE=debug sh ./bin/dev-mode.sh -d --build``` 14 | 15 | 16 | Branches 17 | ------------- 18 | 19 | Each branch (except main, dev, and branches prefixed with 'feature') corresponds to an accompanying series lesson. 20 | 21 | Contributing 22 | ------------ 23 | 24 | Docker PHP is an Open Source project and contributions are welcome. The 'main' branch is read-only as this should not differ from the tutorials so please send pull requests to the develop branch. 25 | 26 | [1]: https://github.com/GaryClarke/docker-php 27 | [2]: https://youtu.be/qv-P_rPFw4c 28 | -------------------------------------------------------------------------------- /docker-compose.yaml: -------------------------------------------------------------------------------- 1 | services: 2 | # nginx 3 | web: 4 | image: garyclarke/nginx-php:1.0 5 | ports: 6 | - "80:80" 7 | # php 8 | app: 9 | image: garyclarke/php-composer:1.2 10 | environment: 11 | MYSQL_HOST: db 12 | MYSQL_PORT: ${MYSQL_PORT} 13 | MYSQL_PASSWORD: ${MYSQL_PASSWORD} 14 | MYSQL_DATABASE: ${MYSQL_DATABASE} 15 | MYSQL_USER: ${MYSQL_USER} 16 | REDIS_HOST: cache 17 | REDIS_PORT: ${REDIS_PORT} 18 | extra_hosts: 19 | # Ensure that host.docker.internal is correctly defined on Linux 20 | - host.docker.internal:host-gateway 21 | # mysql 22 | db: 23 | image: mysql:8.0 24 | volumes: 25 | - mysqldata:/var/lib/mysql 26 | restart: always 27 | environment: 28 | MYSQL_ROOT_PASSWORD: ${MYSQL_PASSWORD} 29 | MYSQL_USER: ${MYSQL_USER} 30 | MYSQL_PASSWORD: ${MYSQL_PASSWORD} 31 | MYSQL_DATABASE: ${MYSQL_DATABASE} 32 | # redis 33 | cache: 34 | image: redis:latest 35 | 36 | volumes: 37 | mysqldata: 38 | -------------------------------------------------------------------------------- /sql/docker-php.sql: -------------------------------------------------------------------------------- 1 | -- ------------------------------------------------------------- 2 | -- TablePlus 5.0.0(454) 3 | -- 4 | -- https://tableplus.com/ 5 | -- 6 | -- Database: docker-php 7 | -- Generation Time: 2022-10-28 20:09:23.6480 8 | -- ------------------------------------------------------------- 9 | 10 | 11 | /*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */; 12 | /*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */; 13 | /*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */; 14 | /*!40101 SET NAMES utf8mb4 */; 15 | /*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */; 16 | /*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */; 17 | /*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */; 18 | /*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */; 19 | 20 | 21 | CREATE TABLE `language` ( 22 | `id` int NOT NULL AUTO_INCREMENT, 23 | `name` varchar(32) NOT NULL, 24 | PRIMARY KEY (`id`) 25 | ); 26 | 27 | CREATE TABLE `translation` ( 28 | `id` int NOT NULL AUTO_INCREMENT, 29 | `language_id` int NOT NULL, 30 | `phrase` varchar(255) NOT NULL, 31 | `translation` varchar(255) NOT NULL, 32 | PRIMARY KEY (`id`), 33 | KEY `IDX_LANGUAGE_ID` (`language_id`), 34 | CONSTRAINT `FK_CONFERENCE_ID` FOREIGN KEY (`language_id`) REFERENCES `language` (`id`) 35 | ); 36 | 37 | INSERT INTO `language` (`id`, `name`) VALUES 38 | (1, 'French'), 39 | (2, 'German'), 40 | (3, 'Spanish'); 41 | 42 | INSERT INTO `translation` (`id`, `language_id`, `phrase`, `translation`) VALUES 43 | (1, 1, 'hello', 'bonjour'), 44 | (2, 2, 'hello', 'hallo'), 45 | (3, 3, 'hello', 'hola'); 46 | 47 | 48 | 49 | /*!40101 SET SQL_MODE=@OLD_SQL_MODE */; 50 | /*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */; 51 | /*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */; 52 | /*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */; 53 | /*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */; 54 | /*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */; 55 | /*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */; -------------------------------------------------------------------------------- /php/Dockerfile: -------------------------------------------------------------------------------- 1 | FROM php:8.1-fpm-alpine as app 2 | 3 | # Useful PHP extension installer image, copy binary into your container 4 | COPY --from=mlocati/php-extension-installer /usr/bin/install-php-extensions /usr/local/bin/ 5 | 6 | # Install php extensions 7 | # exit on errors, exit on unset variables, print every command as it is executed 8 | RUN set -eux; \ 9 | install-php-extensions pdo pdo_mysql; 10 | 11 | # RUN docker-php-ext-install pdo pdo_mysql 12 | 13 | # allow super user - set this if you use Composer as a 14 | # super user at all times like in docker containers 15 | ENV COMPOSER_ALLOW_SUPERUSER=1 16 | 17 | # obtain composer using multi-stage build 18 | # https://docs.docker.com/build/building/multi-stage/ 19 | COPY --from=composer:2.4 /usr/bin/composer /usr/bin/composer 20 | 21 | #Here, we are copying only composer.json and composer.lock (instead of copying the entire source) 22 | # right before doing composer install. 23 | # This is enough to take advantage of docker cache and composer install will 24 | # be executed only when composer.json or composer.lock have indeed changed!- 25 | # https://medium.com/@softius/faster-docker-builds-with-composer-install-b4d2b15d0fff 26 | COPY ./app/composer.* ./ 27 | 28 | # install 29 | RUN composer install --prefer-dist --no-dev --no-scripts --no-progress --no-interaction 30 | 31 | # copy application files to the working directory 32 | COPY ./app . 33 | 34 | # run composer dump-autoload --optimize 35 | RUN composer dump-autoload --optimize 36 | 37 | # Dev image 38 | # This stage is meant to be target-built into a separate image 39 | # https://docs.docker.com/develop/develop-images/multistage-build/#stop-at-a-specific-build-stage 40 | # https://docs.docker.com/compose/compose-file/#target 41 | FROM app as app_dev 42 | 43 | # Xdebug has different modes / functionalities. We can default to 'off' and set to 'debug' 44 | # when we run docker compose up if we need it 45 | ENV XDEBUG_MODE=off 46 | 47 | # Copy xdebug config file into container 48 | COPY ./php/conf.d/xdebug.ini /usr/local/etc/php/conf.d/xdebug.ini 49 | 50 | # Install xdebug 51 | RUN set -eux; \ 52 | install-php-extensions xdebug 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Created by https://www.toptal.com/developers/gitignore/api/phpstorm 2 | # Edit at https://www.toptal.com/developers/gitignore?templates=phpstorm 3 | 4 | ### PhpStorm ### 5 | # Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio, WebStorm and Rider 6 | # Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839 7 | 8 | # User-specific stuff 9 | .idea/**/workspace.xml 10 | .idea/**/tasks.xml 11 | .idea/**/usage.statistics.xml 12 | .idea/**/dictionaries 13 | .idea/**/shelf 14 | 15 | # AWS User-specific 16 | .idea/**/aws.xml 17 | 18 | # Generated files 19 | .idea/**/contentModel.xml 20 | 21 | # Sensitive or high-churn files 22 | .idea/**/dataSources/ 23 | .idea/**/dataSources.ids 24 | .idea/**/dataSources.local.xml 25 | .idea/**/sqlDataSources.xml 26 | .idea/**/dynamic.xml 27 | .idea/**/uiDesigner.xml 28 | .idea/**/dbnavigator.xml 29 | 30 | # Gradle 31 | .idea/**/gradle.xml 32 | .idea/**/libraries 33 | 34 | # Gradle and Maven with auto-import 35 | # When using Gradle or Maven with auto-import, you should exclude module files, 36 | # since they will be recreated, and may cause churn. Uncomment if using 37 | # auto-import. 38 | # .idea/artifacts 39 | # .idea/compiler.xml 40 | # .idea/jarRepositories.xml 41 | # .idea/modules.xml 42 | # .idea/*.iml 43 | # .idea/modules 44 | # *.iml 45 | # *.ipr 46 | 47 | # CMake 48 | cmake-build-*/ 49 | 50 | # Mongo Explorer plugin 51 | .idea/**/mongoSettings.xml 52 | 53 | # File-based project format 54 | *.iws 55 | 56 | # IntelliJ 57 | out/ 58 | 59 | # mpeltonen/sbt-idea plugin 60 | .idea_modules/ 61 | 62 | # JIRA plugin 63 | atlassian-ide-plugin.xml 64 | 65 | # Cursive Clojure plugin 66 | .idea/replstate.xml 67 | 68 | # SonarLint plugin 69 | .idea/sonarlint/ 70 | 71 | # Crashlytics plugin (for Android Studio and IntelliJ) 72 | com_crashlytics_export_strings.xml 73 | crashlytics.properties 74 | crashlytics-build.properties 75 | fabric.properties 76 | 77 | # Editor-based Rest Client 78 | .idea/httpRequests 79 | 80 | # Android studio 3.1+ serialized cache file 81 | .idea/caches/build_file_checksums.ser 82 | 83 | ### PhpStorm Patch ### 84 | # Comment Reason: https://github.com/joeblau/gitignore.io/issues/186#issuecomment-215987721 85 | 86 | # *.iml 87 | # modules.xml 88 | # .idea/misc.xml 89 | # *.ipr 90 | 91 | # Sonarlint plugin 92 | # https://plugins.jetbrains.com/plugin/7973-sonarlint 93 | .idea/**/sonarlint/ 94 | 95 | # SonarQube Plugin 96 | # https://plugins.jetbrains.com/plugin/7238-sonarqube-community-plugin 97 | .idea/**/sonarIssues.xml 98 | 99 | # Markdown Navigator plugin 100 | # https://plugins.jetbrains.com/plugin/7896-markdown-navigator-enhanced 101 | .idea/**/markdown-navigator.xml 102 | .idea/**/markdown-navigator-enh.xml 103 | .idea/**/markdown-navigator/ 104 | 105 | # Cache file creation bug 106 | # See https://youtrack.jetbrains.com/issue/JBR-2257 107 | .idea/$CACHE_FILE$ 108 | 109 | # CodeStream plugin 110 | # https://plugins.jetbrains.com/plugin/12206-codestream 111 | .idea/codestream.xml 112 | 113 | # Azure Toolkit for IntelliJ plugin 114 | # https://plugins.jetbrains.com/plugin/8053-azure-toolkit-for-intellij 115 | .idea/**/azureSettings.xml 116 | 117 | # End of https://www.toptal.com/developers/gitignore/api/phpstorm 118 | 119 | .idea 120 | 121 | branches.txt --------------------------------------------------------------------------------