{"id":20314869,"url":"https://github.com/windvalley/fastapi-uvicorn-gunicorn-docker","last_synced_at":"2026-05-08T00:34:29.322Z","repository":{"id":163372277,"uuid":"256189020","full_name":"windvalley/fastapi-uvicorn-gunicorn-docker","owner":"windvalley","description":"Uvicorn managed by Gunicorn for high-performance FastAPI web applications in Python 3.8","archived":false,"fork":false,"pushed_at":"2020-04-28T09:06:05.000Z","size":13,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-04T08:45:47.034Z","etag":null,"topics":["coroutine","docker","dockerfile","fastapi","gunicorn","parallelism","python38","uvicorn"],"latest_commit_sha":null,"homepage":"","language":"Shell","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/windvalley.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-04-16T11:00:39.000Z","updated_at":"2024-04-28T18:39:23.000Z","dependencies_parsed_at":null,"dependency_job_id":"61b981a9-992e-4516-805e-4dc44504f9c9","html_url":"https://github.com/windvalley/fastapi-uvicorn-gunicorn-docker","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/windvalley/fastapi-uvicorn-gunicorn-docker","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/windvalley%2Ffastapi-uvicorn-gunicorn-docker","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/windvalley%2Ffastapi-uvicorn-gunicorn-docker/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/windvalley%2Ffastapi-uvicorn-gunicorn-docker/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/windvalley%2Ffastapi-uvicorn-gunicorn-docker/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/windvalley","download_url":"https://codeload.github.com/windvalley/fastapi-uvicorn-gunicorn-docker/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/windvalley%2Ffastapi-uvicorn-gunicorn-docker/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":265483318,"owners_count":23774184,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["coroutine","docker","dockerfile","fastapi","gunicorn","parallelism","python38","uvicorn"],"created_at":"2024-11-14T18:17:02.915Z","updated_at":"2026-05-08T00:34:24.280Z","avatar_url":"https://github.com/windvalley.png","language":"Shell","funding_links":[],"categories":[],"sub_categories":[],"readme":"项目说明\n=======\n\n## 文件结构说明\n\n- Dockerfile    \n    默认使用`Python3.8`作为基础镜像.\n- app/main.py    \n    项目的示例入口模块.\n- app/prestart.sh    \n    项目的示例预执行脚本, 在启动`fastapi`前执行的脚本.\n- gunicorn_conf.py    \n    `gunicorn`的配置文件, 使用将在下面介绍的环境变量进行配置, 并设置有默认值.\n- start.sh    \n    容器默认的启动命令, 用于生产环境.\n- start-reload.sh    \n    用于在开发环境下启动容器, 只会运行`uvicorn`, 不会启动`gunicorn`,    \n    `gunicorn_conf.py`文件不起作用, 不过将在下面说明的环境变量都有效.\n\n## 启动容器时可设置的环境变量说明\n\n```\n# Gunicorn要导入的python模块名称;\n# 值为main对应容器内目录结构是/app/main.py;\n# 值为app.main则对应容器内目录结构为/app/app/main.py;\n# 默认为app.main优先, 如不存在则默认main.\nMODULE_NAME=\"app.main\"\n\n# 项目代码入口文件main.py中的FastAPI实例的名称;\n# 默认为app.\nVARIABLE_NAME=\"app\"\n\n# 值默认是MODULE_NAME:VARIABLE_NAME, 可以根据实际情况自定义,\n# 自定义值后会忽略MODULE_NAME和VARIABLE_NAME变量的定义.\nAPP_MODULE=\"app.main:app\"\n\n# 宿主机每个CPU开启几个worker, 默认是1, 如果宿主机只有一个cpu, 则将开启2个worker;\n# 也可以自定义为浮点数, 比如0.5, 此时如果宿主机是4核cpu, 则只会开启2个worker.\nWORKERS_PER_CORE=\"1\"\n\n# 这个变量值默认为WORKERS_PER_CORE变量值*宿主机cpu核数;\n# 我们可以自定义这个变量值来定义一共开启多少个worker,\n# 此时将忽略宿主机有多少核和WORKERS_PER_CORE变量.\nWEB_CONCURRENCY=None\n\n# 容器中Gunicorn监听的地址, 默认为0.0.0.0, 一般不用改.\nHOST=\"0.0.0.0\"\n\n# 容器中Gunicorn监听的端口, 默认为80, 一般不用改.\nPORT=\"80\"\n\n# 容器中Gunicorn监听的地址和端口, 默认值是HOST:PORT, 自定义后将忽略HOST和PORT变量的设置.\nBIND=\"0.0.0.0:80\"\n\n# Gunicorn的日志等级, 默认是info, 全部等级依次为: debug info warning error critical\nLOG_LEVEL=\"info\"\n\n# fastapi项目的预启动脚本的位置, 默认为容器中的/app/prestart.sh\nPRE_START_PATH=\"/app/prestart.sh\"\n\n# 指定Gunicorn的配置文件, 如果不指定,\n# 默认依次查找/app/gunicorn_conf.py、/app/app/gunicorn_conf.py、/gunicorn_conf.py(默认位置)\nGUNICORN_CONF=\"/app/your_custom_gunicorn_conf.py\"\n```\n\n环境变量使用方式举例:\n\n`docker container run -d --name container_name -p80:80 -e WEB_CONCURRENCY=\"2\" image_name`\n\n\n使用说明\n=======\n\n## 构建基础镜像\n\n```bash\ngit clone git@github.com:windvalley/fastapi-uvicorn-gunicorn-docker.git\n\ncd fastapi-uvicorn-gunicorn-docker\n\ndocker image build -t fastapi:python3.8 .\ndocker image ls\n```\n\n## 基于基础镜像构建项目镜像\n\n### 创建并进入到项目目录\n```bash\nmkdir opsapi \u0026\u0026 cd opsapi\n```\n\n### 创建项目的Dockerfile\n```bash\ncat \u003e Dockerfile \u003c\u003c-EOF\n    FROM fastapi:python3.8\n    COPY ./app /app\nEOF\n```\n或者跳过构建基础镜像的步骤, 直接从docker hub上引用该镜像:\n```bash\ncat \u003e Dockerfile \u003c\u003c-EOF\n    FROM windvalley/fastapi-uvicorn-gunicorn:python3.8\n    COPY ./app /app\nEOF\n\n```\n\n### 创建项目源代码目录\n```bash\nmkdir app\n```\n\n### 将fastapi项目代码放到app目录下\n\n如果不放置项目代码, 容器内的`/app`下将是基础镜像提供的示例文件`main.py`和`prestart.sh`;\n\n你的项目代码中的入口文件和预启动文件建议保持文件名`main.py`和`prestart.sh`,\n否则你在启动容器时需要`-e`参数设置相关的环境变量.\n\n### 构建项目镜像\n\n```bash\ndocker image build -t opsapi:0.1 .\ndocker image ls\n```\n\n\n## 运行容器\n\n### 生产环境下运行容器\n\n```bash\ndocker container run -d --name opsapi \\\n    -p 80:80 \\\n    -e WEB_CONCURRENCY=\"6\" \\\n    opsapi:0.1\n```\n\n`fastapi`访问日志和错误日志写到宿主机上的如下目录:\n```bash\ndocker container inspect opsapi -f '{{.Mounts}}'|cut -d' ' -f3\n```\n\n### 开发环境下运行容器\n\n```bash\ndocker container run -d --name opsapi \\\n    -p 80:80 \\\n    -e LOG_LEVEL=\"debug\" \\\n    -v $PWD/app:/app \\\n    opsapi:0.1 /start-reload.sh\n```\n\n查看`fastapi`访问日志和错误日志:\n```bash\ndocker container logs opsapi\n```\n\n## 测试\n\n浏览器分别访问:\n\n```\nlocalhost\n\nlocalhost/docs\n\nlocalhost/redoc\n```\n\n\n参考\n===\n\nhttps://github.com/tiangolo/uvicorn-gunicorn-docker\n\nhttps://github.com/tiangolo/uvicorn-gunicorn-fastapi-docker\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwindvalley%2Ffastapi-uvicorn-gunicorn-docker","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fwindvalley%2Ffastapi-uvicorn-gunicorn-docker","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwindvalley%2Ffastapi-uvicorn-gunicorn-docker/lists"}