【问题标题】:Rails deploying with docker-compose, assets doesn't working使用 docker-compose 部署 Rails,资产不起作用
【发布时间】:2017-07-21 02:30:55
【问题描述】:

这是我关注的一篇非常有用的文章:

Docker for an Existing Rails Application

我被困在这个问题上。应用程序可以正常运行,从网上冲浪可以看到索引页。但是索引页面看起来是连线的,看起来 scss 不起作用并且图像没有出现。

从 Chrome 控制台,我得到了这个:

我做了很多搜索,发现它可能是 assets:premcompile。我更改了 docker-compose.yml ,运行 docker-compose run app rake db: assets:precompile,甚至将 public/assets/* 复制到容器中,但没有运气。

主机:CentOS 7

任何想法都很棒。提前致谢。

docker-compose.yml

1 app:
2     build: .
3
4     env_file: .env.production
5
6     environment:
7         RAILS_ENV: $RAILS_ENV
8
9     links:
10         - db
11

14     expose:
15         - "3000"
16
17 db:
18     image: postgres:9.4.5
19
20     volumes:
21         - eshop-postgres:/var/lib/postgresql/data
22
23 web:
24     build: .
25
26     dockerfile: config/containers/Dockerfile-nginx
27
28     volumes:
29         - ./public:/var/www/eshop/public
30
31     links:
32         - app
33     ports:
34         - "80:80"

Dockerfile

      2 FROM ruby:2.3.3-slim
     12 RUN apt-get update -qq && apt-get install -y build-essential libpq-dev postgresql-client libsqlite3-dev nodejs vim
     15 ENV RAILS_ROOT /var/www/myapp
     18 RUN mkdir -p $RAILS_ROOT/tmp/pids
     19
     21 WORKDIR $RAILS_ROOT
     22
     26 COPY Gemfile Gemfile
     27
     28 COPY Gemfile.lock Gemfile.lock
     29
     31 RUN gem install bundler
     32
     34 RUN bundle install
     35
     37 COPY . .
     38

     39 - RUN RAILS_ENV=production bundle exec rake assets:precompile --trace~
     40 - RUN bundle exec rake assets:precompile --trace~

     40 + RUN bundle exec rake RAILS_ENV=$RAILS_ENV DATABASE_URL=postgresql://$POSTGRES_USER:$POSTGRES_PASSWORD@127.0.0.1/$POSTGRES_PRODUCTION_DB assets:precompile
     41 + VOLUMES ["$RAILS_ROOT/public"]

     46 + CMD [ "config/containers/app_cmd.sh" ]

配置/容器/Docker-nginx

      2 FROM nginx

     12 RUN apt-get update -qq && apt-get -y install apache2-utils vim

     15 ENV RAILS_ROOT /var/www/myapp

     18 WORKDIR $RAILS_ROOT

     21 RUN mkdir log

     25 COPY public public/

     28 COPY config/containers/nginx.conf /tmp/myapp.nginx

     32 RUN envsubst '$RAILS_ROOT' < /tmp/myappv.nginx > /etc/nginx/conf.d/default.conf

     35 CMD [ "nginx", "-g", "daemon off;" ]

更新:

我根据@bkunzi01 的建议做了一些更新。但没有运气。

Dockerfile

 40 RUN bundle exec rake RAILS_ENV=$RAILS_ENV DATABASE_URL=postgresql://$POSTGRES_USER:$POSTGRES_PASSWORD@127.0.0.1/$POSTGRES_PRODUCTION_DB assets:precompile
 41 VOLUME ["$RAILS_ROOT/public"] 

 46 CMD [ "config/containers/app_cmd.sh" ]

.env.production

# *.env.production*    

RAILS_ENV=production
RAILS_ROOT=/var/www/eshop
SECRET_KEY_BASE=the_long_code
POSTGRES_PRODUCTION_DB=production_db
POSTGRES_USER=postgres
POSTGRES_PASSWORD=keep_secret_ps

docker-compose的日志里还有一个[warn]:

web_1  | 2017/03/02 05:45:14 [warn] 1#1: server name "/var/www/eshop/public" has suspicious symbols in /etc/nginx/conf.d/default.conf:16
web_1  | nginx: [warn] server name "/var/www/eshop/public" has suspicious symbols in /etc/nginx/conf.d/default.conf:16

【问题讨论】:

  • 与资产有同样的问题...看起来这些不是将 docker-compose 与 nginx、rails 和数据库一起使用的普通教程。

标签: ruby-on-rails nginx assets unicorn precompile


【解决方案1】:

大多数人都有这个错误,因为他们缺少 node.js 但是你有,所以我认为下一个可能的候选者是添加一个卷指令,并考虑将 dockerfile 第 39 行的预编译命令更改为:

RUN bundle exec rake RAILS_ENV=production DATABASE_URL=postgresql://user:pass@127.0.0.1/dbname SECRET_TOKEN=makeupasecret assets:precompile

VOLUME ["$RAILS_ROOT/public"]

在此处添加此卷指令将通过将预编译资产保存在卷中来将您的资产公开给 nginx。

此外,您可以为 secret_token 和数据库信息添加任何您想要的内容。 原因是当没有提供秘密令牌和用户时,Rails 会发疯,但你可以完全弥补它们,因为 Rails 不会在这里检查它们哈哈......

另外,请确保删除第二个 precompile:assets 命令,因为它不需要。

注意:我还要在 Dockerfile 的末尾添加一行来触发运行 unicorn 或您将在 nginx 后面运行 rails 的任何其他应用服务器。

例如。 CMD bundle exec unicorn -c config/unicorn.rb

【讨论】:

  • 我在上面做了一些更新。在我拥有SECRET_KEY_BASE 之后是否必须设置SECRET_TOKEN?我对此一无所知。如果必须是,我怎样才能获得SECRET_TOKEN 值? @bkunzi01 再次感谢您。
  • VOLUME ["$RAILS_ROOT/public"] 现在正在工作。我可以创建一个新文件,并在容器中看到它。但资产不起作用。
  • 这意味着 Nginx 不知道从哪里为它们提供服务。我在工作,但如果你仍然卡住,今晚我回来时也许可以帮忙!
  • 你真好。我真的需要你的帮助。当然,我会尽力去解决它,但我想我一个人做不到。
【解决方案2】:

我遇到了完全相同的问题。原来真正的原因是我的config/environments/production.rb中没有设置config.public_file_server.enabled

为了解决这个问题,我只是在我的docker-compose.yml 中添加了一个环境变量:

version: '2'
services:
  app:
    ...
    environment:
      - RAILS_SERVE_STATIC_FILES=true

我使用的是 Rails 5.0.1。

希望这会有所帮助。

【讨论】:

  • 您真的不想让您的 Ruby 堆栈处理生产中的静态文件。这充其量只是一个hack。
  • 这行得通,仍然使用资产摘要。我没有看到这个问题。花几个小时弄清楚这一点
【解决方案3】:

我遇到了这个帖子,因为我遇到了同样的问题。从那以后,我通过剥离我的容器几乎解决了它。我查阅了 Rails 的 Docker 文档:https://docs.docker.com/compose/rails/ 并相应地调整了我的 Dockerfile 和 docker-compose.yml 文件。

我继续并暂时取消了 nginx,因为我是 Docker n00b 并且只想让我的东西先工作。

这是我的 Dockerfile

# Base image:
FROM ruby:2.3.1

# Install dependencies
RUN apt-get update -qq && apt-get install -y build-essential libpq-dev postgresql-client nodejs

# Set an environment variable where the Rails app is installed to inside of Docker image:
ENV RAILS_ROOT /Users/jessecalton/WatchNext
RUN mkdir -p $RAILS_ROOT/tmp/pids

# Set working directory, where the commands will be ran:
WORKDIR $RAILS_ROOT



# Gems:
ADD Gemfile* $RAILS_ROOT

RUN gem install bundler
RUN bundle install

# Copy the main application.
COPY . $RAILS_ROOT

还有我的 docker-compose.yml

version: '3'
services:
  # service configuration for our dockerized Rails app
  app:
    # use the Dockerfile next to this file
    build: .



    # sources environment variable configuration for our app
    env_file: .env

    # rely on the RAILS_ENV value of the host machine
    environment:
      RAILS_ENV: $RAILS_ENV


    # makes the app container aware of the DB container
    links:
      - db

    # expose the port we configured Unicorn to bind to
    expose:
      - "3000"

  # service configuration for our database
  db:

    # use the preferred version of the official Postgres image
    # see https://hub.docker.com/_/postgres/
    image: postgres:latest

  # service configuration for our web server
  web:

    # set the build context to the root of the Rails app
    build: .
    command: bundle exec rails s -p 3000 -b '0.0.0.0'

    volumes:
      - /Users/jessecalton/WatchNext/

    # makes the web container aware of the app container
    links:
      - app

    # expose the port we configured Nginx to bind to
    ports:
      - "3000:3000"
    depends_on:
      - db

我按照https://docs.docker.com/compose/rails/ 站点的其他配置说明进行操作,在创建和迁移我的数据库后,一切正常。

完全披露:我仍然必须找到一种方法来关闭并重新打开 Puma 服务器,而不会删除容器并因此删除数据库。换句话说,如果您 Ctrl+C 关闭服务器,然后再次执行docker-compose up,Puma 仍将在后台运行,您的应用程序将退出,并引用“服务器已在运行”。

只是想传递此信息,因为您同样的问题整天给我带来麻烦。

【讨论】:

    猜你喜欢
    • 2020-09-28
    • 1970-01-01
    • 2021-01-01
    • 2020-06-10
    • 2014-08-29
    • 1970-01-01
    • 1970-01-01
    • 2021-07-05
    • 1970-01-01
    相关资源
    最近更新 更多