When working with our applications in EC2, the data of our applications should always be stored in the correct data stores. For...
Development with Yii2 and docker-composemacklus
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.
In order to raise our environment we will need:
- php-cli installed on our computer, with the extensions php-dom and php-mbstring
- Docker compose
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:
- Port 8000 is opened instead of port 80, which I see more convenient
- Container with database is not created
- Container with cache is not created as memcached
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:
- PHP 7.3 (for new projects I always start with the latest stable version)
- Access to the project through port 80
- MySQL version 5.7
- Memcached for data cache.
- A persistent data volume for the database.
- The database is automatically configured with username and password, for direct access from the application.
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.