本文介绍了Docker,无法到达“rails服务器”从localhost开发:3000使用docker标志-p 3000:3000的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正试图用轨道码头工具,在一个容器内建立整个栈。我的目标是使用一个带有runit作为进程管理器的nginx / memcached / unicorn / rails / postgres堆栈。到目前为止,我已经安装了我的应用程序,红宝石和Postgres。在这一点上,我试图测试一切是否正常工作,然后再转到nginx / memcached / unicorn。

这是我的dockerfile:

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

在该容器的内部,我设置postgresql作为后台作业,并运行rails

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
....

但正如标题所示,我无法从localhost连接:3000。
我也试过

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


  • 尝试从0.0.0.0:3000连接

  • 离开-p 3000:3000并尝试从docker文件中的 container_ip :3000

  • EXPOSE 3000连接,使用docker ps查找映射端口并尝试本地主机:
  • 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

另外我不知道是否重要,但是我在OSX上使用boot2docker

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

推荐答案

这实际上是由boot2docker引起的。有关详细信息,请参阅此页面:

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

TLDR

端口转移仅适用于目前在boot2docker上的一个黑客攻击。您需要运行以下命令(并保持运行):

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

其中80是容器上的暴露端口,8080是要从主机上访问的端口。

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

这篇关于Docker,无法到达“rails服务器”从localhost开发:3000使用docker标志-p 3000:3000的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 04:50