From 4cd67f3483545023fae71db6a69f20fdd194d352 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebasti=C3=A1n=20Ram=C3=ADrez?= Date: Mon, 23 Sep 2024 00:29:23 +0200 Subject: [PATCH] =?UTF-8?q?=E2=99=BB=EF=B8=8F=20Use=20Docker=20Compose=20`?= =?UTF-8?q?watch`=20(#1354)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/README.md | 16 ++++++++-------- development.md | 10 +++++----- docker-compose.override.yml | 15 +++++++++++++-- 3 files changed, 26 insertions(+), 15 deletions(-) diff --git a/backend/README.md b/backend/README.md index a8f18e9e5a..e72ec444e7 100644 --- a/backend/README.md +++ b/backend/README.md @@ -41,12 +41,12 @@ During development, you can change Docker Compose settings that will only affect The 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. -For 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. +For example, the directory with the backend code is synchronized in the Docker container, copying 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. -There 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: +There is also a command override that runs `fastapi run --reload` instead of the default `fastapi run`. 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: ```console -$ docker compose up -d +$ docker compose watch ``` There 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. @@ -54,10 +54,10 @@ There is also a commented out `command` override, you can uncomment it and comme To get inside the container with a `bash` session you can start the stack with: ```console -$ docker compose up -d +$ docker compose watch ``` -and then `exec` inside the running container: +and then in another terminal, `exec` inside the running container: ```console $ docker compose exec backend bash @@ -71,16 +71,16 @@ root@7f2607af31c3:/app# that means that you are in a `bash` session inside your container, as a `root` user, under the `/app` directory, this directory has another directory called "app" inside, that's where your code lives inside the container: `/app/app`. -There 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: +There you can use the `fastapi run --reload` command to run the debug live reloading server. ```console -$ bash /start-reload.sh +$ fastapi run --reload app/main.py ``` ...it will look like: ```console -root@7f2607af31c3:/app# bash /start-reload.sh +root@7f2607af31c3:/app# fastapi run --reload app/main.py ``` and then hit enter. That runs the live reloading server that auto reloads when it detects code changes. diff --git a/development.md b/development.md index 38d17eb2cc..ad1cd75cbc 100644 --- a/development.md +++ b/development.md @@ -5,7 +5,7 @@ * Start the local stack with Docker Compose: ```bash -docker compose up -d +docker compose watch ``` * Now you can open your browser and interact with these URLs: @@ -22,7 +22,7 @@ Traefik UI, to see how the routes are being handled by the proxy: http://localho **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. -To check the logs, run: +To check the logs, run (in another terminal): ```bash docker compose logs @@ -42,7 +42,7 @@ For the backend and frontend, they use the same port that would be used by their This way, you could turn off a Docker Compose service and start its local development service, and everything would keep working, because it all uses the same ports. -For example, you can stop that `frontend` service in the Docker Compose: +For example, you can stop that `frontend` service in the Docker Compose, in another terminal, run: ```bash docker compose stop frontend @@ -91,7 +91,7 @@ The domain `localhost.tiangolo.com` is a special domain that is configured (with After you update it, run again: ```bash -docker compose up -d +docker compose watch ``` When deploying, for example in production, the main Traefik is configured outside of the Docker Compose files. For local development, there's an included Traefik in `docker-compose.override.yml`, just to let you test that the domains work as expected, for example with `api.localhost.tiangolo.com` and `dashboard.localhost.tiangolo.com`. @@ -109,7 +109,7 @@ They also use some additional configurations taken from environment variables se After changing variables, make sure you restart the stack: ```bash -docker compose up -d +docker compose watch ``` ## The .env file diff --git a/docker-compose.override.yml b/docker-compose.override.yml index 9257ab8aaa..fd99cb538c 100644 --- a/docker-compose.override.yml +++ b/docker-compose.override.yml @@ -60,8 +60,6 @@ services: ports: - "8888:8888" - "8000:8000" - volumes: - - ./backend/:/app build: context: ./backend args: @@ -72,6 +70,19 @@ services: - run - --reload - "app/main.py" + develop: + watch: + - path: ./backend + action: sync + target: /app + ignore: + - ./backend/.venv + - .venv + - path: ./backend/pyproject.toml + action: rebuild + # TODO: remove once coverage is done locally + volumes: + - ./backend/htmlcov:/app/htmlcov environment: SMTP_HOST: "mailcatcher" SMTP_PORT: "1025"