Skip to content

Commit fbfd28a

Browse files
committed
add docker support
1 parent d0a5df2 commit fbfd28a

File tree

18 files changed

+547
-14
lines changed

18 files changed

+547
-14
lines changed

.env-dist

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
## Built image name
2+
PHP_IMAGE_NAME=local/yii2-php
3+
PHP_IMAGE_VERSION_SUFFIX=
4+
5+
## PHP-fpm & nginx
6+
COMPOSE_PROJECT_NAME=yii2fpm
7+
COMPOSE_FILE=docker-compose.yml
8+
DOCKERFILE_FLAVOUR=debian
9+
PHP_BASE_IMAGE_VERSION=7.3-fpm
10+
11+
## Runtime settings
12+
# Xdebug (calling the xdebug.remote_host on 9005 port)
13+
PHP_ENABLE_XDEBUG=0

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,5 @@ phpunit.phar
3333

3434
# vagrant runtime
3535
/.vagrant
36-
/node_modules
36+
/node_modules
37+
/mysql/data

README.md

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
Yii2-app is Fast and Ready-to-production advanced project template.
44

5+
Dockerized, for development- mysql, nginx, php-fpm containers
6+
57
For you, I downgrade requirement to PHP v5.6. But **PHP 7.1** is better! because it is really fast.
68

