{"id":22933528,"url":"https://github.com/bluebrown/docker-flask","last_synced_at":"2026-04-04T16:31:53.706Z","repository":{"id":52454390,"uuid":"361261024","full_name":"bluebrown/docker-flask","owner":"bluebrown","description":"template project for dockerized flask apps to run in cloud environment","archived":false,"fork":false,"pushed_at":"2021-05-18T11:58:10.000Z","size":1372,"stargazers_count":1,"open_issues_count":0,"forks_count":1,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-04-01T18:37:41.902Z","etag":null,"topics":["docker","flask","mongo","nginx"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/bluebrown.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2021-04-24T20:33:21.000Z","updated_at":"2022-02-09T21:11:37.000Z","dependencies_parsed_at":"2022-09-18T19:03:00.849Z","dependency_job_id":null,"html_url":"https://github.com/bluebrown/docker-flask","commit_stats":null,"previous_names":[],"tags_count":0,"template":true,"template_full_name":null,"purl":"pkg:github/bluebrown/docker-flask","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bluebrown%2Fdocker-flask","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bluebrown%2Fdocker-flask/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bluebrown%2Fdocker-flask/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bluebrown%2Fdocker-flask/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/bluebrown","download_url":"https://codeload.github.com/bluebrown/docker-flask/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bluebrown%2Fdocker-flask/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":31405703,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-04-04T10:20:44.708Z","status":"ssl_error","status_checked_at":"2026-04-04T10:20:06.846Z","response_time":60,"last_error":"SSL_read: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["docker","flask","mongo","nginx"],"created_at":"2024-12-14T11:30:14.213Z","updated_at":"2026-04-04T16:31:53.684Z","avatar_url":"https://github.com/bluebrown.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Docker Flask Example\n\nDemo flask application connected to mongodb with kubernetes compliant health probe endpoints.\n\n```yml\nversion: \"3.9\"\n\nvolumes: \n  prod_data:\n\nservices:\n  flask:\n    image: flaskapp:0.1.8-prod\n    build:\n      context: ./\n      dockerfile: Dockerfile\n      args:\n        build_date: '2021-04-25'\n        version: 0.1.8-prod\n    ports: [5000:5000]\n    volumes: [\"./log:/var/log\"]\n    environment: \n      MONGO_URI: mongodb://root:rootpassword@mongo/     # valid rfc connection string\n      GUNICORN_CMD_ARGS: --capture-output               # see docs for all options\n      LOG_LEVEL: error                                  # debug|info|warning|error|critical\n      LOG_FORMAT: json                                  # json|text\n      FILTER_PROBES: '1'                                # 0|1 - don't log requests to healthcheck endpoints with access logger\n\n  mongo:\n    image: mongo:latest\n    environment:\n      MONGO_INITDB_ROOT_USERNAME: root\n      MONGO_INITDB_ROOT_PASSWORD: rootpassword\n    volumes: [prod_data:/data/db]\n\n```\n\n## Start the app\n\nThe below command will start the flask application and a mongodb in docker container.\n\n```console\ndocker-compose up\n```\n\nOnce the container are running, the flask documentation can be downloaded as pdf\n\n```console\ncurl localhost:5000/pdf \u003e flask.pdf\n```\n\nThere is also an endpoint to accept message posts.\n\n```console\n$ curl -i -H \"Content-Type: application/json\" -X POST -d '{\"message\":\"hello, flask\"}' http://localhost:5000/msg\nHTTP/1.1 201 CREATED\nServer: gunicorn\nDate: Sat, 24 Apr 2021 15:08:22 GMT\nConnection: keep-alive\nContent-Type: application/json\nContent-Length: 34\n\n{\"id\":\"60843466092d7c719ec5063b\"}\n```\n\nA list of all messages can be retrieved on the msg endpoint.\n\n```console\n$ curl localhost:5000/msg\n[{\"_id\": {\"$oid\": \"60843466092d7c719ec5063b\"}, \"message\": \"hello, flask\"}]\n```\n\n## Health Probe\n\nAs long as the app is running it will serve a 200 response on the `/alive` endpoint.\n\n```console\n$ curl -i localhost:5000/alive\nHTTP/1.1 200 OK\nServer: gunicorn\nDate: Mon, 26 Apr 2021 01:33:25 GMT\nConnection: keep-alive\nContent-Type: text/html; charset=utf-8\nContent-Length: 0\n```\n\nAs long as the database connection is working the app will return a 200 response on `/ready`.\n\n```console\n$ curl -Is localhost:5000/ready | head -n 1\nHTTP/1.0 200 OK\n```\n\nThe behavior can be be tested by shutting down the database container. Once the database is down, the application with return a status 503 response.\n\n```console\n$ docker-compose stop mongo\n$ curl -Is localhost:5000/ready | head -n 1\nHTTP/1.0 503 SERVICE UNAVAILABLE\n```\n\nOnce the database is started up again the app will return again a 200.\n\n```console\n$ docker-compose start mongo\n$ curl -Is localhost:5000/ready | head -n 1\nHTTP/1.0 200 OK\n```\n\n## Environment\n\n### Logging\n\nThe format of the output is by default `json` to play nicely with modern tools. However it can be set to `text` via the environment variable `LOG_FORMAT`. Likewise the log level can be set via `LOG_LEVEL`.\n\n```yml\nLOG_LEVEL: info     # debug|info|warning|error|critical\nLOG_FORMAT: json    # json|text\n```\n\nThe application logs by default into to stdout and stderr. The output can also be directed to log files if needed. For this the gunicorns command line args or flags can be used.\n\n```yml\nvolumes: [\"./log:/var/log\"]\nenvironment: [\"GUNICORN_CMD_ARGS=--capture-output\"]\n```\n\n### Database URI\n\nThe connection string is set via\n\n```yml\nMONGO_URI: mongodb://user:password@server/\n```\n\n### Gunicorn\n\nThe `GUNICORN_CMD_ARGS` variable can be used as per [documentation](https://docs.gunicorn.org/en/20.1.0/configure.html). This can be useful do do some tweaking ad hoc.\n\n```yml\nGUNICORN_CMD_ARGS: \"--workers=6 --threads=4\"\n```\n\n### Proxy Fix\n\nWhen running behind a proxy, a fix can be applied which reads information from `X-Proxy-Headers` and puts them accordingly in the `WSGI` request object. The parameters are passed to [ProxyFix](https://werkzeug.palletsprojects.com/en/1.0.x/middleware/proxy_fix/).\n\n```yml\nPROXY_FIX: x_for=1 x_host=1 x_port=1 x_prefix=1 x_proto=1\n```\n\n## Development\n\nThe project is using `pipenv` to manage dependencies. It can be useful to set up the virtual environment locally.\n\n```console\npipenv shell\npipenv install --dev\n```\n\nInstall the `pre-commit hook`\n\n```console\npre-commit install\n```\n\nFlake8 has been configured to accept a maximum line length of 119. When using VS Code, the following settings can be used in order to have alignment with the pre commit hook.\n\n```json\n{\n    \"python.linting.enabled\": true,\n    \"python.linting.pylintEnabled\": false,\n    \"python.linting.flake8Enabled\": true,\n    \"python.formatting.provider\": \"black\"\n}\n```\n\nA test environment with hot reload can be started, it will mount the app directory so code changes are picked up and the workers are restarted.\n\n```console\ndocker-compose -p dev -f dev-compose.yml up\n```\n\n## Testing\n\nFor testing a compose file exists at the root directory. It is configured to connect to a silent test database.\n\nThe following command will start the app and a test database container and run the test suit. Afterwards the db container is shut down.\n\n```console\ndocker-compose -p test -f test-compose.yml up \u0026\u0026 \\\n  docker-compose -p test -f test-compose.yml down\n```\n\n## Nginx\n\nA nginx-compose exists that creates an additional nginx container to reverse proxy the requests to gunicorn over unix sockets in a shared volume.\n\nThis is because gunicorn inst a fully fletched webserver, is is recommend to use a reverse proxy. Nginx plays well with gunicorn.\n\n```console\ndocker-compose -p proxy -f nginx-compose.yml up --build\n```\n\n## Makefile\n\nFor convenience the commands are available via make.\n\n```console\nmake\nmake proxy\nmake test\nmake dev\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbluebrown%2Fdocker-flask","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbluebrown%2Fdocker-flask","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbluebrown%2Fdocker-flask/lists"}