Skip to content

Commit ec5d3b4

Browse files
authored
Merge pull request #160 from tecladocode/jose/cou-537-cover-running-commands-in-running-container-using-docker
feat(compose): add new lecture on running commands in a container
2 parents ed92dab + 685e8bf commit ec5d3b4

File tree

2 files changed

+39
-3
lines changed
  • docs/docs
    • 04_docker_intro/05_run_commands_in_docker_containers
    • 11_deploy_to_render/06_run_everything_docker_compose

2 files changed

+39
-3
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# How to run commands inside a Docker container
2+
3+
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`.

docs/docs/11_deploy_to_render/06_run_everything_docker_compose/README.md

+15-3
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ services:
1212
ports:
1313
- "5000:80"
1414
depends_on:
15-
- db
15+
db:
16+
condition: service_healthy
1617
env_file:
1718
- ./.env
1819
volumes:
@@ -24,14 +25,18 @@ services:
2425
- POSTGRES_DB=myapp
2526
volumes:
2627
- postgres_data:/var/lib/postgresql/data
28+
healthcheck:
29+
test: pg_isready -d $${POSTGRES_DB} -U postgres
30+
interval: 2s
31+
retries: 10
2732
volumes:
2833
postgres_data:
2934
```
3035
3136
The `postgres` image accepts various environment variables, among them:
3237

3338
- `POSTGRES_PASSWORD`, defaulting to `postgres`
34-
- `POSTGERS_DB`, defaulting to `postgres`
39+
- `POSTGRES_DB`, defaulting to `postgres`
3540
- `POSTGRES_USER`, defaulting to `postgres`
3641
- `POSTGRES_HOST`, defaulting to `localhost`
3742
- `POSTGRES_PORT`, defaulting to `5432`
@@ -48,6 +53,12 @@ DATABASE_URL=postgresql://postgres:password@db/myapp
4853
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.
4954
:::
5055

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+
5162
## Named volumes in Docker Compose
5263

5364
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
113124
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.
114125
:::
115126

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/)
128+
[^2]: [pg_isready (PostgreSQL documentation)](https://www.postgresql.org/docs/current/app-pg-isready.html)

0 commit comments

Comments
 (0)