文件文件目录

# 项目根目录
blog-nuxt
 ├─.git
 ├─.nuxt
 ├─assets
 ├─components
 ├─layouts
 ├─node_modules
 ├─pages
 │  ├─about
 │  └─user
 ├─plugins
 ├─router
 ├─static
 ├─store
 ├─.editorconfig
 ├─.gitignore
 ├─nuxt.config.js
 ├─package.json
 ├─README.md
 └─yarn.lock
shell

准备Docker部署文件

准备Dockerfile

在项目根目录创建空白文件,命名为Dockerfile,写入:

#1、基于镜像node,版本选择合适稳定版本即可
FROM node:16.19.0-alpine3.16
#2、作者
MAINTAINER www.hyz.cool
#3、参数,node的环境为生产环境
ENV NODE_ENV=production
#4、任意ip
ENV HOST 0.0.0.0
#5、容器内创建目录lib-blog-nuxt
RUN mkdir -p /BlogNuxt
#6、复制当前的内容到容器内容部目录blog
COPY . /BlogNuxt
#7、切换工作目录到blog
WORKDIR /BlogNuxt
#8、暴露端口3000,默认端口
EXPOSE 3000
#9、配置npm的远程仓库
RUN npm install cnpm -g --registry=https://registry.npmmirror.com
#10、清除缓存
RUN npm cache clean --force
#11、安装依赖
RUN npm install --no-package-lock --legacy-peer-deps
#12、构建,生成dist文件
RUN npm run build
#13、start
CMD ["npm","start"]
Dockerfile

准备docker-compose

在项目根目录创建文件docker-compose.yml,写入:

version: '3.1'
services:
  nuxt:
    build: .
    restart: always
    container_name: nuxt
    ports:
      - 3000:3000
yml

修改package.json

如果是windows下创建的项目,需要将package.json中:

{
  ...
  "scripts": {
    "dev": "SET NODE_OPTIONS=--openssl-legacy-provider && nuxt",
    "build": "SET NODE_OPTIONS=--openssl-legacy-provider && nuxt build",
    "start": "SET NODE_OPTIONS=--openssl-legacy-provider && nuxt start",
    "generate": "SET NODE_OPTIONS=--openssl-legacy-provider && nuxt generate"
  }
  ...
}
json

修改为:

{
  ...
  "scripts": {
    "dev": "NODE_OPTIONS=--openssl-legacy-provider && nuxt",
    "build": "NODE_OPTIONS=--openssl-legacy-provider && nuxt build",
    "start": "NODE_OPTIONS=--openssl-legacy-provider && nuxt start",
    "generate": "NODE_OPTIONS=--openssl-legacy-provider && nuxt generate"
  }
  ...
}
json

这是因为WindowsLinux下的命令不同造成的。
如果不修改的话,会在build时报错。

上传文件到服务器

将下列文件上传到服务器指定目录:

# 项目根目录
blog-nuxt
 ├─assets
 ├─components
 ├─layouts
 ├─pages
 │  ├─about
 │  └─user
 ├─plugins
 ├─router
 ├─static
 ├─store
 ├─docker-compose.yml
 ├─Dockerfile
 ├─nuxt.config.js
 ├─package.json
 ├─README.md
 └─yarn.lock
shell

也就是说,除了node_modulesgit等文件,其他的都需要上传。

Docker部署

运行下面的命令即可:

cd nuxt目录
docker-compose up -d
shell
打赏
  • 微信
  • 支付宝
评论
来发评论吧~
···

歌手: