Dockerfile构建vue项目镜像并部署
1、编写Dockerfile
#wqs_20210331 dockerfile
#使用 nginx最新版本作为基础镜像
FROM nginx
# 删除原来nginx自带的配置文件
RUN rm -vf /etc/nginx/conf.d/default.conf
# 复制自己写的配置文件
COPY ./conf.d/ /etc/nginx/conf.d/
#将当前文件夹的dist文件复制到容器的/usr/share/nginx/html目录
COPY ./dist /usr/share/nginx/html/
#声明运行时容器暴露的端口(容器提供的服务端口,nginx这里默认式80端口,所以可以注释掉)
#EXPOSE 8080
# 持久化数据卷
VOLUME ["messageui"]
CMD echo "finished,----success1"
#CMD:指定容器启动时要运行的命令
CMD ["nginx", "-g", "daemon off;"]
2、修改vue项目下.evn配置文件修改
# 将VUE_APP_BASE_API 的值修改为default.conf中对应的别名
VUE_APP_BASE_API = '/prod-api'
3、将vue项目打包成dist,并将dist放到与Dockerfile在同一目录下
4、编写default.conf配置文件,并将配置文件放到conf.d文件夹下,conf.d与Dockerfile在同一目录下
server {
listen 80;
listen [::]:80;
server_name localhost;
client_max_body_size 100m;
#charset koi8-r;
#access_log /var/log/nginx/host.access.log main;
location / {
root /usr/share/nginx/html;
try_files $uri $uri/ @router; #需要指向下面的@router否则会出现vue的路由在nginx中刷新出现404
index index.html index.htm;
}
# 后端接口
location /prod-api/{
proxy_pass https://xxxxxx.com/;
proxy_redirect default;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
5、开始构建镜像
docker build -t imgtest01 .
- 查看镜像 docker images
6、运行容器
docker run --name test001 -d -p 8080:80 imgtest01
- docker ps查看容器是否运行成功