docker-compose rails应用程序无法在端口3000上访问

时间:2020-04-11 21:44:22

标签: ruby-on-rails docker docker-compose

我正在为一个简单的rails / postgres应用程序构建docker容器。 rails应用程序已启动,并且正在侦听端口3000。我已经为Rails容器暴露了端口3000。但是,http://localhost:3000的响应为ERR_EMPTY_RESPONSE。我以为Rails容器应该可以在端口3000上访问。我还需要做其他事情吗?

greg@MemeMachine ~ $ docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED              STATUS              PORTS                    NAMES
eed45208bbda        realestate_web      "entrypoint.sh bash …"   About a minute ago   Up About a minute   0.0.0.0:3000->3000/tcp   realestate_web_1
a9cb8cae310e        postgres            "docker-entrypoint.s…"   About a minute ago   Up About a minute   5432/tcp                 realestate_db_1
greg@MemeMachine ~ $ docker logs realestate_web_1
=> Booting Puma
=> Rails 6.0.2.2 application starting in development 
=> Run `rails server --help` for more startup options
Puma starting in single mode...
* Version 3.12.4 (ruby 2.6.3-p62), codename: Llamas in Pajamas
* Min threads: 5, max threads: 5
* Environment: development
* Listening on tcp://localhost:3000
Use Ctrl-C to stop
greg@MemeMachine ~ $ curl http://localhost:3000
curl: (52) Empty reply from server

Dockerfile

FROM ruby:2.6.3

RUN apt-get update -qq && apt-get install -y nodejs postgresql-client
RUN mkdir /myapp
WORKDIR /myapp
COPY Gemfile /myapp/Gemfile
COPY Gemfile.lock /myapp/Gemfile.lock
RUN gem install bundler -v 2.0.2
RUN bundle install
COPY . /myapp

# Add a script to be executed every time the container starts.
COPY entrypoint.sh /usr/bin/
RUN chmod +x /usr/bin/entrypoint.sh
ENTRYPOINT ["entrypoint.sh"]
EXPOSE 3000

# Start the main process.
CMD ["rails", "server", "-b", "0.0.0.0"]

docker-compose.yml

version: '3'
services:
  db:
    image: postgres
    volumes:
      - ./tmp/db:/var/lib/postgresql/data
    env_file:
      - '.env'
  web:
    build: .
    command: bash -c "rm -f tmp/pids/server.pid && bundle exec rails s -p 3000 -b '0.0.0.0'"
    volumes:
      - .:/myapp
    ports:
      - "3000:3000"
    depends_on:
      - db
    env_file:
      - '.env'

entrypoint.sh

#!/bin/bash

# Compile the assets
bundle exec rake assets:precompile

# Start the server
bundle exec rails server

2 个答案:

答案 0 :(得分:2)

 * Listening on tcp://localhost:3000

此日志行使我认为Rails仅绑定到localhost ip。这意味着rails将仅侦听来自容器内部的请求。要使rails绑定到所有ip,并使用rails server -b parameter监听来自容器外部的请求。 entrypoint.sh中的最后一行应更改为:

bundle exec rails server -b 0.0.0.0

答案 1 :(得分:1)

同时提供ENTRYPOINTCMD时,Docker combines them together into a single command。如果仅生成docker run映像,则入口点脚本将作为命令行参数传递给命令部分rails server -b 0.0.0.0;但它会忽略它,而只是启动Rails服务器本身(在这种情况下,不带import -b 0.0.0.0选项)。

通常的解决方法是不要尝试直接在入口点运行主进程,而是以exec "$@"结束脚本以从其他参数运行命令。

在这种情况下,有两个附加位。 command:文件中的docker-compose.yml表示在入口点还需要完成一些其他设置(您无需覆盖映像的命令即可运行同一服务器)。您还需要bundle exec提供的其他环境设置。将所有内容移到入口点脚本中,您将得到

#!/bin/sh
# ^^^ this script only uses POSIX shell features

# Compile the assets
bundle exec rake assets:precompile

# Clean a stale pid file
rm -f tmp/pids/server.pid

# Run the main container process, inside the Bundler context
exec bundle exec "$@"

您的Dockerfile可以保持原样;您可以从command:文件中删除重复的docker-compose.yml