Linked Containers¶
Overview¶
Watchtower, by default, ensures that interdependent containers are updated in the correct order to maintain application stability. It automatically detects container dependencies through various mechanisms and uses topological sorting to determine the optimal update sequence.
When containers depend-upon each other (such as a web application depending on a database), updating them in the wrong order can cause service disruptions.
Watchtower addresses this by:
- Detecting dependencies through Docker links, labels, and network configurations
- Performing topological sorting to determine the correct update order
- Stopping containers in reverse dependency order (dependents first - i.e., web app before database)
- Restarting containers in dependency order (dependencies first - i.e., database before web app)
This ensures that dependent services are stopped before their dependencies are updated, and dependencies are available when dependents restart.
How It Works¶
Dependency Detection¶
Watchtower detects container dependencies through multiple mechanisms, checked in the following priority order:
- Watchtower depends-on label (
com.centurylinklabs.watchtower.depends-on) - Docker Compose depends_on label (
com.docker.compose.depends_on) - Docker links and network mode (legacy Docker linking and
network_mode: service:container)
Watchtower Depends-On Label¶
The com.centurylinklabs.watchtower.depends-on label allows explicit declaration of dependencies:
This label accepts a comma-separated list of container names that must be available before this container starts. This supports referencing containers from other Docker Compose projects or stacks, enabling cross-project dependency management.
Warning
Use unique, non-ambiguous source container names both in the Docker Compose configuration and when specifying dependencies in the com.centurylinklabs.watchtower.depends-on label to ensure correct dependency resolution and behavior across Docker Compose projects/stacks.
Ambiguous names can lead to warnings and skipped updates to prevent non-deterministic behavior.
Docker Compose Depends-On Label¶
Watchtower automatically recognizes Docker Compose's depends_on relationships:
services:
web:
image: nginx
depends_on:
- database
database:
image: postgres
The com.docker.compose.depends_on label is automatically set by Docker Compose and parsed by Watchtower to extract service names and support implicitly restarting linked services.
Docker Compose depends_on Restart Behavior
Docker Compose's depends_on has a restart attribute in the long-form syntax:
Watchtower does not support using this to control implicit restarts, because this is an explicit, opt-in feature that defaults to false when omitted, such as when using the short-form syntax.
Docker Links and Network Mode¶
For legacy Docker setups using links or network_mode: service:container, Watchtower treats these as implicit dependencies:
# Container with explicit link
docker run --link database:db nginx
# Container using service network mode
docker run --network container:database nginx
Topological Sorting¶
Watchtower uses topological sorting to determine the correct update order. This algorithm:
- Builds a dependency graph from all detected relationships
- Detects cycles (failing with a circular dependency error)
- Produces a linear ordering where dependencies precede dependents
Warning
Circular dependencies between containers will cause the update process to fail with an error. Ensure your dependency graph is acyclic.
Update Order¶
When updates are needed, Watchtower follows this sequence:
- Identify all containers requiring updates
- Expand the set to include all containers in the dependency chain
- Sort containers using topological order (dependencies first)
- Stop containers in reverse topological order (dependents first)
- Update and restart containers in topological order (dependencies first)
This ensures that:
- Dependent services are stopped before their dependencies change
- Dependencies are fully restarted before dependents attempt to connect
Configuration¶
Automatic Detection¶
In most cases, no additional configuration is required. Watchtower automatically detects dependencies from:
- Docker Compose
depends_ondeclarations - Existing Docker links
network_mode: service:containerconfigurations
Docker Compose Considerations
When using Docker Compose, Watchtower leverages depends_on declarations for dependency detection. Dependencies are resolved using service names, not container names. Ensure your depends_on references service names correctly.
Rolling restart is currently not supported when used in combination with linked-containers.
This limitation exists because linked-containers require coordinated updates across dependency chains, which conflicts with the incremental nature of rolling restarts.
Disable Docker Compose Depends-On¶
If you want to disable automatic dependency detection from the Docker Compose depends_on configuration while preserving other dependency sources, use the following configuration:
This disables parsing of the com.docker.compose.depends_on label while still honoring:
- Watchtower's explicit
com.centurylinklabs.watchtower.depends-onlabel - Legacy Docker links
- Network mode dependencies (
network_mode: service:container)
Explicit Dependencies¶
For cases where automatic detection is insufficient, use the Watchtower depends-on label:
Advanced Scenarios¶
Multiple Dependencies¶
Complex Dependency Chains¶
services:
web:
image: nginx
depends_on:
- api
api:
image: myapi
depends_on:
- database
- cache
database:
image: postgres
cache:
image: redis
In this scenario, Watchtower will update containers in the order: cache, database, api, web.
Examples¶
MySQL-WordPress Scenario¶
Consider a classic WordPress setup with MySQL database:
When Watchtower detects a MySQL update:
- Dependency Detection: Identifies that
wordpressdepends onmysql - Stop Order: Stops
wordpressfirst, thenmysql - Update Order: Updates and restarts
mysqlfirst, thenwordpress
This prevents WordPress from losing database connectivity during the update.
Microservices Architecture¶
For complex applications with multiple services:
Legacy Docker Links¶
For applications using traditional Docker linking:
Network Mode Dependencies¶
Containers using network_mode: service:container:
Troubleshooting¶
Common Issues¶
Updates Failing Due to Circular Dependencies¶
Error
If you see "circular reference detected" errors, check your dependency declarations for cycles.
Solution: Review and remove circular dependencies. For example, if A depends on B and B depends on A, remove one of the dependencies or restructure your services.
Containers Not Updating in Expected Order¶
Check:
- Verify dependency labels are correctly formatted
- Ensure container names match exactly
- Check Docker Compose service names vs container names
Missing Dependencies¶
Symptoms: Containers update out of order or fail to connect after updates.
Debug: Enable debug logging to see detected dependencies:
Look for log messages like:
- "Retrieved links from watchtower depends-on label"
- "Retrieved links from compose depends-on label"
- "Completed dependency sort"
Debugging Commands¶
Enable verbose logging to inspect dependency detection:
docker run --rm \
-v /var/run/docker.sock:/var/run/docker.sock \
ghcr.io/sidneyojr/watchtower \
--debug \
--run-once
Check container labels: