7

我正在尝试将 docker 与 rails 一起使用,在一个容器内构建整个堆栈。我的最终目标是拥有一个带有 runit 作为进程管理器的 nginx/memcached/unicorn/rails/postgres 堆栈。到目前为止,我已经安装了我的应用程序、ruby 和 postgres。在这一点上,我正在尝试在继续使用 nginx/memcached/unicorn 之前测试一切是否正常。
这是我的码头文件:

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

然后我使用以下命令创建一个容器

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

在该容器内,我将 postgresql 设置为后台作业并运行 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 连接。我也试过

  • 尝试从 0.0.0.0:3000 连接
  • 离开 -p 3000:3000 并尝试从container_ip :3000连接
  • EXPOSE 3000 在 dockerfile 中,使用 docker ps 查找映射端口并尝试 localhost: mapped_port

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

4

1 回答 1

12

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.

于 2014-05-02T10:10:10.570 回答