You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
If you run your API using Docker Compose, with the `docker compose up` command, you may also want to be able to execute arbitrary shell commands in the container.
4
+
5
+
For example, later on in the course we will look at database migrations.
6
+
7
+
To execute a database migration, we need to run a specific command, `flask db mgirate`.
8
+
9
+
If we use Docker Compose, we'll need to run the command inside the running container, and not in a local terminal.
10
+
11
+
You can run any arbitrary command in a running container like so:
12
+
13
+
```bash
14
+
docker compose exec web flask db migrate
15
+
```
16
+
17
+
This command is split into 4 parts:
18
+
19
+
-`docker compose`: uses the Docker Compose part of the Docker executable
20
+
-`exec`: used to run a command in a specific Docker Compose service
21
+
-`web`: which Docker Compose service to run the command in
22
+
-`flask db migrate`: the command you want to run
23
+
24
+
That's all! Just remember while following the course, that if I run any commands in my local terminal and you are using Docker Compose, you should precede the commands with `docker compose exec web`.
When Docker Compose runs, it creates a virtual network[^1] which allows you to connect to `db`, which connects to the running `db` service container.
49
54
:::
50
55
56
+
In the `docker-compose.yml` file above you can also see that the `web` service depends on the `db` service, with the condition that it is healthy. A service is deemed "healthy" when its healthcheck passes.
57
+
58
+
We've added a healthcheck to the `db` service which runs the `pg_isready`[^2] program using the supplied database and PostgreSQL user. This just tells us whether the PostgreSQL server is ready to respond to requests.
59
+
60
+
Adding this means the `web` service won't start until the `db` service is ready to respond to requests.
61
+
51
62
## Named volumes in Docker Compose
52
63
53
64
You'll notice that our `docker-compose.yml` file has these lines:
@@ -113,4 +124,5 @@ Note you must be in the folder that contains your `docker-compose.yml` file in o
113
124
Running `docker compose down` will **not** delete your named volumes. You need to use the `-v` flag for that. Deleting the named volumes deletes the data in them irreversibly.
114
125
:::
115
126
116
-
[^1]: [Networking in Compose (official docs)](https://docs.docker.com/compose/networking/)
127
+
[^1]: [Networking in Compose (official docs)](https://docs.docker.com/compose/networking/)
0 commit comments