Заготовка docker-compose.yml для Yii2

Практически у каждого разработчика, активно использующего Docker в своей работе, есть заготовки под разные проекты. Пример ниже является неплохой основой для проекта на Yii2 с использованием шаблона Basic, хотя его можно адаптировать для Advanced или под любую другую архитектуру.

version: '3'

networks:
  default:
    driver: bridge

services:
  php:
    image: yiisoftware/yii2-php:7.4-apache
    volumes:
      - ~/.composer-docker/cache:/root/.composer/cache:delegated
      - ./:/app:delegated
      - ./docker/php/xdebug.ini:/usr/local/etc/php/conf.d/xdebug.ini
      - ./docker/apache/000-default.conf:/etc/apache2/sites-available/000-default.conf
    environment:
      - PHP_ENABLE_XDEBUG=1
      - PHP_IDE_CONFIG=serverName=MyTestServer
    ports:
      - '80:80'
    networks:
      - default
    depends_on:
      - db
  db:
    image: mysql:5.7
    restart: always
    environment:
      - MYSQL_DATABASE=my_database_name
      - MYSQL_USER=my_user_name
      - MYSQL_PASSWORD=my_user_password
      - MYSQL_ROOT_PASSWORD=my_root_password
    ports:
      - '3306:3306'
    expose:
      - '3306'
    volumes:
      - "./docker/mysql:/var/lib/mysql"
    networks:
      - default
  phpmyadmin:
    image: phpmyadmin/phpmyadmin
    ports:
      - '8888:80'
    environment:
      - PMA_ARBITRARY=1
      - PMA_HOST=db
    depends_on:
      - db
Пример настройки xdebug’a 3-ей версии. ./docker/php/xdebug.ini
[xdebug]
xdebug.mode = debug
xdebug.client_host  = host.docker.internal
Пример настройки apacha’a. ./docker/apache/000-default.conf
<Directory /app/web/>
        Options Indexes FollowSymLinks
        AllowOverride All
        Require all granted
</Directory>
 
<VirtualHost *:80>
        # The ServerName directive sets the request scheme, hostname and port that
        # the server uses to identify itself. This is used when creating
        # redirection URLs. In the context of virtual hosts, the ServerName
        # specifies what hostname must appear in the request's Host: header to
        # match this virtual host. For the default virtual host (this file) this
        # value is not decisive as it is used as a last resort host regardless.
        # However, you must set it for any further virtual host explicitly.
        #ServerName www.example.com
 
        ServerAdmin webmaster@localhost
        DocumentRoot /app/web
 
        # Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
        # error, crit, alert, emerg.
        # It is also possible to configure the loglevel for particular
        # modules, e.g.
        #LogLevel info ssl:warn
 
        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined
 
        # For most configuration files from conf-available/, which are
        # enabled or disabled at a global level, it is possible to
        # include a line for only one particular virtual host. For example the
        # following line enables the CGI configuration for this host only
        # after it has been globally disabled with "a2disconf".
        #Include conf-available/serve-cgi-bin.conf
</VirtualHost>

Запуск проекта

docker-compose up -d

После успешного запуска всех контейнеров сайт будет доступен в браузере по адресу 127.0.0.1, он же localhost.

http://127.0.0.1

Подключение к базе данных

КОРЕНЬ_ПРОЕКТА\config\db.php
return [
    'class' => 'yii\db\Connection',
    'dsn' => 'mysql:host=db;dbname=my_database_name',
    'username' => 'my_user_name',
    'password' => 'my_user_password',
    'charset' => 'utf8',
];

PhpMyAdmin

PhpMyAdmin будет также доступен на localhost’e, только через 8888 порт.

http://localhost:8888/

Авторизация root’a:
Сервер: db
Пользователь: root
Пароль: my_root_password

Авторизация пользователя:
Сервер: db
Пользователь: my_user_name
Пароль: my_user_password

xDebug и Docker

Полезные ссылки

11.11.2020

Категория(-и): Docker, Yii

# #

Добавить комментарий