How to deploy Vue project using Docker image + nginx

How to deploy Vue project using Docker image + nginx

1. Packaging Vue project

Enter the following name in the developed vue project and package it to generate a dist folder

yarn build / npm run build

At this point, there will be an extra folder in the root directory: the dist folder, which contains what we want to publish.

If you transfer the entire dist directory to the server and deploy it as a static resource site, you can directly access the project.

2. Get the nginx image

nginx is a high-performance HTTP and reverse proxy server. Here we use the nginx image as the basis to build our vue application image.

Type in the terminal:

docker pull nginx

You can get the nginx image.

A Docker image is a special file system that, in addition to providing the programs, libraries, resources, configurations, and other files required for the container to run, also contains some configuration parameters prepared for runtime (such as anonymous volumes, environment variables, users, etc.). Images do not contain any dynamic data, and their contents will not be changed after they are built.

Enter the following command in the terminal to see the nginx image

docker image ls

The mirroring result is as follows:

3. Create nginx config configuration file

Create an nginx folder in the project root directory and create a new file default.conf in the folder

server {
  listen 80;
  server_name localhost;

  #charset koi8-r;
  access_log /var/log/nginx/host.access.log main;
  error_log /var/log/nginx/error.log error;

  location / {
    root /usr/share/nginx/html;
    index index.html index.htm;
    try_files $uri $uri/ /index.html;
  }

  #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;
  }
}

Note ⚠️: If vue-router is using history mode, try_files $uri $uri/ /index.html; is very important! ! !

Because our application is a single-page client application, if the background is not configured correctly, when the user accesses the address in the browser, 404 will be returned.

Therefore, you need to add a candidate resource on the server side to cover all situations. If the URL does not match any static resource, the same index.html page should be returned. This page is the page that your app depends on.

The above file defines the homepage as /usr/share/nginx/html/index.html, so we can put the constructed index.html file and related static resources into the /usr/share/nginx/html directory later.

4. Create a Dockerfile

# Set the base image FROM nginx
# Define the author MAINTAINER lihui <[email protected]>
#Copy the contents of the dist file to the /usr/share/nginx/html/ directory COPY dist/ /usr/share/nginx/html/
#Replace the default configuration in the nginx image with the local default.conf configuration COPY nginx/default.conf /etc/nginx/conf.d/default.conf

5. Build a Vue application image based on the Dockerfile

Run the following command, and make sure not to miss the last “.”

docker build -t test .

-t is used to name the image, test is the name of the generated image, and . is used to build the image based on the Dockerfile in the current directory.

The vue-based image is generated!

The above is the detailed content of the method of deploying Vue project with Docker image + nginx. For more information about Docker deployment of Vue project, please pay attention to other related articles on 123WORDPRESS.COM!

You may also be interested in:
  • Docker container deployment of front-end Vue service (novice tutorial)
  • Implementation steps for docker deployment of springboot and vue projects
  • Teach you how to deploy Vue project with Docker
  • vue-cli3 project from construction optimization to docker deployment method
  • Deploy Vue program using Docker container

<<:  Pull-down refresh and pull-up loading components based on Vue encapsulation

>>:  Detailed explanation of invisible indexes in MySQL 8.0

Recommend

IDEA complete code to connect to MySQL database and perform query operations

1. Write a Mysql link setting page first package ...

Can MySQL's repeatable read level solve phantom reads?

introduction When I was learning more about datab...

Quickly get started with VUE 3 teleport components and usage syntax

Table of contents 1. Introduction to teleport 1.1...

Ubuntu terminal multi-window split screen Terminator

1. Installation The biggest feature of Terminator...

Solve the problem of garbled data in MySQL database migration

Under the instructions of my leader, I took over ...

Summary of Spring Boot Docker packaging tools

Table of contents Spring Boot Docker spring-boot-...

How to use explain to query SQL execution plan in MySql

The explain command is the primary way to see how...

Implementation of Docker private warehouse registry deployment

As more and more Docker images are used, there ne...

A brief discussion of the interesting box model of CSS3 box-sizing property

Everyone must know the composition of the box mod...

How to publish a locally built docker image to dockerhub

Today we will introduce how to publish the local ...

MySQL learning tutorial clustered index

Clustering is actually relative to the InnoDB dat...

In-depth reading and practice records of conditional types in TypeScript

Table of contents Using conditional types in gene...

Implementation example of Vue+Element+Springboot image upload

Recently, I happened to be in touch with the vue+...