Skip to content

Commit 6f60b30

Browse files
committed
[ctr] add HOSTNAME env for host network
Set the hostname environment var when using host networking via ctr Signed-off-by: Michael Crosby <[email protected]>
1 parent fda782a commit 6f60b30

1 file changed

Lines changed: 11 additions & 1 deletion

File tree

cmd/ctr/commands/run/run_unix.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ package run
2121
import (
2222
gocontext "context"
2323
"fmt"
24+
"os"
2425
"path/filepath"
2526
"strconv"
2627
"strings"
@@ -200,7 +201,16 @@ func NewContainer(ctx gocontext.Context, client *containerd.Client, context *cli
200201
opts = append(opts, oci.WithPrivileged, oci.WithAllDevicesAllowed, oci.WithHostDevices)
201202
}
202203
if context.Bool("net-host") {
203-
opts = append(opts, oci.WithHostNamespace(specs.NetworkNamespace), oci.WithHostHostsFile, oci.WithHostResolvconf)
204+
hostname, err := os.Hostname()
205+
if err != nil {
206+
return nil, errors.Wrap(err, "get hostname")
207+
}
208+
opts = append(opts,
209+
oci.WithHostNamespace(specs.NetworkNamespace),
210+
oci.WithHostHostsFile,
211+
oci.WithHostResolvconf,
212+
oci.WithEnv([]string{fmt.Sprintf("HOSTNAME=%s", hostname)}),
213+
)
204214
}
205215

206216
seccompProfile := context.String("seccomp-profile")

0 commit comments

Comments
 (0)