通过 Docker 打包Vue项目成一个独立应用

5 min read
# Use an official Node.js image as the base image
FROM node:14

# Set the working directory in the image to /app
WORKDIR /app

# Copy the package.json file to the image
COPY package.json ./

# Install the dependencies
RUN npm install

# Copy the rest of the project files to the image
COPY . .

# Specify the command to run when the image is started
CMD ["npm", "run", "serve"]

Docker-compose 文件示范代码:

yaml
version: '3'
services:
  vue-app:
    build: .
    ports:
      - '8080:8080'

在构建 Docker 镜像并运行 Docker 容器时,需要在项目根目录中运行以下命令:

python
# Build the Docker image
docker-compose build

# Start the Docker container
docker-compose up

该代码会在本地构建一个基于 Node.js 的镜像,并在本地启动一个 Docker 容器,该容器可以通过在浏览器中访问 http://localhost:8080 来查看 Vue 项目的运行状态。