Skip to content

[BUG] docker compose stop ignores COMPOSE_FILE when -p is used, skipping pre_stop hooks #14535

Open

Description

@ZenVoich

Description

Expected behavior

COMPOSE_FILE should behave like -f here, as documented. Both stop invocations should load the service definition, run pre_stop, and wait for it before sending the stop signal.

If intentionally bypassing model loading for -p, Compose should at least warn or fail when COMPOSE_FILE is present but ignored, rather than silently skipping lifecycle hooks.

Likely cause

In cmd/compose/compose.go, projectOrName only loads the project when explicit config paths are present or when no project name was provided:

https://github.com/docker/compose/blob/v5.6.1/cmd/compose/compose.go#L248-L278

With -p, o.ConfigPaths is empty even though COMPOSE_FILE is set, so ToProject is skipped and Stop receives Project: nil.

The backend then reconstructs the project from container labels. Those labels do not contain pre_stop, so stopContainer has no hooks to execute:

https://github.com/docker/compose/blob/v5.6.1/pkg/compose/down.go#L426-L445

The same projectOrName condition appears to exist in v5.8.0 as well.

Suggested fix

When deciding whether to load a project in projectOrName, account for COMPOSE_FILE the same way as explicit -f paths. Alternatively, if the label-based project path is intentional, surface a warning or error when a configured Compose file is ignored.

Alternative solution

Store the pre_stop hook definitions in container labels at up/create time, the same way other service metadata is already serialized into labels. Then the label-based project reconstruction path could still find and run a container's pre_stop hooks even when no Compose file is loaded — regardless of whether the file was supplied via COMPOSE_FILE or -f, or is simply unavailable on the machine running stop.

Steps To Reproduce

No response

Compose Version

Docker Compose version v5.6.1

Docker Environment

Client:
 Version:    29.9.2
 Context:    desktop-linux
 Debug Mode: false
 Plugins:
  agent: Docker AI Agent Runner (Docker Inc.)
    Version:  v1.152.0
    Path:     /Users/zen/.docker/cli-plugins/docker-agent
  ai: Docker AI Agent - Ask Gordon (Docker Inc.)
    Version:  v1.33.0
    Path:     /Users/zen/.docker/cli-plugins/docker-ai
  buildx: Docker Buildx (Docker Inc.)
    Version:  v0.38.1
    Path:     /Users/zen/.docker/cli-plugins/docker-buildx
  compose: Docker Compose (Docker Inc.)
    Version:  v5.6.1
    Path:     /Users/zen/.docker/cli-plugins/docker-compose
  debug: Get a shell into any image or container (Docker Inc.)
    Version:  0.49.0
    Path:     /Users/zen/.docker/cli-plugins/docker-debug
  desktop: Docker Desktop commands (Docker Inc.)
    Version:  4.98.0
    Path:     /Users/zen/.docker/cli-plugins/docker-desktop
  dhi: CLI for managing Docker Hardened Images (Docker Inc.)
    Version:  v0.0.9
    Path:     /Users/zen/.docker/cli-plugins/docker-dhi
  extension: Manages Docker extensions (Docker Inc.)
    Version:  v0.2.32
    Path:     /Users/zen/.docker/cli-plugins/docker-extension
  init: Creates Docker-related starter files for your project (Docker Inc.)
    Version:  v1.4.1
    Path:     /Users/zen/.docker/cli-plugins/docker-init
  mcp: Docker MCP Plugin (Docker Inc.)
    Version:  v0.44.2
    Path:     /Users/zen/.docker/cli-plugins/docker-mcp
  model: Docker Model Runner (Docker Inc.)
    Version:  v1.2.9
    Path:     /Users/zen/.docker/cli-plugins/docker-model
  offload: Docker Offload (Docker Inc.)
    Version:  v0.7.35
    Path:     /Users/zen/.docker/cli-plugins/docker-offload
  pass: Docker Pass Secrets Manager Plugin (beta) (Docker Inc.)
    Version:  v0.3.2
    Path:     /Users/zen/.docker/cli-plugins/docker-pass
  sandbox: "docker sandbox" is deprecated, use Docker Sandboxes instead (Docker Inc.)
    Version:  v0.13.0
    Path:     /Users/zen/.docker/cli-plugins/docker-sandbox
  scout: Docker Scout (Docker Inc.)
    Version:  v1.27.0
    Path:     /Users/zen/.docker/cli-plugins/docker-scout

Server:
 Containers: 15
  Running: 6
  Paused: 0
  Stopped: 9
 Images: 29
 Server Version: 29.9.2
 Storage Driver: overlayfs
  driver-type: io.containerd.snapshotter.v1
 Logging Driver: local
 Cgroup Driver: cgroupfs
 Cgroup Version: 2
 Plugins:
  Volume: local
  Network: bridge host ipvlan macvlan null overlay
  Log: awslogs fluentd gcplogs gelf journald json-file local splunk syslog
 CDI spec directories:
  /etc/cdi
  /var/run/cdi
 Discovered Devices:
  cdi: docker.com/gpu=webgpu
 Swarm: inactive
 Runtimes: io.containerd.runc.v2 runc
 Default Runtime: runc
 Init Binary: docker-init
 containerd version: b7a25261e029d68978bbf290df0a62fc819b3c51
 runc version: v1.5.2-0-g29dd3dc2
 init version: de40ad0
 Security Options:
  seccomp
   Profile: builtin
  cgroupns
 Kernel Version: 7.0.14-linuxkit
 Operating System: Docker Desktop
 OSType: linux
 Architecture: aarch64
 CPUs: 16
 Total Memory: 7.747GiB
 Name: docker-desktop
 ID: 42769639-54d4-4e58-8fab-b5780adbc107
 Docker Root Dir: /var/lib/docker
 Debug Mode: false
 HTTP Proxy: http.docker.internal:3128
 HTTPS Proxy: http.docker.internal:3128
 No Proxy: hubproxy.docker.internal
 Labels:
  com.docker.desktop.address=unix:///Users/zen/Library/Containers/com.docker.docker/Data/docker-cli.sock
 Experimental: false
 Insecure Registries:
  hubproxy.docker.internal:5555
  127.0.0.0/8
  ::1/128
 Live Restore Enabled: false
 Firewall Backend: iptables

Anything else?

Related #14476

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions