docker下nginx部署vue项目

环境centos7
准备:

  • 安装git
  • 安装nodejs
  • 安装docker
  1. 拉取git仓库
    cd ~ #进入home目录
    git clone *********.sto.git #后面是仓库地址

  2. 切换分支

cd ~/sto
git branch -a 		#查看分支 以* 开头的是当前分支
*master
  dev
  remotes/origin/HEAD -> origin/master
  remotes/origin/custom
  remotes/origin/customer
  remotes/origin/dev
  remotes/origin/master
git checkout -b dev origin/dev 		#切换到dev分支
  1. 准备nginx
  • 创建目录 ~/nginx-sto/html 用于映射nginx的/usr/share/ngix/html目录 前端工程打包后的文件将放置到~/nginx-sto/html目录中

  • 创建目录~/nginx-sto/conf 用于映射nginx的配置文件的目录 一般是/etc/nginx
    cd ~nginx-sto/conf #进入~nginx-sto/conf

vi nginx.conf			

将以下配置写到nginx.conf中

# For more information on configuration, see:
#   * Official English Documentation: http://nginx.org/en/docs/
#   * Official Russian Documentation: http://nginx.org/ru/docs/

user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

# Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;

events {
    worker_connections 1024;
}

http {
    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;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 2048;

    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;

    # Load modular configuration files from the /etc/nginx/conf.d directory.
    # See http://nginx.org/en/docs/ngx_core_module.html#include
    # for more information.
    include /etc/nginx/conf.d/*.conf;

    server {
        listen       80 default_server;
        listen       [::]:80 default_server;
        server_name  _;
        root         /usr/share/nginx/html;

        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;

	location / {
            try_files $uri $uri/ @router;
            index  index.html index.htm;
        }

        location @router {
        	rewrite ^.*$ /index.html last;
        }

#用于访问后台的api
	location /crs-api {
        	rewrite  ^/crs-api/(.*)$ /$1 break;
             	proxy_pass  http://xxx.xx.xx.xx:8080;
     	}

        error_page 404 /404.html;
            location = /40x.html {
        }

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

5.docker下安装nginx

docker run --name nginx-sto -d -p 8180:80 -v /etc/timezone:/etc/timezone:ro -v /etc/localtime:/etc/localtime:ro -v ~/nginx-sto/html:/usr/share/nginx/html -v ~/nginx-sto/conf:/etc/nginx  registry.docker-cn.com/library/nginx

其中registry.docker-cn.com/library/nginx是nginx的镜像

使用docker ps 查看镜像

docker ps

使用docker pull 拉取镜像

docker pull 镜像名称

6.编写执行脚本
vi ~/sto.sh
写入以下脚本

#!/bin/bash

#进入前端工程目录
cd sto
#拉取代码
git pull
# 构建前端工程
npm install
npm run build
#删除nginx的html映射目录下的文件
rm -rf ~/nginx-sto/html/*
#将构建够的前端代码复制到html映射目录下
cp -r ~/stochastic/dist/* ~/nginx-sto/html/
chmod 775 ~/sto.sh	#赋权

7.执行脚本

./sto.sh
<think>好的,我现在需要回答用户关于如何使用Docker部署包含Vue项目Nginx容器的问题。首先,我得整理一下用户提供的引用内容,看看有哪些步骤和配置可以参考。用户引用了多个不同的Docker部署方法,每个引用中的命令略有不同,比如挂载目录、端口映射等。我需要将这些信息综合起来,形成一个清晰的教程。 首先,用户的需求很明确:使用Docker部署Nginx运行Vue项目。所以步骤应该包括Vue项目的构建、Dockerfile的编写、Nginx配置文件的准备,以及运行容器。用户提供的引用中有几个关键点: 1. 构建Vue项目成dist文件夹,这是部署的前提。 2. 创建Dockerfile,使用nginx镜像,复制dist文件和配置文件。 3. 配置Nginx,可能需要修改默认配置,比如处理路由问题。 4. 使用docker build构建镜像,然后运行容器,注意端口映射和卷挂载。 接下来,我需要确保每个步骤都正确,并且符合最佳实践。例如,在Dockerfile中删除默认配置,替换为自己的配置,这样可以避免冲突。同时,挂载配置文件或目录时,用户可能有不同的路径,需要说明清楚如何调整路径。 另外,用户提供的引用中有不同的挂载方式,有的是在构建时复制文件,有的是在运行时挂载卷。这里需要区分两者的使用场景。构建时复制适合将静态文件打包进镜像,而运行时挂载适合动态修改配置或内容。根据用户的问题,可能更推荐构建时将dist文件打包到镜像中,这样镜像更自包含,而Nginx配置可以通过挂载或在构建时添加。 还需要考虑Nginx的配置是否正确,特别是处理Vue的路由模式(history模式)可能需要额外的配置,避免出现404错误。引用中的例子可能没有详细说明这点,可能需要补充。 然后,生成相关问题部分,需要根据教程内容提出几个相关问题,比如优化镜像大小、处理路由问题、多环境配置等。 最后,确保整个回答结构清晰,步骤明确,代码块正确,符合用户的中文要求,并且正确引用提供的参考资料。需要注意用户提到的引用并不是直接相关,但需要参考他们的命令结构,比如如何编写Dockerfile和docker run命令。</think>### 使用Docker部署Nginx运行Vue项目分步教程 #### 一、准备工作 1. **构建Vue项目** 在项目根目录执行以下命令生成静态文件: ```bash npm run build ``` 生成`dist`文件夹(包含编译后的HTML/CSS/JS文件)[^1]。 2. **创建Nginx配置文件** 新建`default.conf`文件,配置内容如下(重点处理Vue路由模式): ```nginx server { listen 80; server_name localhost; location / { root /usr/share/nginx/html; index index.html; try_files $uri $uri/ /index.html; # 解决history模式路由问题 } error_page 500 502 503 504 /50x.html; location = /50x.html { root /usr/share/nginx/html; } } ``` --- #### 二、编写Docker镜像构建文件 创建`Dockerfile`(无文件扩展名),内容如下: ```dockerfile # 使用官方nginx镜像 FROM nginx:alpine # 删除默认配置 RUN rm /etc/nginx/conf.d/default.conf # 复制自定义配置 COPY default.conf /etc/nginx/conf.d/ # 将Vue编译文件添加到镜像 COPY dist/ /usr/share/nginx/html/ # 暴露80端口 EXPOSE 80 ``` *说明:使用`nginx:alpine`镜像可减小体积[^1][^5]。* --- #### 三、构建与运行容器 1. **构建镜像** 在`Dockerfile`同级目录执行: ```bash docker build -t vue-nginx . ``` 2. **运行容器** ```bash docker run -d --name vue-app -p 8080:80 vue-nginx ``` *参数说明:* - `-d`:后台运行 - `-p 8080:80`:将宿主机8080端口映射到容器80端口 - `--name`:指定容器名称[^1][^4] --- #### 四、验证部署 访问`http://宿主机IP:8080`,若显示Vue应用即成功。通过以下命令查看日志: ```bash docker logs vue-app ``` --- #### 五、高级配置(可选) 1. **挂载本地目录** 修改运行命令实现配置热更新: ```bash docker run -d \ -p 8080:80 \ -v $(pwd)/dist:/usr/share/nginx/html \ -v $(pwd)/default.conf:/etc/nginx/conf.d/default.conf \ --name vue-app \ nginx:alpine ``` *说明:适合开发环境调试[^2][^4]。* 2. **多阶段构建优化** 在`Dockerfile`中集成构建过程: ```dockerfile # 构建阶段 FROM node:16 as build-stage WORKDIR /app COPY package*.json ./ RUN npm install COPY . . RUN npm run build # 生产阶段 FROM nginx:alpine COPY --from=build-stage /app/dist /usr/share/nginx/html COPY default.conf /etc/nginx/conf.d/ ``` ---
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值