title: Is there a way to expose a docker container port bound to 127.0.0.1 to host?
body: <p>I run a service inside a container that binds to <code>127.0.0.1:8888</code>.<br>
  I want to expose this port to the host.<br>
  Does docker-compose support this?</p>
  
  <p>I tried the following in <code>docker-compose.yml</code> but did not work.</p>
  
  <pre><code>expose: 
    - "8888"
  ports:
    - "8888:8888"
  </code></pre>
  
  <p>P.S. Binding the service to 0.0.0.0 inside the container is not possible in my case.</p>
  
  <hr>
  
  <p>UPDATE: Providing a simple example:</p>
  
  <p><code>docker-compose.yml</code></p>
  
  <pre><code>version: '3'
  services:  
    myservice:
      expose: 
        - "8888"
      ports:
        - "8888:8888"
      build: .
  </code></pre>
  
  <p><code>Dockerfile</code></p>
  
  <pre><code>FROM centos:7
  RUN yum install -y nmap-ncat
  CMD ["nc", "-l", "-k", "localhost", "8888"]
  </code></pre>
  
  <p>Commands: </p>
  
  <pre><code>$&gt; docker-compose up --build
  $&gt; # Starting test1_myservice_1 ... done
  $&gt; # Attaching to test1_myservice_1
  
  $&gt; nc -v -v localhost 8888
  $&gt; # Connection to localhost 8888 port [tcp/*] succeeded!
  TEST
  $&gt;
  </code></pre>
  
  <p>After inputing <code>TEST</code> in the console <strong>the connection is closed</strong>, which means <strong>the port is not really exposed</strong>, despite the initial success message. The same issue occurs with with my real service.<br>
  But If I bind to 0.0.0.0 (instead of localhost) inside the container everything works fine.</p>