运行后无法访问Docker Ruby on Rails映像

时间:2018-07-29 21:38:25

标签: ruby-on-rails ruby docker dockerfile docker-image

我是Docker的新手,正在尝试遵循一些教程。 我正在关注这个: http://codingnudge.com/2017/03/17/tutorial-how-to-run-ruby-on-rails-on-docker-part-1/

,并在运行docker映像时最后获得堆栈。

问题如下:

dockerfile:

FROM ruby:2.3.1

#Install essential linux packages
RUN apt-get update -qq
RUN apt-get install -y --no-install-recommends \
build-essential \
npm \
libpq-dev \
nodejs \
postgresql-client \
&& rm -rf /var/lib/apt/lists/*

#Define where the application will live inside the image
ENV RAILS_ROOT /var/www/app

#Create application home. App server will need the pids dir
RUN mkdir -p $RAILS_ROOT/tmp/pids

#Set our working directory inside the image
WORKDIR $RAILS_ROOT

#Install bundler
RUN gem install bundler

#Use the Gemfiles as Docker cache markers. Always bundle before copying app src.

COPY Gemfile Gemfile

COPY Gemfile.lock Gemfile.lock

#Finish establishing our Ruby enviornment
RUN bundle install --deployment

#Copy the Rails application into place
COPY . .

RUN RAILS_ENV=production bin/rails assets:precompile
RUN ls -la
CMD rails server --port 3000

我正在运行以下内容:

docker run -p 3001:3000 myapp

以及尝试通过

访问该应用程序时
localhost:3001 

它不起作用,并说localhost没有发送任何数据。 请帮助我,以了解我在做什么错。

我正在为此运行Mac OS。

谢谢!

1 个答案:

答案 0 :(得分:2)

当您向--port提供rails参数时,Puma将仅侦听本地接口。 没有 --port,它将监听所有界面。

因此,要强制Puma使用 --port监听所有接口,您必须明确地告诉它这样做:

CMD rails server -b 0.0.0.0 --port 3000

这应该允许docker端口转发正常工作。