Docker rails connection refused

I ve been playing with docker and fig for a rails application being built for scripted. This is my first time using docker in an actual project and I came across an issue that doesn’t readily have an answer out there even though in retrospect the answer was staring at me in the face.

Essentially after getting the containers built and running, I was not able to connect to the docker container at all. Everytime I tried connecting to the container, I would get a connection refused error.

~/P/r/schoolio ❯❯❯ boot2docker ip

The VM's Host only interface IP address is: 192.168.59.103

~/P/r/schoolio ❯❯❯ curl -i 192.168.59.103:3000
curl: (7) Failed to connect to 192.168.59.103 port 3000: Connection refused

My Dockerfile looks like

# Choose the official Ruby 2.1.2 image as our starting point
FROM ruby:2.1.2

# Run updates
RUN apt-get update -qq && apt-get install -y build-essential libpq-dev sudo openssl libreadline6 libreadline6-dev curl git-core zlib1g zlib1g-dev libssl-dev libyaml-dev libsqlite3-dev sqlite3 libxml2-dev libxslt-dev autoconf libc6-dev ncurses-dev automake libtool bison nodejs

RUN useradd -ms /bin/bash deployer
RUN echo "deployer:deployer" | chpasswd && adduser deployer sudo
RUN mkdir -p /home/deployer/schoolio && chown -R deployer:deployer /home/deployer/schoolio
ENV HOME /home/deployer
RUN echo "%sudo        ALL=NOPASSWD: ALL" >> /etc/sudoers
USER deployer
ENV DOCKER true

RUN ruby -ropenssl -e 'puts OpenSSL::X509::DEFAULT_CERT_FILE'

# Set up working directory
WORKDIR /home/deployer/schoolio

# RUN sudo gem update --system

# Set up gems
ADD Gemfile /home/deployer/schoolio/Gemfile
ADD Gemfile.lock /home/deployer/schoolio/Gemfile.lock
RUN sudo chown -R deployer /home/deployer/schoolio/*
RUN bundle install

ADD . /home/deployer/schoolio

My fig.yml looked like this:

db:
  image: postgres
  ports:
    - "5432"

# redis:
#   image: redis
#   ports:
#     - "6379"

web:
  build: .
  command: rails s
  volumes:
    - .:/home/deployer/schoolio
  ports:
    - "3000:3000"
  links:
    - db
    # - redis

When I tried to run, the server started fine but wouldnt let me connect to it at all.

~/P/r/schoolio ❯❯❯ fig up
Recreating schoolio_db_1...
Recreating schoolio_web_1...
Attaching to schoolio_db_1, schoolio_web_1
db_1  | LOG:  database system was shut down at 2014-12-07 13:50:32 UTC
db_1  | LOG:  autovacuum launcher started
db_1  | LOG:  database system is ready to accept connections
web_1 | => Booting Puma
web_1 | => Rails 4.2.0 application starting in development on http://localhost:3000
web_1 | => Run `rails server -h` for more startup options
web_1 | => Ctrl-C to shutdown server
web_1 | Puma 2.10.2 starting...
web_1 | * Min threads: 0, max threads: 16
web_1 | * Environment: development
web_1 | * Listening on tcp://localhost:3000

~/P/r/schoolio ❯❯❯ curl -i 192.168.59.103:3000
curl: (7) Failed to connect to 192.168.59.103 port 3000: Connection refused

Docker showed two containers running and an the correct ports

~/P/r/schoolio ❯❯❯ docker ps
CONTAINER ID        IMAGE                 COMMAND                CREATED             STATUS              PORTS                     NAMES
8e0b1fe500b8        schoolio_web:latest   "rails s"              14 seconds ago      Up 13 seconds       0.0.0.0:3000->3000/tcp    schoolio_web_1      
0283bd6147c6        postgres:9            "/docker-entrypoint.   16 seconds ago      Up 15 seconds       0.0.0.0:49153->5432/tcp   schoolio_db_1     

After searching far and wide, I stumbled upon this issue on github and that pretty much solved the issue. All that need to be changed was the rails server startup command. specifically in the fig.yml,

  command: bundle exec rails s -b 0.0.0.0

The problem was that rails was trying to listen on localhost while the server was running on 0.0.0.0 . Hope this helps someone else out there.

 

Ruby split ignores empty fields

Came across a little weirdness with the string split function in ruby today. Basically

"a,b,c".split(",") #=> ["a,b,c"] # this is correct
"a,b,c,,".split(",") #=> ["a", "b", "c"] # this is weird

I was expecting the second one to have 2 extra empty fields. After doing some digging, turns out there is a limit param.

If the limit parameter is omitted, trailing null fields are suppressed. If limit is a positive number, at most that number of fields will be returned (if limit is 1, the entire string is returned as the only entry in an array). If negative, there is no limit to the number of fields returned, and trailing null fields are not suppressed.

source: Class: String (ruby 2.1.4)

Essentially, the split function needs to be called with a limit the same way as you would in Scala.

"a,b,c,,".split(",",-1) #=> ["a", "b", "c", "", ""]