title: Docker container fails to connect to second container when running under Compose
body: <p>I have two processes that I have containerised as container1 and container2. I would like to run both of these together using Docker Compose. container1 should connect to container2. This does not currently work and I cannot figure out the problem.</p>
  
  <p>Some test scenarios I have tried...</p>
  
  <p>Run container2 using Docker Compose: </p>
  
  <pre><code>version: '2'
  services:
    container2:
      image: container2
      ports:
        - "4003:4003"
  </code></pre>
  
  <p>Now run container1 source code from outside Docker and point to 127.0.0.1:4003 - connects ok.</p>
  
  <p>Or run container2 as above; note the IP address, then run container1 also from Compose, referencing the IP directly - also connects ok:</p>
  
  <pre><code>version: '2'
  services:
    container1:
      image: container1
      environment:
        - HOST=172.18.0.2
        - PORT=4003
  </code></pre>
  
  <p>But when I place them both in the same <code>docker-compose.yml</code> file, container1 never connects to container2, but can ping it by name - what gives?</p>
  
  <pre><code>version: '2'
  services:
    container1:
      image: container1
      depends_on:
        - container2
      environment:
        - HOST=container2
        - PORT=4003
    container2:
      image: container2
      ports:
        - "4003:4003"    
  </code></pre>
  
  <p>Edit: it appears that there is a problem with using the container alias, as this also works:</p>
  
  <pre><code>version: '2'
  services:
    container1:
      image: container1
      depends_on:
        - container2
      environment:
        - HOST=172.18.0.2
        - PORT=4003
    container2:
      image: container2
      ports:
        - "4003:4003"  
  </code></pre>
  
  <p>A further piece of the puzzle: internally container2 is using <code>socat</code> to expose the port the app is listening on:</p>
  
  <pre><code>echo "Forking :::4001 onto 0.0.0.0:4003\n"
  socat TCP-LISTEN:4003,fork TCP:127.0.0.1:4001 
  </code></pre>
  
  <p>Seems that this doesn't work with the container alias for some reason?</p>