This is an automated email from the ASF dual-hosted git repository.

potiuk pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/airflow.git


The following commit(s) were added to refs/heads/main by this push:
     new d8c0e3009a adding docker port expose capability (#30730)
d8c0e3009a is described below

commit d8c0e3009a649ce057595539b96a566b7faa5584
Author: Bowrna <[email protected]>
AuthorDate: Wed May 17 14:33:21 2023 +0530

    adding docker port expose capability (#30730)
---
 airflow/providers/docker/operators/docker.py    | 10 ++++++++++
 tests/providers/docker/operators/test_docker.py | 18 ++++++++++++++++++
 2 files changed, 28 insertions(+)

diff --git a/airflow/providers/docker/operators/docker.py 
b/airflow/providers/docker/operators/docker.py
index 4443956f2f..de3ab20b08 100644
--- a/airflow/providers/docker/operators/docker.py
+++ b/airflow/providers/docker/operators/docker.py
@@ -159,6 +159,10 @@ class DockerOperator(BaseOperator):
     :param skip_on_exit_code: If task exits with this exit code, leave the task
         in ``skipped`` state (default: None). If set to ``None``, any non-zero
         exit code will be treated as a failure.
+    :param port_bindings: Publish a container's port(s) to the host. It is a
+        dictionary of value where the key indicates the port to open inside 
the container
+        and value indicates the host port that binds to the container port.
+        Incompatible with ``host`` in ``network_mode``.
     """
 
     template_fields: Sequence[str] = ("image", "command", "environment", 
"env_file", "container_name")
@@ -217,6 +221,7 @@ class DockerOperator(BaseOperator):
         ipc_mode: str | None = None,
         skip_exit_code: int | None = None,
         skip_on_exit_code: int | Container[int] | None = None,
+        port_bindings: dict | None = None,
         **kwargs,
     ) -> None:
         super().__init__(**kwargs)
@@ -293,6 +298,9 @@ class DockerOperator(BaseOperator):
             if skip_on_exit_code
             else []
         )
+        self.port_bindings = port_bindings or {}
+        if self.port_bindings and self.network_mode == "host":
+            raise ValueError("Port bindings is not supported in the host 
network mode")
 
     @cached_property
     def hook(self) -> DockerHook:
@@ -359,6 +367,7 @@ class DockerOperator(BaseOperator):
             command=self.format_command(self.command),
             name=self.container_name,
             environment={**env_file_vars, **self.environment, 
**self._private_environment},
+            ports=list(self.port_bindings),
             host_config=self.cli.create_host_config(
                 auto_remove=False,
                 mounts=target_mounts,
@@ -367,6 +376,7 @@ class DockerOperator(BaseOperator):
                 dns=self.dns,
                 dns_search=self.dns_search,
                 cpu_shares=int(round(self.cpus * 1024)),
+                port_bindings=self.port_bindings,
                 mem_limit=self.mem_limit,
                 cap_add=self.cap_add,
                 extra_hosts=self.extra_hosts,
diff --git a/tests/providers/docker/operators/test_docker.py 
b/tests/providers/docker/operators/test_docker.py
index f1e358d30a..f0c301c29b 100644
--- a/tests/providers/docker/operators/test_docker.py
+++ b/tests/providers/docker/operators/test_docker.py
@@ -225,6 +225,7 @@ class TestDockerOperator:
             working_dir="/container/path",
             tty=True,
             hostname=TEST_CONTAINER_HOSTNAME,
+            ports=[],
         )
         self.client_mock.create_host_config.assert_called_once_with(
             mounts=[
@@ -244,6 +245,7 @@ class TestDockerOperator:
             device_requests=[DeviceRequest(count=-1, capabilities=[["gpu"]])],
             log_config=LogConfig(config={"max-size": "10m", "max-file": "5"}),
             ipc_mode=None,
+            port_bindings={},
         )
         
self.tempdir_mock.assert_called_once_with(dir=TEST_HOST_TEMP_DIRECTORY, 
prefix="airflowtmp")
         self.client_mock.images.assert_called_once_with(name=TEST_IMAGE)
@@ -295,6 +297,7 @@ class TestDockerOperator:
             working_dir="/container/path",
             tty=True,
             hostname=TEST_CONTAINER_HOSTNAME,
+            ports=[],
         )
         self.client_mock.create_host_config.assert_called_once_with(
             mounts=[
@@ -313,6 +316,7 @@ class TestDockerOperator:
             device_requests=None,
             log_config=LogConfig(config={}),
             ipc_mode=None,
+            port_bindings={},
         )
         self.tempdir_mock.assert_not_called()
         self.client_mock.images.assert_called_once_with(name=TEST_IMAGE)
@@ -385,6 +389,7 @@ class TestDockerOperator:
                     working_dir="/container/path",
                     tty=True,
                     hostname=None,
+                    ports=[],
                 ),
                 call(
                     command="env",
@@ -397,6 +402,7 @@ class TestDockerOperator:
                     working_dir="/container/path",
                     tty=True,
                     hostname=None,
+                    ports=[],
                 ),
             ]
         )
@@ -420,6 +426,7 @@ class TestDockerOperator:
                     device_requests=None,
                     log_config=LogConfig(config={}),
                     ipc_mode=None,
+                    port_bindings={},
                 ),
                 call(
                     mounts=[
@@ -438,6 +445,7 @@ class TestDockerOperator:
                     device_requests=None,
                     log_config=LogConfig(config={}),
                     ipc_mode=None,
+                    port_bindings={},
                 ),
             ]
         )
@@ -495,6 +503,7 @@ class TestDockerOperator:
             working_dir="/container/path",
             tty=True,
             hostname=None,
+            ports=[],
         )
         
stringio_mock.assert_called_once_with("UNIT=FILE\nPRIVATE=FILE\nVAR=VALUE")
         
self.dotenv_mock.assert_called_once_with(stream="UNIT=FILE\nPRIVATE=FILE\nVAR=VALUE")
@@ -690,3 +699,12 @@ class TestDockerOperator:
         assert "host_config" in self.client_mock.create_container.call_args[1]
         assert "privileged" in self.client_mock.create_host_config.call_args[1]
         assert privileged is 
self.client_mock.create_host_config.call_args[1]["privileged"]
+
+    def test_port_bindings(self):
+        port_bindings = {8000: 8080}
+        operator = DockerOperator(task_id="test", image="test", 
port_bindings=port_bindings)
+        operator.execute(None)
+        self.client_mock.create_container.assert_called_once()
+        assert "host_config" in self.client_mock.create_container.call_args[1]
+        assert "port_bindings" in 
self.client_mock.create_host_config.call_args[1]
+        assert port_bindings == 
self.client_mock.create_host_config.call_args[1]["port_bindings"]

Reply via email to