Repository: ansible/ansible

Issue:
docker_container: Add support for `--init` option
<!---
Verify first that your issue/request is not already reported on GitHub.
Also test if the latest release, and master branch are affected too.
-->

##### ISSUE TYPE
<!--- Pick one below and delete the rest: -->
 - Feature Idea

##### COMPONENT NAME
<!--- Name of the module/plugin/task/feature -->
docker_container

##### ANSIBLE VERSION
<!--- Paste verbatim output from "ansible --version" between quotes below -->
```
v2.4
```

##### SUMMARY
<!--- Explain the problem briefly -->
I hereby propose to support the docker `--init` option from `docker run`. It was added in docker v1.13 and does the following:  
```
       --init
          Run an init inside the container that forwards signals and reaps processes
```
Should be easy to implement.


Discussion:
cc @cove @dusdanig @jctanner @kassiansun @softzilla @zfil
[click here for bot help](https://github.com/ansible/ansibullbot/blob/master/ISSUE_HELP.md)
<!--- boilerplate: notify --->
cc @chouseknecht @joshuaconner
[click here for bot help](https://github.com/ansible/ansibullbot/blob/master/ISSUE_HELP.md)
<!--- boilerplate: notify --->
cc @ThomasSteinbach
[click here for bot help](https://github.com/ansible/ansibullbot/blob/master/ISSUE_HELP.md)
<!--- boilerplate: notify --->

Resolving patch:
diff --git a/lib/ansible/module_utils/docker_common.py b/lib/ansible/module_utils/docker_common.py
--- a/lib/ansible/module_utils/docker_common.py
+++ b/lib/ansible/module_utils/docker_common.py
@@ -162,6 +162,10 @@ def __init__(self, argument_spec=None, supports_check_mode=False, mutually_exclu
         except Exception as exc:
             self.fail("Error connecting: %s" % exc)
 
+        docker_api_version = self.version()["ApiVersion"]
+        if self.module.params.get("init") and LooseVersion(docker_api_version) < LooseVersion("1.25"):
+            self.fail("docker API version is %s. Minimum version required is 1.25 to set init option." % (docker_api_version,))
+
     def log(self, msg, pretty_print=False):
         pass
         # if self.debug:
diff --git a/lib/ansible/modules/cloud/docker/docker_container.py b/lib/ansible/modules/cloud/docker/docker_container.py
--- a/lib/ansible/modules/cloud/docker/docker_container.py
+++ b/lib/ansible/modules/cloud/docker/docker_container.py
@@ -131,6 +131,13 @@
     description:
       - Repository path and tag used to create the container. If an image is not found or pull is true, the image
         will be pulled from the registry. If no tag is included, 'latest' will be used.
+  init:
+    description:
+      - Run an init inside the container that forwards signals and reaps processes.
+        This option requires Docker API 1.25+.
+    type: bool
+    default: 'no'
+    version_added: "2.6"
   interactive:
     description:
       - Keep stdin open after a container is launched, even if not attached.
@@ -650,6 +657,7 @@ def __init__(self, client):
         self.hostname = None
         self.ignore_image = None
         self.image = None
+        self.init = None
         self.interactive = None
         self.ipc_mode = None
         self.keep_volumes = None
@@ -891,7 +899,8 @@ def _host_config(self):
             group_add='groups',
             devices='devices',
             pid_mode='pid_mode',
-            tmpfs='tmpfs'
+            tmpfs='tmpfs',
+            init='init'
         )
 
         if HAS_DOCKER_PY_2 or HAS_DOCKER_PY_3:
@@ -1992,6 +2001,7 @@ def main():
         hostname=dict(type='str'),
         ignore_image=dict(type='bool', default=False),
         image=dict(type='str'),
+        init=dict(type='bool', default=False),
         interactive=dict(type='bool', default=False),
         ipc_mode=dict(type='str'),
         keep_volumes=dict(type='bool', default=True),


Test patch: