Using Laradock to build a Docker based PHP development environment


 laradock

brief introduction

Laradock is a complete PHP local development environment for Docker. Like Homestead, it provides a series of packaged (including configuration) Docker Images. Laradock focused on creating a Docker development environment for Laravel in its early days, so it was first famous in the Laravel community. Later, with the expansion of its influence, it was gradually accepted and adopted by the PHP community. At present, in addition to Laravel, PHP projects supported include Symfony, CodeIgniter, WordPress, Drupal, etc.

Docker literacy

Before learning and using Laradock, it is necessary for us to learn and understand Docker, and before using Docker, it is necessary to understand the following two issues:

What is Docker

Docker is developed based on Go language and is a container engine built on LXC technology. Container is a way to package software in a fixed format so that software can run in a shared operating system. Unlike virtual machines, containers do not need to bundle this operating system, but only need libraries and settings necessary for the software to work properly, which makes containers more efficient, lightweight It can form its own system and ensure consistent operation results no matter where it is deployed.

Docker provides a way to automatically deploy software in a secure and repeatable environment, and its emergence has opened the prelude to the reform of the way of releasing products based on cloud computing platforms. Developers can use Docker to solve the problem of "only working on my machine" when they need to share code with colleagues; Using Docker, the operator can run and manage in the associated container to obtain better calculation density; Docker can be used by enterprises to build agile software distribution pipelines so that new features can be processed faster.

Why use Docker

Docker is designed to solve the following problems:

  • Complex environment management: From various OSs to various middleware to various apps, a product can be successfully released. As a developer, there are too many things to care about and difficult to manage. This problem is common in the software industry and needs to be faced directly. Docker can simplify the deployment of multiple application instances, such as Web applications, background applications, database applications, big data applications, such as Hadoop clusters, message queues, etc., which can be packaged into one image deployment.
  • The arrival of the cloud computing era: the success of AWS has led developers to transfer applications to the cloud, and solved the hardware management problem. However, problems related to software configuration and management still exist. The emergence of Docker can help software developers broaden their minds and try new software management methods to solve this problem.
  • Changes in virtualization means: in the cloud era, standard hardware is used to reduce costs, and virtualization means are used to meet the resource needs of users allocated on demand and ensure availability and isolation. However, in the opinion of Docker, both KVM and Xen are wasting resources, because users need an efficient running environment rather than OS. GuestOS wastes resources and is difficult to manage. The more lightweight LXC is more flexible and fast.
  • LXC portability: LXC has existed in the Linux 2.6 kernel, but it was not designed for cloud computing at the beginning. It lacks standardized description means and container portability, which makes it difficult to distribute and manage the built environment. Docker has made substantive innovations on this issue.

Docker is usually used in the following scenarios:

  • Automatic packaging and publishing of Web applications;
  • Automated testing and continuous integration and release;
  • Deploy and adjust databases or other background applications in a service-oriented environment;
  • Compile from scratch or extend the existing OpenShift or Cloud Foundry platform to build your own PaaS environment.

In this tutorial, we will not focus on building a portable local development environment through Docker.

Docker installation

First, we need to install the free community version of Docker on the system, and officially provide Windows, Mac and Linux versions for download: Download address After downloading and installing the corresponding version of the operating system, double-click to open the Docker application, and then check whether the installation is successful on the command line:

Mac

Note: To use Docker in Windows, you need to enable Hyper-V components.

Get started quickly

Before we have a deep understanding of Laradock, let's first see how to quickly install Nginx, PHP, Composer, MySQL, Redis and Beanstalkd in Laradock. With these necessary tools and components for developing Larasol, it is no difference.

1. First, clone the Laradock project code to the local:

 git clone  https://github.com/Laradock/laradock.git

2. Enter laradock The directory will env-example Rename to .env

 cp env-example .env

3. Running container:

 docker-compose up -d nginx mysql redis beanstalkd

If the specified port has been occupied, running the above command will report an error. Close the corresponding service and run the above command again.

4. Open the .env File and add the following configuration:

 DB_HOST=mysql REDIS_HOST=redis QUEUE_HOST=beanstalkd

