title: docker-compose: connection refused between containers, but service accessible from host
body: <p>TL;DR: How do I have to change my below <code>docker-compose.yml</code> in order to allow one container to use a service of another over a custom (non-standard) port?</p>
  
  <p>I have a pretty common setup: containers for a web app (Padrino [Ruby]), Postgres, Redis, and a queueing framework (Sidekiq). The web app comes with its custom Dockerfile, the remaining services come either from standard images (Postgres, Redis), or mount the data from the web app (Sidekiq). They are ties together via the following <code>docker-compose.yml</code>:</p>
  
  <pre><code>version: '2'
  
  services:
    web:
      build: .
      command: 'bundle exec puma -C config/puma.rb'
      volumes:
        - .:/myapp
      ports:
        - "9000:3000"
      depends_on:
        - postgres
        - redis
  
    sidekiq:
      build: .
      command: 'bundle exec sidekiq -C config/sidekiq.yml -r ./config/boot.rb'
      volumes:
        - .:/myapp
      depends_on:
        - postgres
        - redis
  
    postgres:
      image: postgres:9.5
      environment:
        POSTGRES_USER: my-postgres-user
        POSTGRES_PASSWORD: my-postgres-pass
      ports:
        - '9001:5432'
      volumes:
        - 'postgres:/var/lib/postgresql/data'
  
    redis:
      image: redis
      ports:
        - '9002:6379'
      volumes:
        - 'redis:/var/lib/redis/data'
  
  volumes:
    redis:
    postgres:
  </code></pre>
  
  <p>One key point to notice here is that I am exposing the containers services on <em>non-standard</em> ports (9000-9002). </p>
  
  <p>If I start the setup with <code>docker-compose up</code>, the Redis and Postgres containers come up fine, but the containers for the web app and Sidekiq fail since they can't connect to Redis at <code>redis:9002</code>. Remarkably enough, the same setup <em>works</em> if I use 6379 (the standard Redis port) instead of 9002.</p>
  
  <p><code>docker ps</code> also looks fine afaik:</p>
  
  <pre><code>CONTAINER ID        IMAGE                   COMMAND                  CREATED                  STATUS              PORTS                              NAMES
  9148566c2509        redis                   "docker-entrypoint.sh"   Less than a second ago   Up About a minute   0.0.0.0:9002-&gt;6379/tcp             rubydockerpadrino_redis_1
  e6d47321c939        postgres:9.5            "/docker-entrypoint.s"   Less than a second ago   Up About a minute   0.0.0.0:9001-&gt;5432/tcp             rubydockerpadrino_postgres_1
  </code></pre>
  
  <p>What's even more confusing: I can access the Redis container <em>from the host</em> via <code>redis-cli -h localhost -p 9002 -n 0</code>, but the web app and Sidekiq containers fail to establish a connection.</p>
  
  <p>I am using this docker version on MacOS:
  <code>Docker version 1.12.3, build 6b644ec, experimental</code></p>
  
  <p>Any ideas what I am doing wrong? I'd appreciate any hint how to get my setup running.</p>