docker + nginx 部署vuejs3.0项目

r囧r小猫 2023-03-02 13:10 194阅读 0赞

转载:https://www.cnblogs.com/longdb/p/10770661.html


1:用指令 npm run build 打包vusjs项目(该项目是在github上下载的)。打包成功后会生成一个目录dist。

format_png

2:把该文件夹拷贝到腾讯云服务器(操作系统 centos7)下的/usr/share目录下。

(也可以不放在/usr/share下,也可以自己在usr建一个文件夹存放,好管理,只要保证dist、Dockerfile、nginx.conf 三个文件在同一级目录即可)

format_png 1

3:在/usr/share目录下新建Dockerfile文件

format_png 2

dockerfile文件内容:

  1. # 设置基础镜像
  2. FROM nginx
  3. # 定义作者
  4. MAINTAINER cai <123@qq.com>
  5. # 将dist文件中的内容复制到 /usr/share/nginx/html/ 这个目录下面
  6. COPY dist/ /usr/share/nginx/html/
  7. COPY nginx.conf /etc/nginx/nginx.conf
  8. RUN echo 'echo init ok!!'

4:新建文件 nginx.conf。

文件内容:

  1. worker_processes auto;
  2. #error_log logs/error.log;
  3. #error_log logs/error.log notice;
  4. #error_log logs/error.log info;
  5. #pid logs/nginx.pid;
  6. events {
  7. worker_connections 1024;
  8. }
  9. http {
  10. include mime.types;
  11. default_type application/octet-stream;
  12. #log_format main '$remote_addr - $remote_user [$time_local] "$request" '
  13. # '$status $body_bytes_sent "$http_referer" '
  14. # '"$http_user_agent" "$http_x_forwarded_for"';
  15. #access_log logs/access.log main;
  16. sendfile on;
  17. #tcp_nopush on;
  18. #keepalive_timeout 0;
  19. keepalive_timeout 65;
  20. #gzip on;
  21. client_max_body_size 20m;
  22. server {
  23. listen 80;
  24. server_name www.longdb.com;
  25. #charset koi8-r;
  26. #access_log logs/host.access.log main;
  27. location / {
  28. root /usr/share/nginx/html;
  29. index index.html index.htm;
  30. try_files $uri $uri/ /index.html;
  31. }
  32. #error_page 404 /404.html;
  33. # redirect server error pages to the static page /50x.html
  34. #
  35. error_page 500 502 503 504 /50x.html;
  36. location = /50x.html {
  37. root html;
  38. }
  39. # proxy the PHP scripts to Apache listening on 127.0.0.1:80
  40. #
  41. #location ~ \.php$ {
  42. # proxy_pass http://127.0.0.1;
  43. #}
  44. # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
  45. #
  46. #location ~ \.php$ {
  47. # root html;
  48. # fastcgi_pass 127.0.0.1:9000;
  49. # fastcgi_index index.php;
  50. # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
  51. # include fastcgi_params;
  52. #}
  53. # deny access to .htaccess files, if Apache's document root
  54. # concurs with nginx's one
  55. #
  56. #location ~ /\.ht {
  57. # deny all;
  58. #}
  59. }
  60. # another virtual host using mix of IP-, name-, and port-based configuration
  61. #
  62. #server {
  63. # listen 8000;
  64. # listen somename:8080;
  65. # server_name somename alias another.alias;
  66. # location / {
  67. # root html;
  68. # index index.html index.htm;
  69. # }
  70. #}
  71. }

5:cd 进入share目录(即dist所在目录),然后 用括号里指令(docker build -t longdb-vue:V1.0.0 . )构建镜像。(注意最后的 . )

(也可以不放在/usr/share下,也可以自己在usr建一个文件夹存放,好管理,只要保证dist、Dockerfile、nginx.conf 三个文件在同一级目录即可)

format_png 3

6:运行生成成功的镜像:docker run -p 3000:80 -d —name longdbvuejs 34d9e8770f1b

format_png 4

参数讲解:

run: 创建一个新的容器并运行一个命令
-d: 后台运行容器,并返回容器ID
-p: 端口映射,格式为:主机(宿主)端口:容器端口
--name=”longdbvuejs“: 为容器指定一个名称

34d9e8770f1b —为生成好的images_id.

7:docker ps 指令查看有哪些在运行,上图中看到longvuejs在运行。(测试 http://193.112.82.83:3000/#/page2)

结果如下:

format_png 5

部署好后,目录dist下的内容可以删掉。

发表评论

表情:
评论列表 (有 0 条评论,194人围观)

还没有评论,来说两句吧...

相关阅读

    相关 docker nginx部署前端项目

    最近一直在搞前后端分类,一直在想前端的html页面应该用什么部署 想来想去,如果用tomcat好像有点浪费资源,作为程序员自然要时时刻刻追求新的东西 一直以来都是在apac