5. To test that the configuration access domain name points to the Docker environment directory, we first laradock Create an interface with the parent directory laradock Sibling wwwroot Directory, and then wwwroot Run under directory laravel new blog Command to create a new Laravel application. The corresponding hierarchical directory relationship is as follows:

Then we need to get to laradock Edit Below .env In APPLICATION Configuration item:

 APPLICATION=../ wwwroot/
Note: The latest version of laradock The corresponding configuration item is APP_CODE_PATH_HOST

This is equivalent to wwwroot With Docker /var/www The directory has established a soft link, and then we modify it laradock/nginx/sites/default.conf Mapping relationship in:

Next, restart the Docker's Nginx:

 docker-compose up -d nginx

In this way, we can use the http://localhost Access this application. Of course, if necessary, you can also localhost Change to any domain name you want, but you still need to configure /etc/hosts and laradock/nginx/sites/default.conf Domain name information:

These are Laradock's simple instructions. The scenario we set here is to create a new application. What should we do if there are multiple applications? It is also very simple wwwroot Create multiple application directories under the directory, and then laradock/nginx/sites Create multiple configuration files under. Finally, don't forget to create multiple configuration files in the system /etc/hosts Configure related domain names to bind IP addresses in.

Note: For more details, please refer to the official document: http://laradock.io/documentation/

Functional characteristics

Finally, let's review the functional features of Laradock:

  • It is easy to switch between different versions of PHP: 7.1, 7.0, 5.6, etc
  • Choose databases freely: MySQL, Postgres, MariaDB, etc
  • Run your own software: Memcached, HHVM, Beanstalkd, etc
  • Each software runs in an isolated container: PHP-FPM, NGINX, PHP-CLI, etc
  • Easily customize the container, just edit the Dockerfile file
  • All images are extended from official images
  • Preconfigured NGINX
  • Laradock can be used in each project, or all projects can share one Laradock
  • Easily install/uninstall software in containers using environment variables
  • Clean and well structured Dockerfile
  • Latest version of docker-compose file
  • Everything is visible and editable
  • Quickly build images

In addition, the software supported by Laradock so far includes but is not limited to:

  • Database engine: MySQL - MariaDB - Percona - MongoDB - Neo4j - RethinkDB - MSSQL - PostgreSQL - Postgres PostGIS
  • Database management tool: PhpMyAdmin - Administrator - PgAdmin
  • Cache tool: Redis - Memcached - Aerospike
  • Web server: NGINX - Apache2 - Caddy
  • PHP compiler: PHP FPM - HHVM
  • Message queue: Beanstalkd - RabbitMQ - PHP Worker
  • Queue manager: Beanstalkd Console - RabbitMQ Console
  • Major powers: HAProxy - Certbot - Blackfire - Selenium - Jenkins - ElasticSearch - Kibana - Mailhog - Minio - Varnish - Swoole - Lavel Echo, etc
  • Laradock also introduced the Workspace image as the development environment, which contains a rich and practical tool set: PHP CLI - Composer - Git - Linuxbrew - Node - V8JS - Gulp - SQLite - xDebug - Envoy - Deployer - Vim - Yarn - SOAP - Drush, etc

Laradock or Homestead

Finally, we can't get around to the question of whether to choose Docker (Laradock) or Vagrant (Homestead) as the development environment. The Mac system also officially provides Valet. For the purpose of tasting new things, making Demos or fast learning, of course, Valet is still the best choice, because it is the smallest, most lightweight, fastest to master, and the world's martial arts, only fast.

As for Laradock or Homestead, different people have different opinions. In terms of function, the two are equal. Laradock is more lightweight than Homestead, because as mentioned earlier, Homestead is a VM level virtualization solution that relies on a complete operating system. Although it has a full range of functions, it is heavy. Laradock is a container that only relies on the software it needs, which is more flexible and efficient.

Another thing to mention is that Docker can be used both locally and online same environment everywhere The Homestead development environment deployed by Vagrant can only be used locally, which can also be an important consideration.


Vote Vote Cancel Collect Collect Cancel

<<Previous: Lightweight development environment: detailed tutorial for installing and using Valet

>>Next: Using Laragon to build the Laravel development environment in Windows