The content of this article is about how to build a swoft development environment through Docker. The content is very detailed. Friends in need can refer to it. I hope it can help you.
Swoft
<br>The first new era PHP based on Swoole native coroutine
High-performance coroutine full-stack component framework, built-in coroutine network server and commonly used coroutine clients, resident in memory, does not rely on traditional PHP-FPM, fully asynchronous non-blocking IO
Implementation, using a writing method similar to that of a synchronous client to implement the use of an asynchronous client. There are no complex asynchronous callbacks, no cumbersome yields, and similar Go
Language coroutines, flexible annotations, powerful global dependency injection containers, complete service governance, flexible and powerful AOP, and standard PSR
Standard implementation, etc., can be used to build high-performance web systems, APIs, middleware, basic services, etc.
Swoft
is a high-performance protocol built on
Swoole Cheng PHP full-stack framework
, and Swoole
is an advanced skill in PHPer
, so it has also caused a lot of trouble to many people in the related environment installation,Swoft
Even more so, this article will solve the deployment of running environment and development environment in an extremely simple way through Docker
.
As you can see from the encyclopedia, Docker
is an open source application container engine that allows developers to package Their applications and dependencies are packaged into a portable container, and then published to any popular Linux
machine. Virtualization can also be achieved. The containers completely use the sandbox mechanism and will not interact with each other. Any interface can also be understood as we can package our code and running environment into a container. The packaged container can be published to any popular Linux
machine, here refers to the Linux machine In fact, it is not accurate. Thanks to the development of Docker for Windows
project and Hyper-V
, Docker
can also run in good condition on Windows 10 system , but the author does not recommend using Docker for Windows
in a production environment.
Here is a brief explanation and explanation of some commonly used nouns in Docker
so that novices can understand the following
Dockerfile
, Dockerfile
is the description file of Docker image
, which is built through the docker build
command Become a mirror
Mirror (Image
), built through Dockerfile
, including operating system and operating environment
Container (Container
), a container is a running image, which can be understood as the building and packaging stage in the Docker
life cycle, and The container is the
mirror warehouse (Repository
) during the startup and execution phases, which is used to store the built Docker image
warehouse, understandable The installation process for installing Docker for a repository similar to Git
Docker
is not Complex, this section will introduce the installation process under Linux
and Windows 10
systems, but is not recommended
on Mac
systems. #Docker environment to run or develop
Swoft projects, because the performance of shared disks on
Mac for Docker is extremely poor, which will cause
Swoft to fail at startup The stage takes an extremely long time.
on
Linux with
docker-compose
Linux via# The process of installing
Docker using ##yum
and apt-get
is quite simple CentOS:
yum install docker -y Ubuntu:
apt-get install docker-engine -yYou only need to execute the above line of commands in the terminal to complete the installation of
Docker
according to the difference of the system. After the installation is completed After that, we need to execute the service docker start
command to start the Docker
service. After installing
, we also need to install docker-compose
to facilitate subsequent use of Docker CentOS:
yum install python -pip -y && pip install --upgrade pip && pip install -U docker-composeUbuntu:
apt-get install python-pip -y && pip install --upgrade pip && pip install - U docker-composeYou only need to execute the above line of commands in the terminal according to the system differences to complete the installation of
docker-compose
. <h3>Installing <code>Docker
and docker-compose
on Windows 10
We go directly to the Docker official website to download the corresponding installation package https:/ /store.docker.com/edit..., non-login users will see Please Login to Download
, which means we need to log in to the Docker account first before downloading. We directly click the button to go to the login page to complete. After registering or logging in, you can download it by clicking Get Docker
on the link page above. Note that we will also use this account later.
After downloading the installation package, you can directly run the installation package for installation. The whole process can be said to be fool-proof. Just continue to the next step. Note that you need to open the system's Hyper-V
before installation. The startup process is relatively simple. Please refer to other articles https://segmentfault.com/a/11... . Note that Hyper-V
conflicts with VMware
and the two cannot coexist. , you can only choose one. If you must use a virtual machine, such as Vagrant
and other tools, you can also run a Linux system
in the virtual machine, and then follow the instructions in this article The installation process of Linux system
is processed, and Docker
is run in the virtual machine as the development environment.
The latest version of Docker
installation package already contains docker-compose
, so there is no need to do extra operations.
After the installation is completed, restart the computer. When you see the Little Whale (Docker Icon)
on the taskbar showing Docker is running
, it means Docker
is started. It worked.
We need to right-click Docker
and click Sign in / Create Docker ID
to log in to the one we just registered Docker ID
so that we can obtain public images from DockerHub.
Since we are using it for development, we also need to authorize the permissions of the shared directory. Right-click Docker
and click Settings
to switch the settings interface to Shared Drives
, check the disk drive letter
where your project code is located, and click Apply
in the lower right corner to complete the authorization.
<span style="font-size: 16px;">docker-compose.yml</span>
File
We use the command git clone https://github.com/swoft-cloud/swoft
to clone (clone) from
Github
Swoft project, and use the docker-compose.yml
file that comes with the project to implement an environment for development. docker-compose.yml
is docker- For the orchestration configuration file of compose
, let’s take a look at the contents of the official default file:
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 orchestration file, with only one service swoft
and no association with it. There is a lot of content. We will not explain too much about the file format of docker-compose.yml
here. You can find the relevant content by yourself for reading and understanding.
A simple interpretation of the content of this file can be understood as using the swoft/swoft
official image and setting the container name to swoft
, binding 80 in the container
Port and the 80
port of the host, set the ./
current directory and the /var/www/swoft
directory in the container as a shared directory, enable Interactive terminal with the container and start the Swoft
service when starting the orchestration file.
We can notice that the command
on the default orchestration file is configured with php /var/www/swoft/bin/swoft start
, which is Start the Swoft service
command, but if we only clone(clone)
the project and execute docker-compose up
to try to start the container
, we will get a failed result, Because composer install
has not been executed to load the dependencies of Composer
and the vendor
folder and autoload
and other related files are missing, resulting in the inability to run correctlySwoft
Example, let's look at the default orchestration file setting stdin_open: true
and tty: true
two parameters, corresponding to the docker
command respectively The simple understanding of the two parameters -i
and -t
is that -i
turns on the input(input)
function,-t
opens a interactive terminal (terminal)
in a connection container. We can use these two parameters and change the command
line of the arrangement file to command: /bin/bash
, so that the Swoft
service is not started directly after the container is started, but we manually enter the container through the interactive terminal (terminal)
Go to boot.
The following is an example of a changed docker-compose.yml
file:
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
此时我们在编排文件的所在目录启动一个 终端(Shell)
, 然后执行 docker-compose up -d
,-d
的意思是以守护模式(Daemon Mode)
运行,便于我们在同一个 终端(Shell)
进入到容器内,命令执行后我们可以看到 Starting swoft ... done
即表示启动容器成功。
如果在执行启动命令时得到一下错误,则说明宿主机的80
端口已经被占用了,更改 docker-compose.yml
文件内的 80:80
为其它未被占用的端口即可,注意第一个80
指的是宿主机的端口,第二个80
指的是容器内的端口,也就是说我们只需要更改第一个即可
ERROR: for swoft Cannot start service swoft: b'driver failed programming external connectivity on endpoint swoft(dab0f4d00620e2f5c07e33084ca5cac6f08cb48018d6b737eadc035e5aa0b597): Bind for 0.0.0.0:80 failed: port is already allocated'
通过执行 docker ps
命令可以查看启动的容器信息,下面为示例信息:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES f22173763374 swoft/swoft:latest "docker-php-entrypoin" About a minute ago Up About a minute 0.0.0.0:80->80/tcp swoft
得知 容器ID(Container ID)
为 f22173763374
,容器名称(Container Name)
为 swoft
,我们可以执行 docker exec -it f22173763374 bash
或 docker exec -it swoft bash
通过 交互式终端(terminal)
进入到容器内。
如执行时报错 the input device is not a TTY. If you are using mintty, try prefixing the command with 'winpty'
,可在 docker exec
命令前面增加 winpty
命令解决,即 winpty docker exec -it swoft bash
<span style="font-size: 16px;">Composer</span>
依赖及生成<span style="font-size: 16px;">自动加载(Autoload)</span>
文件
通过 docker exec
命令进入容器后,我们留意到光标左侧的内容变为 root@f22173763374:
即为已进入容器内,其中 f22173763374
为对应的 容器ID(Container ID)
。
由于 Swoft
官方镜像 swoft/swoft
配置的工作目录为 /var/www/swoft
,而 docker-compose.yml
又将项目当前目录
关联了容器 /var/www/swoft
目录,即通过 docker exec
进入的目录已经为 /var/www/swoft
目录,即项目目录,所以我们可以直接执行 composer install
命令来加载 Composer
的依赖并生成 自动加载(Autoload)
文件。
考虑到国内的网络环境,我们在执行 composer install
命令前可以先执行 composer config -g repo.packagist composer https://packagist.phpcomposer.com
命令配置 Composer 中国镜像源
加速安装速度。
<span style="font-size: 16px;">Swoft</span>
服务
安装完 Composer
依赖后,便可以执行 php bin/swoft start
启动服务了,当你看到
root@f22173763374:/var/www/swoft# php bin/swoft start Server Information ******************************************************************** * HTTP | host: 0.0.0.0, port: 81, type: 1, worker: 1, mode: 3 * TCP | host: 0.0.0.0, 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.
即意味着你的 Swoft
以及启动成功了,我们可以打开浏览器访问一下 http://127.0.0.1:80
,当你看到下图即大功告成了!
如果你绑定宿主机的端口不是80
,则改成对应的即可;
如果访问看到的是 Redis connection failure host=127.0.0.1 port=6379
则说明缺少 Redis
服务,最简单直接的就是直接在当前容器内安装 Redis Server
,直接执行 apt install -y redis-server && service redis-server start
即可完成安装以及启动操作了;
##Swoft There will be a little difference between development in
PHP-FPM mode, in ## In #PHP-FPM
mode, directly change the code content, and then access the corresponding code to get the changed content. This is because in PHP-FPM
mode, each request will reload the PHP code, and Swoft
is persistently run
, which means that after the service is started, the code does not need to be reloaded for accepted requests. Changes in this mode can make Swoft
One of the reasons why a lot of code can be reused without the need to reload and re-instantiate is to greatly improve performance. Such a change will have a certain impact on development, which means that under
Swoft
, you need torestart the Worker
or restart the service
to make the changes The code takes effect, but thanks to the hot reload
function of Swoft
, code changes can be automatically checked and automatically restarted the Worker
, we only need to pass the project root directory Just change the AUTO_RELOAD
item in the .env
file under to true
. If there is no .env
file in the project root directory, you can copy it directly. .env.example
The file is .env
and make corresponding changes. One thing to note is that only changing the code in the app
directory will Being hot reloaded
function overloaded, changes to other codes will not be reloaded. This is because different codes are in different life cycles. Only code loaded after WorkerStart
can Being overloaded, we will further explain this part when it comes to the life cycle of Swoft
. Related recommendations:
The above is the detailed content of How to build a swoft development environment through Docker. For more information, please follow other related articles on the PHP Chinese website!