我正在尝试使用带有rails的docker,在一个容器内构建一个完整的堆栈.我的结论是使用runit作为进程管理器的Nginx / memcached / unicorn / rails / postgres堆栈.到目前为止,我安装了我的app,ruby和postgres.此时我正在尝试测试一切是否正常工作,然后再继续使用Nginx / memcached / unicorn.
这是我的dockerfile:
这是我的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 https://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连接
>在dockerfile中EXPOSE 3000,使用docker ps查找映射端口并尝试localhost:mapped_port
另外我不知道它是否重要,但我在OSX上使用boot2docker.
解决方法
这实际上是由boot2docker引起的.有关详细信息,请参阅此页面:
https://github.com/boot2docker/boot2docker/blob/master/doc/WORKAROUNDS.md
TLDR
Portforwarding目前仅适用于boot2docker上的hack.您需要运行以下命令(并使其保持运行):
boot2docker ssh -L 8080:localhost:80
其中80是容器上的公开端口,8080是您要从主机上访问它的端口.