本文使用的前端项目是 vue-element-admin;将这个项目配置为docker启动,本文主要介绍docker相关的配置;关于前端项目的一些操作,本文不作详解
想要了解vue-element-admin 的朋友,请撮:https://panjiachen.github.io/vue-element-admin-site/zh/
以下涉及创建的文件,本文后面会贴一个目录结构图,仅供参考
1、在项目的根目录创建Dockerfile 文件:
# Statusbar
#
# VERSION 1.0.0
# 基础镜像
FROM nginx:1.16.0
# 维护者信息
MAINTAINER wangquan@quan.icu
# 将vue构建后的dist目录中的内容复制到docker容器中nginx默认配置的部署目录
COPY ./dist /usr/share/nginx/html
# 将docker容器中默认的配置替换为本地配置
COPY ./docker/nginx/nginx.conf /etc/nginx/nginx.conf
COPY ./docker/nginx/default.conf /etc/nginx/conf.d/default.conf
# 端口映射
EXPOSE 80
2、创建 nginx.conf 文件:
user root;
worker_processes 1;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
include /etc/nginx/conf.d/*.conf;
}
3、创建 default.conf 文件:
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log /var/log/nginx/host.access.log main;
#location / {
# root /usr/share/nginx/html;
# index index.html index.htm;
#}
location / {
root /usr/share/nginx/html;
index index.html index.htm;
# 此处主要增加该配置,解决界面刷新报 404 的问题
# 参考博客: https://blog.csdn.net/qq_27985607/article/details/86608392
try_files $uri $uri/ /index.html =404;
}
#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;
#}
}
主要就是创建以上三个文件,本文讲解比较基础,没有涉及很深的理论;关于docker更详细的介绍,大家可以自行官网查询
运行命令:
# 构建docker镜像,别忘了最后面还有一个空格和一个点,镜像名称和标签自定义
docker build -t 镜像名称:标签 .
# 运行docker镜像,-p: 指定端口映射,格式为:主机(宿主)端口:容器端口
docker run --name 自定义容器名称 -p 3000:80 -d 镜像名称:标签
本文使用的完整项目文件配置如图:
运行效果图: