Docker Community Forums

Share and learn in the Docker community.

Resolved: Docker service link for Ruby on Rails application


(Sudhinnationstar) #1

We use a rails app that uses httparty to get data from another REST API. So, the code will be something like the following:

def get_service_response
  response = HTTParty.get("#{api_service_url}/foo/]"
end

def self.api_service_url
  ENV['SERVICE_URL']
end

We are running two HAProxies, one for the rails web and the other for the REST API. So, the Stackfile looks like below:

web:
  image: organization/web-image
  ports:
    - "3000:3000"
  tags:
    - deploy_tag
  links:
    - haproxy-service
  environment:
    SERVICE_URL: haproxy-service

service:
  image: organization/service-image
  ports:
    - "8080:8080"
  tags:
    - deploy_tag

haproxy-web:
  image: 'dockercloud/haproxy:latest'
  environment:
    - BACKEND_PORT=3000
    - BALANCE=roundrobin
  links:
    - web
  ports:
    - '80:80'
  tags:
    - deploy_tag

haproxy-service:
  image: 'dockercloud/haproxy:latest'
  environment:
    - BACKEND_PORT=8080
    - BALANCE=roundrobin
  links:
    - service
  ports:
    - "8000:8000"
  tags:
    - deploy_tag

However, my get_service_response method is failing with the following error:

Errno::ECONNREFUSED (Failed to open TCP connection to :80 (Connection refused - connect(2) for nil port 80)):

When I tried to curl from the terminal in the web container, I’m able to see a response from the service

echo $SERVICE_URL
# displays haproxy-service
curl $SERVICE_URL/foo
# I see the expected response

I’m wondering why the rails app isn’t able to evaluate the service link from the httparty call.


(Sudhinnationstar) #2

The workaround is to expose the service port publicly and access it from the service endpoint. But we don’t want to take this route as the API is yet to be hardened.


(Sudhinnationstar) #3

To override the base_uri in Httparty (in our case it was Nil), we need to start the path to query as http:// or https://. We fixed the above issue by setting our env variable as follows:

environment:
    SERVICE_URL: http://haproxy-service