Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Docker, can't reach "rails server" development from localhost:3000 using docker flag -p 3000:3000

I'm trying to use docker with rails, building an entire stack inside one container. My endgoal is to have an nginx/memcached/unicorn/rails/postgres stack with runit as the process manager. So far I have my app, ruby, and postgres installed. At this point I'm trying to test if everything is working correctly before I move on to nginx/memcached/unicorn.
Here's my dockerfile:

FROM ubuntu:trusty

ADD . /app

# Update repos
RUN apt-get update

# General 
RUN apt-get install -y git curl software-properties-common python-software-properties ssh

# Install ruby (taken from https://gist.github.com/konklone/6662393)
RUN \curl -Lk https://get.rvm.io | bash -s stable
RUN /bin/bash -l -c "rvm requirements"
RUN /bin/bash -l -c "rvm install 2.1.1"
ENV PATH /usr/local/rvm/gems/ruby-2.1.1/bin:/usr/local/rvm/rubies/ruby-2.1.1/bin:$PATH
RUN gem install bundler --no-ri --no-rdoc

# Install nodejs (rails js runtime)
RUN apt-get install -y nodejs

# Install postgresql (adapted from https://wiki.postgresql.org/wiki/Apt)
RUN echo "deb http://apt.postgresql.org/pub/repos/apt/ $(lsb_release -sc)-pgdg main"
RUN curl https://www.postgresql.org/media/keys/ACCC4CF8.asc | apt-key add -
RUN apt-get update
RUN apt-get install -y postgresql-9.3 postgresql-client-9.3 postgresql-contrib-9.3

# Setup postgresql
RUN mkdir /pgsql && chown postgres /pgsql
USER postgres
ENV PATH $PATH:/usr/lib/postgresql/9.3/bin/
RUN initdb -E UTF8 -D /pgsql/data
USER root

# Needed for pg gem (from http://stackoverflow.com/a/20754173)
RUN apt-get install -y libpq-dev

Then I create a container with the following command

docker run -it -p 3000:3000 *dockerfile_image* /bin/bash -l

Inside of that container I set up postgresql as a background job and run rails s

$ sudo -u postgres /usr/lib/postgresql/9.3/bin/postgres -D /pgsql/data
^z
$ bg
$ sudo -u postgres /usr/bin/psql -c "create role *appname* login createdb"
$ cd /app
$ bundle && rake db:create db:migrate
$ rails s
=> Booting WEBrick
=> Rails 4.1.0 application starting in development on http://0.0.0.0:3000
=> Run `rails server -h` for more startup options
=> Notice: server is listening on all interfaces (0.0.0.0). Consider using 127.0.0.1 (--binding option)
=> Ctrl-C to shutdown server
....

But as the title states, I can't connect from localhost:3000. I've also tried

  • trying to connect from 0.0.0.0:3000
  • leaving the -p 3000:3000 and trying to connect from container_ip:3000
  • EXPOSE 3000 in the dockerfile, using docker ps to find the mapped port and trying localhost:mapped_port

Also I don't know if it matters but I'm using boot2docker on OSX.

like image 219
nhjk Avatar asked May 02 '14 06:05

nhjk


1 Answers

This was actually caused by boot2docker. See this page for more info: https://github.com/boot2docker/boot2docker/blob/master/doc/WORKAROUNDS.md

TLDR

Portforwarding only works with a hack on boot2docker currently. You need to run the following command (and keep it running):

boot2docker ssh -L 8080:localhost:80

Where 80 is the exposed port on the container and 8080 is the port you want to access it from on your host.

like image 162
nhjk Avatar answered Oct 16 '22 15:10

nhjk