欢迎光临
我们一直在努力

Docker: 7 部署PHP+Vue项目

本文将介绍如何使用docker镜像 快速部署 php+vue 项目.文末提供源码地址

本地创建Docker映射目录

—— vue_demo         # Demo项目
—— php_vue
—— docker-compose.yaml
—— nginx
———— apps           # 项目代码
———— conf           # nginx配置文件
—————— nginx.conf
———— log            # nginx
———— vhost          # 虚拟机配置目录
—————— default.conf
<span class="copy-code-btn">复制代码</span>

创建docker-compose.yaml

<span class="hljs-string">version</span> <span class="hljs-string">:</span> <span class="hljs-string">"3"</span> <span class="hljs-comment">#docker-compose的版本</span>
<span class="hljs-attr">services:</span> <span class="hljs-comment">#容器的集合</span>
<span class="hljs-attr">      mysql:</span> <span class="hljs-comment">#项目名称</span>
<span class="hljs-attr">              image:</span> <span class="hljs-attr">mysql:5.7</span> <span class="hljs-comment">#镜像名称,如果是通过dockerfile创建的可以使用build属性</span>
<span class="hljs-attr">              container_name:</span> <span class="hljs-string">mysql</span>  <span class="hljs-comment">#容器名称,如果没有这个属性的话,docker-compose会随机分配一个名字给容器</span>
<span class="hljs-attr">              privileged:</span> <span class="hljs-literal">true</span>    <span class="hljs-comment">#允许操作的表示,如果不加的话会出现类似,permission deny的错误</span>
<span class="hljs-attr">              ports:</span>
<span class="hljs-bullet">                      -</span> <span class="hljs-number">3307</span><span class="hljs-string">:3306</span>  <span class="hljs-comment">#开放宿主机和容器的映射端口</span>
<span class="hljs-attr">              environment:</span>
<span class="hljs-attr">                      MYSQL_ROOT_PASSWORD:</span> <span class="hljs-string">root</span>  <span class="hljs-comment">#mysql镜像中的环境变量</span>
<span class="hljs-attr">      php:</span>
<span class="hljs-attr">              image:</span> <span class="hljs-attr">php:7.2-fpm</span>
<span class="hljs-attr">              container_name:</span> <span class="hljs-string">php</span>
<span class="hljs-attr">              privileged:</span> <span class="hljs-literal">true</span>
<span class="hljs-attr">              ports:</span>
<span class="hljs-bullet">                    -</span> <span class="hljs-number">9001</span><span class="hljs-string">:9000</span>
<span class="hljs-attr">              links:</span>
<span class="hljs-bullet">                     -</span> <span class="hljs-string">mysql</span>  <span class="hljs-comment">#容器之间进行关联</span>
<span class="hljs-attr">              volumes:</span>
<span class="hljs-bullet">                     -</span> <span class="hljs-string">./nginx/apps:/usr/share/nginx/html</span>  <span class="hljs-comment">#挂载卷,需要注意的是,php在以模块的形式加载到nginx的时候,需要他们两个的目录结构一致,否则nginx无法加载php,但是 html等静态问价可以正常访问。</span>
<span class="hljs-attr">      nginx:</span>
<span class="hljs-attr">               image:</span>  <span class="hljs-string">nginx</span>
<span class="hljs-attr">               container_name:</span> <span class="hljs-string">nginx</span>
<span class="hljs-attr">               privileged:</span> <span class="hljs-literal">true</span>
<span class="hljs-attr">               links:</span>
<span class="hljs-bullet">                       -</span> <span class="hljs-string">php</span>
<span class="hljs-attr">               ports:</span>
<span class="hljs-bullet">                       -</span> <span class="hljs-number">8088</span><span class="hljs-string">:80</span>
<span class="hljs-attr">               volumes:</span>
<span class="hljs-bullet">                       -</span> <span class="hljs-string">./nginx/vhost:/www/nginx/vhost</span>
<span class="hljs-bullet">                       -</span> <span class="hljs-string">./nginx/conf/nginx.conf:/etc/nginx/nginx.conf</span>
<span class="hljs-bullet">                       -</span> <span class="hljs-string">./nginx/log:/www/nginx/log</span>
<span class="hljs-bullet">                       -</span> <span class="hljs-string">./nginx/apps:/usr/share/nginx/html</span>

