Service and Container correlation is broken
Describe the bug When multiple projects use the same service names, it's possible – and potentially common – for the listed services to refer to another project's containers.
To Reproduce Steps to reproduce the behavior:
- Create two separate
docker-compose.ymlfiles in two different directories. - Add both an
appservice and a project-specific service to eachdocker-compose.ymlfile. - Build and run both project's containers.
- Run
lazydockerin both projects.
Expected behaviour
Each instance of lazydocker shows two services, and both services refer to different containers.
Observed behavior
lazydocker matches the Service record against the first Container with a matching service name label. This can be easily validated by examining the com.docker.compose.project label in the "Container Config" panel for the listed services.
This bug has a few knock-on effects:
- The Containers panel only shows containers that have a service name that does not match a Service in the current project. If (e.g.) the
appService has captured the container for the wrong project, the container for the correctappService is also inaccessible. - Since the
docker-composeproject name cannot be reliably determined externally (without introducing a new convention),lazydockercurrently relies on the appropriate label of the first Service's container to determine the project name (falling back to$PWD). If that Service has captured the container for the wrong project, the displayed project name will also be incorrect.
Desktop (please complete the following information):
- OS: macOS 10.14.5
- Lazydocker v0.5.5
Additional context
The Service and Container matching behavior described is supported by a reading of the current logic here: https://github.com/jesseduffield/lazydocker/blob/bd16a78b06542bb956d869e4d0285188fe6353e9/pkg/commands/docker.go#L249-L290
Damn this is tricky. Does anybody know what might be a better way of linking containers to services?
@jesseduffield Untested thoughts:
- Right now,
lazydockeris usingdocker-compose config --hash='*'to enumerate the services available, which returns the hash of the service's config as an ID. That ID can be correlated by inspecting the docker container with a corresponding label (as indocker container list -aqf label=com.docker.compose.config-hash=${CONFIG_HASH}, yielding one or more container IDs). - Alternately,
docker-compose ps -qwill display all of the container IDs that are part of the "current"docker-composeproject. Each of these containers contains labels naming the service, project name, config has, container number (for multi-container services), and more.
If nobody else jumps on this quickly, I may take a stab at it myself – though I'm sure most others would be a fair sight faster. :)
This is causing some grief in our tooling setup for me. It looks like the PR above has already gone down some paths, that seem to have not worked out very well, race conditions and so.. Do we have any other options for approaches to fix this?
My proposal here is: would it be possible to pass --filter label=value arguments along from the command-line to the whichever "docker ps" / "docker container ls" commands that are invoked to discover containers and such?
The reasoning for this is as a workaround; docker-compose makes sure to set the "com.docker-compose.project" label on starts and I have access to the project name from outside (usually this is the folder name, or COMPOSE_PROJECT_NAME). If this is a viable change to make, I'll gladly try a stab at the PR.
With the additional filtering, lazydocker won't be able to show "standalone containers", but that is an acceptable loss IMO when using it specifically for project monitoring.
@jesseduffield Hello! I opened a PR to fix this issue. Please check out when you have time #633 .
Thank you!