- [Instructor] Many common application architectures
have service dependencies,
meaning one service cannot operate without another.
In the case of Kinetico's Storefront application,
it may require that the database exists
for the application to work as intended.
To run the application container,
we need to make sure that the database container
is running first.
This can be done manually with regular Docker commands,
but Compose provides utilities to enforce
startup order automatically using the depends on flag.
Under a service definition in the Docker composed YAML file,
Ada depends on object.
Nested underneath it are service names in list format.
In this example, that would be database.
Now that this dependency relationship has been established,
compose will start and stop services in dependency order.
Docker-compose up will start the database
before it starts storefront.
And Docker-compose down will stop storefront
before it stops database.
If database had its own dependencies,
those would be started first and stopped last.
Services can have any number of dependencies
and many services can share a single dependency.
Starting a specific service by name
like Docker-compose up storefront will also start
all of its dependencies automatically.
While there is support for enforcing the startup order
for containers, modern versions of compose
explicitly do not guarantee that dependent containers
are running or are healthy.
It only guarantees that they've been started.
That's because in a distributed system,
it is not possible to know that a dependent service,
database or application is always up and running.
In fact, we can be confident that no one's service
will ever have perfect 100% uptime.
All Docker containers should have resiliency
when a dependency becomes unavailable.
In the rare and specific use case
that a dependent service absolutely must be running
before a container can start,
there are third party tools to solve for this.
They'll wrap your container's initialization command
so that the container will not start
if a dependency is unhealthy.
However, this is not supported natively by composed.
It is not recommended to rely on this level
of tight coupling in a distributed system.