把线上的hyperf2.1项目迁移至docker容器上流程步骤
该项目是一个个人采集分享网站(https://blog.zongscan.com/), 里面盈利点主要是谷歌联盟广告,后端基于hyperf2.1开发的,线上跑了一段时间了,装起来比较麻烦。
为了节约成本,所以该项目服务器在购买一次后不会在续费的情况下,基本在一两年左右,会经常迁移项目,本机安装太麻烦lnmp,swoole还有一些乱七八糟的依赖,扩展,配置,系统工具啥的等等等,实在是太难了,
所以打算搞个docker环境一劳永逸,下回也迁移不用瞎折腾。
docker环境我早已经安装了,想了解的可以点进去看看
https://www.zongscan.com/demo333/304.html
下面时间进入正题
拉取hyperf官方的镜像 映射到本地obj目录 启动后台运行
docker run --name hyperf \
-v /home/www/obj:/data/project \
-p 9601:9601 -it \
--privileged -u root \
--entrypoint /bin/sh \
hyperf/hyperf:7.4-alpine-v3.11-swoole
查看运行的docker
[[email protected] ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
103fd0f26448 hyperf/hyperf:7.4-alpine-v3.11-swoole "/bin/sh" 38 minutes ago Created hyperf
启动容器
docker start e14802af67f0
注意:启动容器出现下面错误 需重启docker
[[email protected] ~]# docker start 103fd0f26448
Error response from daemon: driver failed programming external connectivity on endpoint hyperf (17f678568edf9989d532019de9f3d46896c41097b0f44550536476c979106e54): (iptables failed: iptables --wait -t nat -A DOCKER -p tcp -d 0/0 --dport 9601 -j DNAT --to-destination 172.17.0.2:9601 ! -i docker0: iptables: No chain/target/match by that name.
(exit status 1))
Error: failed to start containers: 103fd0f26448
[[email protected] ~]# systemctl restart docker
[[email protected] ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
103fd0f26448 hyperf/hyperf:7.4-alpine-v3.11-swoole "/bin/sh" 38 minutes ago Created hyperf
[[email protected] ~]# docker start 103fd0f26448
103fd0f26448
[[email protected] ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
103fd0f26448 hyperf/hyperf:7.4-alpine-v3.11-swoole "/bin/sh" 39 minutes ago Up About a minute 0.0.0.0:9601->9601/tcp hyperf
进入容器
docker exec -it 103fd0f26448 /bin/sh
进来了 看看跟目录
/ # ls -a
. .dockerenv data etc lib mnt proc run srv tmp var
.. bin dev home media opt root sbin sys usr
composer默认已安装了
/ # composer -v
______
/ ____/___ ____ ___ ____ ____ ________ _____
/ / / __ \/ __ `__ \/ __ \/ __ \/ ___/ _ \/ ___/
/ /___/ /_/ / / / / / / /_/ / /_/ (__ ) __/ /
\____/\____/_/ /_/ /_/ .___/\____/____/\___/_/
/_/
Composer version 2.0.14 2021-05-21 17:03:37
检查一下映射 在宿主机添加个文件
/ # cd data
/data # cd project/
/data/project # ls
123.txt
准备就绪 拉取代码进来
/data/project # ls -l
total 452
-rw-r--r-- 1 root root 1455 Feb 22 09:38 Dockerfile
-rw-r--r-- 1 root root 1797 Feb 22 09:38 README.md
drwxr-xr-x 11 root root 162 May 14 05:59 app
drwxr-xr-x 2 root root 24 Feb 22 09:38 bin
-rw-r--r-- 1 root root 3013 May 18 06:25 composer.json
-rw-r--r-- 1 root root 431022 May 18 06:25 composer.lock
drwxr-xr-x 3 root root 79 Feb 22 09:38 config
-rw-r--r-- 1 root root 580 Feb 22 09:38 deploy.test.yml
-rw-r--r-- 1 root root 474 Feb 22 09:38 phpstan.neon
-rw-r--r-- 1 root root 695 Feb 22 09:38 phpunit.xml
drwxrwxr-x 6 root root 55 May 7 01:51 public
drwxrwxr-x 6 root root 80 May 18 06:26 runtime
drwxrwxr-x 4 root root 35 Mar 31 08:37 storage
drwxr-xr-x 3 root root 64 Feb 22 09:38 test
drwxr-xr-x 53 root root 4096 May 18 06:25 vendor
/data/project #
修改hyperf端口9601,9602 (注意:这是我这里改,至于你改不改无所谓啦)
在/config/autoload/server.php里面
修改mysql , redis连接地址
我这都是装是宿主机上所以mysql,redis都用宿主机ip地址就ok了
开始启动hyperf
/data/project # php bin/hyperf.php start
...
[INFO] WebSocket Server listening at 0.0.0.0:9602
[INFO] HTTP Server listening at 0.0.0.0:9601
...
后面就是域名解析及nginx配置切换了
换完直接大功告成
完
相关文章