Don*_*ald 6 ruby ruby-on-rails docker
我是 Docker 的新手,正在尝试制作一个演示 Rails 应用程序。我制作了一个如下所示的 dockerfile:
FROM ruby:2.2
# Install apt based dependencies required to run Rails as
# well as RubyGems. As the Ruby image itself is based on a
# Debian image, we use apt-get to install those.
RUN apt-get update && apt-get install -y \
build-essential \
nodejs
# Configure the main working directory. This is the base
# directory used in any further RUN, COPY, and ENTRYPOINT
# commands.
RUN mkdir -p /app
WORKDIR /app
# Copy the Gemfile as well as the Gemfile.lock and install
# the RubyGems. This is a separate step so the dependencies
# will be cached unless changes to one of those two files
# are made.
COPY Gemfile Gemfile.lock ./
RUN gem install bundler && bundle install --jobs 20 --retry 5
# Copy the main application.
COPY . ./
# Expose port 3000 to the Docker host, so we can access it
# from the outside.
EXPOSE 3000
# The main command to run when the container starts. Also
# tell the Rails dev server to bind to all interfaces by
# default.
CMD ["bundle", "exec", "rails", "server", "-b", "0.0.0.0"]
Run Code Online (Sandbox Code Playgroud)
然后我构建了它(没有错误):
docker build -t demo .
Run Code Online (Sandbox Code Playgroud)
然后运行它(也没有错误):
docker run -itP demo
=> Booting Puma
=> Rails 5.1.1 application starting in development on http://0.0.0.0:3000
=> Run `rails server -h` for more startup options
Puma starting in single mode...
* Version 3.8.2 (ruby 2.2.7-p470), codename: Sassy Salamander
* Min threads: 5, max threads: 5
* Environment: development
* Listening on tcp://0.0.0.0:9292
Use Ctrl-C to stop
Run Code Online (Sandbox Code Playgroud)
当我docker ps在单独的终端中运行命令以确定端口时,我得到:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
55e8224f7c15 demo "bundle exec rails..." About an hour ago Up About an hour 0.0.0.0:32772->3000/tcp ecstatic_bohr
Run Code Online (Sandbox Code Playgroud)
然而,当我尝试在可以连接到它http://localhost:32772或http://192.168.99.100:32772使用Chrome或通过curl命令,我收到一个“连接被拒绝”。
当我通过bundle exec rails server命令在本地机器上的 docker 之外运行应用程序时,它工作正常。请注意,我在 Win7 机器上使用 Docker Toolbox
我可能做错了什么?
上述技巧的结合奏效了——
我必须使用http://<VM IP ADDRESS>:32772(localhost:32772 不起作用),并且我必须修复暴露的端口以匹配 TCP 侦听端口 9292。
我仍然不明白为什么 TCP 监听端口默认为 9292 而不是 3000,但我会单独研究一下。
感谢您的帮助!
| 归档时间: |
|
| 查看次数: |
7738 次 |
| 最近记录: |