Skip to content

Commit 4cd67f3

Browse files
authored
♻️ Use Docker Compose watch (#1354)
1 parent b262c20 commit 4cd67f3

File tree

3 files changed

+26
-15
lines changed

3 files changed

+26
-15
lines changed

backend/README.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,23 +41,23 @@ During development, you can change Docker Compose settings that will only affect
4141

4242
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.
4343

44-
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.
44+
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.
4545

46-
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:
46+
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:
4747

4848
```console
49-
$ docker compose up -d
49+
$ docker compose watch
5050
```
5151

5252
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.
5353

5454
To get inside the container with a `bash` session you can start the stack with:
5555

5656
```console
57-
$ docker compose up -d
57+
$ docker compose watch
5858
```
5959

60-
and then `exec` inside the running container:
60+
and then in another terminal, `exec` inside the running container:
6161

6262
```console
6363
$ docker compose exec backend bash
@@ -71,16 +71,16 @@ root@7f2607af31c3:/app#
7171

7272
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`.
7373

74-
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:
74+
There you can use the `fastapi run --reload` command to run the debug live reloading server.
7575

7676
```console
77-
$ bash /start-reload.sh
77+
$ fastapi run --reload app/main.py
7878
```
7979

8080
...it will look like:
8181

8282
```console
83-
root@7f2607af31c3:/app# bash /start-reload.sh
83+
root@7f2607af31c3:/app# fastapi run --reload app/main.py
8484
```
8585

8686
and then hit enter. That runs the live reloading server that auto reloads when it detects code changes.

development.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* Start the local stack with Docker Compose:
66

77
```bash
8-
docker compose up -d
8+
docker compose watch
99
```
1010

1111
* 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
2222

2323
**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.
2424

25-
To check the logs, run:
25+
To check the logs, run (in another terminal):
2626

2727
```bash
2828
docker compose logs
@@ -42,7 +42,7 @@ For the backend and frontend, they use the same port that would be used by their
4242

4343
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.
4444

45-
For example, you can stop that `frontend` service in the Docker Compose:
45+
For example, you can stop that `frontend` service in the Docker Compose, in another terminal, run:
4646

4747
```bash
4848
docker compose stop frontend
@@ -91,7 +91,7 @@ The domain `localhost.tiangolo.com` is a special domain that is configured (with
9191
After you update it, run again:
9292

9393
```bash
94-
docker compose up -d
94+
docker compose watch
9595
```
9696

9797
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
109109
After changing variables, make sure you restart the stack:
110110

111111
```bash
112-
docker compose up -d
112+
docker compose watch
113113
```
114114

115115
## The .env file

docker-compose.override.yml

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,6 @@ services:
6060
ports:
6161
- "8888:8888"
6262
- "8000:8000"
63-
volumes:
64-
- ./backend/:/app
6563
build:
6664
context: ./backend
6765
args:
@@ -72,6 +70,19 @@ services:
7270
- run
7371
- --reload
7472
- "app/main.py"
73+
develop:
74+
watch:
75+
- path: ./backend
76+
action: sync
77+
target: /app
78+
ignore:
79+
- ./backend/.venv
80+
- .venv
81+
- path: ./backend/pyproject.toml
82+
action: rebuild
83+
# TODO: remove once coverage is done locally
84+
volumes:
85+
- ./backend/htmlcov:/app/htmlcov
7586
environment:
7687
SMTP_HOST: "mailcatcher"
7788
SMTP_PORT: "1025"

0 commit comments

Comments
 (0)