79
Please, [enable php intl extension](http://php.net/manual/en/intl.installation.php) for better work.
@@ -49,6 +51,23 @@ composer create-project --prefer-dist akiraz2/yii2-app my-site
4951
```
5052
After installation run `init`
5153

54+
## Docker
55+
Install yii2-app using [Docker](https://www.docker.com):
56+
57+
0. copy `.env-dist` to `.env`, configure if needed
58+
1. run command to create project
59+
```
60+
docker run --rm --interactive --tty \
61+
--volume $PWD:/app \
62+
--volume ${COMPOSER_HOME:-$HOME/.composer}:/tmp \
63+
composer create-project --prefer-dist akiraz2/yii2-app my-site
64+
```
65+
2. copy `/mysql/docker-entrypoint-initdb.d/createdb.sql.example` to `createdb.sql`, then edit this file (database, user) from `docker-compose.yml` section db (MYSQL_USER: username, MYSQL_PASSWORD: password, MYSQL_DATABASE: dbname)
66+
3. `docker-compose build`
67+
4. `docker-compose up -d`
68+
5. `docker-compose exec php bash`, in terminal run `php init`, then run other migrations (see next)
69+
6. open localhost:8100 to test
70+
5271
### Migrations
5372

5473
> **NOTE:** Make sure that you have properly configured `db` application component and run the following command
@@ -59,21 +78,9 @@ php yii migrate --migrationPath=@yii/log/migrations/
5978
php yii migrate --migrationPath=vendor/ignatenkovnikita/yii2-queuemanager/migrations/
6079
php yii migrate/up
6180
```
62-
63-
### First Steps
64-
65-
1. You need to create default domain `site.ru` and admin sub-domain `backend.site.ru`, for example. `.htaccess` for apache in `web` folder, or see next chapter `web server config`
66-
2. Read docs of yii2-user module -https://github.com/dektrium/yii2-user/blob/master/docs/README.md
67-
2.1. for example - URL of Login and Registration - https://github.com/dektrium/yii2-user/blob/master/docs/available-actions.md
68-
2.2. Overriding models, views and controllers - also in docs!
69-
2.3. Need RBAC? ok, https://github.com/dektrium/yii2-rbac
70-
71-
72-
7381
### Web server config
7482

75-
For newbies, I will recommend to read these instructions [yiisoft/yii2-app-advanced/start-installation.md](https://github.com/yiisoft/yii2-app-advanced/blob/master/docs/guide/start-installation.md) (apache, nginx, etc\hosts)
76-
83+
For newbies, I will recommend to read these instructions [yiisoft/yii2-app-advanced/start-installation.md](https://github.com/yiisoft/yii2-app-advanced/blob/master/docs/guide/start-installation.md) (apache, nginx, etc\hosts
7784

7885
## Development
7986

docker-compose.yml

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
version: '3'
2+
services:
3+
web:
4+
build:
5+
dockerfile: Dockerfile
6+
context: 'nginx'
7+
ports:
8+
- '8100:80'
9+
volumes:
10+
# Application testing
11+
- ./frontend/web:/app/frontend/web:delegated
12+
php:
13+
build:
14+
dockerfile: Dockerfile-${DOCKERFILE_FLAVOUR}
15+
context: 'php'
16+
args:
17+
- PHP_BASE_IMAGE_VERSION=${PHP_BASE_IMAGE_VERSION}
18+
image: ${PHP_IMAGE_NAME}:${PHP_BASE_IMAGE_VERSION}${PHP_IMAGE_VERSION_SUFFIX}
19+
environment:
20+
- GITHUB_API_TOKEN=${GITHUB_API_TOKEN}
21+
- PHP_ENABLE_XDEBUG
22+
- TEST_RUNTIME_PATH=/tmp/runtime
23+
volumes:
24+
- ./:/app:delegated
25+
# Composer cache
26+
- ~/.composer/cache:/root/.composer/cache:delegated
27+
links:
28+
- db
29+
db:
30+
image: mysql:5.7
31+
environment:
32+
MYSQL_ROOT_PASSWORD: root
33+
MYSQL_USER: username
34+
MYSQL_PASSWORD: password
35+
MYSQL_DATABASE: dbname
36+
volumes:
37+
- ./mysql/data:/var/lib/mysql
38+
- ./mysql/docker-entrypoint-initdb.d:/docker-entrypoint-initdb.d
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
*.sql
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#
2+
# Copy createdb.sql.example to createdb.sql
3+
# then uncomment then set database name and username to create you need databases
4+
#
5+
# example: .env MYSQL_USER=appuser and needed db name is myshop_db
6+
#
7+
# CREATE DATABASE IF NOT EXISTS `myshop_db` ;
8+
# GRANT ALL ON `myshop_db`.* TO 'appuser'@'%' ;
9+
#
10+
#
11+
# this sql script will auto run when the mysql container starts and the $DATA_PATH_HOST/mysql not found.
12+
#
13+
# if your $DATA_PATH_HOST/mysql exists and you do not want to delete it, you can run by manual execution:
14+
#
15+
# docker-compose exec mysql bash
16+
# mysql -u root -p < /docker-entrypoint-initdb.d/createdb.sql
17+
#
18+
CREATE DATABASE IF NOT EXISTS `dbname` COLLATE 'utf8_general_ci' ;
19+
GRANT ALL ON `dbname`.* TO 'username'@'%' ;
20+
21+
FLUSH PRIVILEGES ;

nginx/Dockerfile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
FROM nginx
2+
COPY nginx.conf /etc/nginx/nginx.conf

nginx/gzip.conf

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
gzip on;
2+
gzip_proxied any;
3+
gzip_types text/plain text/xml text/css application/javascript application/x-javascript;
4+
gzip_vary on;
5+
gzip_disable "MSIE [1-6]\.(?!.*SV1)";

nginx/nginx.conf

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
user nginx;
2+
worker_processes 1;
3+
4+
error_log /var/log/nginx/error.log warn;
5+
pid /var/run/nginx.pid;
6+
7+
events {
8+
worker_connections 1024;
9+
}
10+
11+
12+
http {
13+
include /etc/nginx/mime.types;
14+
default_type application/octet-stream;
15+
16+
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
17+
'$status $body_bytes_sent "$http_referer" '
18+
'"$http_user_agent" "$http_x_forwarded_for"';
19+
20+
log_format logstash_json '{ "@timestamp": "$time_iso8601", '
21+
'"@fields": { '
22+
'"remote_addr": "$remote_addr", '
23+
'"remote_user": "$remote_user", '
24+
'"body_bytes_sent": "$body_bytes_sent", '
25+
'"request_time": "$request_time", '
26+
'"status": "$status", '
27+
'"request": "$request", '
28+
'"request_method": "$request_method", '
29+
'"http_x_forwarded_for": "$http_x_forwarded_for", '
30+
'"http_referrer": "$http_referer", '
31+
'"http_user_agent": "$http_user_agent" } }';
32+
33+
access_log /var/log/nginx/access.log logstash_json;
34+
35+
sendfile on;
36+
#tcp_nopush on;
37+
38+
keepalive_timeout 65;
39+
40+
#gzip on;
41+
42+
server {
43+
charset utf-8;
44+
client_max_body_size 512M;
45+
server_name localhost;
46+
47+
listen 80;
48+
49+
root /app/frontend/web;
50+
index index.php;
51+
52+
location / {
53+
# Redirect everything that isn't a real file to index.php
54+
try_files $uri $uri/ /index.php?$args;
55+
}
56+
57+
# uncomment to avoid processing of calls to non-existing static files by Yii
58+
location ~ \.(js|css|png|jpg|gif|swf|ico|pdf|mov|fla|zip|rar)$ {
59+
try_files $uri =404;
60+
}
61+
62+
location ~ \.php$ {
63+
include /etc/nginx/fastcgi_params;
64+
fastcgi_param SCRIPT_FILENAME $document_root/$fastcgi_script_name;
65+
fastcgi_pass php:9000;
66+
try_files $uri =404;
67+
}
68+
69+
location ~ /\.(ht|svn|git) {
70+
deny all;
71+
}
72+
}
73+
74+
include /etc/nginx/conf.d/*.conf;
75+
}

nginx/run.sh

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#!/bin/bash
2+
3+
# Fail on any error
4+
set -o errexit
5+
6+
# Example of using environment variable in configuration at runtime
7+
if [ ! -n "$NGINX_ERROR_LOG_LEVEL" ] ; then
8+
NGINX_ERROR_LOG_LEVEL="warn"
9+
fi
10+
sed -i "s|\${NGINX_ERROR_LOG_LEVEL}|${NGINX_ERROR_LOG_LEVEL}|" /etc/nginx/nginx.conf
11+
12+
if [ ! -n "$SERVER_NAME" ] ; then
13+
SERVER_NAME="app"
14+
fi
15+
sed -i "s|\${SERVER_NAME}|${SERVER_NAME}|" /etc/nginx/nginx.conf
16+
17+
if [ ! -n "$FASTCGI_PASS_HOST" ] ; then
18+
FASTCGI_PASS_HOST="phpfpm"
19+
fi
20+
sed -i "s|\${FASTCGI_PASS_HOST}|${FASTCGI_PASS_HOST}|" /etc/nginx/nginx.conf
21+
22+
# Run nginx
23+
nginx -g 'daemon off;'

0 commit comments

Comments
 (0)