Docker Learninghttps://www.cnblogs.com/poloyy/p/15257059.html Project Structure. ├── app │ ├── __init__.py │ └── main.py ├── Dockerfile └── requirements.txt FastAPI application main.py code from typing import Optional from fastapi import FastAPI app = FastAPI() @app.get("/") def read_root(): return {"Hello": "World"} @app.get("/items/{item_id}") def read_item(item_id: int, q: Optional[str] = None): return {"item_id": item_id, "q": q} Dockerfile # 1. Start from the official Python base image FROM python:3.9 # 2. Set the current working directory to /code # This is where you put your requirements.txt file and your application directory WORKDIR /code # 3. Copy the requirements.txt file first. # Since this file does not change often, Docker will detect it and use the cache in this step, and also enable the cache for the next step. COPY ./requirements.txt /code/requirements.txt # 4. Run pip command to install dependencies RUN pip install --no-cache-dir --upgrade -r /code/requirements.txt # 5. Copy the FastAPI project code COPY ./app /code/app # 6. Run service CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "80"] Step 4: Run pip command analysis
Docker CacheThere is an important trick here Dockerfile, first copy only the files of the dependencies, not the FastAPI application code ./requirements.txt /code/requirements.txt
Build Docker ImageOpen the command line in the Dockerfile
View Mirror
Start the Docker container
View Container
Visit 127.0.0.1/ Visit 127.0.0.1/docs Official Docker image with Gunicorn - Uvicorn
Official Chestnut FROM tiangolo/uvicorn-gunicorn-fastapi:python3.9 COPY ./requirements.txt /app/requirements.txt RUN pip install --no-cache-dir --upgrade -r /app/requirements.txt COPY ./app /app Application Scenario
Using poetry's docker image # Stage 1: Will only be used to install Poetry and generate requirements.txt with project dependencies from Poetry's pyproject.toml file. FROM tiangolo/uvicorn-gunicorn:python3.9 as requirements-stage # Set /tmp as the current working directory; this is where we will generate the file requirements.txt WORKDIR /tmp # Install poetry RUN pip install poetry # COPY ./pyproject.toml ./poetry.lock* /tmp/ # Generate requirements.txt RUN poetry export -f requirements.txt --output requirements.txt --without-hashes # This is the final stage, anything after this will remain in the final container image FROM python:3.9 # Set the current working directory to /code WORKDIR /code # Copy requirements.txt; this file only exists in the previous Docker stage, that's why --from-requirements-stage is used to copy it COPY --from=requirements-stage /tmp/requirements.txt /code/requirements.txt # Run the command RUN pip install --no-cache-dir --upgrade -r /code/requirements.txt # COPY ./app /code/app # Run service CMD ["uvicorn", "app.1_Quick Start:app", "--host", "0.0.0.0", "--port", "80"]
poetry detailed tutorial https://www.jb51.net/article/195070.htm This is the end of this article about FastAPI deployed in Docker. For more related FastAPI deployed in Docker content, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: Detailed explanation of the use of css-vars-ponyfill in IE environment (nextjs build)
>>: Detailed explanation of the implementation of MySQL auto-increment primary key
html2canvas is a library that generates canvas fr...
Preface Sometimes you come across business tables...
Table of contents View network configuration View...
Let's try out nginx's reverse proxy here....
MySQL server has gone away issue in PHP 1. Backgr...
Table of contents react-native project initializa...
This article shares the specific code of jQuery t...
This article mainly summarizes some commonly used...
Description Solution VMware 15 virtual machine br...
There are many types of auto-increment IDs used i...
Slow query log related parameters MySQL slow quer...
This article mainly introduces the analysis of th...
The file server is one of the most commonly used ...
1. Environment and related software Virtual Machi...
Table of contents background Problem Analysis 1. ...