{"id":18314547,"url":"https://github.com/br3ndonland/pythonvue","last_synced_at":"2025-04-05T19:34:47.335Z","repository":{"id":115927033,"uuid":"226239215","full_name":"br3ndonland/pythonvue","owner":"br3ndonland","description":"Full-stack Python and Vue.js application","archived":true,"fork":false,"pushed_at":"2020-09-22T22:21:33.000Z","size":378,"stargazers_count":16,"open_issues_count":0,"forks_count":5,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-01T02:44:12.910Z","etag":null,"topics":["docker","pre-commit","prettier","python","typescript","vue"],"latest_commit_sha":null,"homepage":null,"language":"Python","has_issues":false,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/br3ndonland.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2019-12-06T03:35:14.000Z","updated_at":"2024-09-20T18:26:42.000Z","dependencies_parsed_at":null,"dependency_job_id":"f2fa71a8-963c-4d96-90fa-a78c412aed60","html_url":"https://github.com/br3ndonland/pythonvue","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/br3ndonland%2Fpythonvue","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/br3ndonland%2Fpythonvue/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/br3ndonland%2Fpythonvue/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/br3ndonland%2Fpythonvue/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/br3ndonland","download_url":"https://codeload.github.com/br3ndonland/pythonvue/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247393480,"owners_count":20931807,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["docker","pre-commit","prettier","python","typescript","vue"],"created_at":"2024-11-05T16:34:30.795Z","updated_at":"2025-04-05T19:34:47.324Z","avatar_url":"https://github.com/br3ndonland.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Full-stack Python and Vue.js application\n\n## Backend Requirements\n\n- [Docker](https://www.docker.com/).\n- [Docker Compose](https://docs.docker.com/compose/install/).\n- [Poetry](https://python-poetry.org/) for Python package and environment management.\n\n## Frontend Requirements\n\n- Node.js (with `npm`).\n\n## Backend local development\n\n- Start the stack with Docker Compose:\n\n```bash\ndocker-compose up -d\n```\n\n- Now you can open your browser and interact with these URLs:\n\nFrontend, built with Docker, with routes handled based on the path: http://localhost\n\nBackend, JSON based web API based on OpenAPI: http://localhost/api/\n\nAutomatic interactive documentation with Swagger UI (from the OpenAPI backend): http://localhost/docs\n\nAlternative automatic documentation with ReDoc (from the OpenAPI backend): http://localhost/redoc\n\nPGAdmin, PostgreSQL web administration: http://localhost:5050\n\nFlower, administration of Celery tasks: http://localhost:5555\n\nTraefik UI, to see how the routes are being handled by the proxy: http://localhost:8090\n\n**Note**: The first time you start your stack, it might take a minute for it to be ready. While the backend waits for the database to be ready and configures everything. You can check the logs to monitor it.\n\nTo check the logs, run:\n\n```bash\ndocker-compose logs\n```\n\nTo check the logs of a specific service, add the name of the service, e.g.:\n\n```bash\ndocker-compose logs backend\n```\n\nIf your Docker is not running in `localhost` (the URLs above wouldn't work) check the sections below on **Development with Docker Toolbox** and **Development with a custom IP**.\n\n## Backend local development, additional details\n\n### General workflow\n\nBy default, the dependencies are managed with [Poetry](https://python-poetry.org/), go there and install it.\n\nFrom `./backend/app/` you can install all the dependencies with:\n\n```console\n$ poetry install\n```\n\nThen you can start a shell session with the new environment with:\n\n```console\n$ poetry shell\n```\n\nNext, open your editor at `./backend/app/` (instead of the project root: `./`), so that you see an `./app/` directory with your code inside. That way, your editor will be able to find all the imports, etc. Make sure your editor uses the environment you just created with Poetry.\n\nModify or add SQLAlchemy models in `./backend/app/app/models/`, Pydantic schemas in `./backend/app/app/schemas/`, API endpoints in `./backend/app/app/api/`, CRUD (Create, Read, Update, Delete) utils in `./backend/app/app/crud/`. The easiest might be to copy the ones for Items (models, endpoints, and CRUD utils) and update them to your needs.\n\nAdd and modify tasks to the Celery worker in `./backend/app/app/worker.py`.\n\nIf you need to install any additional package to the worker, add it to the file `./backend/app/celeryworker.dockerfile`.\n\n### Docker Compose Override\n\nDuring development, you can change Docker Compose settings that will only affect the local development environment, in the file `docker-compose.override.yml`.\n\nThe changes to that file only affect the local development environment, not the production environment. So, you can add \"temporary\" changes that help the development workflow.\n\nFor example, the directory with the backend code is mounted as a Docker \"host volume\", mapping the code you change live to the directory inside the container. That allows you to test your changes right away, without having to build the Docker image again. It should only be done during development, for production, you should build the Docker image with a recent version of the backend code. But during development, it allows you to iterate very fast.\n\nThere is also a command override that runs `/start-reload.sh` (included in the base image) instead of the default `/start.sh` (also included in the base image). It starts a single server process (instead of multiple, as would be for production) and reloads the process whenever the code changes. Have in mind that if you have a syntax error and save the Python file, it will break and exit, and the container will stop. After that, you can restart the container by fixing the error and running again:\n\n```console\n$ docker-compose up -d\n```\n\nThere is also a commented out `command` override, you can uncomment it and comment the default one. It makes the backend container run a process that does \"nothing\", but keeps the container alive. That allows you to get inside your running container and execute commands inside, for example a Python interpreter to test installed dependencies, or start the development server that reloads when it detects changes, or start a Jupyter Notebook session.\n\nTo get inside the container with a `bash` session you can start the stack with:\n\n```console\n$ docker-compose up -d\n```\n\nand then `exec` inside the running container:\n\n```console\n$ docker-compose exec backend bash\n```\n\nYou should see an output like:\n\n```console\nroot@7f2607af31c3:/app#\n```\n\nthat means that you are in a `bash` session inside your container, as a `root` user, under the `/app` directory.\n\nThere you can use the script `/start-reload.sh` to run the debug live reloading server. You can run that script from inside the container with:\n\n```console\n$ bash /start-reload.sh\n```\n\n...it will look like:\n\n```console\nroot@7f2607af31c3:/app# bash /start-reload.sh\n```\n\nand then hit enter. That runs the live reloading server that auto reloads when it detects code changes.\n\nNevertheless, if it doesn't detect a change but a syntax error, it will just stop with an error. But as the container is still alive and you are in a Bash session, you can quickly restart it after fixing the error, running the same command (\"up arrow\" and \"Enter\").\n\n...this previous detail is what makes it useful to have the container alive doing nothing and then, in a Bash session, make it run the live reload server.\n\n### Backend tests\n\nTo test the backend run:\n\n```console\n$ DOMAIN=backend sh ./scripts/test.sh\n```\n\nThe file `./scripts/test.sh` has the commands to generate a testing `docker-stack.yml` file, start the stack and test it.\n\nThe tests run with Pytest, modify and add tests to `./backend/app/app/tests/`.\n\nIf you use GitLab CI the tests will run automatically.\n\n#### Local tests\n\nStart the stack with this command:\n\n```Bash\nDOMAIN=backend sh ./scripts/test-local.sh\n```\n\nThe `./backend/app` directory is mounted as a \"host volume\" inside the docker container (set in the file `docker-compose.dev.volumes.yml`).\nYou can rerun the test on live code:\n\n```Bash\ndocker-compose exec backend /app/tests-start.sh\n```\n\n#### Test running stack\n\nIf your stack is already up and you just want to run the tests, you can use:\n\n```bash\ndocker-compose exec backend /app/tests-start.sh\n```\n\nThat `/app/tests-start.sh` script just calls `pytest` after making sure that the rest of the stack is running. If you need to pass extra arguments to `pytest`, you can pass them to that command and they will be forwarded.\n\nFor example, to stop on first error:\n\n```bash\ndocker-compose exec backend bash /app/tests-start.sh -x\n```\n\n#### Test Coverage\n\nBecause the test scripts forward arguments to `pytest`, you can enable test coverage HTML report generation by passing `--cov-report=html`.\n\nTo run the local tests with coverage HTML reports:\n\n```Bash\nDOMAIN=backend sh ./scripts/test-local.sh --cov-report=html\n```\n\nTo run the tests in a running stack with coverage HTML reports:\n\n```bash\ndocker-compose exec backend bash /app/tests-start.sh --cov-report=html\n```\n\n### Live development with Python Jupyter Notebooks\n\nIf you know about Python [Jupyter Notebooks](http://jupyter.org/), you can take advantage of them during local development.\n\nThe `docker-compose.override.yml` file sends a variable `env` with a value `dev` to the build process of the Docker image (during local development) and the `Dockerfile` has steps to then install and configure Jupyter inside your Docker container.\n\nSo, you can enter into the running Docker container:\n\n```bash\ndocker-compose exec backend bash\n```\n\nAnd use the environment variable `$JUPYTER` to run a Jupyter Notebook with everything configured to listen on the public port (so that you can use it from your browser).\n\nIt will output something like:\n\n```console\nroot@73e0ec1f1ae6:/app# $JUPYTER\n[I 12:02:09.975 NotebookApp] Writing notebook server cookie secret to /root/.local/share/jupyter/runtime/notebook_cookie_secret\n[I 12:02:10.317 NotebookApp] Serving notebooks from local directory: /app\n[I 12:02:10.317 NotebookApp] The Jupyter Notebook is running at:\n[I 12:02:10.317 NotebookApp] http://(73e0ec1f1ae6 or 127.0.0.1):8888/?token=f20939a41524d021fbfc62b31be8ea4dd9232913476f4397\n[I 12:02:10.317 NotebookApp] Use Control-C to stop this server and shut down all kernels (twice to skip confirmation).\n[W 12:02:10.317 NotebookApp] No web browser found: could not locate runnable browser.\n[C 12:02:10.317 NotebookApp]\n\n    Copy/paste this URL into your browser when you connect for the first time,\n    to login with a token:\n        http://(73e0ec1f1ae6 or 127.0.0.1):8888/?token=f20939a41524d021fbfc62b31be8ea4dd9232913476f4397\n```\n\nyou can copy that URL and modify the \"host\" to be `localhost` or the domain you are using for development (e.g. `local.dockertoolbox.tiangolo.com`), in the case above, it would be, e.g.:\n\n```\nhttp://localhost:8888/token=f20939a41524d021fbfc62b31be8ea4dd9232913476f4397\n```\n\nand then open it in your browser.\n\nYou will have a full Jupyter Notebook running inside your container that has direct access to your database by the container name (`db`), etc. So, you can just run sections of your backend code directly, for example with [VS Code Python Jupyter Interactive Window](https://code.visualstudio.com/docs/python/jupyter-support-py) or [Hydrogen](https://github.com/nteract/hydrogen).\n\n### Migrations\n\nAs during local development your app directory is mounted as a volume inside the container, you can also run the migrations with `alembic` commands inside the container and the migration code will be in your app directory (instead of being only inside the container). So you can add it to your git repository.\n\nMake sure you create a \"revision\" of your models and that you \"upgrade\" your database with that revision every time you change them. As this is what will update the tables in your database. Otherwise, your application will have errors.\n\n- Start an interactive session in the backend container:\n\n```console\n$ docker-compose exec backend bash\n```\n\n- If you created a new model in `./backend/app/app/models/`, make sure to import it in `./backend/app/app/db/base.py`, that Python module (`base.py`) that imports all the models will be used by Alembic.\n\n- After changing a model (for example, adding a column), inside the container, create a revision, e.g.:\n\n```console\n$ alembic revision --autogenerate -m \"Add column last_name to User model\"\n```\n\n- Commit to the git repository the files generated in the alembic directory.\n\n- After creating the revision, run the migration in the database (this is what will actually change the database):\n\n```console\n$ alembic upgrade head\n```\n\nIf you don't want to use migrations at all, uncomment the line in the file at `./backend/app/app/db/init_db.py` with:\n\n```python\nBase.metadata.create_all(bind=engine)\n```\n\nand comment the line in the file `prestart.sh` that contains:\n\n```console\n$ alembic upgrade head\n```\n\nIf you don't want to start with the default models and want to remove them / modify them, from the beginning, without having any previous revision, you can remove the revision files (`.py` Python files) under `./backend/app/alembic/versions/`. And then create a first migration as described above.\n\n### Development with Docker Toolbox\n\nIf you are using **Docker Toolbox** in Windows or macOS instead of **Docker for Windows** or **Docker for Mac**, Docker will be running in a VirtualBox Virtual Machine, and it will have a local IP different than `127.0.0.1`, which is the IP address for `localhost` in your machine.\n\nThe address of your Docker Toolbox virtual machine would probably be `192.168.99.100` (that is the default).\n\nAs this is a common case, the domain `local.dockertoolbox.tiangolo.com` points to that (private) IP, just to help with development (actually `dockertoolbox.tiangolo.com` and all its subdomains point to that IP). That way, you can start the stack in Docker Toolbox, and use that domain for development. You will be able to open that URL in Chrome and it will communicate with your local Docker Toolbox directly as if it was a cloud server, including CORS (Cross Origin Resource Sharing).\n\nIf you used the default CORS enabled domains while generating the project, `local.dockertoolbox.tiangolo.com` was configured to be allowed. If you didn't, you will need to add it to the list in the variable `BACKEND_CORS_ORIGINS` in the `.env` file.\n\nTo configure it in your stack, follow the section **Change the development \"domain\"** below, using the domain `local.dockertoolbox.tiangolo.com`.\n\nAfter performing those steps you should be able to open: http://local.dockertoolbox.tiangolo.com and it will be server by your stack in your Docker Toolbox virtual machine.\n\nCheck all the corresponding available URLs in the section at the end.\n\n### Development in `localhost` with a custom domain\n\nYou might want to use something different than `localhost` as the domain. For example, if you are having problems with cookies that need a subdomain, and Chrome is not allowing you to use `localhost`.\n\nIn that case, you have two options: you could use the instructions to modify your system `hosts` file with the instructions below in **Development with a custom IP** or you can just use `localhost.tiangolo.com`, it is set up to point to `localhost` (to the IP `127.0.0.1`) and all its subdomains too. And as it is an actual domain, the browsers will store the cookies you set during development, etc.\n\nIf you used the default CORS enabled domains while generating the project, `localhost.tiangolo.com` was configured to be allowed. If you didn't, you will need to add it to the list in the variable `BACKEND_CORS_ORIGINS` in the `.env` file.\n\nTo configure it in your stack, follow the section **Change the development \"domain\"** below, using the domain `localhost.tiangolo.com`.\n\nAfter performing those steps you should be able to open: http://localhost.tiangolo.com and it will be server by your stack in `localhost`.\n\nCheck all the corresponding available URLs in the section at the end.\n\n### Development with a custom IP\n\nIf you are running Docker in an IP address different than `127.0.0.1` (`localhost`) and `192.168.99.100` (the default of Docker Toolbox), you will need to perform some additional steps. That will be the case if you are running a custom Virtual Machine, a secondary Docker Toolbox or your Docker is located in a different machine in your network.\n\nIn that case, you will need to use a fake local domain (`dev.pythonvue.bws.codes`) and make your computer think that the domain is is served by the custom IP (e.g. `192.168.99.150`).\n\nIf you used the default CORS enabled domains, `dev.pythonvue.bws.codes` was configured to be allowed. If you want a custom one, you need to add it to the list in the variable `BACKEND_CORS_ORIGINS` in the `.env` file.\n\n- Open your `hosts` file with administrative privileges using a text editor:\n\n  - **Note for Windows**: If you are in Windows, open the main Windows menu, search for \"notepad\", right click on it, and select the option \"open as Administrator\" or similar. Then click the \"File\" menu, \"Open file\", go to the directory `c:\\Windows\\System32\\Drivers\\etc\\`, select the option to show \"All files\" instead of only \"Text (.txt) files\", and open the `hosts` file.\n  - **Note for Mac and Linux**: Your `hosts` file is probably located at `/etc/hosts`, you can edit it in a terminal running `sudo nano /etc/hosts`.\n\n- Additional to the contents it might have, add a new line with the custom IP (e.g. `192.168.99.150`) a space character, and your fake local domain: `dev.pythonvue.bws.codes`.\n\nThe new line might look like:\n\n```\n192.168.99.100    dev.pythonvue.bws.codes\n```\n\n- Save the file.\n  - **Note for Windows**: Make sure you save the file as \"All files\", without an extension of `.txt`. By default, Windows tries to add the extension. Make sure the file is saved as is, without extension.\n\n...that will make your computer think that the fake local domain is served by that custom IP, and when you open that URL in your browser, it will talk directly to your locally running server when it is asked to go to `dev.pythonvue.bws.codes` and think that it is a remote server while it is actually running in your computer.\n\nTo configure it in your stack, follow the section **Change the development \"domain\"** below, using the domain `dev.pythonvue.bws.codes`.\n\nAfter performing those steps you should be able to open: http://dev.pythonvue.bws.codes and it will be server by your stack in `localhost`.\n\nCheck all the corresponding available URLs in the section at the end.\n\n### Change the development \"domain\"\n\nIf you need to use your local stack with a different domain than `localhost`, you need to make sure the domain you use points to the IP where your stack is set up. See the different ways to achieve that in the sections above (i.e. using Docker Toolbox with `local.dockertoolbox.tiangolo.com`, using `localhost.tiangolo.com` or using `dev.pythonvue.bws.codes`).\n\nTo simplify your Docker Compose setup, for example, so that the API docs (Swagger UI) knows where is your API, you should let it know you are using that domain for development. You will need to edit 1 line in 2 files.\n\n- Open the file located at `./.env`. It would have a line like:\n\n```\nDOMAIN=localhost\n```\n\n- Change it to the domain you are going to use, e.g.:\n\n```\nDOMAIN=localhost.tiangolo.com\n```\n\nThat variable will be used by the Docker Compose files.\n\n- Now open the file located at `./frontend/.env`. It would have a line like:\n\n```\nVUE_APP_DOMAIN_DEV=localhost\n```\n\n- Change that line to the domain you are going to use, e.g.:\n\n```\nVUE_APP_DOMAIN_DEV=localhost.tiangolo.com\n```\n\nThat variable will make your frontend communicate with that domain when interacting with your backend API, when the other variable `VUE_APP_ENV` is set to `development`.\n\nAfter changing the two lines, you can re-start your stack with:\n\n```bash\ndocker-compose up -d\n```\n\nand check all the corresponding available URLs in the section at the end.\n\n## Frontend development\n\n- Enter the `frontend` directory, install the NPM packages and start the live server using the `npm` scripts:\n\n```bash\ncd frontend\nnpm install\nnpm run serve\n```\n\nThen open your browser at http://localhost:8080\n\nNotice that this live server is not running inside Docker, it is for local development, and that is the recommended workflow. Once you are happy with your frontend, you can build the frontend Docker image and start it, to test it in a production-like environment. But compiling the image at every change will not be as productive as running the local development server with live reload.\n\nCheck the file `package.json` to see other available options.\n\nIf you have Vue CLI installed, you can also run `vue ui` to control, configure, serve, and analyze your application using a nice local web user interface.\n\nIf you are only developing the frontend (e.g. other team members are developing the backend) and there is a staging environment already deployed, you can make your local development code use that staging API instead of a full local Docker Compose stack.\n\nTo do that, modify the file `./frontend/.env`, there's a section with:\n\n```\nVUE_APP_ENV=development\n# VUE_APP_ENV=staging\n```\n\n- Switch the comment, to:\n\n```\n# VUE_APP_ENV=development\nVUE_APP_ENV=staging\n```\n\n### Removing the frontend\n\nIf you are developing an API-only app and want to remove the frontend, you can do it easily:\n\n- Remove the `./frontend` directory.\n- In the `docker-compose.yml` file, remove the whole service / section `frontend`.\n- In the `docker-compose.override.yml` file, remove the whole service / section `frontend`.\n\nDone, you have a frontend-less (api-only) app. 🔥 🚀\n\n---\n\nIf you want, you can also remove the `FRONTEND` environment variables from:\n\n- `.env`\n- `.gitlab-ci.yml`\n- `./scripts/*.sh`\n\nBut it would be only to clean them up, leaving them won't really have any effect either way.\n\n## Deployment\n\nYou can deploy the stack to a Docker Swarm mode cluster with a main Traefik proxy, set up using the ideas from \u003ca href=\"https://dockerswarm.rocks\" target=\"_blank\"\u003eDockerSwarm.rocks\u003c/a\u003e, to get automatic HTTPS certificates, etc.\n\nAnd you can use CI (continuous integration) systems to do it automatically.\n\nBut you have to configure a couple things first.\n\n### Traefik network\n\nThis stack expects the public Traefik network to be named `traefik-public`, just as in the tutorials in \u003ca href=\"https://dockerswarm.rocks\" class=\"external-link\" target=\"_blank\"\u003eDockerSwarm.rocks\u003c/a\u003e.\n\nIf you need to use a different Traefik public network name, update it in the `docker-compose.yml` files, in the section:\n\n```YAML\nnetworks:\n  traefik-public:\n    external: true\n```\n\nChange `traefik-public` to the name of the used Traefik network. And then update it in the file `.env`:\n\n```bash\nTRAEFIK_PUBLIC_NETWORK=traefik-public\n```\n\n### Persisting Docker named volumes\n\nYou need to make sure that each service (Docker container) that uses a volume is always deployed to the same Docker \"node\" in the cluster, that way it will preserve the data. Otherwise, it could be deployed to a different node each time, and each time the volume would be created in that new node before starting the service. As a result, it would look like your service was starting from scratch every time, losing all the previous data.\n\nThat's specially important for a service running a database. But the same problem would apply if you were saving files in your main backend service (for example, if those files were uploaded by your users, or if they were created by your system).\n\nTo solve that, you can put constraints in the services that use one or more data volumes (like databases) to make them be deployed to a Docker node with a specific label. And of course, you need to have that label assigned to one (only one) of your nodes.\n\n#### Adding services with volumes\n\nFor each service that uses a volume (databases, services with uploaded files, etc) you should have a label constraint in your `docker-compose.yml` file.\n\nTo make sure that your labels are unique per volume per stack (for example, that they are not the same for `prod` and `stag`) you should prefix them with the name of your stack and then use the same name of the volume.\n\nThen you need to have those constraints in your `docker-compose.yml` file for the services that need to be fixed with each volume.\n\nTo be able to use different environments, like `prod` and `stag`, you should pass the name of the stack as an environment variable. Like:\n\n```bash\nSTACK_NAME=staging-pythonvue-bws-codes sh ./scripts/deploy.sh\n```\n\nTo use and expand that environment variable inside the `docker-compose.yml` files you can add the constraints to the services like:\n\n```yaml\nversion: \"3\"\nservices:\n  db:\n    volumes:\n      - \"app-db-data:/var/lib/postgresql/data/pgdata\"\n    deploy:\n      placement:\n        constraints:\n          - node.labels.${STACK_NAME?Variable not set}.app-db-data == true\n```\n\nnote the `${STACK_NAME?Variable not set}`. In the script `./scripts/deploy.sh`, the `docker-compose.yml` would be converted, and saved to a file `docker-stack.yml` containing:\n\n```yaml\nversion: \"3\"\nservices:\n  db:\n    volumes:\n      - \"app-db-data:/var/lib/postgresql/data/pgdata\"\n    deploy:\n      placement:\n        constraints:\n          - node.labels.pythonvue-bws-codes.app-db-data == true\n```\n\n**Note**: The `${STACK_NAME?Variable not set}` means \"use the environment variable `STACK_NAME`, but if it is not set, show an error `Variable not set`\".\n\nIf you add more volumes to your stack, you need to make sure you add the corresponding constraints to the services that use that named volume.\n\nThen you have to create those labels in some nodes in your Docker Swarm mode cluster. You can use `docker-auto-labels` to do it automatically.\n\n#### `docker-auto-labels`\n\nYou can use [`docker-auto-labels`](https://github.com/tiangolo/docker-auto-labels) to automatically read the placement constraint labels in your Docker stack (Docker Compose file) and assign them to a random Docker node in your Swarm mode cluster if those labels don't exist yet.\n\nTo do that, you can install `docker-auto-labels`:\n\n```bash\npip install docker-auto-labels\n```\n\nAnd then run it passing your `docker-stack.yml` file as a parameter:\n\n```bash\ndocker-auto-labels docker-stack.yml\n```\n\nYou can run that command every time you deploy, right before deploying, as it doesn't modify anything if the required labels already exist.\n\n#### (Optionally) adding labels manually\n\nIf you don't want to use `docker-auto-labels` or for any reason you want to manually assign the constraint labels to specific nodes in your Docker Swarm mode cluster, you can do the following:\n\n- First, connect via SSH to your Docker Swarm mode cluster.\n\n- Then check the available nodes with:\n\n```console\n$ docker node ls\n\n\n// you would see an output like:\n\nID                            HOSTNAME               STATUS              AVAILABILITY        MANAGER STATUS\nnfa3d4df2df34as2fd34230rm *   dog.example.com        Ready               Active              Reachable\n2c2sd2342asdfasd42342304e     cat.example.com        Ready               Active              Leader\nc4sdf2342asdfasd4234234ii     snake.example.com      Ready               Active              Reachable\n```\n\nthen chose a node from the list. For example, `dog.example.com`.\n\n- Add the label to that node. Use as label the name of the stack you are deploying followed by a dot (`.`) followed by the named volume, and as value, just `true`, e.g.:\n\n```bash\ndocker node update --label-add pythonvue-bws-codes.app-db-data=true dog.example.com\n```\n\n- Then you need to do the same for each stack version you have. For example, for staging you could do:\n\n```bash\ndocker node update --label-add staging-pythonvue-bws-codes.app-db-data=true cat.example.com\n```\n\n### Deploy to a Docker Swarm mode cluster\n\nThere are 3 steps:\n\n1. **Build** your app images\n2. Optionally, **push** your custom images to a Docker Registry\n3. **Deploy** your stack\n\n---\n\nHere are the steps in detail:\n\n1. **Build your app images**\n\n- Set these environment variables, right before the next command:\n  - `TAG=prod`\n  - `FRONTEND_ENV=production`\n- Use the provided `scripts/build.sh` file with those environment variables:\n\n```bash\nTAG=prod FRONTEND_ENV=production bash ./scripts/build.sh\n```\n\n2. **Optionally, push your images to a Docker Registry**\n\n**Note**: if the deployment Docker Swarm mode \"cluster\" has more than one server, you will have to push the images to a registry or build the images in each server, so that when each of the servers in your cluster tries to start the containers it can get the Docker images for them, pulling them from a Docker Registry or because it has them already built locally.\n\nIf you are using a registry and pushing your images, you can omit running the previous script and instead using this one, in a single shot.\n\n- Set these environment variables:\n  - `TAG=prod`\n  - `FRONTEND_ENV=production`\n- Use the provided `scripts/build-push.sh` file with those environment variables:\n\n```bash\nTAG=prod FRONTEND_ENV=production bash ./scripts/build-push.sh\n```\n\n3. **Deploy your stack**\n\n- Set these environment variables:\n  - `DOMAIN=pythonvue.bws.codes`\n  - `TRAEFIK_TAG=pythonvue.bws.codes`\n  - `STACK_NAME=pythonvue-bws-codes`\n  - `TAG=prod`\n- Use the provided `scripts/deploy.sh` file with those environment variables:\n\n```bash\nDOMAIN=pythonvue.bws.codes \\\nTRAEFIK_TAG=pythonvue.bws.codes \\\nSTACK_NAME=pythonvue-bws-codes \\\nTAG=prod \\\nbash ./scripts/deploy.sh\n```\n\n---\n\nIf you change your mind and, for example, want to deploy everything to a different domain, you only have to change the `DOMAIN` environment variable in the previous commands. If you wanted to add a different version / environment of your stack, like \"`preproduction`\", you would only have to set `TAG=preproduction` in your command and update these other environment variables accordingly. And it would all work, that way you could have different environments and deployments of the same app in the same cluster.\n\n#### Deployment Technical Details\n\nBuilding and pushing is done with the `docker-compose.yml` file, using the `docker-compose` command. The file `docker-compose.yml` uses the file `.env` with default environment variables. And the scripts set some additional environment variables as well.\n\nThe deployment requires using `docker stack` instead of `docker-swarm`, and it can't read environment variables or `.env` files. Because of that, the `deploy.sh` script generates a file `docker-stack.yml` with the configurations from `docker-compose.yml` and injecting the environment variables in it. And then uses it to deploy the stack.\n\nYou can do the process by hand based on those same scripts if you wanted. The general structure is like this:\n\n```bash\n# Use the environment variables passed to this script, as TAG and FRONTEND_ENV\n# And re-create those variables as environment variables for the next command\nTAG=${TAG?Variable not set} \\\n# Set the environment variable FRONTEND_ENV to the same value passed to this script with\n# a default value of \"production\" if nothing else was passed\nFRONTEND_ENV=${FRONTEND_ENV-production?Variable not set} \\\n# The actual comand that does the work: docker-compose\ndocker-compose \\\n# Pass the file that should be used, setting explicitly docker-compose.yml avoids the\n# default of also using docker-compose.override.yml\n-f docker-compose.yml \\\n# Use the docker-compose sub command named \"config\", it just uses the docker-compose.yml\n# file passed to it and prints their combined contents\n# Put those contents in a file \"docker-stack.yml\", with \"\u003e\"\nconfig \u003e docker-stack.yml\n\n# The previous only generated a docker-stack.yml file,\n# but didn't do anything with it yet\n\n# docker-auto-labels makes sure the labels used for constraints exist in the cluster\ndocker-auto-labels docker-stack.yml\n\n# Now this command uses that same file to deploy it\ndocker stack deploy -c docker-stack.yml --with-registry-auth \"${STACK_NAME?Variable not set}\"\n```\n\n### Continuous Integration / Continuous Delivery\n\nIf you use GitLab CI, the included `.gitlab-ci.yml` can automatically deploy it. You may need to update it according to your GitLab configurations.\n\nIf you use any other CI / CD provider, you can base your deployment from that `.gitlab-ci.yml` file, as all the actual script steps are performed in `bash` scripts that you can easily re-use.\n\nGitLab CI is configured assuming 2 environments following GitLab flow:\n\n- `prod` (production) from the `production` branch.\n- `stag` (staging) from the `master` branch.\n\nIf you need to add more environments, for example, you could imagine using a client-approved `preprod` branch, you can just copy the configurations in `.gitlab-ci.yml` for `stag` and rename the corresponding variables. The Docker Compose file and environment variables are configured to support as many environments as you need, so that you only need to modify `.gitlab-ci.yml` (or whichever CI system configuration you are using).\n\n## Docker Compose files and env vars\n\nThere is a main `docker-compose.yml` file with all the configurations that apply to the whole stack, it is used automatically by `docker-compose`.\n\nAnd there's also a `docker-compose.override.yml` with overrides for development, for example to mount the source code as a volume. It is used automatically by `docker-compose` to apply overrides on top of `docker-compose.yml`.\n\nThese Docker Compose files use the `.env` file containing configurations to be injected as environment variables in the containers.\n\nThey also use some additional configurations taken from environment variables set in the scripts before calling the `docker-compose` command.\n\nIt is all designed to support several \"stages\", like development, building, testing, and deployment. Also, allowing the deployment to different environments like staging and production (and you can add more environments very easily).\n\nThey are designed to have the minimum repetition of code and configurations, so that if you need to change something, you have to change it in the minimum amount of places. That's why files use environment variables that get auto-expanded. That way, if for example, you want to use a different domain, you can call the `docker-compose` command with a different `DOMAIN` environment variable instead of having to change the domain in several places inside the Docker Compose files.\n\nAlso, if you want to have another deployment environment, say `preprod`, you just have to change environment variables, but you can keep using the same Docker Compose files.\n\n### The .env file\n\nThe `.env` file is the one that contains all your configurations, generated keys and passwords, etc.\n\nDepending on your workflow, you could want to exclude it from Git, for example if your project is public. In that case, you would have to make sure to set up a way for your CI tools to obtain it while building or deploying your project.\n\nOne way to do it could be to add each environment variable to your CI/CD system, and updating the `docker-compose.yml` file to read that specific env var instead of reading the `.env` file.\n\n## URLs\n\nThese are the URLs that will be used and generated by the project.\n\n### Production URLs\n\nProduction URLs, from the branch `production`.\n\nFrontend: https://pythonvue.bws.codes\n\nBackend: https://pythonvue.bws.codes/api/\n\nAutomatic Interactive Docs (Swagger UI): https://pythonvue.bws.codes/docs\n\nAutomatic Alternative Docs (ReDoc): https://pythonvue.bws.codes/redoc\n\nPGAdmin: https://pgadmin.pythonvue.bws.codes\n\nFlower: https://flower.pythonvue.bws.codes\n\n### Staging URLs\n\nStaging URLs, from the branch `master`.\n\nFrontend: https://staging.pythonvue.bws.codes\n\nBackend: https://staging.pythonvue.bws.codes/api/\n\nAutomatic Interactive Docs (Swagger UI): https://staging.pythonvue.bws.codes/docs\n\nAutomatic Alternative Docs (ReDoc): https://staging.pythonvue.bws.codes/redoc\n\nPGAdmin: https://pgadmin.staging.pythonvue.bws.codes\n\nFlower: https://flower.staging.pythonvue.bws.codes\n\n### Development URLs\n\nDevelopment URLs, for local development.\n\nFrontend: http://localhost\n\nBackend: http://localhost/api/\n\nAutomatic Interactive Docs (Swagger UI): https://localhost/docs\n\nAutomatic Alternative Docs (ReDoc): https://localhost/redoc\n\nPGAdmin: http://localhost:5050\n\nFlower: http://localhost:5555\n\nTraefik UI: http://localhost:8090\n\n### Development with Docker Toolbox URLs\n\nDevelopment URLs, for local development.\n\nFrontend: http://local.dockertoolbox.tiangolo.com\n\nBackend: http://local.dockertoolbox.tiangolo.com/api/\n\nAutomatic Interactive Docs (Swagger UI): https://local.dockertoolbox.tiangolo.com/docs\n\nAutomatic Alternative Docs (ReDoc): https://local.dockertoolbox.tiangolo.com/redoc\n\nPGAdmin: http://local.dockertoolbox.tiangolo.com:5050\n\nFlower: http://local.dockertoolbox.tiangolo.com:5555\n\nTraefik UI: http://local.dockertoolbox.tiangolo.com:8090\n\n### Development with a custom IP URLs\n\nDevelopment URLs, for local development.\n\nFrontend: http://dev.pythonvue.bws.codes\n\nBackend: http://dev.pythonvue.bws.codes/api/\n\nAutomatic Interactive Docs (Swagger UI): https://dev.pythonvue.bws.codes/docs\n\nAutomatic Alternative Docs (ReDoc): https://dev.pythonvue.bws.codes/redoc\n\nPGAdmin: http://dev.pythonvue.bws.codes:5050\n\nFlower: http://dev.pythonvue.bws.codes:5555\n\nTraefik UI: http://dev.pythonvue.bws.codes:8090\n\n### Development in localhost with a custom domain URLs\n\nDevelopment URLs, for local development.\n\nFrontend: http://localhost.tiangolo.com\n\nBackend: http://localhost.tiangolo.com/api/\n\nAutomatic Interactive Docs (Swagger UI): https://localhost.tiangolo.com/docs\n\nAutomatic Alternative Docs (ReDoc): https://localhost.tiangolo.com/redoc\n\nPGAdmin: http://localhost.tiangolo.com:5050\n\nFlower: http://localhost.tiangolo.com:5555\n\nTraefik UI: http://localhost.tiangolo.com:8090\n\n## Project generation and updating, or re-generating\n\nThis project was generated using https://github.com/tiangolo/full-stack-fastapi-postgresql with:\n\n```bash\npip install cookiecutter\ncookiecutter https://github.com/tiangolo/full-stack-fastapi-postgresql\n```\n\nYou can check the variables used during generation in the file `cookiecutter-config-file.yml`.\n\nYou can generate the project again with the same configurations used the first time.\n\nThat would be useful if, for example, the project generator (`tiangolo/full-stack-fastapi-postgresql`) was updated and you wanted to integrate or review the changes.\n\nYou could generate a new project with the same configurations as this one in a parallel directory. And compare the differences between the two, without having to overwrite your current code but being able to use the same variables used for your current project.\n\nTo achieve that, the generated project includes the file `cookiecutter-config-file.yml` with the current variables used.\n\nYou can use that file while generating a new project to reuse all those variables.\n\nFor example, run:\n\n```console\n$ cookiecutter --config-file ./cookiecutter-config-file.yml --output-dir ../project-copy https://github.com/tiangolo/full-stack-fastapi-postgresql\n```\n\nThat will use the file `cookiecutter-config-file.yml` in the current directory (in this project) to generate a new project inside a sibling directory `project-copy`.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbr3ndonland%2Fpythonvue","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbr3ndonland%2Fpythonvue","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbr3ndonland%2Fpythonvue/lists"}