Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Docker Compose - container receiving connection refused

Using docker-compose, I'm trying to run my API on a publicly available port 8080 and an sqs service on a privately available port 9324. The API needs to be able to communicate with the sqs service. In order to do so, I have set up the following docker-compose.yml:

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

I have tried several iterations, including adding a link alias for the api:

links:
  - "sqs:localhost"

and exposing the port for sqs:

ports:
  - "9324:9324"

but neither seems to work. The API always gets a connection refused error when trying to communicate with the sqs service.

When the sqs port is publicly exposed an API running outside of docker is able to communicate just fine (so the sqs service is getting initialized properly).

Does anyone have any ideas as to how to solve this problem?

like image 747
cscan Avatar asked Sep 30 '16 00:09

cscan


1 Answers

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)

like image 98
user3681304 Avatar answered Sep 18 '22 14:09

user3681304