Skip to content

Commit 4cbb123

Browse files
buck-rossnievesmontero
authored andcommitted
feat: allow custom hostnames for new containers
The new `--hostname` flag is now available when invoking `toolbox create` in order to create a container with a custom hostname. The new flag does not enforce network-resolvability of any custom hostnames, but it does require that the hostname is valid, per RFC 1123. To preserve backward-compatibility, the hostname will still default to "toolbox" if the flag is not specified. Signed-off-by: Buckley Ross <[email protected]> Change-Id: Ib3c0706e3a93a5748453e39998b9893e3e57c305
1 parent d85c717 commit 4cbb123

File tree

2 files changed

+25
-4
lines changed

2 files changed

+25
-4
lines changed

src/cmd/create.go

+24-3
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"fmt"
2222
"os"
2323
"path/filepath"
24+
"regexp"
2425
"strings"
2526
"time"
2627

@@ -45,6 +46,7 @@ var (
4546
authFile string
4647
container string
4748
distro string
49+
hostname string
4850
image string
4951
release string
5052
}
@@ -85,6 +87,12 @@ func init() {
8587
"",
8688
"Create a toolbox container for a different operating system distribution than the host")
8789

90+
flags.StringVarP(&createFlags.hostname,
91+
"hostname",
92+
"",
93+
"",
94+
"Set the container's hostname (defaults to 'toolbox')")
95+
8896
flags.StringVarP(&createFlags.image,
8997
"image",
9098
"i",
@@ -113,6 +121,10 @@ func init() {
113121
}
114122

115123
func create(cmd *cobra.Command, args []string) error {
124+
// This regex filters out strings which are not valid hostnames, according to RFC-1123.
125+
// Source: https://stackoverflow.com/a/106223
126+
var hostnameRegexp = regexp.MustCompile("^(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\\-]*[a-zA-Z0-9])\\.)*([A-Za-z0-9]|[A-Za-z0-9][a-zA-Z0-9\\-]*[a-zA-Z0-9])$")
127+
116128
if utils.IsInsideContainer() {
117129
if !utils.IsInsideToolboxContainer() {
118130
return errors.New("this is not a toolbox container")
@@ -155,8 +167,13 @@ func create(cmd *cobra.Command, args []string) error {
155167
}
156168
}
157169

170+
if cmd.Flag("hostname").Changed && !hostnameRegexp.MatchString(cmd.Flag("hostname").Value.String()) {
171+
return errors.New("invalid hostname")
172+
}
173+
158174
var container string
159175
var containerArg string
176+
var hostname = cmd.Flag("hostname").Value.String()
160177

161178
if len(args) != 0 {
162179
container = args[0]
@@ -176,14 +193,14 @@ func create(cmd *cobra.Command, args []string) error {
176193
return err
177194
}
178195

179-
if err := createContainer(container, image, release, true); err != nil {
196+
if err := createContainer(container, image, release, hostname, true); err != nil {
180197
return err
181198
}
182199

183200
return nil
184201
}
185202

186-
func createContainer(container, image, release string, showCommandToEnter bool) error {
203+
func createContainer(container, image, release string, hostname string, showCommandToEnter bool) error {
187204
if container == "" {
188205
panic("container not specified")
189206
}
@@ -196,6 +213,10 @@ func createContainer(container, image, release string, showCommandToEnter bool)
196213
panic("release not specified")
197214
}
198215

216+
if hostname == "" {
217+
hostname = "toolbox"
218+
}
219+
199220
enterCommand := getEnterCommand(container)
200221

201222
logrus.Debugf("Checking if container %s already exists", container)
@@ -410,7 +431,7 @@ func createContainer(container, image, release string, showCommandToEnter bool)
410431
createArgs = append(createArgs, xdgRuntimeDirEnv...)
411432

412433
createArgs = append(createArgs, []string{
413-
"--hostname", "toolbox",
434+
"--hostname", hostname,
414435
"--ipc", "host",
415436
"--label", "com.github.containers.toolbox=true",
416437
}...)

src/cmd/run.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ func runCommand(container string,
206206
return nil
207207
}
208208

209-
if err := createContainer(container, image, release, false); err != nil {
209+
if err := createContainer(container, image, release, "", false); err != nil {
210210
return err
211211
}
212212
} else if containersCount == 1 && defaultContainer {

0 commit comments

Comments
 (0)