Macklus.Net

Development with Yii2 and docker-compose

Yii Framework is one of the most commonly used PHP Framework today. Its ease of use, speed and its many extensions make it a very serious option to consider when developing in PHP.

To further facilitate the development with Yii we can use an environment created with Docker compose. Using Docker compose we reduce the time needed to start working on a project, we standardize our development environment and we can even expand it very easily to add features such as Codeception.

Requirements

In order to raise our environment we will need:

Creating the Yii application

First, let’s create a Yii application on our local machine, with the commands:

composer global require "fxp/composer-asset-plugin:^1.4.1"
composer create-project --prefer-dist yiisoft/yii2-app-basic basic

Once the application is installed, within the directory we will find the file docker-compose.yml, which defines the structure we want to create, and which by default contains:

version: '2'
services:
  php:
    image: yiisoftware/yii2-php:7.1-apache
    volumes:
      - ~/.composer-docker/cache:/root/.composer/cache:delegated
      - ./:/app:delegated
    ports:
      - '8000:80'

In this base configuration there are several things that have not just convinced me:

So in my case, the first thing I do is put an adapted configuration, like the one I show you here:

version: '2'
services:
  php:
    image: yiisoftware/yii2-php:7.3-apache
    volumes:
      - ~/.composer-docker/cache:/root/.composer/cache:delegated
      - ./:/app:delegated
    ports:
      - '80:80'
    networks:
     - my-network
  db:
    image: mysql:5.7
    restart: always
    environment:
      - MYSQL_DATABASE=yii_db
      - MYSQL_USER=yii_db
      - MYSQL_PASSWORD=user_password
      - MYSQL_ROOT_PASSWORD=root_password
    ports:
      - '3306:3306'
    expose:
      - '3306'
    volumes:
      - my-db:/var/lib/mysql
    #volumes:
    #  - ~/mysql:/var/lib/mysql
    networks:
     - my-network
  memcached:
    container_name: memcached
    image: memcached:latest
    ports:
        - "0.0.0.0:11211:11211"
volumes:
  my-db:
networks:
  my-network:
    driver: bridge

This configuration adds two more containers, with the following characteristics:

So, with each new project, I just have to copy that file, customize the configuration, and I already have a work environment similar to the one I’m going to use in production.

Once we have copied the new configuration, we only need 4 commands to work, which we will execute inside the application directory:

docker-compose up # Levantamos el entorno
docker ps --all # Vemos los contenedores levantados, y sus nombres
docker exec -it $NOMBRE_DE_CONTENEDOR bash # entramos en ese contenedor

It is important to note that, in order to safely execute commands such as ./yii for console commands, it is necessary to enter the PHP container, and to connect to the database we must do so or with a client on port 3306, or connecting directly to the database container.

With this, using a single configuration file that is also very easy to manage, we can build an environment for development in less than 10 minutes.