Merge pull request #5891 from crosbymichael/net-host-env

[ctr] add HOSTNAME env for host network
This commit is contained in:
Fu Wei 2021-08-19 00:04:37 +08:00 committed by GitHub
commit a5eccab278
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -21,6 +21,7 @@ package run
import (
gocontext "context"
"fmt"
"os"
"path/filepath"
"strconv"
"strings"
@ -200,7 +201,16 @@ func NewContainer(ctx gocontext.Context, client *containerd.Client, context *cli
opts = append(opts, oci.WithPrivileged, oci.WithAllDevicesAllowed, oci.WithHostDevices)
}
if context.Bool("net-host") {
opts = append(opts, oci.WithHostNamespace(specs.NetworkNamespace), oci.WithHostHostsFile, oci.WithHostResolvconf)
hostname, err := os.Hostname()
if err != nil {
return nil, errors.Wrap(err, "get hostname")
}
opts = append(opts,
oci.WithHostNamespace(specs.NetworkNamespace),
oci.WithHostHostsFile,
oci.WithHostResolvconf,
oci.WithEnv([]string{fmt.Sprintf("HOSTNAME=%s", hostname)}),
)
}
seccompProfile := context.String("seccomp-profile")