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
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 ahigh-performance protocol built on
SwooleCheng PHP full-stack framework
, andSwoole
is an advanced skill inPHPer
, 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 throughDocker
.
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 popularLinux
machine. Virtualization can also be achieved. The containers completely use the sandbox mechanism and will not interact with each other. Any interfacecan also be understood as we can package our code and running environment into a container. The packaged container can be published to any popularLinux
machine, here refers to the Linux machine In fact, it is not accurate. Thanks to the development ofDocker for Windows
project andHyper-V
,Docker
can also run in good condition on Windows 10 system , but the author does not recommend usingDocker for Windows
in a production environment.
Here is a brief explanation and explanation of some commonly used nouns inDocker
so that novices can understand the following
Dockerfile
,Dockerfile
is the description file ofDocker image
, which is built through thedocker build
command Become amirror
Mirror (Image
), built throughDockerfile
, 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 theDocker
life cycle, and The container is the
mirror warehouse (Repository
) during the startup and execution phases, which is used to store the builtDocker image
warehouse, understandable The installation process for installing Docker for a repository similar toGit
Docker
is not Complex, this section will introduce the installation process underLinux
andWindows 10
systems, butis not recommended
onMac
systems. #Dockerenvironment to run or develop
Swoftprojects, because the performance of shared disks on
Mac for Dockeris extremely poor, which will cause
Swoftto fail at startup The stage takes an extremely long time.
on
Linuxwith
docker-compose
Linuxvia# The process of installing
Dockerusing ##yum
andapt-get
is quite simpleCentOS:
yum install docker -yUbuntu:
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 theservice docker start
command to start theDocker
service.After installing
, we also need to installdocker-compose
to facilitate subsequent use of DockerCentOS:
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
.Installing
Docker
anddocker-compose
onWindows 10
We go directly to the Docker official website to download the corresponding installation package https:/ /store.docker.com/edit..., non-login users will seePlease 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 clickingGet 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'sHyper-V
before installation. The startup process is relatively simple. Please refer to other articles https://segmentfault.com/a/11... . Note thatHyper-V
conflicts withVMware
and the two cannot coexist. , you can only choose one. If you must use a virtual machine, such asVagrant
and other tools, you can also run aLinux system
in the virtual machine, and then follow the instructions in this article The installation process ofLinux system
is processed, andDocker
is run in the virtual machine as the development environment.
The latest version ofDocker
installation package already containsdocker-compose
, so there is no need to do extra operations.
After the installation is completed, restart the computer. When you see theLittle Whale (Docker Icon)
on the taskbar showingDocker is running
, it meansDocker
is started. It worked.
We need to right-clickDocker
and clickSign in / Create Docker ID
to log in to the one we just registeredDocker 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-clickDocker
and clickSettings
to switch the settings interface toShared Drives
, check thedisk drive letter
where your project code is located, and clickApply
in the lower right corner to complete the authorization.
docker-compose.yml
FileWe use the commandgit clone https://github.com/swoft-cloud/swoft
toclone (clone) from
GithubSwoft project, and use the
docker-compose.yml
file that comes with the project to implement an environment for development.docker-compose.yml
isdocker- 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 serviceswoft
and no association with it. There is a lot of content. We will not explain too much about the file format ofdocker-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 theswoft/swoft
official image and setting the container name toswoft
, binding80 in the container
Port and the80
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 theSwoft
service when starting the orchestration file.
We can notice that thecommand
on the default orchestration file is configured withphp /var/www/swoft/bin/swoft start
, which isStart the Swoft service
command, but if we onlyclone(clone)
the project and executedocker-compose up
to try to start thecontainer
, we will get a failed result, Becausecomposer install
has not been executed to load the dependencies ofComposer
and thevendor
folder andautoload
and other related files are missing, resulting in the inability to run correctlySwoft
Example, let's look at the default orchestration file settingstdin_open: true
andtty: true
two parameters, corresponding to thedocker
command respectively The simple understanding of the two parameters-i
and-t
is that-i
turns on theinput(input)
function,-t
opens ainteractive terminal (terminal)
in a connection container. We can use these two parameters and change thecommand
line of the arrangement file tocommand: /bin/bash
, so that theSwoft
service is not started directly after the container is started, but we manually enter the container through theinteractive terminal (terminal)
Go to boot.
The following is an example of a changeddocker-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
Composer
依赖及生成自动加载(Autoload)
文件通过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 中国镜像源
加速安装速度。
Swoft
服务安装完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
即可完成安装以及启动操作了;
##SwoftThere will be a little difference between development in
PHP-FPMmode, in ## In #PHP-FPM
mode, directly change the code content, and then access the corresponding code to get the changed content. This is because inPHP-FPM
mode, each request will reload the PHP code, andSwoft
ispersistently 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 makeSwoft
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
orrestart the service
to make the changes The code takes effect, but thanks to thehot reload
function ofSwoft
, code changes can be automatically checked and automaticallyrestarted the Worker
, we only need to pass the project root directory Just change theAUTO_RELOAD
item in the.env
file under totrue
. 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 theapp
directory will Beinghot reloaded
function overloaded, changes to other codes will not be reloaded. This is because different codes are in different life cycles. Only code loaded afterWorkerStart
can Being overloaded, we will further explain this part when it comes to the life cycle ofSwoft
.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!