Repository: pantsbuild/pants

Issue:
Docker environment not passed to docker publish command
**Describe the bug**
The configured `[docker].env_vars` are not passed to `docker publish`.

**Pants version**
2.9.0.dev1

**OS**
Any

**Additional info**
As reported by chenkai036 on [Slack](https://pantsbuild.slack.com/archives/C046T6T9U/p1637248172462800?thread_ts=1637136003.393600&cid=C046T6T9U)



Discussion:

Resolving patch:
diff --git a/src/python/pants/backend/docker/goals/publish.py b/src/python/pants/backend/docker/goals/publish.py
--- a/src/python/pants/backend/docker/goals/publish.py
+++ b/src/python/pants/backend/docker/goals/publish.py
@@ -19,8 +19,9 @@
     PublishProcesses,
     PublishRequest,
 )
+from pants.engine.environment import Environment, EnvironmentRequest
 from pants.engine.process import InteractiveProcess
-from pants.engine.rules import collect_rules, rule
+from pants.engine.rules import Get, collect_rules, rule
 
 logger = logging.getLogger(__name__)
 
@@ -69,7 +70,8 @@ async def push_docker_images(
             ]
         )
 
-    process = docker.push_image(tags)
+    env = await Get(Environment, EnvironmentRequest(options.env_vars))
+    process = docker.push_image(tags, env)
     return PublishProcesses(
         [
             PublishPackages(
diff --git a/src/python/pants/backend/docker/util_rules/docker_binary.py b/src/python/pants/backend/docker/util_rules/docker_binary.py
--- a/src/python/pants/backend/docker/util_rules/docker_binary.py
+++ b/src/python/pants/backend/docker/util_rules/docker_binary.py
@@ -62,7 +62,9 @@ def build_image(
             cache_scope=ProcessCacheScope.PER_SESSION,
         )
 
-    def push_image(self, tags: tuple[str, ...]) -> Process | None:
+    def push_image(
+        self, tags: tuple[str, ...], env: Mapping[str, str] | None = None
+    ) -> Process | None:
         if not tags:
             return None
 
@@ -70,6 +72,7 @@ def push_image(self, tags: tuple[str, ...]) -> Process | None:
             argv=(self.path, "push", *tags),
             cache_scope=ProcessCacheScope.PER_SESSION,
             description=f"Pushing docker image {tags[0]}",
+            env=env,
         )
 
 


Test patch:
diff --git a/src/python/pants/backend/docker/goals/publish_test.py b/src/python/pants/backend/docker/goals/publish_test.py
--- a/src/python/pants/backend/docker/goals/publish_test.py
+++ b/src/python/pants/backend/docker/goals/publish_test.py
@@ -24,6 +24,7 @@
 from pants.engine.fs import EMPTY_DIGEST
 from pants.testutil.option_util import create_subsystem
 from pants.testutil.rule_runner import QueryRule, RuleRunner
+from pants.util.frozendict import FrozenDict
 
 
 @pytest.fixture
@@ -40,7 +41,6 @@ def rule_runner() -> RuleRunner:
     rule_runner.set_options(
         [],
         env_inherit={"PATH", "PYENV_ROOT", "HOME"},
-        # env={""},
     )
     rule_runner.write_files(
         {
@@ -157,3 +157,26 @@ def test_docker_push_registries(rule_runner: RuleRunner) -> None:
             )
         ),
     )
+
+
+def test_docker_push_env(rule_runner: RuleRunner) -> None:
+    rule_runner.set_options(
+        ["--docker-env-vars=DOCKER_CONFIG"],
+        env_inherit={"PATH", "PYENV_ROOT", "HOME"},
+        env={"DOCKER_CONFIG": "/etc/docker/custom-config"},
+    )
+    result, docker = run_publish(rule_runner, Address("src/default"))
+    assert len(result) == 1
+    assert_publish(
+        result[0],
+        ("default/default:latest",),
+        None,
+        process_assertion(
+            argv=(
+                docker.path,
+                "push",
+                "default/default:latest",
+            ),
+            env=FrozenDict({"DOCKER_CONFIG": "/etc/docker/custom-config"}),
+        ),
+    )