Skip to content

Commit

Permalink
♻️ Use Docker Compose watch (#1354)
Browse files Browse the repository at this point in the history
  • Loading branch information
tiangolo authored Sep 22, 2024
1 parent b262c20 commit 4cd67f3
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 15 deletions.
16 changes: 8 additions & 8 deletions backend/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,23 +41,23 @@ 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.

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
Expand All @@ -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.
Expand Down
10 changes: 5 additions & 5 deletions development.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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`.
Expand All @@ -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
Expand Down
15 changes: 13 additions & 2 deletions docker-compose.override.yml
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,6 @@ services:
ports:
- "8888:8888"
- "8000:8000"
volumes:
- ./backend/:/app
build:
context: ./backend
args:
Expand All @@ -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"
Expand Down

0 comments on commit 4cd67f3

Please sign in to comment.