diff --git a/changelogs/fragments/53905-docker_image-proxy-config.yml b/changelogs/fragments/53905-docker_image-proxy-config.yml new file mode 100644 index 00000000000..13b88089c81 --- /dev/null +++ b/changelogs/fragments/53905-docker_image-proxy-config.yml @@ -0,0 +1,2 @@ +minor_changes: +- "docker_image - add option ``build.use_proxy_config`` to pass proxy config from the docker client configuration to the container while building." diff --git a/lib/ansible/modules/cloud/docker/docker_image.py b/lib/ansible/modules/cloud/docker/docker_image.py index 94b7bb5adc6..86fcc726c0c 100644 --- a/lib/ansible/modules/cloud/docker/docker_image.py +++ b/lib/ansible/modules/cloud/docker/docker_image.py @@ -118,6 +118,13 @@ options: description: - CPUs in which to allow execution, e.g., "0-3", "0,1". type: str + use_config_proxy: + description: + - If set to `yes` and a proxy configuration is specified in the docker client configuration + (by default C($HOME/.docker/config.json)), the corresponding environment variables will + be set in the container being built. + - Needs Docker SDK for Python >= 3.7.0. + type: bool version_added: "2.8" archive_path: description: @@ -454,6 +461,7 @@ class ImageManager(DockerBaseClass): self.http_timeout = build.get('http_timeout') self.push = parameters.get('push') self.buildargs = build.get('args') + self.use_config_proxy = build.get('use_config_proxy') # If name contains a tag, it takes precedence over tag parameter. if not is_image_name_id(self.name): @@ -711,6 +719,12 @@ class ImageManager(DockerBaseClass): params['cache_from'] = self.cache_from if self.network: params['network_mode'] = self.network + if self.use_config_proxy: + params['use_config_proxy'] = self.use_config_proxy + # Due to a bug in docker-py, it will crash if + # use_config_proxy is True and buildargs is None + if 'buildargs' not in params: + params['buildargs'] = {} for line in self.client.build(**params): # line = json.loads(line) @@ -776,6 +790,7 @@ def main(): pull=dict(type='bool'), rm=dict(type='bool', default=True), args=dict(type='dict'), + use_config_proxy=dict(type='bool'), )), archive_path=dict(type='path'), container_limits=dict(type='dict', options=dict( @@ -811,14 +826,18 @@ def main(): ] def detect_build_cache_from(client): - return bool(client.params['build'] and client.params['build']['cache_from'] is not None) + return client.params['build'] and client.params['build']['cache_from'] is not None def detect_build_network(client): - return bool(client.params['build'] and client.params['build']['network'] is not None) + return client.params['build'] and client.params['build']['network'] is not None + + def detect_use_config_proxy(client): + return client.params['build'] and client.params['build']['use_config_proxy'] is not None option_minimal_versions = dict() option_minimal_versions["build.cache_from"] = dict(docker_py_version='2.1.0', docker_api_version='1.25', detect_usage=detect_build_cache_from) option_minimal_versions["build.network"] = dict(docker_py_version='2.4.0', docker_api_version='1.25', detect_usage=detect_build_network) + option_minimal_versions["build.use_config_proxy"] = dict(docker_py_version='3.7.0', detect_usage=detect_use_config_proxy) client = AnsibleDockerClient( argument_spec=argument_spec,