├── .github ├── DISCUSSION_TEMPLATE │ └── questions.yml ├── FUNDING.yml ├── ISSUE_TEMPLATE │ ├── config.yml │ └── privileged.yml ├── dependabot.yml ├── labeler.yml └── workflows │ ├── deploy.yml │ ├── issue-manager.yml │ ├── labeler.yml │ ├── latest-changes.yml │ └── test.yml ├── .gitignore ├── LICENSE.txt ├── README.md ├── SECURITY.md ├── docker-images ├── app │ ├── main.py │ ├── prestart.sh │ └── uwsgi.ini ├── entrypoint.sh ├── install-nginx-debian.sh ├── python3.10.dockerfile ├── python3.11.dockerfile ├── python3.12.dockerfile ├── python3.9.dockerfile ├── requirements.txt ├── start.sh ├── stop-supervisor.sh ├── supervisord-alpine.ini ├── supervisord-debian.conf └── uwsgi.ini └── tests ├── .gitignore ├── __init__.py ├── test_01_main ├── __init__.py ├── test_defaults.py └── test_env_vars_1.py ├── test_02_app ├── __init__.py ├── app_with_installs │ └── app │ │ └── main.py ├── broken_app │ └── app │ │ └── main.py ├── custom_app │ ├── application │ │ └── custom_app │ │ │ ├── __init__.py │ │ │ ├── main.py │ │ │ └── uwsgi.ini │ └── prestart.sh ├── custom_nginx_app │ └── app │ │ ├── main.py │ │ └── nginx.conf ├── simple_app │ └── app │ │ └── main.py ├── test_app_and_env_vars.py ├── test_app_with_installs.py ├── test_broken_app.py ├── test_custom_nginx_app.py └── test_simple_app.py └── utils.py /.github/DISCUSSION_TEMPLATE/questions.yml: -------------------------------------------------------------------------------- 1 | labels: [question] 2 | body: 3 | - type: markdown 4 | attributes: 5 | value: | 6 | Thanks for your interest in this project! 🚀 7 | 8 | Please follow these instructions, fill every question, and do every step. 🙏 9 | 10 | I'm asking this because answering questions and solving problems in GitHub is what consumes most of the time. 11 | 12 | I end up not being able to add new features, fix bugs, review pull requests, etc. as fast as I wish because I have to spend too much time handling questions. 13 | 14 | All that, on top of all the incredible help provided by a bunch of community members, that give a lot of their time to come here and help others. 15 | 16 | That's a lot of work, but if more users came to help others like them just a little bit more, it would be much less effort for them (and you and me 😅). 17 | 18 | By asking questions in a structured way (following this) it will be much easier to help you. 19 | 20 | And there's a high chance that you will find the solution along the way and you won't even have to submit it and wait for an answer. 😎 21 | 22 | As there are too many questions, I'll have to discard and close the incomplete ones. That will allow me (and others) to focus on helping people like you that follow the whole process and help us help you. 🤓 23 | - type: checkboxes 24 | id: checks 25 | attributes: 26 | label: First Check 27 | description: Please confirm and check all the following options. 28 | options: 29 | - label: I added a very descriptive title here. 30 | required: true 31 | - label: I used the GitHub search to find a similar question and didn't find it. 32 | required: true 33 | - label: I searched in the documentation/README. 34 | required: true 35 | - label: I already searched in Google "How to do X" and didn't find any information. 36 | required: true 37 | - label: I already read and followed all the tutorial in the docs/README and didn't find an answer. 38 | required: true 39 | - type: checkboxes 40 | id: help 41 | attributes: 42 | label: Commit to Help 43 | description: | 44 | After submitting this, I commit to one of: 45 | 46 | * Read open questions until I find 2 where I can help someone and add a comment to help there. 47 | * I already hit the "watch" button in this repository to receive notifications and I commit to help at least 2 people that ask questions in the future. 48 | 49 | options: 50 | - label: I commit to help with one of those options 👆 51 | required: true 52 | - type: textarea 53 | id: example 54 | attributes: 55 | label: Example Code 56 | description: | 57 | Please add a self-contained, [minimal, reproducible, example](https://stackoverflow.com/help/minimal-reproducible-example) with your use case. 58 | 59 | If I (or someone) can copy it, run it, and see it right away, there's a much higher chance I (or someone) will be able to help you. 60 | 61 | placeholder: | 62 | Write your example code here. 63 | render: Text 64 | validations: 65 | required: true 66 | - type: textarea 67 | id: description 68 | attributes: 69 | label: Description 70 | description: | 71 | What is the problem, question, or error? 72 | 73 | Write a short description telling me what you are doing, what you expect to happen, and what is currently happening. 74 | placeholder: | 75 | * Open the browser and call the endpoint `/`. 76 | * It returns a JSON with `{"message": "Hello World"}`. 77 | * But I expected it to return `{"message": "Hello Morty"}`. 78 | validations: 79 | required: true 80 | - type: dropdown 81 | id: os 82 | attributes: 83 | label: Operating System 84 | description: What operating system are you on? 85 | multiple: true 86 | options: 87 | - Linux 88 | - Windows 89 | - macOS 90 | - Other 91 | validations: 92 | required: true 93 | - type: textarea 94 | id: os-details 95 | attributes: 96 | label: Operating System Details 97 | description: You can add more details about your operating system here, in particular if you chose "Other". 98 | validations: 99 | required: true 100 | - type: input 101 | id: python-version 102 | attributes: 103 | label: Python Version 104 | description: | 105 | What Python version are you using? 106 | 107 | You can find the Python version with: 108 | 109 | ```bash 110 | python --version 111 | ``` 112 | validations: 113 | required: true 114 | - type: textarea 115 | id: context 116 | attributes: 117 | label: Additional Context 118 | description: Add any additional context information or screenshots you think are useful. 119 | -------------------------------------------------------------------------------- /.github/FUNDING.yml: -------------------------------------------------------------------------------- 1 | github: [tiangolo] 2 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/config.yml: -------------------------------------------------------------------------------- 1 | blank_issues_enabled: false 2 | contact_links: 3 | - name: Security Contact 4 | about: Please report security vulnerabilities to security@tiangolo.com 5 | - name: Question or Problem 6 | about: Ask a question or ask about a problem in GitHub Discussions. 7 | url: https://github.com/tiangolo/uwsgi-nginx-docker/discussions/categories/questions 8 | - name: Feature Request 9 | about: To suggest an idea or ask about a feature, please start with a question saying what you would like to achieve. There might be a way to do it already. 10 | url: https://github.com/tiangolo/uwsgi-nginx-docker/discussions/categories/questions 11 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/privileged.yml: -------------------------------------------------------------------------------- 1 | name: Privileged 2 | description: You are @tiangolo or he asked you directly to create an issue here. If not, check the other options. 👇 3 | body: 4 | - type: markdown 5 | attributes: 6 | value: | 7 | Thanks for your interest in this project! 🚀 8 | 9 | If you are not @tiangolo or he didn't ask you directly to create an issue here, please start the conversation in a [Question in GitHub Discussions](https://github.com/tiangolo/uwsgi-nginx-docker/discussions/categories/questions) instead. 10 | - type: checkboxes 11 | id: privileged 12 | attributes: 13 | label: Privileged issue 14 | description: Confirm that you are allowed to create an issue here. 15 | options: 16 | - label: I'm @tiangolo or he asked me directly to create an issue here. 17 | required: true 18 | - type: textarea 19 | id: content 20 | attributes: 21 | label: Issue Content 22 | description: Add the content of the issue here. 23 | -------------------------------------------------------------------------------- /.github/dependabot.yml: -------------------------------------------------------------------------------- 1 | version: 2 2 | updates: 3 | # GitHub Actions 4 | - package-ecosystem: "github-actions" 5 | directory: "/" 6 | schedule: 7 | interval: "daily" 8 | commit-message: 9 | prefix: ⬆ 10 | # Python 11 | - package-ecosystem: "pip" 12 | directory: "/" 13 | schedule: 14 | interval: "daily" 15 | commit-message: 16 | prefix: ⬆ 17 | -------------------------------------------------------------------------------- /.github/labeler.yml: -------------------------------------------------------------------------------- 1 | internal: 2 | - all: 3 | - changed-files: 4 | - any-glob-to-any-file: 5 | - .github/** 6 | - .gitignore 7 | - all-globs-to-all-files: 8 | - '!docker-images/requirements.txt' 9 | 10 | upgrade: 11 | - all: 12 | - changed-files: 13 | - any-glob-to-any-file: 14 | - docker-images/requirements.txt 15 | -------------------------------------------------------------------------------- /.github/workflows/deploy.yml: -------------------------------------------------------------------------------- 1 | name: Deploy 2 | 3 | on: 4 | push: 5 | branches: 6 | - master 7 | schedule: 8 | # cron every week on monday 9 | - cron: "0 0 * * 1" 10 | 11 | jobs: 12 | deploy: 13 | strategy: 14 | matrix: 15 | image: 16 | - name: latest 17 | python_version: "3.12" 18 | - name: python3.12 19 | python_version: "3.12" 20 | - name: python3.11 21 | python_version: "3.11" 22 | - name: python3.10 23 | python_version: "3.10" 24 | - name: python3.9 25 | python_version: "3.9" 26 | fail-fast: true 27 | runs-on: ubuntu-latest 28 | steps: 29 | - uses: actions/checkout@v4 30 | - name: Set Dockerfile name 31 | if: matrix.image.name != 'latest' 32 | run: echo "DOCKERFILE_NAME=${{ matrix.image.name }}" >> $GITHUB_ENV 33 | - name: Set Dockerfile name latest 34 | if: matrix.image.name == 'latest' 35 | run: echo "DOCKERFILE_NAME=python${{ matrix.image.python_version }}" >> $GITHUB_ENV 36 | - name: Set up Docker Buildx 37 | uses: docker/setup-buildx-action@v3 38 | - name: Login to DockerHub 39 | uses: docker/login-action@v3 40 | with: 41 | username: ${{ secrets.DOCKERHUB_USERNAME }} 42 | password: ${{ secrets.DOCKERHUB_TOKEN }} 43 | - name: Get date for tags 44 | run: echo "DATE_TAG=$(date -I)" >> "$GITHUB_ENV" 45 | - name: Build and push 46 | uses: docker/build-push-action@v6 47 | with: 48 | push: true 49 | platforms: linux/amd64,linux/arm64 50 | tags: | 51 | tiangolo/uwsgi-nginx:${{ matrix.image.name }} 52 | tiangolo/uwsgi-nginx:${{ matrix.image.name }}-${{ env.DATE_TAG }} 53 | context: ./docker-images/ 54 | file: ./docker-images/${{ env.DOCKERFILE_NAME }}.dockerfile 55 | - name: Docker Hub Description 56 | uses: peter-evans/dockerhub-description@v4 57 | with: 58 | username: ${{ secrets.DOCKERHUB_USERNAME }} 59 | password: ${{ secrets.DOCKERHUB_TOKEN }} 60 | repository: tiangolo/uwsgi-nginx 61 | -------------------------------------------------------------------------------- /.github/workflows/issue-manager.yml: -------------------------------------------------------------------------------- 1 | name: Issue Manager 2 | 3 | on: 4 | schedule: 5 | - cron: "15 11 * * *" 6 | issue_comment: 7 | types: 8 | - created 9 | issues: 10 | types: 11 | - labeled 12 | pull_request_target: 13 | types: 14 | - labeled 15 | workflow_dispatch: 16 | 17 | permissions: 18 | issues: write 19 | pull-requests: write 20 | 21 | jobs: 22 | issue-manager: 23 | runs-on: ubuntu-latest 24 | steps: 25 | - uses: tiangolo/issue-manager@0.5.1 26 | with: 27 | token: ${{ secrets.GITHUB_TOKEN }} 28 | config: > 29 | { 30 | "answered": { 31 | "delay": 864000, 32 | "message": "Assuming the original need was handled, this will be automatically closed now. But feel free to add more comments or create new issues or PRs." 33 | }, 34 | "waiting": { 35 | "delay": 2628000, 36 | "message": "As this PR has been waiting for the original user for a while but seems to be inactive, it's now going to be closed. But if there's anyone interested, feel free to create a new PR." 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /.github/workflows/labeler.yml: -------------------------------------------------------------------------------- 1 | name: Labels 2 | on: 3 | pull_request_target: 4 | types: 5 | - opened 6 | - synchronize 7 | - reopened 8 | # For label-checker 9 | - labeled 10 | - unlabeled 11 | 12 | jobs: 13 | labeler: 14 | permissions: 15 | contents: read 16 | pull-requests: write 17 | runs-on: ubuntu-latest 18 | steps: 19 | - uses: actions/labeler@v5 20 | if: ${{ github.event.action != 'labeled' && github.event.action != 'unlabeled' }} 21 | - run: echo "Done adding labels" 22 | # Run this after labeler applied labels 23 | check-labels: 24 | needs: 25 | - labeler 26 | permissions: 27 | pull-requests: read 28 | runs-on: ubuntu-latest 29 | steps: 30 | - uses: docker://agilepathway/pull-request-label-checker:latest 31 | with: 32 | one_of: breaking,security,feature,bug,refactor,upgrade,docs,internal 33 | repo_token: ${{ secrets.GITHUB_TOKEN }} 34 | -------------------------------------------------------------------------------- /.github/workflows/latest-changes.yml: -------------------------------------------------------------------------------- 1 | name: Latest Changes 2 | 3 | on: 4 | pull_request_target: 5 | branches: 6 | - master 7 | types: 8 | - closed 9 | workflow_dispatch: 10 | inputs: 11 | number: 12 | description: PR number 13 | required: true 14 | debug_enabled: 15 | description: 'Run the build with tmate debugging enabled (https://github.com/marketplace/actions/debugging-with-tmate)' 16 | required: false 17 | default: 'false' 18 | 19 | jobs: 20 | latest-changes: 21 | runs-on: ubuntu-latest 22 | steps: 23 | - uses: actions/checkout@v4.1.2 24 | with: 25 | # To allow latest-changes to commit to master 26 | token: ${{ secrets.UWSGI_NGINX_DOCKER_LATEST_CHANGES }} 27 | - uses: tiangolo/latest-changes@0.3.2 28 | with: 29 | token: ${{ secrets.GITHUB_TOKEN }} 30 | -------------------------------------------------------------------------------- /.github/workflows/test.yml: -------------------------------------------------------------------------------- 1 | name: Test 2 | 3 | on: 4 | push: 5 | branches: 6 | - master 7 | pull_request: 8 | types: 9 | - opened 10 | - synchronize 11 | schedule: 12 | # cron every week on monday 13 | - cron: "0 0 * * 1" 14 | 15 | jobs: 16 | test: 17 | strategy: 18 | matrix: 19 | image: 20 | - name: latest 21 | python_version: "3.12" 22 | - name: python3.12 23 | python_version: "3.12" 24 | - name: python3.11 25 | python_version: "3.11" 26 | - name: python3.10 27 | python_version: "3.10" 28 | - name: python3.9 29 | python_version: "3.9" 30 | fail-fast: true 31 | runs-on: ubuntu-latest 32 | steps: 33 | - uses: actions/checkout@v4 34 | - name: Set Dockerfile name 35 | if: matrix.image.name != 'latest' 36 | run: echo "DOCKERFILE_NAME=${{ matrix.image.name }}" >> $GITHUB_ENV 37 | - name: Set Dockerfile name latest 38 | if: matrix.image.name == 'latest' 39 | run: echo "DOCKERFILE_NAME=python${{ matrix.image.python_version }}" >> $GITHUB_ENV 40 | - name: Build 41 | uses: docker/build-push-action@v6 42 | with: 43 | push: false 44 | tags: tiangolo/uwsgi-nginx:${{ matrix.image.name }} 45 | context: ./docker-images/ 46 | file: ./docker-images/${{ env.DOCKERFILE_NAME }}.dockerfile 47 | - name: Set up Python 48 | uses: actions/setup-python@v5 49 | with: 50 | python-version: "3.10" 51 | - name: Install Dependencies 52 | run: python -m pip install docker pytest 53 | - name: Test Image 54 | run: pytest tests 55 | env: 56 | NAME: ${{ matrix.image.name }} 57 | PYTHON_VERSION: ${{ matrix.image.python_version }} 58 | SLEEP_TIME: "5" 59 | check: 60 | if: always() 61 | needs: 62 | - test 63 | runs-on: ubuntu-latest 64 | steps: 65 | - name: Decide whether the needed jobs succeeded or failed 66 | uses: re-actors/alls-green@release/v1 67 | with: 68 | jobs: ${{ toJSON(needs) }} 69 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | ### Python template 2 | # Byte-compiled / optimized / DLL files 3 | __pycache__/ 4 | *.py[cod] 5 | *$py.class 6 | .mypy_cache 7 | .vscode 8 | 9 | # C extensions 10 | *.so 11 | 12 | # Distribution / packaging 13 | .Python 14 | env/ 15 | build/ 16 | develop-eggs/ 17 | dist/ 18 | downloads/ 19 | eggs/ 20 | .eggs/ 21 | lib/ 22 | lib64/ 23 | parts/ 24 | sdist/ 25 | var/ 26 | *.egg-info/ 27 | .installed.cfg 28 | *.egg 29 | 30 | # PyInstaller 31 | # Usually these files are written by a python script from a template 32 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 33 | *.manifest 34 | *.spec 35 | 36 | # Installer logs 37 | pip-log.txt 38 | pip-delete-this-directory.txt 39 | 40 | # Unit test / coverage reports 41 | htmlcov/ 42 | .tox/ 43 | .coverage 44 | .coverage.* 45 | .cache 46 | nosetests.xml 47 | coverage.xml 48 | *,cover 49 | 50 | # Translations 51 | *.mo 52 | *.pot 53 | 54 | # Django stuff: 55 | *.log 56 | 57 | # Sphinx documentation 58 | docs/_build/ 59 | 60 | # PyBuilder 61 | target/ 62 | ### JetBrains template 63 | # Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio 64 | 65 | *.iml 66 | 67 | ## Directory-based project format: 68 | .idea/ 69 | # if you remove the above rule, at least ignore the following: 70 | 71 | # User-specific stuff: 72 | # .idea/workspace.xml 73 | # .idea/tasks.xml 74 | # .idea/dictionaries 75 | 76 | # Sensitive or high-churn files: 77 | # .idea/dataSources.ids 78 | # .idea/dataSources.xml 79 | # .idea/sqlDataSources.xml 80 | # .idea/dynamic.xml 81 | # .idea/uiDesigner.xml 82 | 83 | # Gradle: 84 | # .idea/gradle.xml 85 | # .idea/libraries 86 | 87 | # Mongo Explorer plugin: 88 | # .idea/mongoSettings.xml 89 | 90 | ## File-based project format: 91 | *.ipr 92 | *.iws 93 | 94 | ## Plugin-specific files: 95 | 96 | # IntelliJ 97 | /out/ 98 | 99 | # mpeltonen/sbt-idea plugin 100 | .idea_modules/ 101 | 102 | # JIRA plugin 103 | atlassian-ide-plugin.xml 104 | 105 | # Crashlytics plugin (for Android Studio and IntelliJ) 106 | com_crashlytics_export_strings.xml 107 | crashlytics.properties 108 | crashlytics-build.properties 109 | 110 | # Custom 111 | Pipfile.lock 112 | poetry.lock 113 | -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "{}" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright {yyyy} {name of copyright owner} 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | [![Test](https://github.com/tiangolo/uwsgi-nginx-docker/actions/workflows/test.yml/badge.svg)](https://github.com/tiangolo/uwsgi-nginx-docker/actions/workflows/test.yml) [![Deploy](https://github.com/tiangolo/uwsgi-nginx-docker/workflows/Deploy/badge.svg)](https://github.com/tiangolo/uwsgi-nginx-docker/actions?query=workflow%3ADeploy) 2 | 3 | ## Supported tags and respective `Dockerfile` links 4 | 5 | * [`python3.12`, `latest` _(Dockerfile)_](https://github.com/tiangolo/uwsgi-nginx-docker/blob/master/docker-images/python3.12.dockerfile) 6 | * [`python3.11`, _(Dockerfile)_](https://github.com/tiangolo/uwsgi-nginx-docker/blob/master/docker-images/python3.11.dockerfile) 7 | * [`python3.10`, _(Dockerfile)_](https://github.com/tiangolo/uwsgi-nginx-docker/blob/master/docker-images/python3.10.dockerfile) 8 | * [`python3.9`, _(Dockerfile)_](https://github.com/tiangolo/uwsgi-nginx-docker/blob/master/docker-images/python3.9.dockerfile) 9 | 10 | ## Deprecated tags 11 | 12 | 🚨 These tags are no longer supported or maintained, they are removed from the GitHub repository, but the last versions pushed might still be available in Docker Hub if anyone has been pulling them: 13 | 14 | * `python3.8` 15 | * `python3.8-alpine` 16 | * `python3.7` 17 | * `python3.6` 18 | * `python2.7` 19 | 20 | The last date tags for these versions are: 21 | 22 | * `python3.8-2024-10-28` 23 | * `python3.8-alpine-2024-03-11` 24 | * `python3.7-2024-10-28` 25 | * `python3.6-2022-11-25` 26 | * `python2.7-2022-11-25` 27 | 28 | --- 29 | 30 | **Note**: There are [tags for each build date](https://hub.docker.com/r/tiangolo/uwsgi-nginx/tags). If you need to "pin" the Docker image version you use, you can select one of those tags. E.g. `tiangolo/uwsgi-nginx:python3.12-2024-11-02`. 31 | 32 | # uwsgi-nginx 33 | 34 | **Docker** image with **uWSGI** and **Nginx** for web applications in **Python** (as **Flask**) in a single container. 35 | 36 | ## Description 37 | 38 | This [**Docker**](https://www.docker.com/) image allows you to create [**Python**](https://www.python.org/) web applications that run with [**uWSGI**](https://uwsgi-docs.readthedocs.org/en/latest/) and [**Nginx**](http://nginx.org/en/) in a single container. 39 | 40 | The combination of uWSGI with Nginx is a [common way to deploy Python web applications like Flask and Django](http://flask.pocoo.org/docs/1.0/deploying/uwsgi/). It is widely used in the industry and would give you decent performance. (*) 41 | 42 | This image was created to be the base image for [**tiangolo/uwsgi-nginx-flask**](https://hub.docker.com/r/tiangolo/uwsgi-nginx-flask/) but could be used as the base image for any other (WSGI-based) Python web application, like Django. 43 | 44 | ### * Note on performance and features 45 | 46 | If you are starting a new project, you might benefit from a newer and faster framework based on ASGI instead of WSGI (Flask and Django are WSGI-based). 47 | 48 | You could use an ASGI framework like: 49 | 50 | * [**FastAPI**](https://github.com/tiangolo/fastapi) (which is based on Starlette) with this Docker image: [**tiangolo/uvicorn-gunicorn-fastapi**](https://github.com/tiangolo/uvicorn-gunicorn-fastapi-docker). 51 | * [**Starlette**](https://github.com/encode/starlette) directly, with this Docker image: [**tiangolo/uvicorn-gunicorn-starlette**](https://github.com/tiangolo/uvicorn-gunicorn-starlette-docker). 52 | * Or any other ASGI framework with this Docker image: [**tiangolo/uvicorn-gunicorn**](https://github.com/tiangolo/uvicorn-gunicorn-docker). 53 | 54 | FastAPI, or Starlette, would give you about 800% (8x) the performance achievable with this image (**tiangolo/uwsgi-nginx**). [You can see the third-party benchmarks here](https://www.techempower.com/benchmarks/#section=test&runid=a979de55-980d-4721-a46f-77298b3f3923&hw=ph&test=query&l=z8kflr-v&a=2). 55 | 56 | Also, if you want to use new technologies like WebSockets it would be easier (and *possible*) with a newer framework based on ASGI, like FastAPI or Starlette. As the standard ASGI was designed to be able to handle asynchronous code like the one needed for WebSockets. 57 | 58 | #### If you need a WSGI-based application (like Flask or Django) 59 | 60 | If you need to use an older WSGI-based framework like Flask or Django (instead of something based on ASGI) and you need to have the best performance possible, you can use the alternative image: [**tiangolo/meinheld-gunicorn**](https://github.com/tiangolo/meinheld-gunicorn-docker). 61 | 62 | **tiangolo/meinheld-gunicorn** will give you about 400% (4x) the performance of this image. 63 | 64 | --- 65 | 66 | **GitHub repo**: [https://github.com/tiangolo/uwsgi-nginx-docker](https://github.com/tiangolo/uwsgi-nginx-docker) 67 | 68 | **Docker Hub image**: [https://hub.docker.com/r/tiangolo/uwsgi-nginx/](https://hub.docker.com/r/tiangolo/uwsgi-nginx/) 69 | 70 | ## 🚨 WARNING: You Probably Don't Need this Docker Image 71 | 72 | You are probably using **Kubernetes** or similar tools. In that case, you probably **don't need this image** (or any other **similar base image**). You are probably better off **building a Docker image from scratch**. 73 | 74 | --- 75 | 76 | If you have a cluster of machines with **Kubernetes**, Docker Swarm Mode, Nomad, or other similar complex system to manage distributed containers on multiple machines, then you will probably want to **handle replication** at the **cluster level** instead of using a **process manager** in each container that starts multiple **worker processes**, which is what this Docker image does. 77 | 78 | In those cases (e.g. using Kubernetes) you would probably want to build a **Docker image from scratch**, installing your dependencies, and running **a single process** instead of this image. 79 | 80 | For example, using [Gunicorn](https://gunicorn.org/) you could have a file `app/gunicorn_conf.py` with: 81 | 82 | ```Python 83 | # Gunicorn config variables 84 | loglevel = "info" 85 | errorlog = "-" # stderr 86 | accesslog = "-" # stdout 87 | worker_tmp_dir = "/dev/shm" 88 | graceful_timeout = 120 89 | timeout = 120 90 | keepalive = 5 91 | threads = 3 92 | ``` 93 | 94 | And then you could have a `Dockerfile` with: 95 | 96 | ```Dockerfile 97 | FROM python:3.9 98 | 99 | WORKDIR /code 100 | 101 | COPY ./requirements.txt /code/requirements.txt 102 | 103 | RUN pip install --no-cache-dir --upgrade -r /code/requirements.txt 104 | 105 | COPY ./app /code/app 106 | 107 | CMD ["gunicorn", "--conf", "app/gunicorn_conf.py", "--bind", "0.0.0.0:80", "app.main:app"] 108 | ``` 109 | 110 | You can read more about these ideas in the [FastAPI documentation about: FastAPI in Containers - Docker](https://fastapi.tiangolo.com/deployment/docker/#replication-number-of-processes) as the same ideas would apply to other web applications in containers. 111 | 112 | ## When to Use this Docker Image 113 | 114 | ### A Simple App 115 | 116 | You could want a process manager running multiple worker processes in the container if your application is **simple enough** that you don't need (at least not yet) to fine-tune the number of processes too much, and you can just use an automated default, and you are running it on a **single server**, not a cluster. 117 | 118 | ### Docker Compose 119 | 120 | You could be deploying to a **single server** (not a cluster) with **Docker Compose**, so you wouldn't have an easy way to manage replication of containers (with Docker Compose) while preserving the shared network and **load balancing**. 121 | 122 | Then you could want to have **a single container** with a **process manager** starting **several worker processes** inside, as this Docker image does. 123 | 124 | ### Prometheus and Other Reasons 125 | 126 | You could also have **other reasons** that would make it easier to have a **single container** with **multiple processes** instead of having **multiple containers** with **a single process** in each of them. 127 | 128 | For example (depending on your setup) you could have some tool like a Prometheus exporter in the same container that should have access to **each of the requests** that come. 129 | 130 | In this case, if you had **multiple containers**, by default, when Prometheus came to **read the metrics**, it would get the ones for **a single container each time** (for the container that handled that particular request), instead of getting the **accumulated metrics** for all the replicated containers. 131 | 132 | Then, in that case, it could be simpler to have **one container** with **multiple processes**, and a local tool (e.g. a Prometheus exporter) on the same container collecting Prometheus metrics for all the internal processes and exposing those metrics on that single container. 133 | 134 | --- 135 | 136 | Read more about it all in the [FastAPI documentation about: FastAPI in Containers - Docker](https://fastapi.tiangolo.com/deployment/docker/), as the same concepts apply to other web applications in containers. 137 | 138 | ## How to use 139 | 140 | You don't have to clone this repo. 141 | 142 | You can use this image as a base image for other images. 143 | 144 | Assuming you have a file `requirements.txt`, you could have a `Dockerfile` like this: 145 | 146 | ```Dockerfile 147 | FROM tiangolo/uwsgi-nginx:python3.12 148 | 149 | COPY ./requirements.txt /app/requirements.txt 150 | 151 | RUN pip install --no-cache-dir --upgrade -r /app/requirements.txt 152 | 153 | COPY ./app /app 154 | 155 | # Your Dockerfile code... 156 | ``` 157 | 158 | * By default it will try to find a uWSGI config file in `/app/uwsgi.ini`. 159 | 160 | * That `uwsgi.ini` file will make it try to run a Python file in `/app/main.py`. 161 | 162 | If you are building a **Flask** web application you should use instead [**tiangolo/uwsgi-nginx-flask**](https://hub.docker.com/r/tiangolo/uwsgi-nginx-flask/). 163 | 164 | ## Advanced usage 165 | 166 | ### Custom app directory 167 | 168 | If you need to use a directory for your app different than `/app`, you can override the uWSGI config file path with an environment variable `UWSGI_INI`, and put your custom `uwsgi.ini` file there. 169 | 170 | For example, if you needed to have your application directory in `/application` instead of `/app`, your `Dockerfile` would look like: 171 | 172 | ```Dockerfile 173 | FROM tiangolo/uwsgi-nginx:python3.12 174 | 175 | ENV UWSGI_INI /application/uwsgi.ini 176 | 177 | COPY ./application /application 178 | WORKDIR /appapplication 179 | ``` 180 | 181 | And your `uwsgi.ini` file in `./application/uwsgi.ini` would contain: 182 | 183 | ```ini 184 | [uwsgi] 185 | wsgi-file=/application/main.py 186 | ``` 187 | 188 | **Note**: it's important to include the `WORKDIR` option, otherwise uWSGI will start the application in `/app`. 189 | 190 | ### Custom uWSGI process number 191 | 192 | By default, the image starts with 2 uWSGI processes running. When the server is experiencing a high load, it creates up to 16 uWSGI processes to handle it on demand. 193 | 194 | If you need to configure these numbers you can use environment variables. 195 | 196 | The starting number of uWSGI processes is controlled by the variable `UWSGI_CHEAPER`, by default set to `2`. 197 | 198 | The maximum number of uWSGI processes is controlled by the variable `UWSGI_PROCESSES`, by default set to `16`. 199 | 200 | Have in mind that `UWSGI_CHEAPER` must be lower than `UWSGI_PROCESSES`. 201 | 202 | So, if, for example, you need to start with 4 processes and grow to a maximum of 64, your `Dockerfile` could look like: 203 | 204 | ```Dockerfile 205 | FROM tiangolo/uwsgi-nginx:python3.12 206 | 207 | ENV UWSGI_CHEAPER 4 208 | ENV UWSGI_PROCESSES 64 209 | 210 | COPY ./app /app 211 | ``` 212 | 213 | ### Custom max upload size 214 | 215 | In this image, Nginx is configured to allow unlimited upload file sizes. This is done because by default a simple Python server would allow that, so that's the simplest behavior a developer would expect. 216 | 217 | If you need to restrict the maximum upload size in Nginx, you can add an environment variable `NGINX_MAX_UPLOAD` and assign a value corresponding to the [standard Nginx config `client_max_body_size`](http://nginx.org/en/docs/http/ngx_http_core_module.html#client_max_body_size). 218 | 219 | For example, if you wanted to set the maximum upload file size to 1 MB (the default in a normal Nginx installation), you would need to set the `NGINX_MAX_UPLOAD` environment variable to the value `1m`. Then the image would take care of adding the corresponding configuration file (this is done by the `entrypoint.sh`). 220 | 221 | So, your `Dockerfile` would look something like: 222 | 223 | ```Dockerfile 224 | FROM tiangolo/uwsgi-nginx:python3.12 225 | 226 | ENV NGINX_MAX_UPLOAD 1m 227 | 228 | COPY ./app /app 229 | ``` 230 | 231 | ### Custom listen port 232 | 233 | By default, the container made from this image will listen on port 80. 234 | 235 | To change this behavior, set the `LISTEN_PORT` environment variable. 236 | 237 | You might also need to create the respective `EXPOSE` Docker instruction. 238 | 239 | You can do that in your `Dockerfile`, it would look something like: 240 | 241 | ```Dockerfile 242 | FROM tiangolo/uwsgi-nginx:python3.12 243 | 244 | ENV LISTEN_PORT 8080 245 | 246 | EXPOSE 8080 247 | 248 | COPY ./app /app 249 | ``` 250 | 251 | ### Custom `/app/prestart.sh` 252 | 253 | If you need to run anything before starting the app, you can add a file `prestart.sh` to the directory `/app`. The image will automatically detect and run it before starting everything. 254 | 255 | For example, if you want to add database migrations that are run on startup (e.g. with Alembic, or Django migrations), before starting the app, you could create a `./app/prestart.sh` file in your code directory (that will be copied by your `Dockerfile`) with: 256 | 257 | ```bash 258 | #! /usr/bin/env bash 259 | 260 | # Let the DB start 261 | sleep 10; 262 | # Run migrations 263 | alembic upgrade head 264 | ``` 265 | 266 | and it would wait 10 seconds to give the database some time to start and then run that `alembic` command (you could update that to run Django migrations or any other tool you need). 267 | 268 | If you need to run a Python script before starting the app, you could make the `/app/prestart.sh` file run your Python script, with something like: 269 | 270 | ```bash 271 | #! /usr/bin/env bash 272 | 273 | # Run custom Python script before starting 274 | python /app/my_custom_prestart_script.py 275 | ``` 276 | 277 | **Note**: The image uses `.` to run the script (as in `. /app/prestart.sh`), so for example, environment variables would persist. If you don't understand the previous sentence, you probably don't need it. 278 | 279 | ### Custom Nginx processes number 280 | 281 | By default, Nginx will start one "worker process". 282 | 283 | If you want to set a different number of Nginx worker processes you can use the environment variable `NGINX_WORKER_PROCESSES`. 284 | 285 | You can use a specific single number, e.g.: 286 | 287 | ```Dockerfile 288 | ENV NGINX_WORKER_PROCESSES 2 289 | ``` 290 | 291 | or you can set it to the keyword `auto` and it will try to autodetect the number of CPUs available and use that for the number of workers. 292 | 293 | For example, using `auto`, your Dockerfile could look like: 294 | 295 | ```Dockerfile 296 | FROM tiangolo/uwsgi-nginx:python3.12 297 | 298 | ENV NGINX_WORKER_PROCESSES auto 299 | 300 | COPY ./app /app 301 | ``` 302 | 303 | ### Custom Nginx maximum connections per worker 304 | 305 | By default, Nginx will start with a maximum limit of 1024 connections per worker. 306 | 307 | If you want to set a different number you can use the environment variable `NGINX_WORKER_CONNECTIONS`, e.g: 308 | 309 | ```Dockerfile 310 | ENV NGINX_WORKER_CONNECTIONS 2048 311 | ``` 312 | 313 | It cannot exceed the current limit on the maximum number of open files. See how to configure it in the next section. 314 | 315 | ### Custom Nginx maximum open files 316 | 317 | The number connections per Nginx worker cannot exceed the limit on the maximum number of open files. 318 | 319 | You can change the limit of open files with the environment variable `NGINX_WORKER_OPEN_FILES`, e.g.: 320 | 321 | ```Dockerfile 322 | ENV NGINX_WORKER_OPEN_FILES 2048 323 | ``` 324 | 325 | ### Customizing Nginx additional configurations 326 | 327 | If you need to configure Nginx further, you can add `*.conf` files to `/etc/nginx/conf.d/` in your `Dockerfile`. 328 | 329 | Just have in mind that the default configurations are created during startup in a file at `/etc/nginx/conf.d/nginx.conf` and `/etc/nginx/conf.d/upload.conf`. So you shouldn't overwrite them. You should name your `*.conf` file with something different than `nginx.conf` or `upload.conf`, for example: `custom.conf`. 330 | 331 | **Note**: if you are customizing Nginx, maybe copying configurations from a blog or a StackOverflow answer, have in mind that you probably need to use the [configurations specific to uWSGI](http://nginx.org/en/docs/http/ngx_http_uwsgi_module.html), instead of those for other modules, like for example, `ngx_http_fastcgi_module`. 332 | 333 | ### Overriding Nginx configuration completely 334 | 335 | If you need to configure Nginx even further, completely overriding the defaults, you can add a custom Nginx configuration to `/app/nginx.conf`. 336 | 337 | It will be copied to `/etc/nginx/nginx.conf` and used instead of the generated one. 338 | 339 | Have in mind that, in that case, this image won't generate any of the Nginx configurations, it will only copy and use your configuration file. 340 | 341 | That means that all the environment variables described above that are specific to Nginx won't be used. 342 | 343 | It also means that it won't use additional configurations from files in `/etc/nginx/conf.d/*.conf`, unless you explicitly have a section in your custom file `/app/nginx.conf` with: 344 | 345 | ```conf 346 | include /etc/nginx/conf.d/*.conf; 347 | ``` 348 | 349 | If you want to add a custom `/app/nginx.conf` file but don't know where to start from, you can use [the `nginx.conf` used for the tests](https://github.com/tiangolo/uwsgi-nginx-docker/blob/master/tests/test_02_app/custom_nginx_app/app/nginx.conf) and customize it or modify it further. 350 | 351 | ## Technical details 352 | 353 | The combination of uWSGI with Nginx is a [common way to deploy Python web applications](http://flask.pocoo.org/docs/1.0/deploying/uwsgi/). 354 | 355 | Roughly: 356 | 357 | * **Nginx** is a web server, it takes care of the HTTP connections and also can serve static files directly and more efficiently. 358 | 359 | * **uWSGI** is an application server, that's what runs your Python code and it talks with Nginx. 360 | 361 | * **Your Python code** has the actual web application, and is run by uWSGI. 362 | 363 | This image takes advantage of already slim and optimized existing Docker images (based on Debian as [recommended by Docker](https://docs.docker.com/engine/userguide/eng-image/dockerfile_best-practices/)) and implements Docker best practices. 364 | 365 | It uses the official Python Docker image, installs uWSGI and on top of that, with the least amount of modifications, adds the official Nginx image (as of 2016-02-14). 366 | 367 | And it controls all these processes with Supervisord. 368 | 369 | --- 370 | 371 | There's the rule of thumb that you should have "one process per container". 372 | 373 | That helps, for example, isolating an app and its database in different containers. 374 | 375 | But if you want to have a "micro-services" approach you may want to [have more than one process in one container](https://valdhaus.co/writings/docker-misconceptions/) if they are all related to the same "service", and you may want to include your Flask code, uWSGI and Nginx in the same container (and maybe run another container with your database). 376 | 377 | That's the approach taken in this image. 378 | 379 | --- 380 | 381 | This image has a default sample "Hello World" app in the container's `/app` directory using the example in the [uWSGI documentation](http://uwsgi-docs.readthedocs.org/en/latest/WSGIquickstart.html). 382 | 383 | You probably want to override it or delete it in your project. 384 | 385 | It is there in case you run this image by itself and not as a base image for your own `Dockerfile`, so that you get a sample app without errors. 386 | 387 | ## 🚨 Alpine Python Warning 388 | 389 | In short: You probably shouldn't use Alpine for Python projects, instead use the `slim` Docker image versions. 390 | 391 | --- 392 | 393 | Do you want more details? Continue reading 👇 394 | 395 | Alpine is more useful for other languages where you build a static binary in one Docker image stage (using multi-stage Docker building) and then copy it to a simple Alpine image, and then just execute that binary. For example, using Go. 396 | 397 | But for Python, as Alpine doesn't use the standard tooling used for building Python extensions, when installing packages, in many cases Python (`pip`) won't find a precompiled installable package (a "wheel") for Alpine. And after debugging lots of strange errors you will realize that you have to install a lot of extra tooling and build a lot of dependencies just to use some of these common Python packages. 😩 398 | 399 | This means that, although the original Alpine image might have been small, you end up with a an image with a size comparable to the size you would have gotten if you had just used a standard Python image (based on Debian), or in some cases even larger. 🤯 400 | 401 | And in all those cases, it will take much longer to build, consuming much more resources, building dependencies for longer, and also increasing its carbon footprint, as you are using more CPU time and energy for each build. 🌳 402 | 403 | If you want slim Python images, you should instead try and use the `slim` versions that are still based on Debian, but are smaller. 🤓 404 | 405 | ## Tests 406 | 407 | All the image tags, configurations, environment variables and application options are tested. 408 | 409 | ## Updates 410 | 411 | Updates are announced in the releases. 412 | 413 | You can click the "watch" button at the top right and select "Releases only" to receive an email notification when there's a new release. 414 | 415 | ## Release Notes 416 | 417 | ### Latest Changes 418 | 419 | #### Features 420 | 421 | * ♻️ Add support for Python 3.12. PR [#228](https://github.com/tiangolo/uwsgi-nginx-docker/pull/228) by [@tiangolo](https://github.com/tiangolo). 422 | * 🔧 Avoid creating unnecessary `*.pyc` files with `PYTHONDONTWRITEBYTECODE=1` and ensure logs are printed immediately with `PYTHONUNBUFFERED=1`. PR [#208](https://github.com/tiangolo/uwsgi-nginx-docker/pull/208) by [@estebanx64](https://github.com/estebanx64). 423 | 424 | #### Refactors 425 | 426 | * ♻️ Do not `EXPOSE` ports `80` and `443` by default as they can be customized. PR [#227](https://github.com/tiangolo/uwsgi-nginx-docker/pull/227) by [@tiangolo](https://github.com/tiangolo). 427 | 428 | #### Upgrades 429 | 430 | * ⬆ Bump uwsgi from 2.0.29 to 2.0.30. PR [#240](https://github.com/tiangolo/uwsgi-nginx-docker/pull/240) by [@dependabot[bot]](https://github.com/apps/dependabot). 431 | * ⬆ Bump uwsgi from 2.0.28 to 2.0.29. PR [#237](https://github.com/tiangolo/uwsgi-nginx-docker/pull/237) by [@dependabot[bot]](https://github.com/apps/dependabot). 432 | * ⬆️ Upgrade Docker images to Debian Bookworm and latest Nginx. PR [#238](https://github.com/tiangolo/uwsgi-nginx-docker/pull/238) by [@tiangolo](https://github.com/tiangolo). 433 | * ⬆ Bump uwsgi from 2.0.26 to 2.0.28. PR [#232](https://github.com/tiangolo/uwsgi-nginx-docker/pull/232) by [@dependabot[bot]](https://github.com/apps/dependabot). 434 | * 🔥 Drop support for Python 3.7 and 3.8. PR [#233](https://github.com/tiangolo/uwsgi-nginx-docker/pull/233) by [@tiangolo](https://github.com/tiangolo). 435 | * ⬆ Bump uwsgi from 2.0.22 to 2.0.26. PR [#210](https://github.com/tiangolo/uwsgi-nginx-docker/pull/210) by [@dependabot[bot]](https://github.com/apps/dependabot). 436 | 437 | #### Internal 438 | 439 | * 👷 Add CI Labeler. PR [#236](https://github.com/tiangolo/uwsgi-nginx-docker/pull/236) by [@tiangolo](https://github.com/tiangolo). 440 | * ⬆ Bump tiangolo/latest-changes from 0.3.1 to 0.3.2. PR [#235](https://github.com/tiangolo/uwsgi-nginx-docker/pull/235) by [@dependabot[bot]](https://github.com/apps/dependabot). 441 | * 🔥 Remove old unused files. PR [#234](https://github.com/tiangolo/uwsgi-nginx-docker/pull/234) by [@tiangolo](https://github.com/tiangolo). 442 | * ⬆ Bump tiangolo/issue-manager from 0.5.0 to 0.5.1. PR [#229](https://github.com/tiangolo/uwsgi-nginx-docker/pull/229) by [@dependabot[bot]](https://github.com/apps/dependabot). 443 | * ⬆ Bump docker/build-push-action from 5 to 6. PR [#225](https://github.com/tiangolo/uwsgi-nginx-docker/pull/225) by [@dependabot[bot]](https://github.com/apps/dependabot). 444 | * 👷 Update `issue-manager.yml`. PR [#226](https://github.com/tiangolo/uwsgi-nginx-docker/pull/226) by [@tiangolo](https://github.com/tiangolo). 445 | * ⬆ Bump docker/login-action from 1 to 3. PR [#204](https://github.com/tiangolo/uwsgi-nginx-docker/pull/204) by [@dependabot[bot]](https://github.com/apps/dependabot). 446 | * ⬆ Bump peter-evans/dockerhub-description from 3 to 4. PR [#205](https://github.com/tiangolo/uwsgi-nginx-docker/pull/205) by [@dependabot[bot]](https://github.com/apps/dependabot). 447 | * ⬆ Bump docker/setup-buildx-action from 1 to 3. PR [#206](https://github.com/tiangolo/uwsgi-nginx-docker/pull/206) by [@dependabot[bot]](https://github.com/apps/dependabot). 448 | * ⬆ Bump docker/build-push-action from 2 to 5. PR [#207](https://github.com/tiangolo/uwsgi-nginx-docker/pull/207) by [@dependabot[bot]](https://github.com/apps/dependabot). 449 | * 👷 Update `latest-changes` GitHub Action. PR [#214](https://github.com/tiangolo/uwsgi-nginx-docker/pull/214) by [@tiangolo](https://github.com/tiangolo). 450 | * 👷 Update issue-manager.yml GitHub Action permissions. PR [#213](https://github.com/tiangolo/uwsgi-nginx-docker/pull/213) by [@tiangolo](https://github.com/tiangolo). 451 | * 🔧 Add GitHub templates for discussions and issues, and security policy. PR [#203](https://github.com/tiangolo/uwsgi-nginx-docker/pull/203) by [@alejsdev](https://github.com/alejsdev). 452 | * 🔧 Update `latest-changes.yml`. PR [#201](https://github.com/tiangolo/uwsgi-nginx-docker/pull/201) by [@alejsdev](https://github.com/alejsdev). 453 | 454 | ### 2.1.0 455 | 456 | #### Features 457 | 458 | * ✨ Add support for multiarch builds, including ARM (e.g. Mac M1). PR [#200](https://github.com/tiangolo/uwsgi-nginx-docker/pull/200) by [@tiangolo](https://github.com/tiangolo). 459 | 460 | #### Refactors 461 | 462 | * 🔥 Remove support for Alpine. PR [#198](https://github.com/tiangolo/uwsgi-nginx-docker/pull/198) by [@tiangolo](https://github.com/tiangolo). 463 | 464 | #### Upgrades 465 | 466 | * ⬆️ Bump uwsgi from 2.0.21 to 2.0.22 in /docker-images. PR [#188](https://github.com/tiangolo/uwsgi-nginx-docker/pull/188) by [@dependabot[bot]](https://github.com/apps/dependabot). 467 | 468 | #### Docs 469 | 470 | * 📝 Update test badge in `README.md`. PR [#199](https://github.com/tiangolo/uwsgi-nginx-docker/pull/199) by [@alejsdev](https://github.com/alejsdev). 471 | 472 | #### Internal 473 | 474 | * 👷 Update dependabot. PR [#183](https://github.com/tiangolo/uwsgi-nginx-docker/pull/183) by [@tiangolo](https://github.com/tiangolo). 475 | * 👷 Update latest changes token. PR [#180](https://github.com/tiangolo/uwsgi-nginx-docker/pull/180) by [@tiangolo](https://github.com/tiangolo). 476 | * 👷 Add GitHub Action for Docker Hub description. PR [#172](https://github.com/tiangolo/uwsgi-nginx-docker/pull/172) by [@tiangolo](https://github.com/tiangolo). 477 | * ⬆ Bump actions/checkout from 3.1.0 to 4.1.2. PR [#197](https://github.com/tiangolo/uwsgi-nginx-docker/pull/197) by [@dependabot[bot]](https://github.com/apps/dependabot). 478 | * ⬆ Bump tiangolo/issue-manager from 0.4.1 to 0.5.0. PR [#193](https://github.com/tiangolo/uwsgi-nginx-docker/pull/193) by [@dependabot[bot]](https://github.com/apps/dependabot). 479 | * ⬆ Bump peter-evans/dockerhub-description from 3 to 4. PR [#192](https://github.com/tiangolo/uwsgi-nginx-docker/pull/192) by [@dependabot[bot]](https://github.com/apps/dependabot). 480 | * ⬆ Bump actions/setup-python from 4 to 5. PR [#189](https://github.com/tiangolo/uwsgi-nginx-docker/pull/189) by [@dependabot[bot]](https://github.com/apps/dependabot). 481 | * ⬆ Bump tiangolo/issue-manager from 0.4.0 to 0.4.1. PR [#191](https://github.com/tiangolo/uwsgi-nginx-docker/pull/191) by [@dependabot[bot]](https://github.com/apps/dependabot). 482 | * 👷 Update latest-changes GitHub Action. PR [#182](https://github.com/tiangolo/uwsgi-nginx-docker/pull/182) by [@tiangolo](https://github.com/tiangolo). 483 | 484 | ### 2.0.0 485 | 486 | Highlights of this release: 487 | 488 | * Support for Python 3.10, 3.11, and 3.9. 489 | * Deprecation of Python 3.6 and 2.7. 490 | * The last Python 3.6 and 2.7 images are available in Docker Hub, but they won't be updated or maintained anymore. 491 | * The last images with a date tag are `python3.6-2022-11-25` and `python2.7-2022-11-25`. 492 | * Upgraded versions of all the dependencies. 493 | * Small improvements and fixes. 494 | 495 | #### Features 496 | 497 | * ✨ Add support for Python 3.11. PR [#171](https://github.com/tiangolo/uwsgi-nginx-docker/pull/171) by [@tiangolo](https://github.com/tiangolo). 498 | * ✨ Add support for Python 3.10 and upgrade uWSGI to `2.0.20`. PR [#127](https://github.com/tiangolo/uwsgi-nginx-docker/pull/127) by [@tiangolo](https://github.com/tiangolo). 499 | * ⬆️ Update pip install command with flag --no-cache-dir to reduce disk used. PR [#120](https://github.com/tiangolo/uwsgi-nginx-docker/pull/120) by [@tiangolo](https://github.com/tiangolo). 500 | * ✨ Quit Supervisor on errors, to allow orchestrators to handle it. PR [#110](https://github.com/tiangolo/uwsgi-nginx-docker/pull/110) by [@tiangolo](https://github.com/tiangolo). 501 | * ✨ Add Python 3.9. PR [#101](https://github.com/tiangolo/uwsgi-nginx-docker/pull/101) by [@sjadema](https://github.com/sjadema). 502 | 503 | #### Breaking Changes 504 | 505 | * 🔥 Deprecate and remove Python 3.6 and 2.7. PR [#164](https://github.com/tiangolo/uwsgi-nginx-docker/pull/164) by [@tiangolo](https://github.com/tiangolo). 506 | * 🔥 Remove support for Python 2.7. PR [#123](https://github.com/tiangolo/uwsgi-nginx-docker/pull/123) by [@tiangolo](https://github.com/tiangolo). 507 | 508 | #### Upgrades 509 | 510 | * ⬆️ Upgrade Nginx to the latest version 1.23.2, and Debian to bullseye. PR [#163](https://github.com/tiangolo/uwsgi-nginx-docker/pull/163) by [@tiangolo](https://github.com/tiangolo). 511 | * ⬆️ Bump uwsgi from 2.0.20 to 2.0.21. PR [#159](https://github.com/tiangolo/uwsgi-nginx-docker/pull/159) by [@dependabot[bot]](https://github.com/apps/dependabot). 512 | * ⬆ Upgrade Nginx to version 1.21.6 and Alpine to version 3.13. PR [#148](https://github.com/tiangolo/uwsgi-nginx-docker/pull/148) by [@haley-comet](https://github.com/haley-comet). 513 | * ⬆ Upgrade Nginx to the latest version of the official images. PR [#107](https://github.com/tiangolo/uwsgi-nginx-docker/pull/107) by [@tiangolo](https://github.com/tiangolo). 514 | 515 | #### Docs 516 | 517 | * 📝 Add note to discourage Alpine with Python. PR [#124](https://github.com/tiangolo/uwsgi-nginx-docker/pull/124) by [@tiangolo](https://github.com/tiangolo). 518 | * 📝 Add Kubernetes warning, when to use this image. PR [#122](https://github.com/tiangolo/uwsgi-nginx-docker/pull/122) by [@tiangolo](https://github.com/tiangolo). 519 | * ✏️ Fix typo duplicate "Note" in Readme. PR [#121](https://github.com/tiangolo/uwsgi-nginx-docker/pull/121) by [@tiangolo](https://github.com/tiangolo). 520 | * 🐛 Fix broken link to TechEmpower benchmarks. PR [#96](https://github.com/tiangolo/uwsgi-nginx-docker/pull/96) by [@tiangolo](https://github.com/tiangolo). 521 | 522 | #### Internal 523 | 524 | * ⬆️ Update autoflake requirement from ^1.3.1 to ^2.0.0. PR [#166](https://github.com/tiangolo/uwsgi-nginx-docker/pull/166) by [@dependabot[bot]](https://github.com/apps/dependabot). 525 | * ⬆️ Update mypy requirement from ^0.971 to ^0.991. PR [#167](https://github.com/tiangolo/uwsgi-nginx-docker/pull/167) by [@dependabot[bot]](https://github.com/apps/dependabot). 526 | * ⬆️ Update docker requirement from ^5.0.3 to ^6.0.1. PR [#168](https://github.com/tiangolo/uwsgi-nginx-docker/pull/168) by [@dependabot[bot]](https://github.com/apps/dependabot). 527 | * ⬆️ Update black requirement from ^20.8b1 to ^22.10. PR [#169](https://github.com/tiangolo/uwsgi-nginx-docker/pull/169) by [@dependabot[bot]](https://github.com/apps/dependabot). 528 | * ⬆️ Upgrade CI OS. PR [#170](https://github.com/tiangolo/uwsgi-nginx-docker/pull/170) by [@tiangolo](https://github.com/tiangolo). 529 | * 🔧 Update Dependabot config. PR [#165](https://github.com/tiangolo/uwsgi-nginx-docker/pull/165) by [@tiangolo](https://github.com/tiangolo). 530 | * ⬆️ Bump tiangolo/issue-manager from 0.2.0 to 0.4.0. PR [#112](https://github.com/tiangolo/uwsgi-nginx-docker/pull/112) by [@dependabot[bot]](https://github.com/apps/dependabot). 531 | * 👷 Add scheduled CI. PR [#162](https://github.com/tiangolo/uwsgi-nginx-docker/pull/162) by [@tiangolo](https://github.com/tiangolo). 532 | * 👷 Add alls-green GitHub Action. PR [#161](https://github.com/tiangolo/uwsgi-nginx-docker/pull/161) by [@tiangolo](https://github.com/tiangolo). 533 | * ⬆️ Update black requirement from ^19.10b0 to ^20.8b1. PR [#116](https://github.com/tiangolo/uwsgi-nginx-docker/pull/116) by [@dependabot[bot]](https://github.com/apps/dependabot). 534 | * ⬆️ Update isort requirement from ^4.3.21 to ^5.8.0. PR [#118](https://github.com/tiangolo/uwsgi-nginx-docker/pull/118) by [@dependabot[bot]](https://github.com/apps/dependabot). 535 | * ⬆️ Update docker requirement from ^4.2.0 to ^5.0.3. PR [#126](https://github.com/tiangolo/uwsgi-nginx-docker/pull/126) by [@dependabot[bot]](https://github.com/apps/dependabot). 536 | * ⬆️ Bump actions/setup-python from 1 to 4.1.0. PR [#155](https://github.com/tiangolo/uwsgi-nginx-docker/pull/155) by [@dependabot[bot]](https://github.com/apps/dependabot). 537 | * ⬆️ Update mypy requirement from ^0.770 to ^0.971. PR [#156](https://github.com/tiangolo/uwsgi-nginx-docker/pull/156) by [@dependabot[bot]](https://github.com/apps/dependabot). 538 | * ⬆️ Update pytest requirement from ^5.4.1 to ^7.0.1. PR [#138](https://github.com/tiangolo/uwsgi-nginx-docker/pull/138) by [@dependabot[bot]](https://github.com/apps/dependabot). 539 | * ⬆️ Bump actions/checkout from 2 to 3.1.0. PR [#157](https://github.com/tiangolo/uwsgi-nginx-docker/pull/157) by [@dependabot[bot]](https://github.com/apps/dependabot). 540 | * 🔧 Run tests only on PRs or when pushing on master to avoid double CI. PR [#149](https://github.com/tiangolo/uwsgi-nginx-docker/pull/149) by [@tiangolo](https://github.com/tiangolo). 541 | * 👷 Update Latest Changes GitHub Action. PR [#119](https://github.com/tiangolo/uwsgi-nginx-docker/pull/119) by [@tiangolo](https://github.com/tiangolo). 542 | * 👷 Add Dependabot and external dependencies, to get automatic upgrade PRs. PR [#111](https://github.com/tiangolo/uwsgi-nginx-docker/pull/111) by [@tiangolo](https://github.com/tiangolo). 543 | * 👷 Add GitHub Action latest-changes, update issue-manager. PR [#92](https://github.com/tiangolo/uwsgi-nginx-docker/pull/92) by [@tiangolo](https://github.com/tiangolo). 544 | * Fix Python 3.8 Alpine environment for installed packages. PR [#84](https://github.com/tiangolo/uwsgi-nginx-docker/pull/84). 545 | 546 | ### 1.4.0 547 | 548 | * Add [GitHub Sponsors](https://github.com/sponsors/tiangolo) button. 549 | * Add new image for Python 3.8, and new image for Python 3.8 on Alpine. PR [#83](https://github.com/tiangolo/uwsgi-nginx-docker/pull/83). 550 | * Upgrade Nginx to latest version, `1.17.10`, based on latest Debian, Buster. PR [#82](https://github.com/tiangolo/uwsgi-nginx-docker/pull/82). 551 | * Remove support for Python 3.5. PR [#81](https://github.com/tiangolo/uwsgi-nginx-docker/pull/81). 552 | 553 | ### 1.3.0 554 | 555 | * This is the last version to support: 556 | * Debian Stretch (before upgrading to Buster). 557 | * Python 3.5. 558 | * Alpine 3.7, 3.8, 3.9 (before upgrading to Alpine 3.11). 559 | * Alpine in older versions of Python, 2.7 and 3.6 (Before upgrading to Python 3.8). 560 | * If you need any of those, make sure to use a tag for the build date `2020-05-04`. 561 | * Refactor build set up: 562 | * Re-use code and configs. 563 | * Migrate to GitHub Actions. 564 | * Simplify tests. 565 | * PR [#78](https://github.com/tiangolo/uwsgi-nginx-docker/pull/78). 566 | * Migrate Travis to .com, update badge. PR [#77](https://github.com/tiangolo/uwsgi-nginx-docker/pull/77). 567 | 568 | ### 1.2.0 569 | 570 | * 2019-10-14: 571 | * Refactor and simplify test scripts. PR [#66](https://github.com/tiangolo/uwsgi-nginx-docker/pull/66). 572 | * 2019-09-28: 573 | * Refactor build scripts and add image tags for each build date, like `tiangolo/uwsgi-nginx:python3.7-2019-09-28`. PR [#65](https://github.com/tiangolo/uwsgi-nginx-docker/pull/65). 574 | 575 | * Upgrade Travis. PR [#60](https://github.com/tiangolo/uwsgi-nginx-docker/pull/60). 576 | 577 | ### 1.1.0 578 | 579 | * Added support for `/app/prestart.sh` script to run arbitrary code before starting the app (for example, Alembic - SQLAlchemy migrations). The [documentation for the `/app/prestart.sh` is in the main README](https://github.com/tiangolo/uwsgi-nginx-docker#custom-appprestartsh). [PR #59](https://github.com/tiangolo/uwsgi-nginx-docker/pull/59). 580 | 581 | ### 1.0.0 582 | 583 | * 2019-05-04: 584 | * Add Alpine Linux 3.9. PR [#55](https://github.com/tiangolo/uwsgi-nginx-docker/pull/55) by [evilgoldfish](https://github.com/evilgoldfish). 585 | * Build images using Travis matrix to improve development/testing speed. Needed for some recent PRs. [PR #58](https://github.com/tiangolo/uwsgi-nginx-docker/pull/58). 586 | 587 | * 2019-02-02: 588 | * The Nginx configurations are generated dynamically from the entrypoint, instead of modifying pre-existing files. [PR #50](https://github.com/tiangolo/uwsgi-nginx-docker/pull/50). 589 | * Support for a completely custom `/app/nginx.conf` file that overrides the generated one. [PR #51](https://github.com/tiangolo/uwsgi-nginx-docker/pull/51). 590 | 591 | * 2018-11-23: New Alpine 3.8 images for Python 2.7, Python 3.6 and Python 3.7 (Python 3.7 temporarily disabled). Thanks to [philippfreyer](https://github.com/philippfreyer) in [PR #45](https://github.com/tiangolo/uwsgi-nginx-docker/pull/45) 592 | 593 | * 2018-09-22: New Python 3.7 versions, standard and Alpine based. Thanks to [desaintmartin](https://github.com/desaintmartin) in [this PR](https://github.com/tiangolo/uwsgi-nginx-docker/pull/39). 594 | 595 | * 2018-06-22: You can now use `NGINX_WORKER_CONNECTIONS` to set the maximum number of Nginx worker connections and `NGINX_WORKER_OPEN_FILES` to set the maximum number of open files. Thanks to [ronlut](https://github.com/ronlut) in [this PR](https://github.com/tiangolo/uwsgi-nginx-docker/pull/26). 596 | 597 | * 2018-06-22: Make uWSGI require an app to run, instead of going in "full dynamic mode" while there was an error. Supervisord doesn't terminate itself but tries to restart uWSGI and shows the errors. Uses `need-app` as suggested by [luckydonald](https://github.com/luckydonald) in [this comment](https://github.com/tiangolo/uwsgi-nginx-flask-docker/issues/3#issuecomment-321991279). 598 | 599 | * 2018-06-22: Correctly handled graceful shutdown of uWSGI and Nginx. Thanks to [desaintmartin](https://github.com/desaintmartin) in [this PR](https://github.com/tiangolo/uwsgi-nginx-docker/pull/30). 600 | 601 | * 2018-02-04: It's now possible to set the number of Nginx worker processes with the environment variable `NGINX_WORKER_PROCESSES`. Thanks to [naktinis](https://github.com/naktinis) in [this PR](https://github.com/tiangolo/uwsgi-nginx-docker/pull/22). 602 | 603 | * 2018-01-14: There are now two Alpine based versions, `python2.7-alpine3.7` and `python3.6-alpine3.7`. 604 | 605 | * 2017-12-08: Now you can configure which port the container should listen on, using the environment variable `LISTEN_PORT` thanks to [tmshn](https://github.com/tmshn) in [this PR](https://github.com/tiangolo/uwsgi-nginx-docker/pull/16). 606 | 607 | * 2017-08-09: You can set a custom maximum upload file size using an environment variable `NGINX_MAX_UPLOAD`, by default it has a value of `0`, that allows unlimited upload file sizes. This differs from Nginx's default value of 1 MB. It's configured this way because that's the simplest experience a developer that is not expert in Nginx would expect. 608 | 609 | * 2017-08-09: Now you can override where to look for the `uwsgi.ini` file, and with that, change the default directory from `/app` to something else, using the envirnoment variable `UWSGI_INI`. 610 | 611 | * 2017-08-08: There's a new `latest` tag image, just to show a warning for those still using `latest` for Python 2.7 web applications. As of now, [everyone](https://www.python.org/dev/peps/pep-0373/) [should be](http://flask.pocoo.org/docs/0.12/python3/#python3-support) [using Python 3](https://docs.djangoproject.com/en/1.11/faq/install/#what-python-version-should-i-use-with-django). 612 | 613 | * 2017-08-08: Supervisord now terminates uWSGI on `SIGTERM`, so if you run `docker stop` or something similar, it will actually stop everything, instead of waiting for Docker's timeout to kill the container. 614 | 615 | * 2017-07-31: There's now an image tag for Python 3.6, based on the official image for Python 3.6 thanks to [jrd](https://github.com/jrd) in [this PR](https://github.com/tiangolo/uwsgi-nginx-docker/pull/6). 616 | 617 | * 2016-10-01: Now you can override default `uwsgi.ini` parameters from the file in `/app/uwsgi.ini`. 618 | 619 | * 2016-08-16: There's now an image tag for Python 3.5, based on the official image for Python 3.5. So now you can use this image for your projects in Python 2.7 and Python 3.5. 620 | 621 | * 2016-08-16: Use dynamic a number of worker processes for uWSGI, from 2 to 16 depending on load. This should work for most cases. This helps especially when there are some responses that are slow and take some time to be generated, this change allows all the other responses to keep fast (in a new process) without having to wait for the first (slow) one to finish. 622 | 623 | * Also, it now uses a base `uwsgi.ini` file under `/etc/uwsgi/` with most of the general configurations, so, the `uwsgi.ini` inside `/app` (the one you could need to modify) is now a lot simpler. 624 | 625 | * 2016-04-05: Nginx and uWSGI logs are now redirected to stdout, allowing to use `docker logs`. 626 | 627 | ## License 628 | 629 | This project is licensed under the terms of the Apache license. 630 | -------------------------------------------------------------------------------- /SECURITY.md: -------------------------------------------------------------------------------- 1 | # Security Policy 2 | 3 | Security is very important for this project and its community. 🔒 4 | 5 | Learn more about it below. 👇 6 | 7 | ## Versions 8 | 9 | The latest version or release is supported. 10 | 11 | You are encouraged to write tests for your application and update your versions frequently after ensuring that your tests are passing. This way you will benefit from the latest features, bug fixes, and **security fixes**. 12 | 13 | ## Reporting a Vulnerability 14 | 15 | If you think you found a vulnerability, and even if you are not sure about it, please report it right away by sending an email to: security@tiangolo.com. Please try to be as explicit as possible, describing all the steps and example code to reproduce the security issue. 16 | 17 | I (the author, [@tiangolo](https://twitter.com/tiangolo)) will review it thoroughly and get back to you. 18 | 19 | ## Public Discussions 20 | 21 | Please restrain from publicly discussing a potential security vulnerability. 🙊 22 | 23 | It's better to discuss privately and try to find a solution first, to limit the potential impact as much as possible. 24 | 25 | --- 26 | 27 | Thanks for your help! 28 | 29 | The community and I thank you for that. 🙇 30 | -------------------------------------------------------------------------------- /docker-images/app/main.py: -------------------------------------------------------------------------------- 1 | import sys 2 | 3 | 4 | def application(env, start_response): 5 | version = "{}.{}".format(sys.version_info.major, sys.version_info.minor) 6 | start_response("200 OK", [("Content-Type", "text/plain")]) 7 | message = "Hello World from a default Nginx uWSGI Python {} app in a Docker container (default)".format( 8 | version 9 | ) 10 | return [message.encode("utf-8")] 11 | -------------------------------------------------------------------------------- /docker-images/app/prestart.sh: -------------------------------------------------------------------------------- 1 | #! /usr/bin/env sh 2 | 3 | echo "Running inside /app/prestart.sh, you could add migrations to this file, e.g.:" 4 | 5 | echo " 6 | #! /usr/bin/env bash 7 | 8 | # Let the DB start 9 | sleep 10; 10 | # Run migrations 11 | alembic upgrade head 12 | " 13 | -------------------------------------------------------------------------------- /docker-images/app/uwsgi.ini: -------------------------------------------------------------------------------- 1 | [uwsgi] 2 | wsgi-file=/app/main.py 3 | -------------------------------------------------------------------------------- /docker-images/entrypoint.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env sh 2 | set -e 3 | 4 | # Get the maximum upload file size for Nginx, default to 0: unlimited 5 | USE_NGINX_MAX_UPLOAD=${NGINX_MAX_UPLOAD:-0} 6 | 7 | # Get the number of workers for Nginx, default to 1 8 | USE_NGINX_WORKER_PROCESSES=${NGINX_WORKER_PROCESSES:-1} 9 | 10 | # Set the max number of connections per worker for Nginx, if requested 11 | # Cannot exceed worker_rlimit_nofile, see NGINX_WORKER_OPEN_FILES below 12 | NGINX_WORKER_CONNECTIONS=${NGINX_WORKER_CONNECTIONS:-1024} 13 | 14 | # Get the listen port for Nginx, default to 80 15 | USE_LISTEN_PORT=${LISTEN_PORT:-80} 16 | 17 | if [ -f /app/nginx.conf ]; then 18 | cp /app/nginx.conf /etc/nginx/nginx.conf 19 | else 20 | content='user nginx;\n' 21 | # Set the number of worker processes in Nginx 22 | content=$content"worker_processes ${USE_NGINX_WORKER_PROCESSES};\n" 23 | content=$content'error_log /var/log/nginx/error.log warn;\n' 24 | content=$content'pid /var/run/nginx.pid;\n' 25 | content=$content'events {\n' 26 | content=$content" worker_connections ${NGINX_WORKER_CONNECTIONS};\n" 27 | content=$content'}\n' 28 | content=$content'http {\n' 29 | content=$content' include /etc/nginx/mime.types;\n' 30 | content=$content' default_type application/octet-stream;\n' 31 | content=$content' log_format main '"'\$remote_addr - \$remote_user [\$time_local] \"\$request\" '\n" 32 | content=$content' '"'\$status \$body_bytes_sent \"\$http_referer\" '\n" 33 | content=$content' '"'\"\$http_user_agent\" \"\$http_x_forwarded_for\"';\n" 34 | content=$content' access_log /var/log/nginx/access.log main;\n' 35 | content=$content' sendfile on;\n' 36 | content=$content' keepalive_timeout 65;\n' 37 | content=$content' include /etc/nginx/conf.d/*.conf;\n' 38 | content=$content'}\n' 39 | content=$content'daemon off;\n' 40 | # Set the max number of open file descriptors for Nginx workers, if requested 41 | if [ -n "${NGINX_WORKER_OPEN_FILES}" ] ; then 42 | content=$content"worker_rlimit_nofile ${NGINX_WORKER_OPEN_FILES};\n" 43 | fi 44 | # Save generated /etc/nginx/nginx.conf 45 | printf "$content" > /etc/nginx/nginx.conf 46 | 47 | content_server='server {\n' 48 | content_server=$content_server" listen ${USE_LISTEN_PORT};\n" 49 | content_server=$content_server' location / {\n' 50 | content_server=$content_server' include uwsgi_params;\n' 51 | content_server=$content_server' uwsgi_pass unix:///tmp/uwsgi.sock;\n' 52 | content_server=$content_server' }\n' 53 | content_server=$content_server'}\n' 54 | # Save generated server /etc/nginx/conf.d/nginx.conf 55 | printf "$content_server" > /etc/nginx/conf.d/nginx.conf 56 | 57 | # Generate Nginx config for maximum upload file size 58 | printf "client_max_body_size $USE_NGINX_MAX_UPLOAD;\n" > /etc/nginx/conf.d/upload.conf 59 | fi 60 | 61 | exec "$@" 62 | -------------------------------------------------------------------------------- /docker-images/install-nginx-debian.sh: -------------------------------------------------------------------------------- 1 | #! /usr/bin/env bash 2 | 3 | # From official Nginx Docker image, as a script to re-use it, removing internal comments 4 | # Ref: https://github.com/nginx/docker-nginx/blob/7f1d49f6f222f7e588a9066fd53a0ce43c3466a5/mainline/debian/Dockerfile 5 | # Override group id from 101 to 102 as the original clashes with build-deps _ssh gid 101 6 | 7 | 8 | # Standard set up Nginx 9 | export NGINX_VERSION=1.27.5 10 | export NJS_VERSION=0.8.10 11 | export NJS_RELEASE=1~bookworm 12 | export PKG_RELEASE=1~bookworm 13 | export DYNPKG_RELEASE=1~bookworm 14 | 15 | set -x \ 16 | && groupadd --system --gid 102 nginx \ 17 | && useradd --system --gid nginx --no-create-home --home /nonexistent --comment "nginx user" --shell /bin/false --uid 102 nginx \ 18 | && apt-get update \ 19 | && apt-get install --no-install-recommends --no-install-suggests -y gnupg1 ca-certificates \ 20 | && \ 21 | NGINX_GPGKEYS="573BFD6B3D8FBC641079A6ABABF5BD827BD9BF62 8540A6F18833A80E9C1653A42FD21310B49F6B46 9E9BE90EACBCDE69FE9B204CBCDCD8A38D88A2B3"; \ 22 | NGINX_GPGKEY_PATH=/etc/apt/keyrings/nginx-archive-keyring.gpg; \ 23 | export GNUPGHOME="$(mktemp -d)"; \ 24 | found=''; \ 25 | for NGINX_GPGKEY in $NGINX_GPGKEYS; do \ 26 | for server in \ 27 | hkp://keyserver.ubuntu.com:80 \ 28 | pgp.mit.edu \ 29 | ; do \ 30 | echo "Fetching GPG key $NGINX_GPGKEY from $server"; \ 31 | gpg1 --keyserver "$server" --keyserver-options timeout=10 --recv-keys "$NGINX_GPGKEY" && found=yes && break; \ 32 | done; \ 33 | test -z "$found" && echo >&2 "error: failed to fetch GPG key $NGINX_GPGKEY" && exit 1; \ 34 | done; \ 35 | gpg1 --export "$NGINX_GPGKEYS" > "$NGINX_GPGKEY_PATH" ; \ 36 | rm -rf "$GNUPGHOME"; \ 37 | apt-get remove --purge --auto-remove -y gnupg1 && rm -rf /var/lib/apt/lists/* \ 38 | && dpkgArch="$(dpkg --print-architecture)" \ 39 | && nginxPackages=" \ 40 | nginx=${NGINX_VERSION}-${PKG_RELEASE} \ 41 | nginx-module-xslt=${NGINX_VERSION}-${DYNPKG_RELEASE} \ 42 | nginx-module-geoip=${NGINX_VERSION}-${DYNPKG_RELEASE} \ 43 | nginx-module-image-filter=${NGINX_VERSION}-${DYNPKG_RELEASE} \ 44 | nginx-module-njs=${NGINX_VERSION}+${NJS_VERSION}-${NJS_RELEASE} \ 45 | " \ 46 | && case "$dpkgArch" in \ 47 | amd64|arm64) \ 48 | echo "deb [signed-by=$NGINX_GPGKEY_PATH] https://nginx.org/packages/mainline/debian/ bookworm nginx" >> /etc/apt/sources.list.d/nginx.list \ 49 | && apt-get update \ 50 | ;; \ 51 | *) \ 52 | tempDir="$(mktemp -d)" \ 53 | && chmod 777 "$tempDir" \ 54 | \ 55 | && savedAptMark="$(apt-mark showmanual)" \ 56 | \ 57 | && apt-get update \ 58 | && apt-get install --no-install-recommends --no-install-suggests -y \ 59 | curl \ 60 | devscripts \ 61 | equivs \ 62 | git \ 63 | libxml2-utils \ 64 | lsb-release \ 65 | xsltproc \ 66 | && ( \ 67 | cd "$tempDir" \ 68 | && REVISION="${NGINX_VERSION}-${PKG_RELEASE}" \ 69 | && REVISION=${REVISION%~*} \ 70 | && curl -f -L -O https://github.com/nginx/pkg-oss/archive/${REVISION}.tar.gz \ 71 | && PKGOSSCHECKSUM="c773d98b567bd585c17f55702bf3e4c7d82b676bfbde395270e90a704dca3c758dfe0380b3f01770542b4fd9bed1f1149af4ce28bfc54a27a96df6b700ac1745 *${REVISION}.tar.gz" \ 72 | && if [ "$(openssl sha512 -r ${REVISION}.tar.gz)" = "$PKGOSSCHECKSUM" ]; then \ 73 | echo "pkg-oss tarball checksum verification succeeded!"; \ 74 | else \ 75 | echo "pkg-oss tarball checksum verification failed!"; \ 76 | exit 1; \ 77 | fi \ 78 | && tar xzvf ${REVISION}.tar.gz \ 79 | && cd pkg-oss-${REVISION} \ 80 | && cd debian \ 81 | && for target in base module-geoip module-image-filter module-njs module-xslt; do \ 82 | make rules-$target; \ 83 | mk-build-deps --install --tool="apt-get -o Debug::pkgProblemResolver=yes --no-install-recommends --yes" \ 84 | debuild-$target/nginx-$NGINX_VERSION/debian/control; \ 85 | done \ 86 | && make base module-geoip module-image-filter module-njs module-xslt \ 87 | ) \ 88 | && apt-mark showmanual | xargs apt-mark auto > /dev/null \ 89 | && { [ -z "$savedAptMark" ] || apt-mark manual $savedAptMark; } \ 90 | \ 91 | && ls -lAFh "$tempDir" \ 92 | && ( cd "$tempDir" && dpkg-scanpackages . > Packages ) \ 93 | && grep '^Package: ' "$tempDir/Packages" \ 94 | && echo "deb [ trusted=yes ] file://$tempDir ./" > /etc/apt/sources.list.d/temp.list \ 95 | && apt-get -o Acquire::GzipIndexes=false update \ 96 | ;; \ 97 | esac \ 98 | \ 99 | && apt-get install --no-install-recommends --no-install-suggests -y \ 100 | $nginxPackages \ 101 | gettext-base \ 102 | curl \ 103 | && apt-get remove --purge --auto-remove -y && rm -rf /var/lib/apt/lists/* /etc/apt/sources.list.d/nginx.list \ 104 | \ 105 | && if [ -n "$tempDir" ]; then \ 106 | apt-get purge -y --auto-remove \ 107 | && rm -rf "$tempDir" /etc/apt/sources.list.d/temp.list; \ 108 | fi \ 109 | && ln -sf /dev/stdout /var/log/nginx/access.log \ 110 | && ln -sf /dev/stderr /var/log/nginx/error.log \ 111 | # && mkdir /docker-entrypoint.d 112 | # Standard set up Nginx finished 113 | -------------------------------------------------------------------------------- /docker-images/python3.10.dockerfile: -------------------------------------------------------------------------------- 1 | FROM python:3.10-bookworm 2 | 3 | LABEL maintainer="Sebastian Ramirez " 4 | 5 | ENV PYTHONDONTWRITEBYTECODE=1 6 | ENV PYTHONUNBUFFERED=1 7 | 8 | COPY install-nginx-debian.sh / 9 | 10 | RUN bash /install-nginx-debian.sh 11 | 12 | # Install requirements 13 | COPY requirements.txt /tmp/requirements.txt 14 | RUN pip install --no-cache-dir -r /tmp/requirements.txt 15 | 16 | # Remove default configuration from Nginx 17 | RUN rm /etc/nginx/conf.d/default.conf 18 | # Copy the base uWSGI ini file to enable default dynamic uwsgi process number 19 | COPY uwsgi.ini /etc/uwsgi/ 20 | 21 | # Install Supervisord 22 | RUN apt-get update && apt-get install -y supervisor \ 23 | && rm -rf /var/lib/apt/lists/* 24 | # Custom Supervisord config 25 | COPY supervisord-debian.conf /etc/supervisor/conf.d/supervisord.conf 26 | 27 | # Copy stop-supervisor.sh to kill the supervisor and substasks on app failure 28 | COPY stop-supervisor.sh /etc/supervisor/stop-supervisor.sh 29 | RUN chmod +x /etc/supervisor/stop-supervisor.sh 30 | 31 | # Which uWSGI .ini file should be used, to make it customizable 32 | ENV UWSGI_INI /app/uwsgi.ini 33 | 34 | # By default, run 2 processes 35 | ENV UWSGI_CHEAPER 2 36 | 37 | # By default, when on demand, run up to 16 processes 38 | ENV UWSGI_PROCESSES 16 39 | 40 | # By default, allow unlimited file sizes, modify it to limit the file sizes 41 | # To have a maximum of 1 MB (Nginx's default) change the line to: 42 | # ENV NGINX_MAX_UPLOAD 1m 43 | ENV NGINX_MAX_UPLOAD 0 44 | 45 | # By default, Nginx will run a single worker process, setting it to auto 46 | # will create a worker for each CPU core 47 | ENV NGINX_WORKER_PROCESSES 1 48 | 49 | # By default, Nginx listens on port 80. 50 | # To modify this, change LISTEN_PORT environment variable. 51 | # (in a Dockerfile or with an option for `docker run`) 52 | ENV LISTEN_PORT 80 53 | 54 | # Copy start.sh script that will check for a /app/prestart.sh script and run it before starting the app 55 | COPY start.sh /start.sh 56 | RUN chmod +x /start.sh 57 | 58 | # Copy the entrypoint that will generate Nginx additional configs 59 | COPY entrypoint.sh /entrypoint.sh 60 | RUN chmod +x /entrypoint.sh 61 | 62 | ENTRYPOINT ["/entrypoint.sh"] 63 | 64 | # Add demo app 65 | COPY ./app /app 66 | WORKDIR /app 67 | 68 | # Run the start script, it will check for an /app/prestart.sh script (e.g. for migrations) 69 | # And then will start Supervisor, which in turn will start Nginx and uWSGI 70 | CMD ["/start.sh"] 71 | -------------------------------------------------------------------------------- /docker-images/python3.11.dockerfile: -------------------------------------------------------------------------------- 1 | FROM python:3.11-bookworm 2 | 3 | LABEL maintainer="Sebastian Ramirez " 4 | 5 | ENV PYTHONDONTWRITEBYTECODE=1 6 | ENV PYTHONUNBUFFERED=1 7 | 8 | COPY install-nginx-debian.sh / 9 | 10 | RUN bash /install-nginx-debian.sh 11 | 12 | # Install requirements 13 | COPY requirements.txt /tmp/requirements.txt 14 | RUN pip install --no-cache-dir -r /tmp/requirements.txt 15 | 16 | # Remove default configuration from Nginx 17 | RUN rm /etc/nginx/conf.d/default.conf 18 | # Copy the base uWSGI ini file to enable default dynamic uwsgi process number 19 | COPY uwsgi.ini /etc/uwsgi/ 20 | 21 | # Install Supervisord 22 | RUN apt-get update && apt-get install -y supervisor \ 23 | && rm -rf /var/lib/apt/lists/* 24 | # Custom Supervisord config 25 | COPY supervisord-debian.conf /etc/supervisor/conf.d/supervisord.conf 26 | 27 | # Copy stop-supervisor.sh to kill the supervisor and substasks on app failure 28 | COPY stop-supervisor.sh /etc/supervisor/stop-supervisor.sh 29 | RUN chmod +x /etc/supervisor/stop-supervisor.sh 30 | 31 | # Which uWSGI .ini file should be used, to make it customizable 32 | ENV UWSGI_INI /app/uwsgi.ini 33 | 34 | # By default, run 2 processes 35 | ENV UWSGI_CHEAPER 2 36 | 37 | # By default, when on demand, run up to 16 processes 38 | ENV UWSGI_PROCESSES 16 39 | 40 | # By default, allow unlimited file sizes, modify it to limit the file sizes 41 | # To have a maximum of 1 MB (Nginx's default) change the line to: 42 | # ENV NGINX_MAX_UPLOAD 1m 43 | ENV NGINX_MAX_UPLOAD 0 44 | 45 | # By default, Nginx will run a single worker process, setting it to auto 46 | # will create a worker for each CPU core 47 | ENV NGINX_WORKER_PROCESSES 1 48 | 49 | # By default, Nginx listens on port 80. 50 | # To modify this, change LISTEN_PORT environment variable. 51 | # (in a Dockerfile or with an option for `docker run`) 52 | ENV LISTEN_PORT 80 53 | 54 | # Copy start.sh script that will check for a /app/prestart.sh script and run it before starting the app 55 | COPY start.sh /start.sh 56 | RUN chmod +x /start.sh 57 | 58 | # Copy the entrypoint that will generate Nginx additional configs 59 | COPY entrypoint.sh /entrypoint.sh 60 | RUN chmod +x /entrypoint.sh 61 | 62 | ENTRYPOINT ["/entrypoint.sh"] 63 | 64 | # Add demo app 65 | COPY ./app /app 66 | WORKDIR /app 67 | 68 | # Run the start script, it will check for an /app/prestart.sh script (e.g. for migrations) 69 | # And then will start Supervisor, which in turn will start Nginx and uWSGI 70 | CMD ["/start.sh"] 71 | -------------------------------------------------------------------------------- /docker-images/python3.12.dockerfile: -------------------------------------------------------------------------------- 1 | FROM python:3.12-bookworm 2 | 3 | LABEL maintainer="Sebastian Ramirez " 4 | 5 | ENV PYTHONDONTWRITEBYTECODE=1 6 | ENV PYTHONUNBUFFERED=1 7 | 8 | COPY install-nginx-debian.sh / 9 | 10 | RUN bash /install-nginx-debian.sh 11 | 12 | # Install requirements 13 | COPY requirements.txt /tmp/requirements.txt 14 | RUN pip install --no-cache-dir -r /tmp/requirements.txt 15 | 16 | # Remove default configuration from Nginx 17 | RUN rm /etc/nginx/conf.d/default.conf 18 | # Copy the base uWSGI ini file to enable default dynamic uwsgi process number 19 | COPY uwsgi.ini /etc/uwsgi/ 20 | 21 | # Install Supervisord 22 | RUN apt-get update && apt-get install -y supervisor \ 23 | && rm -rf /var/lib/apt/lists/* 24 | # Custom Supervisord config 25 | COPY supervisord-debian.conf /etc/supervisor/conf.d/supervisord.conf 26 | 27 | # Copy stop-supervisor.sh to kill the supervisor and substasks on app failure 28 | COPY stop-supervisor.sh /etc/supervisor/stop-supervisor.sh 29 | RUN chmod +x /etc/supervisor/stop-supervisor.sh 30 | 31 | # Which uWSGI .ini file should be used, to make it customizable 32 | ENV UWSGI_INI /app/uwsgi.ini 33 | 34 | # By default, run 2 processes 35 | ENV UWSGI_CHEAPER 2 36 | 37 | # By default, when on demand, run up to 16 processes 38 | ENV UWSGI_PROCESSES 16 39 | 40 | # By default, allow unlimited file sizes, modify it to limit the file sizes 41 | # To have a maximum of 1 MB (Nginx's default) change the line to: 42 | # ENV NGINX_MAX_UPLOAD 1m 43 | ENV NGINX_MAX_UPLOAD 0 44 | 45 | # By default, Nginx will run a single worker process, setting it to auto 46 | # will create a worker for each CPU core 47 | ENV NGINX_WORKER_PROCESSES 1 48 | 49 | # By default, Nginx listens on port 80. 50 | # To modify this, change LISTEN_PORT environment variable. 51 | # (in a Dockerfile or with an option for `docker run`) 52 | ENV LISTEN_PORT 80 53 | 54 | # Copy start.sh script that will check for a /app/prestart.sh script and run it before starting the app 55 | COPY start.sh /start.sh 56 | RUN chmod +x /start.sh 57 | 58 | # Copy the entrypoint that will generate Nginx additional configs 59 | COPY entrypoint.sh /entrypoint.sh 60 | RUN chmod +x /entrypoint.sh 61 | 62 | ENTRYPOINT ["/entrypoint.sh"] 63 | 64 | # Add demo app 65 | COPY ./app /app 66 | WORKDIR /app 67 | 68 | # Run the start script, it will check for an /app/prestart.sh script (e.g. for migrations) 69 | # And then will start Supervisor, which in turn will start Nginx and uWSGI 70 | CMD ["/start.sh"] 71 | -------------------------------------------------------------------------------- /docker-images/python3.9.dockerfile: -------------------------------------------------------------------------------- 1 | FROM python:3.9-bookworm 2 | 3 | LABEL maintainer="Sebastian Ramirez " 4 | 5 | ENV PYTHONDONTWRITEBYTECODE=1 6 | ENV PYTHONUNBUFFERED=1 7 | 8 | COPY install-nginx-debian.sh / 9 | 10 | RUN bash /install-nginx-debian.sh 11 | 12 | # Install requirements 13 | COPY requirements.txt /tmp/requirements.txt 14 | RUN pip install --no-cache-dir -r /tmp/requirements.txt 15 | 16 | # Remove default configuration from Nginx 17 | RUN rm /etc/nginx/conf.d/default.conf 18 | # Copy the base uWSGI ini file to enable default dynamic uwsgi process number 19 | COPY uwsgi.ini /etc/uwsgi/ 20 | 21 | # Install Supervisord 22 | RUN apt-get update && apt-get install -y supervisor \ 23 | && rm -rf /var/lib/apt/lists/* 24 | # Custom Supervisord config 25 | COPY supervisord-debian.conf /etc/supervisor/conf.d/supervisord.conf 26 | 27 | # Copy stop-supervisor.sh to kill the supervisor and substasks on app failure 28 | COPY stop-supervisor.sh /etc/supervisor/stop-supervisor.sh 29 | RUN chmod +x /etc/supervisor/stop-supervisor.sh 30 | 31 | # Which uWSGI .ini file should be used, to make it customizable 32 | ENV UWSGI_INI /app/uwsgi.ini 33 | 34 | # By default, run 2 processes 35 | ENV UWSGI_CHEAPER 2 36 | 37 | # By default, when on demand, run up to 16 processes 38 | ENV UWSGI_PROCESSES 16 39 | 40 | # By default, allow unlimited file sizes, modify it to limit the file sizes 41 | # To have a maximum of 1 MB (Nginx's default) change the line to: 42 | # ENV NGINX_MAX_UPLOAD 1m 43 | ENV NGINX_MAX_UPLOAD 0 44 | 45 | # By default, Nginx will run a single worker process, setting it to auto 46 | # will create a worker for each CPU core 47 | ENV NGINX_WORKER_PROCESSES 1 48 | 49 | # By default, Nginx listens on port 80. 50 | # To modify this, change LISTEN_PORT environment variable. 51 | # (in a Dockerfile or with an option for `docker run`) 52 | ENV LISTEN_PORT 80 53 | 54 | # Copy start.sh script that will check for a /app/prestart.sh script and run it before starting the app 55 | COPY start.sh /start.sh 56 | RUN chmod +x /start.sh 57 | 58 | # Copy the entrypoint that will generate Nginx additional configs 59 | COPY entrypoint.sh /entrypoint.sh 60 | RUN chmod +x /entrypoint.sh 61 | 62 | ENTRYPOINT ["/entrypoint.sh"] 63 | 64 | # Add demo app 65 | COPY ./app /app 66 | WORKDIR /app 67 | 68 | # Run the start script, it will check for an /app/prestart.sh script (e.g. for migrations) 69 | # And then will start Supervisor, which in turn will start Nginx and uWSGI 70 | CMD ["/start.sh"] 71 | -------------------------------------------------------------------------------- /docker-images/requirements.txt: -------------------------------------------------------------------------------- 1 | uwsgi==2.0.30 2 | -------------------------------------------------------------------------------- /docker-images/start.sh: -------------------------------------------------------------------------------- 1 | #! /usr/bin/env sh 2 | set -e 3 | 4 | # If there's a prestart.sh script in the /app directory, run it before starting 5 | PRE_START_PATH=/app/prestart.sh 6 | echo "Checking for script in $PRE_START_PATH" 7 | if [ -f $PRE_START_PATH ] ; then 8 | echo "Running script $PRE_START_PATH" 9 | . $PRE_START_PATH 10 | else 11 | echo "There is no script $PRE_START_PATH" 12 | fi 13 | 14 | # Start Supervisor, with Nginx and uWSGI 15 | exec /usr/bin/supervisord 16 | -------------------------------------------------------------------------------- /docker-images/stop-supervisor.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env sh 2 | 3 | # Ref: 4 | # * https://github.com/tiangolo/uwsgi-nginx-docker/issues/61#issuecomment-508034634 5 | # * https://gist.github.com/ReallyLiri/f833510d350b242ff89b9b76fdf21ea5 6 | # * https://serverfault.com/a/922943 7 | # * https://gist.github.com/tomazzaman/63265dfab3a9a61781993212fa1057cb 8 | # * https://gist.github.com/tomazzaman/63265dfab3a9a61781993212fa1057cb#gistcomment-2812931 9 | # * https://github.com/Supervisor/supervisor/issues/733 10 | # * 11 | printf "READY\n"; 12 | 13 | while read line; do 14 | echo "Processing Event: $line" >&2; 15 | kill $PPID 16 | done < /dev/stdin 17 | -------------------------------------------------------------------------------- /docker-images/supervisord-alpine.ini: -------------------------------------------------------------------------------- 1 | [supervisord] 2 | nodaemon=true 3 | 4 | [program:uwsgi] 5 | command=/usr/sbin/uwsgi --ini /etc/uwsgi/uwsgi.ini 6 | stdout_logfile=/dev/stdout 7 | stdout_logfile_maxbytes=0 8 | stderr_logfile=/dev/stderr 9 | stderr_logfile_maxbytes=0 10 | startsecs = 0 11 | autorestart=false 12 | 13 | [program:nginx] 14 | command=/usr/sbin/nginx 15 | stdout_logfile=/dev/stdout 16 | stdout_logfile_maxbytes=0 17 | stderr_logfile=/dev/stderr 18 | stderr_logfile_maxbytes=0 19 | # Graceful stop, see http://nginx.org/en/docs/control.html 20 | stopsignal=QUIT 21 | startsecs = 0 22 | autorestart=false 23 | 24 | [eventlistener:quit_on_failure] 25 | events=PROCESS_STATE_STOPPED,PROCESS_STATE_EXITED,PROCESS_STATE_FATAL 26 | command=/etc/supervisor/stop-supervisor.sh 27 | -------------------------------------------------------------------------------- /docker-images/supervisord-debian.conf: -------------------------------------------------------------------------------- 1 | [supervisord] 2 | nodaemon=true 3 | 4 | [program:uwsgi] 5 | command=/usr/local/bin/uwsgi --ini /etc/uwsgi/uwsgi.ini 6 | stdout_logfile=/dev/stdout 7 | stdout_logfile_maxbytes=0 8 | stderr_logfile=/dev/stderr 9 | stderr_logfile_maxbytes=0 10 | startsecs = 0 11 | autorestart=false 12 | 13 | [program:nginx] 14 | command=/usr/sbin/nginx 15 | stdout_logfile=/dev/stdout 16 | stdout_logfile_maxbytes=0 17 | stderr_logfile=/dev/stderr 18 | stderr_logfile_maxbytes=0 19 | # Graceful stop, see http://nginx.org/en/docs/control.html 20 | stopsignal=QUIT 21 | startsecs = 0 22 | autorestart=false 23 | 24 | [eventlistener:quit_on_failure] 25 | events=PROCESS_STATE_STOPPED,PROCESS_STATE_EXITED,PROCESS_STATE_FATAL 26 | command=/etc/supervisor/stop-supervisor.sh 27 | -------------------------------------------------------------------------------- /docker-images/uwsgi.ini: -------------------------------------------------------------------------------- 1 | [uwsgi] 2 | socket = /tmp/uwsgi.sock 3 | chown-socket = nginx:nginx 4 | chmod-socket = 664 5 | # Graceful shutdown on SIGTERM, see https://github.com/unbit/uwsgi/issues/849#issuecomment-118869386 6 | hook-master-start = unix_signal:15 gracefully_kill_them_all 7 | need-app = true 8 | die-on-term = true 9 | # For debugging and testing 10 | show-config = true 11 | -------------------------------------------------------------------------------- /tests/.gitignore: -------------------------------------------------------------------------------- 1 | Dockerfile 2 | -------------------------------------------------------------------------------- /tests/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tiangolo/uwsgi-nginx-docker/e2b9958cea8089b166137f7d92846f9e97bb6d33/tests/__init__.py -------------------------------------------------------------------------------- /tests/test_01_main/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tiangolo/uwsgi-nginx-docker/e2b9958cea8089b166137f7d92846f9e97bb6d33/tests/test_01_main/__init__.py -------------------------------------------------------------------------------- /tests/test_01_main/test_defaults.py: -------------------------------------------------------------------------------- 1 | import os 2 | import time 3 | 4 | import docker 5 | import requests 6 | from docker.models.containers import Container 7 | from requests import Response 8 | 9 | from ..utils import ( 10 | CONTAINER_NAME, 11 | get_logs, 12 | get_nginx_config, 13 | get_response_text1, 14 | remove_previous_container, 15 | ) 16 | 17 | client = docker.from_env() 18 | 19 | 20 | def verify_container(container: Container, response_text: str) -> None: 21 | response: Response = requests.get("http://127.0.0.1:8000") 22 | assert response.text == response_text 23 | nginx_config = get_nginx_config(container) 24 | assert "client_max_body_size 0;" in nginx_config 25 | assert "worker_processes 1;" in nginx_config 26 | assert "listen 80;" in nginx_config 27 | assert "worker_connections 1024;" in nginx_config 28 | assert "worker_rlimit_nofile;" not in nginx_config 29 | assert "daemon off;" in nginx_config 30 | assert "include uwsgi_params;" in nginx_config 31 | assert "uwsgi_pass unix:///tmp/uwsgi.sock;" in nginx_config 32 | logs = get_logs(container) 33 | assert "getting INI configuration from /app/uwsgi.ini" in logs 34 | assert "getting INI configuration from /etc/uwsgi/uwsgi.ini" in logs 35 | assert "ini = /app/uwsgi.ini" in logs 36 | assert "ini = /etc/uwsgi/uwsgi.ini" in logs 37 | assert "socket = /tmp/uwsgi.sock" in logs 38 | assert "chown-socket = nginx:nginx" in logs 39 | assert "chmod-socket = 664" in logs 40 | assert "hook-master-start = unix_signal:15 gracefully_kill_them_all" in logs 41 | assert "need-app = true" in logs 42 | assert "die-on-term = true" in logs 43 | assert "show-config = true" in logs 44 | assert "wsgi-file = /app/main.py" in logs 45 | assert "processes = 16" in logs 46 | assert "cheaper = 2" in logs 47 | assert "Checking for script in /app/prestart.sh" in logs 48 | assert "Running script /app/prestart.sh" in logs 49 | assert ( 50 | "Running inside /app/prestart.sh, you could add migrations to this file" in logs 51 | ) 52 | assert "spawned uWSGI master process" in logs 53 | assert "spawned uWSGI worker 1" in logs 54 | assert "spawned uWSGI worker 2" in logs 55 | assert "spawned uWSGI worker 3" not in logs 56 | assert 'running "unix_signal:15 gracefully_kill_them_all" (master-start)' in logs 57 | assert "success: nginx entered RUNNING state, process has stayed up for" in logs 58 | assert "success: uwsgi entered RUNNING state, process has stayed up for" in logs 59 | 60 | 61 | def test_defaults() -> None: 62 | name = os.getenv("NAME") 63 | image = f"tiangolo/uwsgi-nginx:{name}" 64 | response_text = get_response_text1() 65 | sleep_time = int(os.getenv("SLEEP_TIME", 3)) 66 | remove_previous_container(client) 67 | container = client.containers.run( 68 | image, name=CONTAINER_NAME, ports={"80": "8000"}, detach=True 69 | ) 70 | time.sleep(sleep_time) 71 | verify_container(container, response_text) 72 | container.stop() 73 | # Test that everything works after restarting too 74 | container.start() 75 | time.sleep(sleep_time) 76 | verify_container(container, response_text) 77 | container.stop() 78 | container.remove() 79 | -------------------------------------------------------------------------------- /tests/test_01_main/test_env_vars_1.py: -------------------------------------------------------------------------------- 1 | import os 2 | import time 3 | 4 | import docker 5 | import requests 6 | from docker.models.containers import Container 7 | 8 | from ..utils import ( 9 | CONTAINER_NAME, 10 | get_logs, 11 | get_nginx_config, 12 | get_response_text1, 13 | remove_previous_container, 14 | ) 15 | 16 | client = docker.from_env() 17 | 18 | 19 | def verify_container(container: Container, response_text: str) -> None: 20 | response = requests.get("http://127.0.0.1:8000") 21 | assert response.text == response_text 22 | nginx_config = get_nginx_config(container) 23 | assert "client_max_body_size 1m;" in nginx_config 24 | assert "worker_processes 2;" in nginx_config 25 | assert "listen 80;" in nginx_config 26 | assert "worker_connections 2048;" in nginx_config 27 | assert "worker_rlimit_nofile 2048;" in nginx_config 28 | assert "daemon off;" in nginx_config 29 | assert "listen 80;" in nginx_config 30 | assert "include uwsgi_params;" in nginx_config 31 | assert "uwsgi_pass unix:///tmp/uwsgi.sock;" in nginx_config 32 | logs = get_logs(container) 33 | assert "getting INI configuration from /app/uwsgi.ini" in logs 34 | assert "getting INI configuration from /etc/uwsgi/uwsgi.ini" in logs 35 | assert "ini = /app/uwsgi.ini" in logs 36 | assert "ini = /etc/uwsgi/uwsgi.ini" in logs 37 | assert "socket = /tmp/uwsgi.sock" in logs 38 | assert "chown-socket = nginx:nginx" in logs 39 | assert "chmod-socket = 664" in logs 40 | assert "hook-master-start = unix_signal:15 gracefully_kill_them_all" in logs 41 | assert "need-app = true" in logs 42 | assert "die-on-term = true" in logs 43 | assert "show-config = true" in logs 44 | assert "wsgi-file = /app/main.py" in logs 45 | assert "processes = 8" in logs 46 | assert "cheaper = 3" in logs 47 | assert "Checking for script in /app/prestart.sh" in logs 48 | assert "Running script /app/prestart.sh" in logs 49 | assert ( 50 | "Running inside /app/prestart.sh, you could add migrations to this file" in logs 51 | ) 52 | assert "spawned uWSGI master process" in logs 53 | assert "spawned uWSGI worker 1" in logs 54 | assert "spawned uWSGI worker 2" in logs 55 | assert "spawned uWSGI worker 3" in logs 56 | assert "spawned uWSGI worker 4" not in logs 57 | assert 'running "unix_signal:15 gracefully_kill_them_all" (master-start)' in logs 58 | assert "success: nginx entered RUNNING state, process has stayed up for" in logs 59 | assert "success: uwsgi entered RUNNING state, process has stayed up for" in logs 60 | 61 | 62 | def test_env_vars_1() -> None: 63 | name = os.getenv("NAME") 64 | image = f"tiangolo/uwsgi-nginx:{name}" 65 | response_text = get_response_text1() 66 | sleep_time = int(os.getenv("SLEEP_TIME", 3)) 67 | remove_previous_container(client) 68 | container = client.containers.run( 69 | image, 70 | name=CONTAINER_NAME, 71 | environment={ 72 | "UWSGI_CHEAPER": 3, 73 | "UWSGI_PROCESSES": 8, 74 | "NGINX_MAX_UPLOAD": "1m", 75 | "NGINX_WORKER_PROCESSES": 2, 76 | "NGINX_WORKER_CONNECTIONS": 2048, 77 | "NGINX_WORKER_OPEN_FILES": 2048, 78 | }, 79 | ports={"80": "8000"}, 80 | detach=True, 81 | ) 82 | time.sleep(sleep_time) 83 | verify_container(container, response_text) 84 | container.stop() 85 | # Test that everything works after restarting too 86 | container.start() 87 | time.sleep(sleep_time) 88 | verify_container(container, response_text) 89 | container.stop() 90 | container.remove() 91 | -------------------------------------------------------------------------------- /tests/test_02_app/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tiangolo/uwsgi-nginx-docker/e2b9958cea8089b166137f7d92846f9e97bb6d33/tests/test_02_app/__init__.py -------------------------------------------------------------------------------- /tests/test_02_app/app_with_installs/app/main.py: -------------------------------------------------------------------------------- 1 | import sys 2 | 3 | from flask import Flask 4 | 5 | application = Flask(__name__) 6 | 7 | 8 | @application.route("/") 9 | def hello(): 10 | version = "{}.{}".format(sys.version_info.major, sys.version_info.minor) 11 | message = "Hello World from Nginx uWSGI Python {} app in a Docker container".format( 12 | version 13 | ) 14 | return message 15 | -------------------------------------------------------------------------------- /tests/test_02_app/broken_app/app/main.py: -------------------------------------------------------------------------------- 1 | # No application, error out 2 | -------------------------------------------------------------------------------- /tests/test_02_app/custom_app/application/custom_app/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tiangolo/uwsgi-nginx-docker/e2b9958cea8089b166137f7d92846f9e97bb6d33/tests/test_02_app/custom_app/application/custom_app/__init__.py -------------------------------------------------------------------------------- /tests/test_02_app/custom_app/application/custom_app/main.py: -------------------------------------------------------------------------------- 1 | import sys 2 | 3 | 4 | def application(env, start_response): 5 | version = "{}.{}".format(sys.version_info.major, sys.version_info.minor) 6 | start_response("200 OK", [("Content-Type", "text/plain")]) 7 | message = "Hello World from Nginx uWSGI Python {} app in a Docker container".format( 8 | version 9 | ) 10 | return [message.encode("utf-8")] 11 | -------------------------------------------------------------------------------- /tests/test_02_app/custom_app/application/custom_app/uwsgi.ini: -------------------------------------------------------------------------------- 1 | [uwsgi] 2 | wsgi-file=/application/custom_app/main.py 3 | -------------------------------------------------------------------------------- /tests/test_02_app/custom_app/prestart.sh: -------------------------------------------------------------------------------- 1 | #! /usr/bin/env bash 2 | echo "custom prestart.sh running" 3 | -------------------------------------------------------------------------------- /tests/test_02_app/custom_nginx_app/app/main.py: -------------------------------------------------------------------------------- 1 | import sys 2 | 3 | 4 | def application(env, start_response): 5 | version = "{}.{}".format(sys.version_info.major, sys.version_info.minor) 6 | start_response("200 OK", [("Content-Type", "text/plain")]) 7 | message = "Hello World from Nginx uWSGI Python {} app in a Docker container".format( 8 | version 9 | ) 10 | return [message.encode("utf-8")] 11 | -------------------------------------------------------------------------------- /tests/test_02_app/custom_nginx_app/app/nginx.conf: -------------------------------------------------------------------------------- 1 | user nginx; 2 | worker_processes 2; 3 | error_log /var/log/nginx/error.log warn; 4 | pid /var/run/nginx.pid; 5 | events { 6 | worker_connections 2048; 7 | } 8 | http { 9 | include /etc/nginx/mime.types; 10 | default_type application/octet-stream; 11 | log_format main '$remote_addr - $remote_user [$time_local] "$request" ' 12 | '$status $body_bytes_sent "$http_referer" ' 13 | '"$http_user_agent" "$http_x_forwarded_for"'; 14 | access_log /var/log/nginx/access.log main; 15 | sendfile on; 16 | keepalive_timeout 300; 17 | server { 18 | listen 8080; 19 | location / { 20 | include uwsgi_params; 21 | uwsgi_pass unix:///tmp/uwsgi.sock; 22 | } 23 | } 24 | } 25 | daemon off; 26 | -------------------------------------------------------------------------------- /tests/test_02_app/simple_app/app/main.py: -------------------------------------------------------------------------------- 1 | import sys 2 | 3 | 4 | def application(env, start_response): 5 | version = "{}.{}".format(sys.version_info.major, sys.version_info.minor) 6 | start_response("200 OK", [("Content-Type", "text/plain")]) 7 | message = "Hello World from Nginx uWSGI Python {} app in a Docker container".format( 8 | version 9 | ) 10 | return [message.encode("utf-8")] 11 | -------------------------------------------------------------------------------- /tests/test_02_app/test_app_and_env_vars.py: -------------------------------------------------------------------------------- 1 | import os 2 | import time 3 | from pathlib import Path 4 | 5 | import docker 6 | import requests 7 | from docker.models.containers import Container 8 | 9 | from ..utils import ( 10 | CONTAINER_NAME, 11 | generate_dockerfile_content_custom_app, 12 | get_logs, 13 | get_nginx_config, 14 | get_response_text2, 15 | remove_previous_container, 16 | ) 17 | 18 | client = docker.from_env() 19 | 20 | 21 | def verify_container(container: Container, response_text: str) -> None: 22 | response = requests.get("http://127.0.0.1:8000") 23 | assert response.text == response_text 24 | nginx_config = get_nginx_config(container) 25 | assert "client_max_body_size 0;" in nginx_config 26 | assert "worker_processes 1;" in nginx_config 27 | assert "listen 8080;" in nginx_config 28 | assert "worker_connections 1024;" in nginx_config 29 | assert "worker_rlimit_nofile;" not in nginx_config 30 | assert "daemon off;" in nginx_config 31 | assert "include uwsgi_params;" in nginx_config 32 | assert "uwsgi_pass unix:///tmp/uwsgi.sock;" in nginx_config 33 | logs = get_logs(container) 34 | assert "getting INI configuration from /application/custom_app/uwsgi.ini" in logs 35 | assert "getting INI configuration from /etc/uwsgi/uwsgi.ini" in logs 36 | assert "ini = /application/custom_app/uwsgi.ini" in logs 37 | assert "ini = /etc/uwsgi/uwsgi.ini" in logs 38 | assert "socket = /tmp/uwsgi.sock" in logs 39 | assert "chown-socket = nginx:nginx" in logs 40 | assert "chmod-socket = 664" in logs 41 | assert "hook-master-start = unix_signal:15 gracefully_kill_them_all" in logs 42 | assert "need-app = true" in logs 43 | assert "die-on-term = true" in logs 44 | assert "show-config = true" in logs 45 | assert "wsgi-file = /application/custom_app/main.py" in logs 46 | assert "processes = 16" in logs 47 | assert "cheaper = 2" in logs 48 | assert "Checking for script in /app/prestart.sh" in logs 49 | assert "Running script /app/prestart.sh" in logs 50 | assert "custom prestart.sh running" in logs 51 | assert "spawned uWSGI master process" in logs 52 | assert "spawned uWSGI worker 1" in logs 53 | assert "spawned uWSGI worker 2" in logs 54 | assert "spawned uWSGI worker 3" not in logs 55 | assert 'running "unix_signal:15 gracefully_kill_them_all" (master-start)' in logs 56 | assert "success: nginx entered RUNNING state, process has stayed up for" in logs 57 | assert "success: uwsgi entered RUNNING state, process has stayed up for" in logs 58 | 59 | 60 | def test_env_vars_1() -> None: 61 | name = os.getenv("NAME", "") 62 | dockerfile_content = generate_dockerfile_content_custom_app(name) 63 | dockerfile = "Dockerfile" 64 | response_text = get_response_text2() 65 | sleep_time = int(os.getenv("SLEEP_TIME", 3)) 66 | remove_previous_container(client) 67 | tag = "uwsgi-nginx-testimage" 68 | test_path = Path(__file__) 69 | path = test_path.parent / "custom_app" 70 | dockerfile_path = path / dockerfile 71 | dockerfile_path.write_text(dockerfile_content) 72 | client.images.build(path=str(path), dockerfile=dockerfile, tag=tag) 73 | container = client.containers.run( 74 | tag, 75 | name=CONTAINER_NAME, 76 | environment={ 77 | "UWSGI_INI": "/application/custom_app/uwsgi.ini", 78 | "LISTEN_PORT": "8080", 79 | }, 80 | ports={"8080": "8000"}, 81 | detach=True, 82 | ) 83 | time.sleep(sleep_time) 84 | verify_container(container, response_text) 85 | container.stop() 86 | # Test that everything works after restarting too 87 | container.start() 88 | time.sleep(sleep_time) 89 | verify_container(container, response_text) 90 | container.stop() 91 | container.remove() 92 | -------------------------------------------------------------------------------- /tests/test_02_app/test_app_with_installs.py: -------------------------------------------------------------------------------- 1 | import os 2 | import time 3 | from pathlib import Path 4 | 5 | import docker 6 | import requests 7 | from docker.models.containers import Container 8 | 9 | from ..utils import ( 10 | CONTAINER_NAME, generate_dockerfile_content_app_with_installs, 11 | get_logs, 12 | get_nginx_config, 13 | get_response_text2, 14 | remove_previous_container, 15 | ) 16 | 17 | client = docker.from_env() 18 | 19 | 20 | def verify_container(container: Container, response_text: str) -> None: 21 | response = requests.get("http://127.0.0.1:8000") 22 | assert response.text == response_text 23 | nginx_config = get_nginx_config(container) 24 | assert "client_max_body_size 0;" in nginx_config 25 | assert "worker_processes 1;" in nginx_config 26 | assert "listen 80;" in nginx_config 27 | assert "worker_connections 1024;" in nginx_config 28 | assert "worker_rlimit_nofile;" not in nginx_config 29 | assert "daemon off;" in nginx_config 30 | assert "include uwsgi_params;" in nginx_config 31 | assert "uwsgi_pass unix:///tmp/uwsgi.sock;" in nginx_config 32 | logs = get_logs(container) 33 | assert "getting INI configuration from /app/uwsgi.ini" in logs 34 | assert "getting INI configuration from /etc/uwsgi/uwsgi.ini" in logs 35 | assert "ini = /app/uwsgi.ini" in logs 36 | assert "ini = /etc/uwsgi/uwsgi.ini" in logs 37 | assert "socket = /tmp/uwsgi.sock" in logs 38 | assert "chown-socket = nginx:nginx" in logs 39 | assert "chmod-socket = 664" in logs 40 | assert "hook-master-start = unix_signal:15 gracefully_kill_them_all" in logs 41 | assert "need-app = true" in logs 42 | assert "die-on-term = true" in logs 43 | assert "show-config = true" in logs 44 | assert "wsgi-file = /app/main.py" in logs 45 | assert "processes = 16" in logs 46 | assert "cheaper = 2" in logs 47 | assert "Checking for script in /app/prestart.sh" in logs 48 | assert "Running script /app/prestart.sh" in logs 49 | assert ( 50 | "Running inside /app/prestart.sh, you could add migrations to this file" in logs 51 | ) 52 | assert "spawned uWSGI master process" in logs 53 | assert "spawned uWSGI worker 1" in logs 54 | assert "spawned uWSGI worker 2" in logs 55 | assert "spawned uWSGI worker 3" not in logs 56 | assert 'running "unix_signal:15 gracefully_kill_them_all" (master-start)' in logs 57 | assert "success: nginx entered RUNNING state, process has stayed up for" in logs 58 | assert "success: uwsgi entered RUNNING state, process has stayed up for" in logs 59 | 60 | 61 | def test_env_vars_1() -> None: 62 | name = os.getenv("NAME", "") 63 | dockerfile_content = generate_dockerfile_content_app_with_installs(name) 64 | dockerfile = "Dockerfile" 65 | response_text = get_response_text2() 66 | sleep_time = int(os.getenv("SLEEP_TIME", 3)) 67 | remove_previous_container(client) 68 | tag = "uwsgi-nginx-testimage" 69 | test_path = Path(__file__) 70 | path = test_path.parent / "app_with_installs" 71 | dockerfile_path = path / dockerfile 72 | dockerfile_path.write_text(dockerfile_content) 73 | client.images.build(path=str(path), dockerfile=dockerfile, tag=tag) 74 | container = client.containers.run( 75 | tag, name=CONTAINER_NAME, ports={"80": "8000"}, detach=True 76 | ) 77 | time.sleep(sleep_time) 78 | verify_container(container, response_text) 79 | container.stop() 80 | # Test that everything works after restarting too 81 | container.start() 82 | time.sleep(sleep_time) 83 | verify_container(container, response_text) 84 | container.stop() 85 | container.remove() 86 | -------------------------------------------------------------------------------- /tests/test_02_app/test_broken_app.py: -------------------------------------------------------------------------------- 1 | import os 2 | import time 3 | from pathlib import Path 4 | 5 | import docker 6 | from docker.models.containers import Container 7 | 8 | from ..utils import ( 9 | CONTAINER_NAME, 10 | generate_dockerfile_content_simple_app, 11 | get_logs, 12 | remove_previous_container, 13 | ) 14 | 15 | client = docker.from_env() 16 | 17 | 18 | def verify_container(container: Container) -> None: 19 | logs = get_logs(container) 20 | print(logs) 21 | assert 'unable to find "application" callable in file /app/main.py' in logs 22 | assert ( 23 | "unable to load app 0 (mountpoint='') (callable not found or import error)" 24 | in logs 25 | ) 26 | assert "*** no app loaded. GAME OVER ***" in logs 27 | assert "exited: uwsgi (exit status 22; not expected)" in logs 28 | assert ( 29 | "INFO success: quit_on_failure entered RUNNING state, process has stayed up for > than 1 seconds (startsecs)" 30 | in logs 31 | ) 32 | assert "WARN received SIGTERM indicating exit request" in logs 33 | assert "INFO stopped: nginx (exit status 0)" in logs 34 | assert "stopped: quit_on_failure (terminated by SIGTERM)" in logs 35 | 36 | 37 | def test_on_broken_quit_container() -> None: 38 | name = os.getenv("NAME", "") 39 | dockerfile_content = generate_dockerfile_content_simple_app(name) 40 | dockerfile = "Dockerfile" 41 | remove_previous_container(client) 42 | tag = "uwsgi-nginx-testimage" 43 | test_path = Path(__file__) 44 | path = test_path.parent / "broken_app" 45 | dockerfile_path = path / dockerfile 46 | dockerfile_path.write_text(dockerfile_content) 47 | client.images.build(path=str(path), dockerfile=dockerfile, tag=tag) 48 | container: Container = client.containers.run( 49 | tag, name=CONTAINER_NAME, ports={"80": "8000"}, detach=True 50 | ) 51 | while container.status != "exited": 52 | time.sleep(1) 53 | container.reload() 54 | verify_container(container) 55 | updated_container: Container = client.containers.get(container.id) 56 | assert updated_container.status == "exited" 57 | container.stop() 58 | -------------------------------------------------------------------------------- /tests/test_02_app/test_custom_nginx_app.py: -------------------------------------------------------------------------------- 1 | import os 2 | import time 3 | from pathlib import Path 4 | 5 | import docker 6 | import requests 7 | from docker.models.containers import Container 8 | 9 | from ..utils import ( 10 | CONTAINER_NAME, 11 | generate_dockerfile_content_custom_nginx_app, 12 | get_logs, 13 | get_nginx_config, 14 | get_response_text2, 15 | remove_previous_container, 16 | ) 17 | 18 | client = docker.from_env() 19 | 20 | 21 | def verify_container(container: Container, response_text: str) -> None: 22 | response = requests.get("http://127.0.0.1:8080") 23 | assert response.text == response_text 24 | nginx_config = get_nginx_config(container) 25 | assert "client_max_body_size 0;" not in nginx_config 26 | assert "worker_processes 2;" in nginx_config 27 | assert "listen 8080;" in nginx_config 28 | assert "worker_connections 2048;" in nginx_config 29 | assert "worker_rlimit_nofile;" not in nginx_config 30 | assert "daemon off;" in nginx_config 31 | assert "include uwsgi_params;" in nginx_config 32 | assert "uwsgi_pass unix:///tmp/uwsgi.sock;" in nginx_config 33 | assert "keepalive_timeout 300;" in nginx_config 34 | logs = get_logs(container) 35 | assert "getting INI configuration from /app/uwsgi.ini" in logs 36 | assert "getting INI configuration from /etc/uwsgi/uwsgi.ini" in logs 37 | assert "ini = /app/uwsgi.ini" in logs 38 | assert "ini = /etc/uwsgi/uwsgi.ini" in logs 39 | assert "socket = /tmp/uwsgi.sock" in logs 40 | assert "chown-socket = nginx:nginx" in logs 41 | assert "chmod-socket = 664" in logs 42 | assert "hook-master-start = unix_signal:15 gracefully_kill_them_all" in logs 43 | assert "need-app = true" in logs 44 | assert "die-on-term = true" in logs 45 | assert "show-config = true" in logs 46 | assert "wsgi-file = /app/main.py" in logs 47 | assert "processes = 16" in logs 48 | assert "cheaper = 2" in logs 49 | assert "Checking for script in /app/prestart.sh" in logs 50 | assert "Running script /app/prestart.sh" in logs 51 | assert ( 52 | "Running inside /app/prestart.sh, you could add migrations to this file" in logs 53 | ) 54 | assert "spawned uWSGI master process" in logs 55 | assert "spawned uWSGI worker 1" in logs 56 | assert "spawned uWSGI worker 2" in logs 57 | assert "spawned uWSGI worker 3" not in logs 58 | assert 'running "unix_signal:15 gracefully_kill_them_all" (master-start)' in logs 59 | assert "success: nginx entered RUNNING state, process has stayed up for" in logs 60 | assert "success: uwsgi entered RUNNING state, process has stayed up for" in logs 61 | 62 | 63 | def test_env_vars_1() -> None: 64 | name = os.getenv("NAME", "") 65 | dockerfile_content = generate_dockerfile_content_custom_nginx_app(name) 66 | dockerfile = "Dockerfile" 67 | response_text = get_response_text2() 68 | sleep_time = int(os.getenv("SLEEP_TIME", 3)) 69 | remove_previous_container(client) 70 | tag = "uwsgi-nginx-testimage" 71 | test_path = Path(__file__) 72 | path = test_path.parent / "custom_nginx_app" 73 | dockerfile_path = path / dockerfile 74 | dockerfile_path.write_text(dockerfile_content) 75 | client.images.build(path=str(path), dockerfile=dockerfile, tag=tag) 76 | container = client.containers.run( 77 | tag, name=CONTAINER_NAME, ports={"8080": "8080"}, detach=True 78 | ) 79 | time.sleep(sleep_time) 80 | verify_container(container, response_text) 81 | container.stop() 82 | # Test that everything works after restarting too 83 | container.start() 84 | time.sleep(sleep_time) 85 | verify_container(container, response_text) 86 | container.stop() 87 | container.remove() 88 | -------------------------------------------------------------------------------- /tests/test_02_app/test_simple_app.py: -------------------------------------------------------------------------------- 1 | import os 2 | import time 3 | from pathlib import Path 4 | 5 | import docker 6 | import requests 7 | from docker.models.containers import Container 8 | 9 | from ..utils import ( 10 | CONTAINER_NAME, 11 | generate_dockerfile_content_simple_app, 12 | get_logs, 13 | get_nginx_config, 14 | get_response_text2, 15 | remove_previous_container, 16 | ) 17 | 18 | client = docker.from_env() 19 | 20 | 21 | def verify_container(container: Container, response_text: str) -> None: 22 | response = requests.get("http://127.0.0.1:8000") 23 | assert response.text == response_text 24 | nginx_config = get_nginx_config(container) 25 | assert "client_max_body_size 0;" in nginx_config 26 | assert "worker_processes 1;" in nginx_config 27 | assert "listen 80;" in nginx_config 28 | assert "worker_connections 1024;" in nginx_config 29 | assert "worker_rlimit_nofile;" not in nginx_config 30 | assert "daemon off;" in nginx_config 31 | assert "include uwsgi_params;" in nginx_config 32 | assert "uwsgi_pass unix:///tmp/uwsgi.sock;" in nginx_config 33 | logs = get_logs(container) 34 | assert "getting INI configuration from /app/uwsgi.ini" in logs 35 | assert "getting INI configuration from /etc/uwsgi/uwsgi.ini" in logs 36 | assert "ini = /app/uwsgi.ini" in logs 37 | assert "ini = /etc/uwsgi/uwsgi.ini" in logs 38 | assert "socket = /tmp/uwsgi.sock" in logs 39 | assert "chown-socket = nginx:nginx" in logs 40 | assert "chmod-socket = 664" in logs 41 | assert "hook-master-start = unix_signal:15 gracefully_kill_them_all" in logs 42 | assert "need-app = true" in logs 43 | assert "die-on-term = true" in logs 44 | assert "show-config = true" in logs 45 | assert "wsgi-file = /app/main.py" in logs 46 | assert "processes = 16" in logs 47 | assert "cheaper = 2" in logs 48 | assert "Checking for script in /app/prestart.sh" in logs 49 | assert "Running script /app/prestart.sh" in logs 50 | assert ( 51 | "Running inside /app/prestart.sh, you could add migrations to this file" in logs 52 | ) 53 | assert "spawned uWSGI master process" in logs 54 | assert "spawned uWSGI worker 1" in logs 55 | assert "spawned uWSGI worker 2" in logs 56 | assert "spawned uWSGI worker 3" not in logs 57 | assert 'running "unix_signal:15 gracefully_kill_them_all" (master-start)' in logs 58 | assert "success: nginx entered RUNNING state, process has stayed up for" in logs 59 | assert "success: uwsgi entered RUNNING state, process has stayed up for" in logs 60 | 61 | 62 | def test_env_vars_1() -> None: 63 | name = os.getenv("NAME", "") 64 | dockerfile_content = generate_dockerfile_content_simple_app(name) 65 | dockerfile = "Dockerfile" 66 | response_text = get_response_text2() 67 | sleep_time = int(os.getenv("SLEEP_TIME", 3)) 68 | remove_previous_container(client) 69 | tag = "uwsgi-nginx-testimage" 70 | test_path = Path(__file__) 71 | path = test_path.parent / "simple_app" 72 | dockerfile_path = path / dockerfile 73 | dockerfile_path.write_text(dockerfile_content) 74 | client.images.build(path=str(path), dockerfile=dockerfile, tag=tag) 75 | container = client.containers.run( 76 | tag, name=CONTAINER_NAME, ports={"80": "8000"}, detach=True 77 | ) 78 | time.sleep(sleep_time) 79 | verify_container(container, response_text) 80 | container.stop() 81 | # Test that everything works after restarting too 82 | container.start() 83 | time.sleep(sleep_time) 84 | verify_container(container, response_text) 85 | container.stop() 86 | container.remove() 87 | -------------------------------------------------------------------------------- /tests/utils.py: -------------------------------------------------------------------------------- 1 | import os 2 | 3 | from docker.client import DockerClient 4 | from docker.errors import NotFound 5 | from docker.models.containers import Container 6 | 7 | CONTAINER_NAME = "uwsgi-nginx-test" 8 | 9 | 10 | def get_logs(container: Container) -> str: 11 | logs = container.logs() 12 | return logs.decode("utf-8") 13 | 14 | 15 | def get_nginx_config(container: Container) -> str: 16 | result = container.exec_run(f"/usr/sbin/nginx -T") 17 | return result.output.decode() 18 | 19 | 20 | def remove_previous_container(client: DockerClient) -> None: 21 | try: 22 | previous = client.containers.get(CONTAINER_NAME) 23 | previous.stop() 24 | previous.remove() 25 | except NotFound: 26 | return None 27 | 28 | 29 | def get_response_text1() -> str: 30 | python_version = os.getenv("PYTHON_VERSION") 31 | return f"Hello World from a default Nginx uWSGI Python {python_version} app in a Docker container (default)" 32 | 33 | 34 | def get_response_text2() -> str: 35 | python_version = os.getenv("PYTHON_VERSION") 36 | return f"Hello World from Nginx uWSGI Python {python_version} app in a Docker container" 37 | 38 | 39 | def generate_dockerfile_content_custom_app(name: str) -> str: 40 | content = f"FROM tiangolo/uwsgi-nginx:{name}\n" 41 | content += "COPY ./application /application\n" 42 | content += "COPY ./prestart.sh /app/prestart.sh\n" 43 | content += "WORKDIR /application\n" 44 | return content 45 | 46 | 47 | def generate_dockerfile_content_custom_nginx_app(name: str) -> str: 48 | content = f"FROM tiangolo/uwsgi-nginx:{name}\n" 49 | content += "COPY app /app\n" 50 | return content 51 | 52 | 53 | def generate_dockerfile_content_simple_app(name: str) -> str: 54 | content = f"FROM tiangolo/uwsgi-nginx:{name}\n" 55 | content += "COPY ./app/main.py /app/main.py\n" 56 | return content 57 | 58 | 59 | def generate_dockerfile_content_app_with_installs(name: str) -> str: 60 | content = f"FROM tiangolo/uwsgi-nginx:{name}\n" 61 | content += "RUN pip install flask\n" 62 | content += "COPY ./app/main.py /app/main.py\n" 63 | return content 64 | --------------------------------------------------------------------------------