副标
Dockerize your Vue Application
dockerfile Vue example
docker Vue example
Vue应用docker容器化打包

docker忽略文件

.dockerignore

node_modules

www.conf

server {
    listen 80;
    #listen [::]:80;
    listen 443 ssl http2;
    #listen [::]:443 ssl http2;

    server_name h5.iamle.com web-app-h5;

    ssl_certificate      /etc/nginx/ssl/star.iamle.com.crt;
    ssl_certificate_key  /etc/nginx/ssl/star.iamle.com.key;
    #ssl_session_cache    shared:SSL:10m;
    ssl_session_timeout  10m;
    ssl_protocols        TLSv1 TLSv1.1 TLSv1.2;
    ssl_ciphers          HIGH:!aNULL:!MD5;
    ssl_prefer_server_ciphers   on;

    root /var/www/html;
    index index.html;

    location / {
        try_files $uri $uri/ /index.html;
    }

    location /api {
        proxy_pass http://${API_HOST_BASE};
    }

    error_page 500 502 503 504 /50x.html;
    location = /50x.html {
        root html;
    }
}

Dockerfile

# 构建阶段 vue build stage
FROM node:10.16.0-alpine  AS build-stage-node

## 配置 apk包加速镜像为阿里
RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories

## 安装 一些基础包
RUN apk update \
  && apk upgrade \
  && apk add git \
  && apk add bash \
  #&& apk add nghttp2-dev \
  && apk add ca-certificates \
  && apk add wget \
  #&& apk add curl \
  #&& apk add tcpdump \
  && apk add iputils \
  && apk add iproute2 \
  && apk add libc6-compat \
  && apk add -U tzdata \
  && rm -rf /var/cache/apk/*

# ## 设置 操作系统时区
# RUN rm -rf /etc/localtime \
#   && ln -s /usr/share/zoneinfo/Asia/Shanghai /etc/localtime

# 设置 工作目录
ENV MY_HOME=/app
RUN mkdir -p $MY_HOME
WORKDIR $MY_HOME

## 安装 全局依赖
# RUN npm install -g express

# 安装 项目依赖
COPY package.json ./
RUN npm config set registry https://registry.npm.taobao.org \
  && npm set sass_binary_site=https://npm.taobao.org/mirrors/node-sass/ \
  && npm install

# 复制vue项目文件
COPY . .

# 构建
RUN npm run build

RUN ls -lsah /app

# 打包应用
# 生产阶段 nginx
# 使用nginx镜像运行构建物
FROM nginx:stable-alpine
COPY --from=build-stage-node /app/dist /var/www/html
RUN ls -lsha /var/www/html
COPY ./www.conf /etc/nginx/conf.d/
RUN rm /etc/nginx/conf.d/default.conf
EXPOSE 80
CMD ["nginx","-g","daemon off;"]

需求

在vue单页应用开发中,如果为了省事PHP代码和vue代码都放在同一个GIT仓库中
那么如何进行vue前端代码和php Api 后端代码组织呢?
下面提供一个组织方式

项目目录结构

frontend
public
.gitignore
.travis.yml
application
build.php
composer.json
extend
LICENSE.txt
phpunit.xml
README.md
runtime
tests
think
thinkphp
vendor

其中
frontend 为vue前端项目目录
public 为php的web目录,index.php入口文件在这里,nginx也配置的是public

vue 配置

文件 frontend/config/index.js

module.exports = {
  build: {
    env: require('./prod.env'),
    index: path.resolve(__dirname, '../../public/ui/index.html'),
    assetsRoot: path.resolve(__dirname, '../../public/ui'),
    assetsSubDirectory: 'static',
    assetsPublicPath: '/ui/',
    .........其他代码.......
  },

前台访问

api接口为
http://appweb.dev/v1/api

访问/ui/即可
http://appweb.dev/ui/

需要注意的问题

public/ui 目录为前端项目build后的文件,不适宜纳入git管理
线上发布的通过CI&CD工具解决
目前适用于PHP项目的CI&CD工具有
Jenkins
Walle 瓦力上线部署系统
Piplin 持续集成与部署系统