diff --git a/docs/docs/04_docker_intro/05_run_commands_in_docker_containers/README.md b/docs/docs/04_docker_intro/05_run_commands_in_docker_containers/README.md new file mode 100644 index 00000000..0dec9a9c --- /dev/null +++ b/docs/docs/04_docker_intro/05_run_commands_in_docker_containers/README.md @@ -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`. diff --git a/docs/docs/11_deploy_to_render/06_run_everything_docker_compose/README.md b/docs/docs/11_deploy_to_render/06_run_everything_docker_compose/README.md index 5a3290c5..76186184 100644 --- a/docs/docs/11_deploy_to_render/06_run_everything_docker_compose/README.md +++ b/docs/docs/11_deploy_to_render/06_run_everything_docker_compose/README.md @@ -12,7 +12,8 @@ services: ports: - "5000:80" depends_on: - - db + db: + condition: service_healthy env_file: - ./.env volumes: @@ -24,6 +25,10 @@ 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: ``` @@ -31,7 +36,7 @@ volumes: 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` @@ -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: @@ -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/) \ No newline at end of file +[^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) \ No newline at end of file