本文主要总结怎么用docker部署vue项目。

直接上代码。

1. Dockerfile

FROM node:8.17.0
#定义时区参数
ENV TZ=Asia/Shanghai
# 设置编码
ENV LC_ALL C.UTF-8
#设置时区
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo '$TZ' > /etc/timezone

WORKDIR /usr/src/app
COPY debian.tsinghua.source.txt .
RUN cat /usr/src/app/debian.tsinghua.source.txt > /etc/apt/sources.list
RUN apt update && apt install nginx -y

ARG build_env
COPY package*.json ./
RUN npm install --registry=https://registry.npm.taobao.org
COPY . .
RUN npm run ${build_env} && cp -r dist/* /var/www/html && if [ "$build_env" = "build" ] ; \
    then cp nginx/default.conf /etc/nginx/conf.d/default.conf; \
    else cp nginx/default-test.conf /etc/nginx/conf.d/default.conf; fi

CMD ["nginx", "-g", "daemon off;"]

 

2. debian.tsinghua.source.txt

deb http://mirrors.163.com/debian/ stretch main non-free contrib
deb http://mirrors.163.com/debian/ stretch-updates main non-free contrib
deb http://mirrors.163.com/debian/ stretch-backports main non-free contrib
deb-src http://mirrors.163.com/debian/ stretch main non-free contrib
deb-src http://mirrors.163.com/debian/ stretch-updates main non-free contrib
deb-src http://mirrors.163.com/debian/ stretch-backports main non-free contrib
deb http://mirrors.163.com/debian-security/ stretch/updates main non-free contrib
deb-src http://mirrors.163.com/debian-security/ stretch/updates main non-free contrib

 

3. default.conf

server {
    listen       80;
    server_name  admin.xxx.com.cn;

    #charset koi8-r;
    access_log  /var/log/nginx/host.access.log;
    error_log  /var/log/nginx/error.log;

    location / {
        root   /var/www/html;
        try_files $uri $uri/ /index.html;
    }
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /var/www/html;
    }
}

default-test.conf 的内容同上,不同点为第三行:server_name admin-test.xxx.com.cn;

 

4. 对外暴露的端口可自行确定,如(17401):

docker build --build-arg build_env=build -t business-front .
docker rm -f business-f
docker run -itd  --name business-f -p 17401:80 business-front