Skip to content

Commit

Permalink
Merge pull request #160 from tecladocode/jose/cou-537-cover-running-c…
Browse files Browse the repository at this point in the history
…ommands-in-running-container-using-docker

feat(compose): add new lecture on running commands in a container
  • Loading branch information
jslvtr authored Jun 21, 2024
2 parents ed92dab + 685e8bf commit ec5d3b4
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# How to run commands inside a Docker container

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.

For example, later on in the course we will look at database migrations.

To execute a database migration, we need to run a specific command, `flask db mgirate`.

If we use Docker Compose, we'll need to run the command inside the running container, and not in a local terminal.

You can run any arbitrary command in a running container like so:

```bash
docker compose exec web flask db migrate
```

This command is split into 4 parts:

- `docker compose`: uses the Docker Compose part of the Docker executable
- `exec`: used to run a command in a specific Docker Compose service
- `web`: which Docker Compose service to run the command in
- `flask db migrate`: the command you want to run

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`.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ services:
ports:
- "5000:80"
depends_on:
- db
db:
condition: service_healthy
env_file:
- ./.env
volumes:
Expand All @@ -24,14 +25,18 @@ services:
- POSTGRES_DB=myapp
volumes:
- postgres_data:/var/lib/postgresql/data
healthcheck:
test: pg_isready -d $${POSTGRES_DB} -U postgres
interval: 2s
retries: 10
volumes:
postgres_data:
```
The `postgres` image accepts various environment variables, among them:

- `POSTGRES_PASSWORD`, defaulting to `postgres`
- `POSTGERS_DB`, defaulting to `postgres`
- `POSTGRES_DB`, defaulting to `postgres`
- `POSTGRES_USER`, defaulting to `postgres`
- `POSTGRES_HOST`, defaulting to `localhost`
- `POSTGRES_PORT`, defaulting to `5432`
Expand All @@ -48,6 +53,12 @@ DATABASE_URL=postgresql://postgres:password@db/myapp
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.
:::

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.

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.

Adding this means the `web` service won't start until the `db` service is ready to respond to requests.

## Named volumes in Docker Compose

You'll notice that our `docker-compose.yml` file has these lines:
Expand Down Expand Up @@ -113,4 +124,5 @@ Note you must be in the folder that contains your `docker-compose.yml` file in o
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.
:::

[^1]: [Networking in Compose (official docs)](https://docs.docker.com/compose/networking/)
[^1]: [Networking in Compose (official docs)](https://docs.docker.com/compose/networking/)
[^2]: [pg_isready (PostgreSQL documentation)](https://www.postgresql.org/docs/current/app-pg-isready.html)

0 comments on commit ec5d3b4

Please sign in to comment.