Repository: Kinto/kinto

Issue:
updated default kinto config breaks docker (kinto server not reacheable from host)
In e4e70fb, the default listen ip has been changed to 127.0.0.1.
I couldn't manage to export the port using docker, using this command from the [install documentation](http://kinto.readthedocs.io/en/latest/tutorials/install.html#environment-variables):
```
docker run --env-file kinto.env --link kinto_db:db -p 127.0.0.1:8888:8888 --name kinto_web croco/kinto:latest
```
For instance, when I do `curl http://127.0.0.1:8888` from the host I get `Recv failure: Connection reset by peer`.

When I reverted kinto.ini to listen on 0.0.0.0, curl from the host succeeds.

Maybe there is an environment variable to override the `host` entry in the `server:main` section? Then it would be fine, provided an updated documentation...

This is not visible with the published kinto/kinto-server image, since it's still 5.1. I built my own from master because I needed an armhf image.


Discussion:
#949 was the rational to change the default config (fails when no network).
Contrary to what I said in the description, it's visible in kinto/kinto-server:latest
I'm tempted to classify this as a regression, but I think listening on either 127.0.0.1 or 0.0.0.0 are equally "correct" in the general case. (See [this Pyramid issue](https://github.com/Pylons/pyramid/issues/489#issuecomment-4671393) for some heated discussion on this point.) And it doesn't seem like pserve respects any environment variables about host, and we don't do anything special with those variables. Given that, I think we should update `kinto init` to allow the user to specify what address to listen on, and update the Dockerfile to pass 0.0.0.0 to this argument.

Resolving patch:
diff --git a/kinto/__main__.py b/kinto/__main__.py
--- a/kinto/__main__.py
+++ b/kinto/__main__.py
@@ -57,6 +57,11 @@ def main(args=None):
                                    dest='backend',
                                    required=False,
                                    default=None)
+            subparser.add_argument('--host',
+                                   help='Host to listen() on.',
+                                   dest='host',
+                                   required=False,
+                                   default='127.0.0.1')
         elif command == 'migrate':
             subparser.add_argument('--dry-run',
                                    action='store_true',
@@ -114,7 +119,7 @@ def main(args=None):
                 except KeyError:
                     pass
 
-        init(config_file, backend)
+        init(config_file, backend, parsed_args['host'])
 
         # Install postgresql libraries if necessary
         if backend == "postgresql":
diff --git a/kinto/config/__init__.py b/kinto/config/__init__.py
--- a/kinto/config/__init__.py
+++ b/kinto/config/__init__.py
@@ -26,9 +26,10 @@ def render_template(template, destination, **kwargs):
             output.write(rendered)
 
 
-def init(config_file, backend):
+def init(config_file, backend, host='127.0.0.1'):
     values = {}
 
+    values['host'] = host
     values['secret'] = core_utils.random_bytes_hex(32)
 
     values['kinto_version'] = __version__


Test patch:
diff --git a/tests/test_config.py b/tests/test_config.py
--- a/tests/test_config.py
+++ b/tests/test_config.py
@@ -17,6 +17,7 @@ def test_transpose_parameters_into_template(self):
         template = "kinto.tpl"
         dest = tempfile.mktemp()
         config.render_template(template, dest,
+                               host='127.0.0.1',
                                secret='secret',
                                storage_backend='storage_backend',
                                cache_backend='cache_backend',
@@ -41,6 +42,7 @@ def test_create_destination_directory(self):
         dest = os.path.join(tempfile.mkdtemp(), 'config', 'kinto.ini')
 
         config.render_template("kinto.tpl", dest,
+                               host='127.0.0.1',
                                secret='secret',
                                storage_backend='storage_backend',
                                cache_backend='cache_backend',
@@ -68,6 +70,7 @@ def test_init_postgresql_values(self, mocked_render_template):
 
         postgresql_url = "postgres://postgres:postgres@localhost/postgres"
         self.assertDictEqual(kwargs, {
+            'host': '127.0.0.1',
             'secret': kwargs['secret'],
             'storage_backend': 'kinto.core.storage.postgresql',
             'cache_backend': 'kinto.core.cache.postgresql',
@@ -90,6 +93,7 @@ def test_init_redis_values(self, mocked_render_template):
 
         self.maxDiff = None  # See the full diff in case of error
         self.assertDictEqual(kwargs, {
+            'host': '127.0.0.1',
             'secret': kwargs['secret'],
             'storage_backend': 'kinto_redis.storage',
             'cache_backend': 'kinto_redis.cache',
@@ -109,6 +113,7 @@ def test_init_memory_values(self, mocked_render_template):
         self.assertEquals(args, ('kinto.tpl', 'kinto.ini'))
 
         self.assertDictEqual(kwargs, {
+            'host': '127.0.0.1',
             'secret': kwargs['secret'],
             'storage_backend': 'kinto.core.storage.memory',
             'cache_backend': 'kinto.core.cache.memory',
@@ -120,26 +125,11 @@ def test_init_memory_values(self, mocked_render_template):
             'config_file_timestamp': strftime('%a, %d %b %Y %H:%M:%S %z')
         })
 
-    def test_render_template_creates_directory_if_necessary(self):
-        temp_path = tempfile.mkdtemp()
-        destination = os.path.join(temp_path, 'config/kinto.ini')
-        config.render_template('kinto.tpl', destination, **{
-            'secret': "abcd-ceci-est-un-secret",
-            'storage_backend': 'kinto.core.storage.memory',
-            'cache_backend': 'kinto.core.cache.memory',
-            'permission_backend': 'kinto.core.permission.memory',
-            'storage_url': '',
-            'cache_url':  '',
-            'permission_url': '',
-            'kinto_version': '',
-            'config_file_timestamp': ''
-        })
-        self.assertTrue(os.path.exists(destination))
-
     def test_render_template_works_with_file_in_cwd(self):
         temp_path = tempfile.mkdtemp()
         os.chdir(temp_path)
         config.render_template('kinto.tpl', 'kinto.ini', **{
+            'host': '127.0.0.1',
             'secret': "abcd-ceci-est-un-secret",
             'storage_backend': 'kinto.core.storage.memory',
             'cache_backend': 'kinto.core.cache.memory',