In order to quickly raise the working environment there are many ways. One of them is to pick up all the necessary services in the Docker containers. To speed up the creation of new projects on the Yii-framework, I wrote such a small instruction that is used by the developers in our team.
At the start you should have docker, docker-compose, php and php-composer.
Create a folder with the project and the docker folder in it.
mkdir project-dir cd project-dir && mkdir docker
In the docker folder, create a configuration file for our Dockerfile container.
# nginx php FROM richarvey/nginx-php-fpm # ADD app /var/www/app # RUN rm -Rf /etc/nginx/sites-enabled/* # ADD docker/conf/nginx/site.conf /etc/nginx/sites-available/site.conf # RUN ln -s /etc/nginx/sites-available/site.conf /etc/nginx/sites-enabled/site.conf
In the same docker folder, create docker-compose.yml to raise the development environment.
# docker-compose version: '3' # deafult networks: default: driver: bridge # services: # - app: # Dockerfile build: # context: ../ dockerfile: ./docker/Dockerfile # 80 ports: - "80:80" # networks: - default # db depends_on: - db # volumes: - "../app:/var/www/app" # nginx - "./conf/nginx:/etc/nginx/sites-available" # db: image: mysql:latest # networks: - default # ports: - "3336:3306" # environment: # MYSQL_ROOT_PASSWORD: root # MYSQL_DATABASE: yii-template-db # volumes: - "./database:/var/lib/mysql"
For nginx, create the docker / conf / nginx folder and the site.conf file in it. The file may change, depending on how you want to configure nginx on your project. It can be changed locally, since It connects through volume. But do not forget to reload nginx inside the container: nginx -s reload
server { charset utf-8; client_max_body_size 128M; listen 80; ## listen for ipv4 root /var/www/app/frontend/web/; index index.php; access_log /var/www/app/log/frontend-access.log; error_log /var/www/app/log/frontend-error.log; location / { try_files $uri $uri/ /index.php$is_args$args; } # uncomment to avoid processing of calls to non-existing static files by Yii #location ~ \.(js|css|png|jpg|gif|swf|ico|pdf|mov|fla|zip|rar)$ { # try_files $uri =404; #} #error_page 404 /404.html; # deny accessing php files for the /assets directory location ~ ^/assets/.*\.php$ { deny all; } location ~ \.php$ { include fastcgi_params; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_pass unix:/var/run/php-fpm.sock; try_files $uri =404; } location ~* /\. { deny all; } }
All commands are executed from the root folder.
composer create-project --prefer-dist yiisoft/yii2-app-advanced app
.docker-compose -f docker/docker-compose.yml up -d
environment docker-compose -f docker/docker-compose.yml up -d
app/init --env=Development --overwrite=All
docker exec -it docker_app_1 bash
container docker exec -it docker_app_1 bash
php /var/www/app/yii migrate
mkdir /var/www/app/log
exit
docker-compose -f docker/docker-compose.yml down
docker-compose -f docker/docker-compose.yml up -d
Upd: It is definitely worth mentioning that there is always an official Docker image of Yii2 .
Source: https://habr.com/ru/post/428688/
All Articles