diff --git a/docs/Extensions.md b/docs/Extensions.md index d8e101d..fd7941f 100644 --- a/docs/Extensions.md +++ b/docs/Extensions.md @@ -32,6 +32,18 @@ For explanations of these extensions, please refer to the [Podman Documentation] The following extension keys are available under network configuration: * `x-podman.disable-dns` - Disable the DNS plugin for the network when set to 'true'. +* `x-podman.dns` - Set nameservers for the network using supplied addresses (cannot be used with x-podman.disable-dns`). + +For example, the following docker-compose.yml allows all containers on the same network to use the +specified nameservers: +```yml +version: "3" +network: + my_network: + x-podman.dns: + - "10.1.2.3" + - "10.1.2.4" +``` For explanations of these extensions, please refer to the [Podman network create command Documentation](https://docs.podman.io/en/latest/markdown/podman-network-create.1.html). diff --git a/newsfragments/x-podman.dns.feature b/newsfragments/x-podman.dns.feature new file mode 100644 index 0000000..4adb995 --- /dev/null +++ b/newsfragments/x-podman.dns.feature @@ -0,0 +1 @@ +- Add support for 'x-podman.dns' to allow setting DNS nameservers for defined networks. diff --git a/podman_compose.py b/podman_compose.py index d506802..5a668c8 100755 --- a/podman_compose.py +++ b/podman_compose.py @@ -835,6 +835,11 @@ def get_network_create_args(net_desc, proj_name, net_name): args.append("--ipv6") if net_desc.get("x-podman.disable_dns"): args.append("--disable-dns") + if net_desc.get("x-podman.dns"): + args.extend(( + "--dns", + ",".join(norm_as_list(net_desc.get("x-podman.dns"))), + )) if isinstance(ipam_config_ls, dict): ipam_config_ls = [ipam_config_ls] diff --git a/tests/unit/test_get_network_create_args.py b/tests/unit/test_get_network_create_args.py index 53aa63d..8951138 100644 --- a/tests/unit/test_get_network_create_args.py +++ b/tests/unit/test_get_network_create_args.py @@ -225,3 +225,53 @@ class TestGetNetworkCreateArgs(unittest.TestCase): ] args = get_network_create_args(net_desc, proj_name, net_name) self.assertEqual(args, expected_args) + + def test_dns_string(self): + net_desc = { + "labels": [], + "internal": False, + "driver": None, + "driver_opts": {}, + "ipam": {"config": []}, + "enable_ipv6": False, + "x-podman.dns": "192.168.1.2", + } + proj_name = "test_project" + net_name = "test_network" + expected_args = [ + "create", + "--label", + f"io.podman.compose.project={proj_name}", + "--label", + f"com.docker.compose.project={proj_name}", + "--dns", + "192.168.1.2", + net_name, + ] + args = get_network_create_args(net_desc, proj_name, net_name) + self.assertEqual(args, expected_args) + + def test_dns_list(self): + net_desc = { + "labels": [], + "internal": False, + "driver": None, + "driver_opts": {}, + "ipam": {"config": []}, + "enable_ipv6": False, + "x-podman.dns": ["192.168.1.2", "192.168.1.3"], + } + proj_name = "test_project" + net_name = "test_network" + expected_args = [ + "create", + "--label", + f"io.podman.compose.project={proj_name}", + "--label", + f"com.docker.compose.project={proj_name}", + "--dns", + "192.168.1.2,192.168.1.3", + net_name, + ] + args = get_network_create_args(net_desc, proj_name, net_name) + self.assertEqual(args, expected_args)