How to build a swoft development environment through Docker

Source: Internet
Author: User
Tags autoload composer install docker ps redis server
本篇文章给大家分享的内容是关于如何通过Docker搭建一个swoft开发环境 ,内容很详细,有需要的朋友可以参考一下,希望可以帮助到你们。

首个基于 Swoole 原生协程的新时代 PHP 高性能协程全栈组件化框架,内置协程网络服务器及常用的协程客户端,常驻内存,不依赖传统的 PHP-FPM,全异步非阻塞 IO 实现,以类似于同步客户端的写法实现异步客户端的使用,没有复杂的异步回调,没有繁琐的 yield,有类似 Go 语言的协程、灵活的注解、强大的全局依赖注入容器、完善的服务治理、灵活强大的 AOP、标准的 PSR 规范实现等等,可以用于构建高性能的Web系统、API、中间件、基础服务等等。


Swoftis a Swoole built on top of a 高性能协程 PHP 全栈框架 , and Swoole in the PHPer inside is a high-level skills, so in the context of the installation of a lot of people have caused great trouble, especially Swoft , this article will be through Docker Solve the deployment of the runtime and development environment in an extremely simple way.


As you can see from the encyclopedia, Docker It is an open-source application container engine that allows developers to package their applications and dependencies into a portable container, then publish them to any popular machine, Linux or virtualize them, and containers are completely sandbox-based. There will be no interface between each other , and it can be understood that we can package our code and operating environment into a container, packaged containers can be published on any popular Linux machine, the Linux machine in this case is not accurate, thanks to the Docker for Windows project and Hyper-V Development, Dockeryou can also run on a Windows 10 system in a good state, but the author does not recommend it Docker for Windows for production environments.

Docker noun Concept

Some of the Docker commonly used nouns are described and explained briefly so that the novice can understand the following

    • Dockerfile, Dockerfile is Docker镜像 the description file that is docker build constructed by command to become镜像

    • Mirroring ( Image ), Dockerfile built-in, including operating system and running environment

    • Container ( Container ), a container is a running mirror that can be understood as mirroring is the Docker build and package phase of the lifecycle, and the container is the start and execute phase

    • A mirrored warehouse ( Repository ) for storing a built-in warehouse Docker镜像 that can be understood as Git a similar warehouse

Installing Docker

DockerThe installation process is not complex, this section describes Linux Windows 10 the installation process under the system, and the Mac environment is 不建议 used Docker to run or develop the Swoft project, because the Mac for Docker performance of the shared disk is extremely poor, resulting in The start-up phase takes a long time.

On the Linux install Docker withdocker-compose

The Linux flow through yum and apt-get to the Docker installation is quite simple
Centos:yum install docker -y
Ubuntu:apt-get install docker-engine -y
Just based on the difference between the system, the terminal to perform the above line of command to complete Docker the installation, after the installation is completed we need to execute a service docker start command to start the Docker service.

Dockerafter installation, we also need to install it docker-compose to facilitate subsequent use of Docker
Centos:yum install python-pip -y && pip install --upgrade pip && pip install -U docker-compose
Ubuntu:apt-get install python-pip -y && pip install --upgrade pip && pip install -U docker-compose
The installation can be done at the terminal by executing a single line of commands, depending on the system. docker-compose

On the Windows 10 install Docker withdocker-compose

We go directly to the Docker website to download the corresponding installation package Https:// ..., non-login users we will see Please Login to Download , meaning that we first login to the Docker account and then download, We directly click the button to the login page to complete the account registration or login can be in the above link page by clicking on the Get Docker download, note that after the account we use when used.
After downloading the installation package can directly run the installation package for installation, the whole process can be said to be a fool-style, the next step, pay attention to the installation before the system must be opened Hyper-V , the opening process is relatively simple reference to other articles HTTPS://SEGMENTFAULT.COM/A/11 ..., note Hyper-Vis VMware conflict with the two can not coexist, can only choose one, if you have to use the virtual machine, such as Vagrant tools, can also run in a virtual machine Linux 系统 , and then according to the installation process described in this article Linux 系统 , run Docker within the virtual machine As a development environment.
The latest version of the Docker installation package is already included, and there is docker-compose no need to do extra work.
After the installation is complete, restart the computer, and when you see the taskbar is displayed, the 小鲸鱼(Docker Icon) Docker is running Docker boot is successful.

We need to right Docker -click Sign in / Create Docker ID on the login we just registered to Docker ID get permission to get the public image from DockerHub.

Since we are used for development use, so we also need to authorize the permissions of the shared directory, right Docker and click Settings , set the interface to switch to Shared Drives , tick your project code is located 磁盘盘符 , and click on the bottom right corner to Apply complete the authorization.

Swoft Development environment

Modify the official default docker-compose.yml file

We use the command git clone from the Github 克隆(clone) Swoft project, and using the project's own docker-compose.yml files to implement a development environment, docker-compose.yml is docker-compose the Orchestration configuration file, we look at the official default file content:

Version: ' 3 ' services:    swoft:        container_name:swoft        image:swoft/swoft        ports:            -"80:80"        Volumes:            -./:/var/www/swoft        stdin_open:true        tty:true        command:php/var/www/swoft/bin/swoft start

