Document your deployment process, starting with the front end simple. Use the simplest way to deploy, provided you understand what Docker does and have Docker installed.

Deploy front-end projects:

From the Nuggets to see a synthesis of the big watermelon game source:Juejin. Cn/post / 692380…

Here’s a little magic twist: rebuild it with cocosDashbord instead of materialOpen index. HTML directly, and the result looks like this

Original Project Address:Github.com/tangxiangmi…Next create a Dockerfile file in the index. HTML folder and type the following

FROM nginx 
# Specifies the base image, which must be the first command
 
COPY . /usr/share/nginx/html/  
Add all contents of the local Dockerfile directory to the /usr/share/nginx/ HTML/directory of the container
Copy the code

Open the command line tool in the index. HTML file and enter the image packaging command:

docker build -t testimage . 
Copy the code

Testimage after -t is the image name and tag, and the ‘. ‘symbol looks for DockerFile files for the current directory. Execute the contents of the DockerFile file and generate an image named testimage. The image will then be run out of a container or uploaded to the image repositoryNext, type docker images on the command line to see the TESTIMage imageNext, enter the docker command on the command line:

docker run --name testcontainer -d -p 8090:80 testimage
Copy the code

Testcontainer after –name specifies the container name. /usr/share/nginx/ HTML / : /usr/share/nginx/html/ : /usr/share/nginx/html/ : /usr/share/nginx/html/ : /usr/share/nginx/html/ : /usr/share/nginx/html/ To see the front-end project directly by accessing index.html, accessing host port 8090 is equivalent to accessing container port 80. -d is the container running in the background and returns the ID of the container. Testimage is the image name.Access to 127.0.0.1:8090 / index. HTML try:You can see it just opens up. You can run the image directly on a machine with a public IP address and directly access the public IP address: public IP :8090/index.html. You can also install nginx on the machine with a public IP address and resolve it with the domain name, and the project will go online. I did a little bit of my own deployment:xg.twgcs.xyz

Deploying back-end projects:

The back-end project is a little more complex than the front end, and requires installing some dependency packages: Let’s use FastAPI, a python-based Web framework that’s been a hit lately. It’s a very high performance asynchronous framework with very little code, just like Flask

pip install uvicorn
pip install fastapi
Copy the code

Uvicorn is a very fast ASGI server built on UvLoop and HttpTools. The Asynchronous Gateway Protocol interface, a standard interface between network protocol services and Python applications, can handle a variety of common protocol types, including HTTP, HTTP2, and WebSocket

Create a new main.py and enter the simplest framework code:

from fastapi import FastAPI

app = FastAPI()


@app.get("/index")
async def index() :
    return {'code': 200.'msg': 'FastAPI niubi'}
Copy the code

Start command:

uvicorn main:app  --reload
Copy the code

By default, the framework exposes port 8000. Let’s try this:

Next, create a new Dockerfile file in the main.py folder and enter the following

FROM wynemo/python38 
#I think running this container in python38 requires Docker Search to search for related images, not directly FROM Python38.
ADD . /code
#The ADD command copies all code from the host Dockerfile directory to the /code directory of the wynemo/python38 virtual machine image
WORKDIR /code
#WORKDIR means that future commands are executed in this folder, which is equivalent tocdThe command 
RUN python3 -m pip install --upgrade pip -i http://pypi.douban.com/simple/ --trusted-host pypi.douban.com \
&& pip3 install -i http://mirrors.aliyun.com/pypi/simple/ --trusted-host mirrors.aliyun.com fastapi \
&& pip3 install -i http://mirrors.aliyun.com/pypi/simple/ --trusted-host mirrors.aliyun.com uvicorn

#The RUN command is used to manage upgrade packages and install dependency packages. The -i command directly specifies the source address of dependency packages to speed up the build process. You can also do many other functions, such as database migration, clearing the cache and so on.

CMD uvicorn main:app --host '0.0.0.0' --port 8080 --reload
#CMD is the command executed when the image is running to get the services in the container running. --host can specify the FastAPI runtime IP address, not 127.0.0.1, otherwise only container access. --port refers to the port on which the framework runs, and is used later to run the container
Copy the code

In the root directory of the project to execute the package image command, here directly say command, not careful screenshot

docker build -t docker-fastapi:latest .
Copy the code

Run the container

docker run --name docker-fastapi -d -p 8100:8080 docker-fastapi
Copy the code

Finally, go to 127.0.0.1:8100/index and try itSuccessful deployment, because the backend project simply went offline, and that’s it.

supplement

Finally, when running online, you need to make Docker start automatically when it starts up. In addition, you need to automatically restart the container after docker service restarts and add the –restart=always command

docker run --restart=always
Copy the code

Since the container is already started, use the docker update command

docker update --restart=always <CONTAINER ID>
Copy the code