Dependencies and troubleshooting in Docker Compose

Intermediate Docker

Mike Metzger

Data Engineering Consultant

What are dependencies?

  • Dependencies define the order of resource startup
  • Resources (containers) may require other resources
  • Example web application
    • Database container postgresql must start first

Container Dependency Example - postgres

Intermediate Docker

What are dependencies?

  • Dependencies define the order of resources
  • Resources (containers) may require other resources
  • Example web application
    • Database container postgresql must start first
    • Then the python_app

Container Dependency Example - python_app

Intermediate Docker

What are dependencies?

  • Dependencies define the order of resources
  • Resources (containers) may require other resources
  • Example web application
    • Database container postgresql must start first
    • Then the python_app
    • Finally, the nginx web server

Container Dependency Example - Full

Intermediate Docker

depends_on

  • Dependencies defined using the depends_on attribute
  • Can chain dependencies as per example
  • Or, can have multiple dependencies per resource if required
  • Order of the compose.yaml file does not matter
services:
  postgresql:
    image: postgresql:latest

python_app: image: custom_app depends_on: - postgresql
nginx: image: nginx/latest depends_on: - python_app
Intermediate Docker

Shutting down applications

  • Shutting down an application occurs in reverse order
  • Stops nginx resource

Container Dependency Example - Full

Intermediate Docker

Shutting down applications

  • Shutting down an application occurs in reverse order
  • Stops nginx resource
  • Then stops the python_app resource

Container Dependency Example - python_app

Intermediate Docker

Shutting down applications

  • Shutting down an application occurs in reverse order
  • Stops nginx resource
  • Then stops the python_app resource
  • And finally the postgresql resource

Container Dependency Example - postgres

Intermediate Docker

Other options

  • Docker Compose provides other options for dependencies
  • condition: defines how to decide when resource is ready.
    • service_started - Resource has started normally
      • Default behavior
    • service_completed_successfully - Resource ran to completion, such as a initial configuration / etc
    • service_healthy - Resource meets a criteria defined by healthcheck
services:
  nginx:
    image: nginx/latest
    depends_on:
      python_app:
        condition: service_started

python_app: image: custom_app depends_on: postgresql: condition: service_healthy
Intermediate Docker

Docker Compose troubleshooting tools

  • Docker Compose has additional troubleshooting tools
  • docker compose logs - Gathers output from all resources in application
    redis-1  | * oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
    redis-1  | * Running mode=standalone, port=6379.
    redis-1  | * Server initialized
    redis-1  | * Ready to accept connections tcp
    web-1    | * Serving Flask app 'app.py'
    web-1    |  * Running on all addresses (0.0.0.0)
    web-1    |  * Running on http://172.20.0.2:5000
    web-1    | Press CTRL+C to quit
    
  • docker compose logs <resourcename>
Intermediate Docker

docker compose top

  • docker compose top shows status of resources within an application
composetest-redis-1
UID   PID    PPID   C    STIME   TTY   TIME       CMD
999   2767   2726   0    01:16   ?     00:03:27   redis-server *:6379

composetest-web-1
UID    PID    PPID   C    STIME   TTY   TIME       CMD
root   2768   2740   0    01:16   ?     00:00:23   /usr/local/bin/python /usr/local/bin/flask run
Intermediate Docker

Let's practice!

Intermediate Docker

Preparing Video For Download...