Repository: moby/moby (language: go)

Issue:
`--net=host`: remove `/var/run/docker/netns/default` from OCI Runtime Config
### Description

A container running with `--net=host` has `{"type":"network","path":"/var/run/docker/netns/default"}` in the `.linux.namespaces` field of OCI Runtime Config, but this shouldn't be needed

### Reproduce

```console
$ docker run --net=host ...
$ jq .linux.namespaces < /run/containerd/io.containerd.runtime.v2.task/moby/5de6ff74780ab5e299f6e5c2e92f5224e26eaece301e6f24d8b93ef7d366deb4/config.json 
[
  {
    "type": "mount"
  },
  {
    "type": "network",
    "path": "/var/run/docker/netns/default"
  },
  {
    "type": "uts"
  },
  {
    "type": "pid"
  },
  {
    "type": "ipc"
  },
  {
    "type": "cgroup"
  }
]
```

### Expected behavior

The netns should be just left unspecified

### docker version

```bash
Client: Docker Engine - Community
 Version:           24.0.7
 API version:       1.43
 Go version:        go1.20.10
 Git commit:        afdd53b
 Built:             Thu Oct 26 09:07:41 2023
 OS/Arch:           linux/amd64
 Context:           default

Server: Docker Engine - Community
 Engine:
  Version:          24.0.7
  API version:      1.43 (minimum version 1.12)
  Go version:       go1.20.10
  Git commit:       311b9ff
  Built:            Thu Oct 26 09:07:41 2023
  OS/Arch:          linux/amd64
  Experimental:     false
 containerd:
  Version:          1.6.27
  GitCommit:        a1496014c916f9e62104b33d1bb5bd03b0858e59
 runc:
  Version:          1.1.11
  GitCommit:        v1.1.11-0-g4bccb38
 docker-init:
  Version:          0.19.0
  GitCommit:        de40ad0
```


### docker info

```bash
Client: Docker Engine - Community
 Version:    24.0.7
 Context:    default
 Debug Mode: false
 Plugins:
  buildx: Docker Buildx (Docker Inc.)
    Version:  v0.11.2
    Path:     /usr/libexec/docker/cli-plugins/docker-buildx
  compose: Docker Compose (Docker Inc.)
    Version:  v2.21.0
    Path:     /usr/libexec/docker/cli-plugins/docker-compose

Server:
 Containers: 1
  Running: 1
  Paused: 0
  Stopped: 0
 Images: 1
 Server Version: 24.0.7
 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 logentries splunk syslog
 Swarm: inactive
 Runtimes: io.containerd.runc.v2 runc
 Default Runtime: runc
 Init Binary: docker-init
 containerd version: a1496014c916f9e62104b33d1bb5bd03b0858e59
 runc version: v1.1.11-0-g4bccb38
 init version: de40ad0
 Security Options:
  apparmor
  seccomp
   Profile: builtin
  cgroupns
 Kernel Version: 5.15.0-91-generic
 Operating System: Ubuntu 22.04.3 LTS
 OSType: linux
 Architecture: x86_64
 CPUs: 4
 Total Memory: 3.814GiB
 Name: lima-docker-rootful
 ID: 25e8c1e6-194c-4775-8298-5efa72274f15
 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 refactoring will help supporting `--net=host` properly in rootless in future, with detach-netns mode https://github.com/containerd/nerdctl/pull/2723 .
(The refactoring itself does not automatically support `--net=host` in rootless)


Discussion / hints:
The concept of the "default" sandbox was apparently introduced in:
- https://github.com/moby/libnetwork/pull/108/commits/5f9906ddffc25c9af89e20027338892e9f70d60c

Ah, thanks for digging a bit. I was curious indeed if this was really _intentional_ or another case of some magic "default" value that is not handled, and therefore leading to this (so a bug that nobody noticed before).


Also /cc @akerouanton @robmry 
Commit above is from this PR;

- https://github.com/moby/libnetwork/pull/108

Which, is "just a bunch of changes for Docker" without much context;

> A set of commits required for docker integration. Please see individual commits for more detailed description.

There _is_ a comment on the PR about the `NoSandboxCreate` boolean, which looks a bit weird; does that mean we have two separate mechanisms for "special" cases (host networking, so using the host's networking namespace); `default` namespace _AND_ `NoSandboxCreate` ? (and perhaps `host` networking should've had that `NoSandboxCreate` set?)
> Commit above is from this PR;
> 
> * [Docker integration commits libnetwork#108](https://github.com/moby/libnetwork/pull/108)
> 
> Which, is "just a bunch of changes for Docker" without much context;
> 
> > A set of commits required for docker integration. Please see individual commits for more detailed description.
> 
> There _is_ a comment on the PR about the `NoSandboxCreate` boolean, which looks a bit weird; does that mean we have two separate mechanisms for "special" cases (host networking, so using the host's networking namespace); `default` namespace _AND_ `NoSandboxCreate` ? (and perhaps `host` networking should've had that `NoSandboxCreate` set?)

Apparently, "NoSandboxCreate" was replaced with the concept of the "default" sandbox in https://github.com/moby/libnetwork/commit/5f9906ddffc25c9af89e20027338892e9f70d60c
Oh! You're right! Yes, that looks highly suspicious; quite likely that was not taken into account, and we may have ended up creating a sandbox still?

Are you looking at fixing this, or want someone else to look into this?

Fix patch:
diff --git a/daemon/oci_linux.go b/daemon/oci_linux.go
index 942d094093f39..c7fdedcb31f10 100644
--- a/daemon/oci_linux.go
+++ b/daemon/oci_linux.go
@@ -285,10 +285,7 @@ func WithNamespaces(daemon *Daemon, c *container.Container) coci.SpecOpts {
 					})
 				}
 			case networkMode.IsHost():
-				setNamespace(s, specs.LinuxNamespace{
-					Type: specs.NetworkNamespace,
-					Path: c.NetworkSettings.SandboxKey,
-				})
+				oci.RemoveNamespace(s, specs.NetworkNamespace)
 			default:
 				setNamespace(s, specs.LinuxNamespace{
 					Type: specs.NetworkNamespace,


Test patch: