• Stars
    star
    169
  • Rank 224,453 (Top 5 %)
  • Language
    PHP
  • Created over 9 years ago
  • Updated over 3 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A template for docker based Yii 2 applications

Yii 2 Dockerized

A template for docker based Yii 2 applications.

  • Ephemeral container, configured via environment variables
  • Application specific base image (Nginx + PHP-FPM)
  • Optional local configuration overrides for development/debugging (git-ignored)
  • Base scaffold code for login, signup and forgot-password actions
  • Flat configuration file structure
  • Optional cron integration for periodic jobs

Note: The included example base image is now based on Alpine Linux and uses s6-overlay to supervise Nginx + PHP-FPM. You can of course change this to any setup you prefer. You find the "old" setup based on Apache and mod_php in the "apache" branch. Note though, that it's no longer maintained.

1 Main Concepts

1.1 Base Image

The core idea of this template is that you build a bespoke base image for your application that meets your project's exact requirements. This image contains:

  • PHP runtime environment (e.g. Nginx + PHP-FPM)
  • PHP extensions
  • Composer packages

The base image will hardly ever change unless

  • you want to upgrade to a newer PHP or Yii version or
  • you want to add a missing PHP extension or composer package.

Its configuration can be found in the ./build directory:

  • Dockerfile adds PHP extensions and required system packages
  • composer.json and composer.lock list composer packages

The actual app image extends from this base image and uses ./Dockerfile in the main directory. It basically only adds your app sources and productive PHP config on top.

In the recommended scenario you would build the base image once then upload it to your container registry and share it with your co-developers.

If you don't have a container registry you can still use our template. But then each developer in your team will have to build the same base image locally.

1.2 Runtime Configuration via Environment Variables

We follow docker's principle that containers are configured via environment variables. This only includes runtime configuration, of course: Things like whether to run in debug mode or DB credentials. Most other settings like for example URL rules will be hardcoded in ./config/web.php.

You should continue to follow this principle when developing your app. For more details also see our yii2-configloader that we use in this template.

Note: There's also one important main setting for php-fpm that affects how many children should be started. This depends on the RAM you have available. See PHP_FPM_MAX_CHILDREN in docker-compose-example.yml.

2 Initial Project Setup

2.1 Download Application Template

First fetch a copy of our application template, for example with git:

git clone https://github.com/codemix/yii2-dockerized.git myproject
rm -rf myproject/.git

You could also download the files as ZIP archive from GitHub.

2.2 Init Composer Packages

To manage composer packages We use a container that is based on the official composer image. First go to the ./build directory of the app:

cd myproject/build

To add a package run:

docker-compose run --rm composer require some/library

To update all packages run:

docker-compose run --rm composer update

This will update composer.json and composer.lock respectively. You can also run other composer commands, of course.

You now have to rebuild your base image! (see below).

Note: As docker's composer image may not meet the PHP requirements of all your packages you may have to add --ignore-platform-reqs to be able to install some packages.

2.3 Build the Base Image

Before you continue with building the base image you should:

  • Set a tag name for the base image in ./build/docker-compose.yml
  • Use the same tag name in ./Dockerfile in the main directory
  • Optionally add more PHP extensions or system packages in ./build/Dockerfile
  • Choose a timezone in ./build/Dockerfile. This is only really relevant if you want to enable crond, to let the jobs run at correct local times.

To start you first need to create an initial composer.lock. So go to the ./build directory and run:

docker-compose run --rm composer install

Then you can build the base image:

docker-compose build

Now you could upload that image to your container registry.

2.4 Cleanup and Initial Commit

At this point you may want to modify our application template, add some default configuration and remove those parts that you don't need. Afterwards you are ready for the initial commit to your project repository.

3 Local Development

During development we map the local app directory into the app image. This way we always run the code that we currently work on.

As your local docker setup may differ from production (e.g. use different docker network settings) we usually keep docker-compose.yml out of version control. Instead we provide a docker-compose-example.yml with a reasonable example setup.

Since the runtime configuration should happen with environment variables, we use a .env file. We also keep this out of version control and only include a .env-example to get developers started.

3.1 Initial Setup of a Development Environment

After you've cloned your project you need to prepare two files:

cd myproject
cp docker-compose-example.yml docker-compose.yml
cp .env-example .env

