我正在尝试使用Docker与Rails,将整个堆栈构建在一个容器内。我的最终目标是拥有一个nginx / memcached / unicorn / rails / postgres堆栈,并使用runit作为进程管理器。到目前为止,我已经安装了我的应用程序,Ruby和Postgres。此时,我正在尝试测试是否一切正常工作,然后再转向nginx / memcached / unicorn。
以下是我的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://dev59.com/W2Ii5IYBdhLWcg3w_QiG#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。