diff --git a/cmd/nerdctl/container_run.go b/cmd/nerdctl/container_run.go index 6475f0e453f..1d873842629 100644 --- a/cmd/nerdctl/container_run.go +++ b/cmd/nerdctl/container_run.go @@ -57,7 +57,8 @@ import ( "github.com/containerd/nerdctl/pkg/referenceutil" "github.com/containerd/nerdctl/pkg/strutil" "github.com/containerd/nerdctl/pkg/taskutil" - dopts "github.com/docker/cli/opts" + dockercliopts "github.com/docker/cli/opts" + dockerops "github.com/docker/docker/opts" "github.com/opencontainers/runtime-spec/specs-go" "github.com/sirupsen/logrus" "github.com/spf13/cobra" @@ -707,10 +708,25 @@ func createContainer(ctx context.Context, cmd *cobra.Command, client *containerd return nil, nil, err } extraHosts = strutil.DedupeStrSlice(extraHosts) - for _, host := range extraHosts { - if _, err := dopts.ValidateExtraHost(host); err != nil { + for i, host := range extraHosts { + if _, err := dockercliopts.ValidateExtraHost(host); err != nil { return nil, nil, err } + parts := strings.SplitN(host, ":", 2) + // If the IP Address is a string called "host-gateway", replace this value with the IP address stored + // in the daemon level HostGateway IP config variable. + if parts[1] == dockerops.HostGatewayName { + gateway, err := cmd.Flags().GetString("host-gateway-ip") + logrus.Info("gateway: " + gateway) // TODO: remove in final PR + if err != nil { + return nil, nil, err + } + if gateway == "" { + return nil, nil, fmt.Errorf("unable to derive the IP value for host-gateway") + } + parts[1] = gateway + extraHosts[i] = fmt.Sprintf(`%s:%s`, parts[0], parts[1]) + } } internalLabels.extraHosts = extraHosts @@ -995,7 +1011,7 @@ func readKVStringsMapfFromLabel(cmd *cobra.Command) (map[string]string, error) { return nil, err } labelsFilePath = strutil.DedupeStrSlice(labelsFilePath) - labels, err := dopts.ReadKVStrings(labelsFilePath, labelsMap) + labels, err := dockercliopts.ReadKVStrings(labelsFilePath, labelsMap) if err != nil { return nil, err } diff --git a/cmd/nerdctl/flagutil.go b/cmd/nerdctl/flagutil.go index 54a035201bc..e64b4980033 100644 --- a/cmd/nerdctl/flagutil.go +++ b/cmd/nerdctl/flagutil.go @@ -70,6 +70,10 @@ func processRootCmdFlags(cmd *cobra.Command) (types.GlobalCommandOptions, error) if err != nil { return types.GlobalCommandOptions{}, err } + hostGatewayIP, err := cmd.Flags().GetString("host-gateway-ip") + if err != nil { + return types.GlobalCommandOptions{}, err + } return types.GlobalCommandOptions{ Debug: debug, DebugFull: debugFull, @@ -83,5 +87,6 @@ func processRootCmdFlags(cmd *cobra.Command) (types.GlobalCommandOptions, error) InsecureRegistry: insecureRegistry, HostsDir: hostsDir, Experimental: experimental, + HostGatewayIP: hostGatewayIP, }, nil } diff --git a/cmd/nerdctl/main.go b/cmd/nerdctl/main.go index 1bfe1c697a9..99757f23ee5 100644 --- a/cmd/nerdctl/main.go +++ b/cmd/nerdctl/main.go @@ -171,6 +171,7 @@ func initRootCmdFlags(rootCmd *cobra.Command, tomlPath string) (*pflag.FlagSet, rootCmd.PersistentFlags().StringSlice("hosts-dir", cfg.HostsDir, "A directory that contains /hosts.toml (containerd style) or /{ca.cert, cert.pem, key.pem} (docker style)") // Experimental enable experimental feature, see in https://github.com/containerd/nerdctl/blob/main/docs/experimental.md AddPersistentBoolFlag(rootCmd, "experimental", nil, nil, cfg.Experimental, "NERDCTL_EXPERIMENTAL", "Control experimental: https://github.com/containerd/nerdctl/blob/main/docs/experimental.md") + rootCmd.PersistentFlags().String("host-gateway-ip", cfg.HostGatewayIP, "IP address that the special 'host-gateway' string in --add-host resolves to. Defaults to the IP address of the default bridge") return aliasToBeInherited, nil } diff --git a/pkg/config/config.go b/pkg/config/config.go index 4e84cb26059..36ec17011f8 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -38,6 +38,7 @@ type Config struct { InsecureRegistry bool `toml:"insecure_registry"` HostsDir []string `toml:"hosts_dir"` Experimental bool `toml:"experimental"` + HostGatewayIP string `toml:"host_gateway_ip"` } // New creates a default Config object statically, @@ -56,5 +57,6 @@ func New() *Config { InsecureRegistry: false, HostsDir: ncdefaults.HostsDirs(), Experimental: true, + HostGatewayIP: "", } }