You should modify these two files e.g. to enable debug mode or configure a database. Then you should be ready to start your container and initialize your database (if your project has one):

docker-compose up -d
# Wait some seconds to let the DB container fire up ...
docker-compose exec web ./yii migrate

Finally you need to set write permissions for some directories. It's sufficient if the www-data group in the container has write permissions. This way your local user can still own these directories:

docker-compose exec web chgrp www-data web/assets runtime var/sessions
docker-compose exec web chmod g+rwx web/assets runtime var/sessions

When done, you can access the new app from http://localhost:8080.

3.2 Development Session

A development session will usually go like this:

docker-compose up -d
# edit files, check, tests, ...
git add .
git commit -m 'Implemented stuff'
docker-compose stop

Note: Another approach is to leave a terminal window open and start the container with

docker-compose up

This way you can always follow the live logs of your container. To stop all containers, press Ctrl-c.

3.2.1 Running yiic Commands

If you need to run yiic commands you execute them in the running development container:

docker-compose exec web ./yiic migrate/create add_column_name

Note: If a command creates new files on your host (e.g. a new migration) you may have to change file permissions to make them writeable on your host system:

chown -R mike migrations/*

3.2.2 Adding or Updating Composer Packages

The procedure for adding or updating composer packages is the same as described in 2.2 above. Remember that you have to rebuild the base image afterwards. It should probably also receive an updated version tag.

3.2.3 Working with Complex Local Configuration

Sometimes you may have to add complex parts to config/web.php but want to avoid the risk of accidentally committing them. You therefore can activate support for a config/local.php file in your .env. This config file will be merged into config/web.php.

# Whether to load config/(local|console-local).php. Default is 0.
ENABLE_LOCALCONF=1

3.2.3 Make Composer Packages Available Locally

For some IDEs it's useful to have the composer packages available on your local host system. To do so you can simply copy them from inside the container:

docker-compose exec web cp -rf /var/www/vendor ./

Note: Inside the container composer packages live in /var/www/vendor instead of the app's ./vendor directory. This way we don't override the vendor directory when we map the local app directory into the container.

3.2.3 Configuring Cron Jobs

To run periodic jobs the integrated crond (busybox implementation) can be activated by setting ENABLE_CROND=1 in the docker-compose.yml file.

Cron jobs are added in config/crontabs/<username>. There's an example file for www-data included. When changing a file there the container must be restarted to activate the crontab.

More Repositories

1

fast.js

Faster user-land reimplementations for several common builtin native JavaScript functions.
JavaScript
3,412
star
2

ts-sql

A SQL database implemented purely in TypeScript type annotations.
TypeScript
3,181
star
3

babel-plugin-typecheck

Static and runtime type checking for JavaScript in the form of a Babel plugin.
JavaScript
886
star
4

deprank

Use PageRank to find the most important files in your codebase.
TypeScript
878
star
5

yii2-localeurls

Automatic locale/language management for URLs
PHP
412
star
6

babel-plugin-closure-elimination

A Babel plugin which eliminates closures from your JavaScript wherever possible.
JavaScript
369
star
7

babel-plugin-contracts

Design by Contract for JavaScript via a Babel plugin.
JavaScript
266
star
8

babel-plugin-macros

Hygienic, non-syntactic macros for JavaScript via a Babel plugin.
JavaScript
261
star
9

oriento

Former official node.js driver for OrientDB. Fast, lightweight, uses the binary protocol. Now deprecated.
JavaScript
196
star
10

htmling

Polymer / HTML5 templating syntax for node.js
JavaScript
177
star
11

yii2-excelexport

A utility to quickly create Excel files from query results or raw data
PHP
102
star
12

gitignore-parser

A simple .gitignore parser for node.js
JavaScript
97
star
13

contractual

Unobtrusive, backwards compatible, syntactic sugar for Design by contract in JavaScript.
JavaScript
72
star
14

babel-plugin-trace

This is a Babel plugin which adds a straightforward, declarative syntax for adding debug logging to JavaScript applications.
JavaScript
63
star
15

yii2-configloader

Build configuration arrays from config files and env vars.
PHP
61
star
16

yii2-streamlog

A Yii 2 log target for streams in URL format
PHP
52
star
17

yii2-dockerbase

Yii 2 base image for dockerized yii2 projects
Shell
39
star
18

YiiElasticSearch

Elastic Search client for Yii
PHP
33
star
19

malloc

Simple malloc() & free() implementation for node.js, built on top of array buffers.
JavaScript
25
star
20

reign

A persistent, typed objects implementation for node.js and the browser.
JavaScript
23
star
21

binary-protocol

Easy, fast, writers and readers for implementing custom binary protocols in node.js.
JavaScript
20
star
22

oauth2yii

An OAuth2 client / server extension for the Yii framework
PHP
17
star
23

modeling

Fast and flexible data models for node.js and the browser.
JavaScript
15
star
24

restyii

A RESTful extension for Yii.
PHP
15
star
25

babel-plugin-hyperhtml

Babel plugin which compiles JSX into hyperHTML
JavaScript
12
star
26

yii2-excel-message

Translate messages via Excel files
PHP
12
star
27

backing

Provides a virtual address space for large segments of memory via JavaScript ArrayBuffers, and operations for allocating and freeing within the address space, optionally via a simple reference counting garbage collector.
JavaScript
11
star
28

validating

Quick and easy validators for node.js and the browser.
JavaScript
10
star
29

babel-plugin-conditional

Conditionally applies a set of babel plugins based on the result of an expression evaluated at runtime.
JavaScript
10
star
30

yii2-bs3activeform

A Bootstrap 3 enhanced ActiveForm for Yii 2
PHP
9
star
31

url-route

Web component providing URL routing
JavaScript
9
star
32

htmling-demo-app

HTMLing demo running on express
CSS
8
star
33

garbage-collector

A garbage collector for JavaScript built on top of typed arrays.
JavaScript
8
star
34

geonames-importer

Imports geonames data into elasticsearch
JavaScript
7
star
35

orientdb-protobufs

An experiment to see how the orientdb binary protocol could look if it used protocol buffers.
Java
6
star
36

handlebarsphp

Transpiles handlebars templates into native PHP templates
PHP
6
star
37

atomicbuffers

Atomic `readInt32()`, `writeInt32()`, `readUInt32()` and `writeUInt32()` for node.js buffers.
JavaScript
6
star
38

classing

Fluent classes for node.js and the browser.
JavaScript
6
star
39

dispatching

Tiny routing / dispatch library for node and the browser.
JavaScript
5
star
40

casting

Tiny type casting library for node.js and the browser.
JavaScript
5
star
41

php-orientdb

A fast PHP driver for the OrientDB binary protocol.
PHP
5
star
42

obligations

Tiny JavaScript library for preconditions and postconditions, intended for use with Contractual.
JavaScript
4
star
43

AccessRestrictable

A Yii ActiveRecordBehavior that automatically applies conditions for access restriction to every query.
PHP
2
star
44

component-testing-library

A library for testing component driven UIs
TypeScript
2
star
45

bootstrap-css

Twitter Bootstrap CSS / LESS packaged for component.js instead of bower
CSS
2
star
46

miming

Processing and formatting for various mime types.
JavaScript
2
star
47

bs3activeform

A lightweight utility to render Bootstrap 3 forms in Yii
PHP
2
star
48

handlebarsgen

An extendable static code generator for handlebars templates, targetting languages other than JavaScript, e.g. PHP
CoffeeScript
2
star
49

malloc-append

Simple append-only alloc() implementation on top of buffers and array buffers.
JavaScript
1
star
50

jsx-email-nextjs

Reproduce an error with renderToStaticMarkup() in Next.js
TypeScript
1
star
51

bootstrap-tooltip

Twitter Bootstrap Tooltip plugin packaged for component.js instead of bower
JavaScript
1
star
52

bencha

Mocha-esque UI for the excellent benchmarkjs benchmarking library
CoffeeScript
1
star
53

bootstrap-transition

Twitter Bootstrap Transition plugin packaged for component.js instead of bower
JavaScript
1
star
54

urlrouter

Tiny URL routing for the browser
CoffeeScript
1
star
55

bootstrap-affix

Twitter Bootstrap Affix plugin packaged for component.js instead of bower
JavaScript
1
star
56

bootstrap-scrollspy

Twitter Bootstrap Scrollspy plugin packaged for component.js instead of bower
JavaScript
1
star