{"id":20963873,"url":"https://github.com/codemix/yii2-dockerized","last_synced_at":"2025-05-14T09:31:56.271Z","repository":{"id":27394098,"uuid":"30870378","full_name":"codemix/yii2-dockerized","owner":"codemix","description":"A template for docker based Yii 2 applications","archived":false,"fork":false,"pushed_at":"2021-02-22T10:42:19.000Z","size":206,"stargazers_count":166,"open_issues_count":1,"forks_count":51,"subscribers_count":15,"default_branch":"master","last_synced_at":"2023-11-07T15:23:11.202Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/codemix.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2015-02-16T13:55:00.000Z","updated_at":"2023-07-13T07:44:50.000Z","dependencies_parsed_at":"2022-08-20T07:50:11.272Z","dependency_job_id":null,"html_url":"https://github.com/codemix/yii2-dockerized","commit_stats":null,"previous_names":[],"tags_count":2,"template":null,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/codemix%2Fyii2-dockerized","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/codemix%2Fyii2-dockerized/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/codemix%2Fyii2-dockerized/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/codemix%2Fyii2-dockerized/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/codemix","download_url":"https://codeload.github.com/codemix/yii2-dockerized/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":225284594,"owners_count":17449933,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-19T02:48:41.998Z","updated_at":"2024-11-19T02:48:42.634Z","avatar_url":"https://github.com/codemix.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"Yii 2 Dockerized\n================\n\nA template for docker based Yii 2 applications.\n\n * Ephemeral container, configured via environment variables\n * Application specific base image (Nginx + PHP-FPM)\n * Optional local configuration overrides for development/debugging (git-ignored)\n * Base scaffold code for login, signup and forgot-password actions\n * Flat configuration file structure\n * Optional cron integration for periodic jobs\n\n\u003e **Note:** The included example base image is now based on Alpine Linux and\n\u003e uses [s6-overlay](https://github.com/just-containers/s6-overlay) to supervise\n\u003e Nginx + PHP-FPM. You can of course change this to any setup you prefer.\n\u003e You find the \"old\" setup based on Apache and mod_php in the \"apache\" branch.\n\u003e Note though, that it's no longer maintained.\n\n# 1 Main Concepts\n\n## 1.1 Base Image\n\nThe core idea of this template is that you build a bespoke **base image**\nfor your application that meets your project's exact requirements. This image\ncontains:\n\n * PHP runtime environment (e.g. Nginx + PHP-FPM)\n * PHP extensions\n * Composer packages\n\nThe base image will hardly ever change unless\n\n * you want to upgrade to a newer PHP or Yii version or\n * you want to add a missing PHP extension or composer package.\n\nIts configuration can be found in the `./build` directory:\n\n * `Dockerfile` adds PHP extensions and required system packages\n * `composer.json` and `composer.lock` list composer packages\n\nThe actual **app image** extends from this base image and uses `./Dockerfile`\nin the main directory. It basically only adds your app sources and productive\nPHP config on top.\n\nIn the recommended scenario you would build the base image once then upload\nit to your container registry and share it with your co-developers.\n\nIf you don't have a container registry you can still use our template. But then\neach developer in your team will have to build the same base image locally.\n\n## 1.2 Runtime Configuration via Environment Variables\n\nWe follow docker's principle that containers are configured via environment\nvariables. This only includes runtime configuration, of course: Things like\nwhether to run in debug mode or DB credentials. Most other settings like for\nexample URL rules will be hardcoded in `./config/web.php`.\n\nYou should continue to follow this principle when developing your app. For\nmore details also see our\n[yii2-configloader](https://github.com/codemix/yii2-configloader) that we use\nin this template.\n\n\u003e **Note:** There's also one important main setting for php-fpm that affects\n\u003e how many children should be started. This depends on the RAM you have\n\u003e available. See `PHP_FPM_MAX_CHILDREN` in `docker-compose-example.yml`.\n\n# 2 Initial Project Setup\n\n## 2.1 Download Application Template\n\nFirst fetch a copy of our application template, for example with git:\n\n```sh\ngit clone https://github.com/codemix/yii2-dockerized.git myproject\nrm -rf myproject/.git\n```\n\nYou could also download the files as ZIP archive from GitHub.\n\n## 2.2 Init Composer Packages\n\nTo manage composer packages We use a container that is based on the official\n[composer](https://hub.docker.com/r/library/composer/) image. First go to the\n`./build` directory of the app:\n\n```sh\ncd myproject/build\n```\n\nTo add a package run:\n\n```sh\ndocker-compose run --rm composer require some/library\n```\n\nTo update all packages run:\n\n```sh\ndocker-compose run --rm composer update\n```\n\nThis will update `composer.json` and `composer.lock` respectively. You can\nalso run other composer commands, of course.\n\n\n**You now have to rebuild your base image!** (see below).\n\n\n\u003e **Note:** As docker's composer image may not meet the PHP requirements of all\n\u003e your packages you may have to add `--ignore-platform-reqs` to be able to\n\u003e install some packages.\n\n\n## 2.3 Build the Base Image\n\nBefore you continue with building the base image you should:\n\n * Set a tag name for the base image in `./build/docker-compose.yml`\n * Use the same tag name in `./Dockerfile` in the main directory\n * Optionally add more PHP extensions or system packages in `./build/Dockerfile`\n * Choose a timezone in `./build/Dockerfile`. This is only really relevant if\n   you want to enable crond, to let the jobs run at correct local times.\n\nTo start you first need to create an initial `composer.lock`. So go to the\n`./build` directory and run:\n\n```sh\ndocker-compose run --rm composer install\n```\n\nThen you can build the base image:\n\n```sh\ndocker-compose build\n```\n\nNow you could upload that image to your container registry.\n\n## 2.4 Cleanup and Initial Commit\n\nAt this point you may want to modify our application template, add some default\nconfiguration and remove those parts that you don't need. Afterwards you are\nready for the initial commit to your project repository.\n\n\n# 3 Local Development\n\nDuring development we map the local app directory into the app image.\nThis way we always run the code that we currently work on.\n\nAs your local docker setup may differ from production (e.g. use different\ndocker network settings) we usually keep `docker-compose.yml` out of version\ncontrol. Instead we provide a `docker-compose-example.yml` with a reasonable\nexample setup.\n\nSince the runtime configuration should happen with environment variables, we\nuse a `.env` file. We also keep this out of version control and only include a\n`.env-example` to get developers started.\n\n## 3.1 Initial Setup of a Development Environment\n\nAfter you've cloned your project you need to prepare two files:\n\n```sh\ncd myproject\ncp docker-compose-example.yml docker-compose.yml\ncp .env-example .env\n```\n\nYou should modify these two files e.g. to enable debug mode or configure a\ndatabase. Then you should be ready to start your container and initialize\nyour database (if your project has one):\n\n```sh\ndocker-compose up -d\n# Wait some seconds to let the DB container fire up ...\ndocker-compose exec web ./yii migrate\n```\n\nFinally you need to set write permissions for some directories. It's sufficient\nif the `www-data` group in the container has write permissions. This way your\nlocal user can still own these directories:\n\n```sh\ndocker-compose exec web chgrp www-data web/assets runtime var/sessions\ndocker-compose exec web chmod g+rwx web/assets runtime var/sessions\n```\n\nWhen done, you can access the new app from\n[http://localhost:8080](http://localhost:8080).\n\n\n## 3.2 Development Session\n\nA development session will usually go like this:\n\n```sh\ndocker-compose up -d\n# edit files, check, tests, ...\ngit add .\ngit commit -m 'Implemented stuff'\ndocker-compose stop\n```\n\n\u003e **Note:** Another approach is to leave a terminal window open and start the\n\u003e container with\n\u003e\n\u003e     docker-compose up\n\u003e\n\u003e This way you can always follow the live logs of your container. To stop all\n\u003e containers, press `Ctrl-c`.\n\n### 3.2.1 Running yiic Commands\n\nIf you need to run yiic commands you execute them in the running development\ncontainer:\n\n```sh\ndocker-compose exec web ./yiic migrate/create add_column_name\n```\n\n\u003e **Note:** If a command creates new files on your host (e.g. a new migration)\n\u003e you may have to change file permissions to make them writeable on your host\n\u003e system:\n\u003e\n\u003e     chown -R mike migrations/*\n\u003e\n\n### 3.2.2 Adding or Updating Composer Packages\n\nThe procedure for adding or updating composer packages is the same as described\nin 2.2 above. Remember that you have to rebuild the base image afterwards. It\nshould probably also receive an updated version tag.\n\n### 3.2.3 Working with Complex Local Configuration\n\nSometimes you may have to add complex parts to `config/web.php` but want\nto avoid the risk of accidentally committing them. You therefore can activate\nsupport for a `config/local.php` file in your `.env`. This config file will\nbe merged into `config/web.php`.\n\n```sh\n# Whether to load config/(local|console-local).php. Default is 0.\nENABLE_LOCALCONF=1\n```\n\n### 3.2.3 Make Composer Packages Available Locally\n\nFor some IDEs it's useful to have the composer packages available on your local\nhost system. To do so you can simply copy them from inside the container:\n\n```\ndocker-compose exec web cp -rf /var/www/vendor ./\n```\n\n\u003e **Note:** Inside the container composer packages live in `/var/www/vendor`\n\u003e instead of the app's `./vendor` directory. This way we don't override the\n\u003e vendor directory when we map the local app directory into the container.\n\n\n### 3.2.3 Configuring Cron Jobs\n\nTo run periodic jobs the integrated crond (busybox implementation) can be\nactivated by setting `ENABLE_CROND=1` in the `docker-compose.yml` file.\n\nCron jobs are added in `config/crontabs/\u003cusername\u003e`. There's an example file\nfor `www-data` included. When changing a file there the container must be\nrestarted to activate the crontab.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcodemix%2Fyii2-dockerized","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcodemix%2Fyii2-dockerized","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcodemix%2Fyii2-dockerized/lists"}