Skip to content

Commit 893a393

Browse files
committed
feat: add support for 'domainname' option in container create
Signed-off-by: Swagat Bora <[email protected]>
1 parent 8e9e464 commit 893a393

File tree

16 files changed

+160
-23
lines changed

16 files changed

+160
-23
lines changed

cmd/nerdctl/container/container_run.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ func setCreateFlags(cmd *cobra.Command) {
131131
cmd.Flags().String("ip", "", "IPv4 address to assign to the container")
132132
cmd.Flags().String("ip6", "", "IPv6 address to assign to the container")
133133
cmd.Flags().StringP("hostname", "h", "", "Container host name")
134+
cmd.Flags().String("domainname", "", "Container domain name")
134135
cmd.Flags().String("mac-address", "", "MAC address to assign to the container")
135136
// #endregion
136137

cmd/nerdctl/container/container_run_linux_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,8 @@ func TestRunUtsHost(t *testing.T) {
166166
base.Cmd("run", "--rm", "--uts=host", testutil.AlpineImage, "hostname").AssertOutContains(hostName)
167167
// Validate we can't provide a hostname with uts=host
168168
base.Cmd("run", "--rm", "--uts=host", "--hostname=foobar", testutil.AlpineImage, "hostname").AssertFail()
169+
// Validate we can't provide a domainname with uts=host
170+
base.Cmd("run", "--rm", "--uts=host", "--domainname=example.com", testutil.AlpineImage, "hostname").AssertFail()
169171
}
170172

171173
func TestRunPidContainer(t *testing.T) {

cmd/nerdctl/container/container_run_network.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,13 @@ func loadNetworkFlags(cmd *cobra.Command) (types.NetworkOptions, error) {
9393
}
9494
netOpts.Hostname = hostName
9595

96+
// --domainname=<container domainname>
97+
domainname, err := cmd.Flags().GetString("domainname")
98+
if err != nil {
99+
return netOpts, err
100+
}
101+
netOpts.Domainname = domainname
102+
96103
// --dns=<DNS host> ...
97104
dnsSlice, err := cmd.Flags().GetStringSlice("dns")
98105
if err != nil {

cmd/nerdctl/container/container_run_test.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -722,3 +722,54 @@ func TestRunFromOCIArchive(t *testing.T) {
722722
base.Cmd("build", "--tag", tag, fmt.Sprintf("--output=type=oci,dest=%s", tarPath), buildCtx).AssertOK()
723723
base.Cmd("run", "--rm", fmt.Sprintf("oci-archive://%s", tarPath)).AssertOutContainsAll(fmt.Sprintf("Loaded image: %s", tag), sentinel)
724724
}
725+
726+
func TestRunDomainname(t *testing.T) {
727+
t.Parallel()
728+
729+
if runtime.GOOS == "windows" {
730+
t.Skip("run --hostname not implemented on Windows yet")
731+
}
732+
733+
testCases := []struct {
734+
name string
735+
hostname string
736+
domainname string
737+
Cmd string
738+
CmdFlag string
739+
expectedOut string
740+
}{
741+
{
742+
name: "Check domain name",
743+
hostname: "foobar",
744+
domainname: "example.com",
745+
Cmd: "hostname",
746+
CmdFlag: "-d",
747+
expectedOut: "example.com",
748+
},
749+
{
750+
name: "check fqdn",
751+
hostname: "foobar",
752+
domainname: "example.com",
753+
Cmd: "hostname",
754+
CmdFlag: "-f",
755+
expectedOut: "foobar.example.com",
756+
},
757+
}
758+
759+
for _, tc := range testCases {
760+
tc := tc // capture range variable
761+
t.Run(tc.name, func(t *testing.T) {
762+
t.Parallel()
763+
base := testutil.NewBase(t)
764+
765+
base.Cmd("run",
766+
"--rm",
767+
"--hostname", tc.hostname,
768+
"--domainname", tc.domainname,
769+
testutil.CommonImage,
770+
tc.Cmd,
771+
tc.CmdFlag,
772+
).AssertOutContains(tc.expectedOut)
773+
})
774+
}
775+
}

docs/command-reference.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,7 @@ Network flags:
186186
- :whale: `--dns-search`: Set custom DNS search domains
187187
- :whale: `--dns-opt, --dns-option`: Set DNS options
188188
- :whale: `-h, --hostname`: Container host name
189+
- :whale: `--domainname`: Container domain name
189190
- :whale: `--add-host`: Add a custom host-to-IP mapping (host:ip). `ip` could be a special string `host-gateway`,
190191
- which will be resolved to the `host-gateway-ip` in nerdctl.toml or global flag.
191192
- :whale: `--ip`: Specific static IP address(es) to use. Note that unlike docker, nerdctl allows specifying it with the default bridge network.
@@ -413,7 +414,7 @@ IPFS flags:
413414

414415
Unimplemented `docker run` flags:
415416
`--blkio-weight-device`, `--cpu-rt-*`, `--device-*`,
416-
`--disable-content-trust`, `--domainname`, `--expose`, `--health-*`, `--isolation`, `--no-healthcheck`,
417+
`--disable-content-trust`, `--expose`, `--health-*`, `--isolation`, `--no-healthcheck`,
417418
`--link*`, `--publish-all`, `--storage-opt`,
418419
`--userns`, `--volume-driver`
419420

pkg/api/types/container_network_types.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ type NetworkOptions struct {
3232
IP6Address string
3333
// Hostname set container host name
3434
Hostname string
35+
// Domainname specifies the container's domain name
36+
Domainname string
3537
// DNSServers set custom DNS servers
3638
DNSServers []string
3739
// DNSResolvConfOptions set DNS options

pkg/cmd/container/create.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -621,8 +621,9 @@ type internalLabels struct {
621621
extraHosts []string
622622
pidFile string
623623
// labels from cmd options or automatically set
624-
name string
625-
hostname string
624+
name string
625+
hostname string
626+
domainname string
626627
// automatically generated
627628
stateDir string
628629
// network
@@ -652,6 +653,7 @@ func withInternalLabels(internalLabels internalLabels) (containerd.NewContainerO
652653
m[labels.Name] = internalLabels.name
653654
}
654655
m[labels.Hostname] = internalLabels.hostname
656+
m[labels.Domainname] = internalLabels.domainname
655657
extraHostsJSON, err := json.Marshal(internalLabels.extraHosts)
656658
if err != nil {
657659
return nil, err
@@ -729,6 +731,7 @@ func withInternalLabels(internalLabels internalLabels) (containerd.NewContainerO
729731
// loadNetOpts loads network options into InternalLabels.
730732
func (il *internalLabels) loadNetOpts(opts types.NetworkOptions) {
731733
il.hostname = opts.Hostname
734+
il.domainname = opts.Domainname
732735
il.ports = opts.PortMappings
733736
il.ipAddress = opts.IPAddress
734737
il.ip6Address = opts.IP6Address

pkg/containerutil/container_network_manager.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,8 @@ func (m *containerNetworkManager) VerifyNetworkOptions(_ context.Context) error
209209

210210
// Note that mac-address is accepted, though it is a no-op
211211
nonZeroParams := nonZeroMapValues(map[string]interface{}{
212-
"--hostname": m.netOpts.Hostname,
212+
"--hostname": m.netOpts.Hostname,
213+
"--domainname": m.netOpts.Domainname,
213214
// NOTE: an empty slice still counts as a non-zero value so we check its length:
214215
"-p/--publish": len(m.netOpts.PortMappings) != 0,
215216
"--dns": len(m.netOpts.DNSServers) != 0,
@@ -576,6 +577,9 @@ func (m *hostNetworkManager) ContainerNetworkingOpts(_ context.Context, containe
576577
if hostnameOpts != nil {
577578
specs = append(specs, hostnameOpts...)
578579
}
580+
if m.netOpts.Domainname != "" {
581+
specs = append(specs, oci.WithDomainname(m.netOpts.Domainname))
582+
}
579583
}
580584

581585
if rootlessutil.IsRootless() {
@@ -643,9 +647,9 @@ func validateUtsSettings(netOpts types.NetworkOptions) error {
643647
}
644648

645649
// Docker considers this a validation error so keep compat.
646-
// https://docs.docker.com/engine/reference/run/#uts-settings---uts
647-
if utsNamespace == UtsNamespaceHost && netOpts.Hostname != "" {
648-
return fmt.Errorf("conflicting options: cannot set a --hostname with --uts=host")
650+
// https://docs.docker.com/reference/cli/docker/container/run/#uts
651+
if utsNamespace == UtsNamespaceHost && (netOpts.Hostname != "" || netOpts.Domainname != "") {
652+
return fmt.Errorf("conflicting options: cannot set --hostname and/or --domainname with --uts=host")
649653
}
650654

651655
return nil

pkg/containerutil/container_network_manager_linux.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,9 @@ func (m *cniNetworkManager) ContainerNetworkingOpts(_ context.Context, container
129129
if hostnameOpts != nil {
130130
opts = append(opts, hostnameOpts...)
131131
}
132+
if m.netOpts.Domainname != "" {
133+
opts = append(opts, oci.WithDomainname(m.netOpts.Domainname))
134+
}
132135
}
133136

134137
return opts, cOpts, nil

pkg/containerutil/container_network_manager_windows.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,9 @@ func (m *cniNetworkManager) VerifyNetworkOptions(_ context.Context) error {
4848
}
4949

5050
nonZeroArgs := nonZeroMapValues(map[string]interface{}{
51-
"--hostname": m.netOpts.Hostname,
52-
"--uts": m.netOpts.UTSNamespace,
51+
"--hostname": m.netOpts.Hostname,
52+
"--domainname": m.netOpts.Domainname,
53+
"--uts": m.netOpts.UTSNamespace,
5354
// NOTE: IP and MAC settings are currently ignored on Windows.
5455
"--ip-address": m.netOpts.IPAddress,
5556
"--mac-address": m.netOpts.MACAddress,

0 commit comments

Comments
 (0)