Using Laradock in Mac/Windows to build a Docker based Laravel development environment


 Laradock Getting Started Tutorial

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 the corresponding version of the operating system, install it according to the boot process. Finally, open the Docker application, and check whether the installation is successful on the command line:

Mac

Windows

Note: The use of Docker under Windows requires the Hyper-V component to be enabled (it is recommended to use it in the native system, and Windows 10 Professional Edition is enabled by default. In addition, Docker for Windows Desktop is not supported in Windows 10 Home Edition). If you use Windows in VMware or Parallels virtual machines, you need to enable nested virtualization support in settings (Settings ->Processor and Memory ->Advanced Options ->Enable Virtualization Manager). If the virtual machine does not support this configuration or Hyper-V, You can try to install Docker ToolBox additionally to support the use of Docker (refer to Official Documents )。 My environment here is Mac+VMware Fusion+Windows 10 (virtual machine).

Configure Image Accelerator

After installing Docker on the local operating system, you need to configure an image accelerator for the Docker Hub image warehouse to speed up the download of domestic Docker images, such as the image accelerator provided by Alibaba Cloud. For Mac systems, click the Docker icon on the taskbar and select Preferences ->Docker Engine, The Windows system can access the image accelerator configuration interface through Settings ->Docker Engine (the following figure is a Windows example, and the Mac is similar, except that the portal is Preferences. Alibaba Cloud Docker image accelerator can get a reference from this tutorial: https://yq.aliyun.com/articles/29941 ):

 Windows Configure Docker Image Source

Then click the "Sign In" menu to log in to the Docker Hub through the Docker ID/password (note that the Docker ID is not a registered mailbox) to avoid authentication problems in the image pulling process.

Get started quickly

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

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

Then .env Modify the Linux software source to be a domestic image during the image building process in order to avoid the network timeout problem during the image building process:

 # If you need to change the sources (i.e. to China),  set CHANGE_SOURCE to true CHANGE_SOURCE=true # Set CHANGE_SOURCE and UBUNTU_SOURCE option if you want to change the Ubuntu system sources.list file. UBUNTU_SOURCE=aliyun

3. Build image&start container:

 docker-compose up -d nginx mysql redis

nginx The image is built on php-fpm above, php-fpm Built on workspace So starting nginx will start first workspace and php-fpm

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.

If an error is reported during the image building process of the above command in the Windows system:

 /tmp/sources.sh: not found

Please refer to this issue for solutions: https://github.com/laradock/laradock/issues/2450

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 composer create-project laravel/laravel blog --prefer-dist 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 APP_CODE_PATH_HOST Configuration item:

 APP_CODE_PATH_HOST=../wwwroot/

This is equivalent to wwwroot With Docker /var/www The directory creates a soft link, and then we laradock/nginx/sites Add a new one in the directory blog.conf Configure and set the virtual domain name to blog.test

 server { listen 80; listen [::]:80; server_name blog.test; root /var/www/blog/public; index index.php index.html index.htm; location / { try_files $uri $uri/ /index.php$is_args$args; } location ~ \.php$ { try_files $uri /index.php =404; fastcgi_pass php-upstream; fastcgi_index index.php; fastcgi_buffers 16 16k; fastcgi_buffer_size 32k; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; #fixes timeouts fastcgi_read_timeout 600; include fastcgi_params; } location ~ /\.ht { deny all; } location /.well-known/acme-challenge/ { root /var/www/letsencrypt/; log_not_found off; } }

Next, you need to /etc/hosts (The corresponding file path under Windows is C:\Windows\System32\drivers\etc\hosts )The following configuration line is added to the file:

 127.0.0.1      blog.test

Finally, restart the Docker's Nginx:

 docker-compose down docker-compose up -d nginx

In this way, we can use the http://blog.test Access this app:

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.2, 7.1, 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 containers by editing 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, so far, Laradock supports software including but 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

Docker or Vagrant

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 can only be broken quickly.

As for Laradock or Homestead, different people have different opinions. Laradock is more lightweight than Homestead, because as mentioned above, 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 depends on the software it needs, which is more flexible and efficient.

Another thing to mention is that Docker can be used locally or online. The so-called same environment everywhere, while the development environment deployed by Vagrant can only be used locally, which can also be an important consideration.

Note: The local development environment used by Xuejun for daily development is Laradock.


give the thumbs-up Cancel Like Collection Cancel Collection

<<Previous: The Mac system installs the specified version of MySQL for the Valet development environment

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