Docker Compose - container receiving connection refused

Please note that links is a legacy feature of Docker now (see Docs).

When you declare multiple services within a single docker-compose file, they will actually be inside the same network (be it the default one or a user-defined one). The trick then is to use the name of your service rather than localhost. With your original example then:

version: '2'
services:
  api:
    image: api
    ports:
      - "8080:8080"
  sqs:
    image: pakohan/elasticmq

You simply need to make sure you're accessing sqs rather than localhost (your hostname for the sqs service should literally be: sqs)


Trying to trick the api container into connecting to sqs by telling it that it's actually connecting to localhost isn't going to work. You need to make sqs available to api under a name that doesn't already have a special meaning, like:

links:
    - sqs:original-name

or even:

links:
    - sqs

which makes sqs accessible to api under the name "sqs".

Then, of course, you need to tell api that it should connect to port 9324 of original-name or sqs instead of port 9324 of localhost. How you do that depends on what api actually is.