Skip to content

Commit 2de671c

Browse files
committed
common: add support for default_host_ips in containers.conf
This adds support for configuring default host IPs via containers.conf to bind published container ports to when no host IP is explicitly specified (e.g. -p 8000:8000). If multiple IPs are specified, separate port mapping for each of the specified IP would be created. For instance, setting this to `["127.0.0.1", "::1"]` and port specified as `-p 8080:80` will result into two port mappings in podman-- `127.0.0.1:8080:80` and `[::1]:8080:80`. Note that explicit host IP still overrides the default option set in containers.conf. Refers containers/podman#27186 Signed-off-by: Danish Prakash <contact@danishpraka.sh>
1 parent fb1cbee commit 2de671c

File tree

6 files changed

+52
-0
lines changed

6 files changed

+52
-0
lines changed

common/docs/containers.conf.5.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -525,6 +525,17 @@ run on the machine.
525525
A list of default pasta options that should be used running pasta.
526526
It accepts the pasta cli options, see pasta(1) for the full list of options.
527527

528+
**default_host_ips**=[]
529+
530+
The default host IPs to bind published container ports to when no host IP
531+
is explicitly specified in the `-p` flag (e.g., `-p 8000:8000`). If empty, the default
532+
behavior is to bind to all network interfaces (`0.0.0.0` for IPv4 and `::` for IPv6). If multiple IPs are specified,
533+
separate port mapping for each of the specified IP would be created. For instance, setting
534+
this to `["127.0.0.1", "::1"]` and port specified as `-p 8080:80` will result into two
535+
port mappings in podman, `127.0.0.1:8080:80` and `[::1]:8080:80`.
536+
Note that explicitly specifying a host IP in the `-p` flag (e.g., `-p 192.168.1.10:8000:8000`)
537+
will always override this default.
538+
528539
## ENGINE TABLE
529540
The `engine` table contains configuration options used to set up container engines such as Podman and Buildah.
530541

common/pkg/config/config.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -636,6 +636,13 @@ type NetworkConfig struct {
636636
// PastaOptions contains a default list of pasta(1) options that should
637637
// be used when running pasta.
638638
PastaOptions attributedstring.Slice `toml:"pasta_options,omitempty"`
639+
640+
// DefaultHostIPs is the default host IPs to bind published container ports
641+
// to when no host IP is explicitly specified in the -p flag (e.g., -p 80:80).
642+
// If empty, the default behavior is to bind to all interfaces (0.0.0.0).
643+
// If multiple IPs are specified, separate port mapping for each of the specified
644+
// IP would be created.
645+
DefaultHostIPs attributedstring.Slice `toml:"default_host_ips,omitempty"`
639646
}
640647

641648
type SubnetPool struct {

common/pkg/config/config_local_test.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,18 @@ var _ = Describe("Config Local", func() {
142142
gomega.Expect(config2.Network.PastaOptions.Get()).To(gomega.Equal([]string{"-t", "auto"}))
143143
})
144144

145+
It("parse default_host_ips", func() {
146+
// Given
147+
config, err := newLocked(&Options{}, &paths{})
148+
gomega.Expect(err).ToNot(gomega.HaveOccurred())
149+
gomega.Expect(config.Network.DefaultHostIPs.Get()).To(gomega.BeEmpty())
150+
// When
151+
config2, err := newLocked(&Options{}, &paths{etc: "testdata/containers_default.conf"})
152+
// Then
153+
gomega.Expect(err).ToNot(gomega.HaveOccurred())
154+
gomega.Expect(config2.Network.DefaultHostIPs.Get()).To(gomega.Equal([]string{"127.0.0.1", "::1"}))
155+
})
156+
145157
It("parse default_rootless_network_cmd", func() {
146158
// Given
147159
config, err := newLocked(&Options{}, &paths{})

common/pkg/config/containers.conf

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -445,6 +445,16 @@ default_sysctls = [
445445
#
446446
#pasta_options = []
447447

448+
# The default host IPs to bind published container ports to when no host IP
449+
# is explicitly specified in the -p flag (e.g., -p 8000:8000). If empty, the default
450+
# behavior is to bind to all network interfaces (0.0.0.0). If multiple IPs are specified,
451+
# separate port mapping for each of the specified IP would be created. For instance, setting
452+
# this to ["127.0.0.1", "::1"] and port specified as -p 8080:80 will result into two
453+
# port mappings in podman--127.0.0.1:8080:80 and [::1]:8080:80.
454+
# Note that explicitly specifying a host IP via -p will always override this.
455+
#
456+
#default_host_ips = []
457+
448458
[engine]
449459
# Index to the active service
450460
#

common/pkg/config/containers.conf-freebsd

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,16 @@ default_sysctls = [
335335
#
336336
#network_config_dir = "/usr/local/etc/cni/net.d/"
337337

338+
# The default host IPs to bind published container ports to when no host IP
339+
# is explicitly specified in the -p flag (e.g., -p 8000:8000). If empty, the default
340+
# behavior is to bind to all network interfaces (0.0.0.0). If multiple IPs are specified,
341+
# separate port mapping for each of the specified IP would be created. For instance, setting
342+
# this to ["127.0.0.1", "::1"] and port specified as -p 8080:80 will result into two
343+
# port mappings in podman--127.0.0.1:8080:80 and [::1]:8080:80.
344+
# Note that explicitly specifying a host IP via -p will always override this.
345+
#
346+
#default_host_ips = []
347+
338348
[engine]
339349
# Index to the active service
340350
#

common/pkg/config/testdata/containers_default.conf

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,8 @@ netavark_plugin_dirs = [
140140

141141
pasta_options = ["-t", "auto"]
142142

143+
default_host_ips = ["127.0.0.1", "::1"]
144+
143145
[engine]
144146

145147
add_compression = ["zstd", "zstd:chunked"]

0 commit comments

Comments
 (0)