Docker, can't reach “rails server” development from localhost:3000 using docker flag -p 3000:3000
我尝试使用带轨道的Docker,在一个容器中构建一个完整的堆栈。我的最终目标是拥有一个nginx/memcached/unicorn/rails/postgres堆栈,runit作为流程管理器。到目前为止,我已经安装了我的应用程序Ruby和Postgres。现在,我正在尝试在转到nginx/memcached/unicorn之前测试是否一切正常。这是我的文件:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | 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 |
然后我用以下命令创建一个容器
1 | docker run -it -p 3000:3000 *dockerfile_image* /bin/bash -l |
在容器内部,我将PostgreSQL设置为后台作业并运行Rails
1 2 3 4 5 6 7 8 9 10 11 12 13 | $ 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 .... |
但正如标题所述,我无法从localhost:3000连接。我也试过了
- 正在尝试从0.0.0.0:3000连接
- 离开-p 3000:3000并尝试从容器连接ip:3000
- 在dockerfile中暴露3000,使用docker ps查找映射端口并尝试localhost:mapped_port
另外,我不知道这是否重要,但我正在OSX上使用boot2docker。
这实际上是由boot2docker引起的。有关详细信息,请参阅此页面:https://github.com/boot2docker/boot2docker/blob/master/doc/workarounds.md
TLDR
PortForwarding目前只适用于boot2docker上的黑客。您需要运行以下命令(并保持其运行):
1 | boot2docker ssh -L 8080:localhost:80 |
其中80是容器上的暴露端口,8080是您希望从主机上访问它的端口。