<span class="hljs-attr">      phpmyadmin:</span>
<span class="hljs-attr">              image:</span> <span class="hljs-string">phpmyadmin/phpmyadmin</span>
<span class="hljs-attr">              container_name:</span> <span class="hljs-string">phpmyadmin</span>
<span class="hljs-attr">              privileged:</span> <span class="hljs-literal">true</span>
<span class="hljs-attr">              links:</span>
<span class="hljs-bullet">                      -</span> <span class="hljs-string">mysql</span>
<span class="hljs-attr">              ports:</span>
<span class="hljs-bullet">                      -</span> <span class="hljs-number">7001</span><span class="hljs-string">:80</span>
<span class="hljs-attr">              environment:</span>
<span class="hljs-attr">                      MYSQL_ROOT_PASSWORD:</span> <span class="hljs-string">root</span>
<span class="hljs-attr">                      PMA_HOST:</span> <span class="hljs-string">mysql</span>

<span class="hljs-attr">      redis:</span>
<span class="hljs-attr">              image:</span> <span class="hljs-attr">redis:4.0.14</span>
<span class="hljs-attr">              container_name:</span> <span class="hljs-string">redis</span>
<span class="hljs-attr">              privileged:</span> <span class="hljs-literal">true</span>
<span class="hljs-attr">              ports:</span>
<span class="hljs-bullet">                      -</span> <span class="hljs-number">6379</span><span class="hljs-string">:6379</span>
<span class="hljs-attr">      mongo:</span>
<span class="hljs-attr">              image:</span> <span class="hljs-string">mongo</span>
<span class="hljs-attr">              restart:</span> <span class="hljs-string">always</span>
<span class="hljs-attr">              ports:</span>
<span class="hljs-bullet">                      -</span> <span class="hljs-number">27017</span><span class="hljs-string">:27017</span>
<span class="copy-code-btn">复制代码</span>

建立容器,执行命令 docker-compose up -d

创建前端项目

使用vue create vue_demovue ui创建项目vue_demo

打包项目cd vue_demo && npm run build,复制 dist文件夹到 php_vue/nginx/apps

测试 curl localhost:9099 返回html代码

创建php文件

<span class="hljs-meta"><?php</span>
<span class="hljs-keyword">echo</span> <span class="hljs-string">'hello Docker'</span>;
<span class="copy-code-btn">复制代码</span>

测试 curl localhost:8088 返回 hello Docker

过程中的疑难杂症

image拉取速度过慢

解决方法 - 修改仓库源 创建、修改 /etc/docker/daemon.json 文件,修改为如下形式

vi /etc/docker/daemon.json          # 编辑daemon.json 

{
    "registry-mirrors": ["http://hub-mirror.c.163.com"]
}

systemctl restart docker            # 重启docker服务
<span class="copy-code-btn">复制代码</span>

国内加速地址有:

docker-compose 拉取镜像,成功之后就不会重复拉取。
所以导出现某个镜像速度慢、卡死。 Ctrl + c终止换源,重启docker再执行命令 docker-compose up -d

docker 创建 iptables链 报错

... iptables --wait -t nat -I DOCKER -i br-d38d92300109 -j RETURN:iptables: No chain/target/match by that name.

docker 服务启动的时候,docker服务会向iptables注册一个链,以便让docker服务管理的containner所暴露的端口之间进行通信 通过命令 iptables -L 可以查看iptables链

在开发环境中,如果你删除了iptables中的docker链,或者iptables的规则被丢失了(例如重启firewalld),docker就会报iptables error例如:failed programming external connectivity … iptables: No chain/target/match by that name 要解决这个问题,只要 重启docker服务,之后,正确的iptables规则就会被创建出来

项目源码

github.com/ruidao/demo…

不足 & 后续跟进

  • 上述部署还是不够快,正确打开方式应该是创建自己centos项目镜像
  • 项目代码迭代还可以更自动一点

作者:锐玩道
链接:https://juejin.im/post/5da362706fb9a04e4047971d
来源:掘金
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

未经允许不得转载:798VPS » Docker: 7 部署PHP+Vue项目

相关推荐

  • 暂无文章