This is a relatively simple arrangement of files, onlyswoftA service, and there is not too much content associated with it, aboutdocker-compose.ymlFile format We do not make too many explanations here, we can find the relevant content for reading comprehension.
A simple interpretation of the contents of this file can be understood as the use ofswoft/swoftThe official image and set the container name asswoft, within the binding container80Port and the host's80Port, setting./In the current directory and in the container/var/www/swoftDirectory is a shared directory, opens an interactive terminal with the container and starts when the orchestration file is startedSwoftService.
We can notice that the default orchestration filecommandConfigured withphp /var/www/swoft/bin/swoft start, namely启动 Swoft 服务The command, but if only克隆(clone)Project and Executedocker-compose upTo try to start容器Then we'll get a failed result because not yet executedcomposer installTo loadComposerDependence and lack ofvendorFolders, andautoloadand other related files, resulting in incorrect operationSwoftinstance, let's look at the default Orchestration file settingsstdin_open: trueAndtty: trueTwo parameters, corresponding to each otherdockerOn the command-iAnd-tTwo parameters, a simple understanding is-iOpens the输入(input)Function-tA connection container is opened.交互式终端(terminal), we can take advantage of these two parameters and will orchestrate the file'scommandLine insteadcommand: /bin/bashSo that the container does not start directly after it startsSwoftService, but by us manually through交互式终端(terminal)Go inside the container to start.
The following is a changeddocker-compose.ymlFile instance:

Version: ' 3 ' services:    swoft:        container_name:swoft        image:swoft/swoft        ports:            -"80:80"        volumes:            -./:/var/www/swoft        stdin_open:true        tty:true        command:/bin/bash

Start the Development environment container

At this point we start in the directory where the files are organized, 终端(Shell) and then execute docker-compose up -d , -d meaning to 守护模式(Daemon Mode) run, so that we in the same 终端(Shell) into the container, after the command execution we can see that the Starting swoft ... done boot container is successful.
If you get an error when you execute the start command, the host 80 port is already occupied, the other unused ports in the file are changed, the first one refers to the port of the host, and the docker-compose.yml second refers to the 80:80 80 80 port inside the container. That means we just need to change the first one.

Error:for Swoft cannot start service SWOFT:B ' driver failed programming external connectivity on endpoint Swoft (dab0f4d00 620e2f5c07e33084ca5cac6f08cb48018d6b737eadc035e5aa0b597): Bind for Failed:port is already allocated '

Enter the development environment container

docker psYou can view the boot container information by executing a command, as shown in the sample information below:

CONTAINER ID  IMAGE               COMMAND                 CREATED             STATUS             PORTS               NAMESf22173763374  swoft/swoft: Latest  "Docker-php-entrypoin" about  a minute ago up about  a minute>80/tcp  swoft

Learned 容器ID(Container ID) as f22173763374 , 容器名称(Container Name) for swoft , that we can execute docker exec -it f22173763374 bash or docker exec -it swoft bash pass 交互式终端(terminal) into the container.

If the execution times are wrong the input device is not a TTY. If you are using mintty, try prefixing the command with 'winpty' , you can add a command to the docker exec command before winptywinpty docker exec -it swoft bash

Operation and Development debugging

installation Composer dependency and Generation 自动加载(Autoload) file

docker execafter entering the container through the command, we notice that the contents of the left side of the cursor become the inside of the root@f22173763374: container, which f22173763374 corresponds to 容器ID(Container ID) .
Because the Swoft official mirror swoft/swoft configuration of the working directory is /var/www/swoft , and docker-compose.yml will be associated with the container directory, that is, 项目当前目录 /var/www/swoft by docker exec entering the directory is already a /var/www/swoft directory, that is, the project directory, so we can directly execute command to load Composer the dependencies and generate the 自动加载(Autoload) file.
Taking into account the domestic network environment, we can execute commands before executing the command composer install to composer config -g repo.packagist composer configure the speed of Composer 中国镜像源 installation.

Start Swoft Service

After the Composer dependencies are installed, you can perform the php bin/swoft start start-up service when you see

root@f22173763374:/var/www/swoft# php bin/swoft start                         Server information************************************** HTTP | host:, port:81, Type:1, worker:1, mode:3* TCP  | host:, port:8099, Type:1, worker:1 (Enabled) * * * Server has been started. (Master pid:15, manager pid:16) You can use CTRL + C to stop run.

That means your Swoft and startup success, we can open a browser to access it , when you see that it's done!

If you bind the host port is not 80 , then the corresponding can be changed;
If the access to see is the Redis connection failure host= port=6379 lack of Redis services, the simplest direct is directly in the current container installation Redis Server , direct execution to apt install -y redis-server && service redis-server start complete the installation and start the operation;

Modify the code and make the code effective

swoft and php-fpm mode of development will be a little different, in the php-fpm mode to directly change the contents of the code, and then access the corresponding code can be changed content, because The PHP code is reloaded for each request in php-fpm mode, and Swoft is persisted, which means that the code accepts the request without reloading after the service starts. This pattern change can make it possible for a large number of Swoft code to be reused without reloading and re-instantiating one of the reasons for performance.
Such changes will have a degree of impact on development, that is, under swoft , you need to restart Worker or Restart service for the changed code to take effect, but thanks to The hot reload feature of Swoft , which automatically checks for code changes and automatically restart Worker , can only be changed by the . env file in the project root directory. The auto_reload entry is true , as there is no . env file under the project root, and you can directly copy the . Env.example File as Code>.env
and make the corresponding changes, one thing to note is that only the code under the Change app directory will be overloaded with the hot reload feature, changing the other code will not be overloaded. This is due to the fact that different codes are in different life cycles, and only the code that is loaded after Workerstart can be overloaded, and we will explain further about the content of this section later when it comes to Swoft's life cycle .

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.