Repository: moby/moby (language: go)

Issue:
Duplicate Events for Network Disconnect in Docker Events Command
### Description

We have found a bug in the `docker events` command that captures Docker events. Specifically, when a network disconnect event occurs, it generates two events instead of one.

### Reproduce

1. Run the `docker events` command to observe events:
   ```
   docker events --filter "event=disconnect"
   ```
2. Run the command to start a container connected to a network:
   ```
   docker run -d --name test-container --network test-network nginx
   ```
3. Disconnect the network from the container:
   ```
   docker network disconnect test-network test-container
   ```

### Expected behavior

The `docker events` command should generate a single event for each network disconnect. For example:
```
2024-10-30T19:05:20.743496360 network disconnect d331b8f1511379d6ddb431e249c478c3239e2ad54f14896a3c25f41bf8f70974 (container=6835ba7cb725b84add7e83f356b1fbae191b48a442e085d013fd29a153a58c45, name=test-network, type=bridge)
```

### Actual Behavior

The  `docker events` command generates two events for each network disconnect. For example:
```
2024-10-30T19:05:20.743496360 network disconnect d331b8f1511379d6ddb431e249c478c3239e2ad54f14896a3c25f41bf8f70974 (container=6835ba7cb725b84add7e83f356b1fbae191b48a442e085d013fd29a153a58c45, name=test-network, type=bridge)
2024-10-30T19:05:20.745043405 network disconnect d331b8f1511379d6ddb431e249c478c3239e2ad54f14896a3c25f41bf8f70974 (container=6835ba7cb725b84add7e83f356b1fbae191b48a442e085d013fd29a153a58c45, name=test-network, type=bridge)
```


### docker version
```
Client: Docker Engine - Community
 Version:           27.3.1
 API version:       1.47
 Go version:        go1.22.7
 Git commit:        ce12230
 Built:             Fri Sep 20 11:41:00 2024
 OS/Arch:           linux/amd64
 Context:           default

Server: Docker Engine - Community
 Engine:
  Version:          27.3.1
  API version:      1.47 (minimum version 1.24)
  Go version:       go1.22.7
  Git commit:       41ca978
  Built:            Fri Sep 20 11:41:00 2024
  OS/Arch:          linux/amd64
  Experimental:     false
 containerd:
  Version:          1.7.22
  GitCommit:        7f7fdf5fed64eb6a7caf99b3e12efcf9d60e311c
 runc:
  Version:          1.1.14
  GitCommit:        v1.1.14-0-g2c9f560
 docker-init:
  Version:          0.19.0
  GitCommit:        de40ad0
```

### docker info
```
Client: Docker Engine - Community
 Version:    27.3.1
 Context:    default
 Debug Mode: false
 Plugins:
  buildx: Docker Buildx (Docker Inc.)
    Version:  v0.17.1
    Path:     /usr/libexec/docker/cli-plugins/docker-buildx
  compose: Docker Compose (Docker Inc.)
    Version:  v2.29.7
    Path:     /usr/libexec/docker/cli-plugins/docker-compose

Server:
 Containers: 1
  Running: 0
  Paused: 0
  Stopped: 1
 Images: 21
 Server Version: 27.3.1
 Storage Driver: overlay2
  Backing Filesystem: extfs
  Supports d_type: true
  Using metacopy: false
  Native Overlay Diff: true
  userxattr: false
 Logging Driver: json-file
 Cgroup Driver: systemd
 Cgroup Version: 2
 Plugins:
  Volume: local
  Network: bridge host ipvlan macvlan null overlay
  Log: awslogs fluentd gcplogs gelf journald json-file local splunk syslog
 Swarm: inactive
 Runtimes: io.containerd.runc.v2 runc
 Default Runtime: runc
 Init Binary: docker-init
 containerd version: 7f7fdf5fed64eb6a7caf99b3e12efcf9d60e311c
 runc version: v1.1.14-0-g2c9f560
 init version: de40ad0
 Security Options:
  apparmor
  seccomp
   Profile: builtin
  cgroupns
 Kernel Version: 6.9.3-76060903-generic
 Operating System: Pop!_OS 22.04 LTS
 OSType: linux
 Architecture: x86_64
 CPUs: 16
 Total Memory: 15.41GiB
 Name: pop-os
 ID: 1fa450f3-a97d-4219-9081-e02e277aa110
 Docker Root Dir: /var/lib/docker
 Debug Mode: false
 Experimental: false
 Insecure Registries:
  127.0.0.0/8
 Live Restore Enabled: false
```

### Additional Info

This bug has been tested and confirmed to be valid for the following environments:

- Docker Version: 27.3.1
- Operating Systems:
  - Pop!_OS 22.04 LTS
  - Arch Linux 6.11.5-arch1-1

---

- Docker Version: 27.0.3
- Operating Systems:
  - Ubuntu 22.04.4 LTS (Jammy Jellyfish)

We have checked the source code and identified the part of the code responsible for this behavior. We also have a fix ready and would be happy to contribute a pull request if needed.


Discussion / hints:

Fix patch:
diff --git a/daemon/container_operations.go b/daemon/container_operations.go
index a7daf87f0e9ab..7a4e35695b671 100644
--- a/daemon/container_operations.go
+++ b/daemon/container_operations.go
@@ -1125,12 +1125,6 @@ func (daemon *Daemon) DisconnectFromNetwork(ctx context.Context, ctr *container.
 		return err
 	}
 
-	if n != nil {
-		daemon.LogNetworkEventWithAttributes(n, events.ActionDisconnect, map[string]string{
-			"container": ctr.ID,
-		})
-	}
-
 	return nil
 }
 


Test patch: