質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
docker-compose

docker-composeとは、複数のコンテナで構成されるサービスを提供する手順を自動的し管理を簡単にするツール。composeファイルを使用しコマンド1回で設定した全サービスを作成・起動することが可能です。

AWS(Amazon Web Services)

Amazon Web Services (AWS)は、仮想空間を機軸とした、クラスター状のコンピュータ・ネットワーク・データベース・ストーレッジ・サポートツールをAWSというインフラから提供する商用サービスです。

React.js

Reactは、アプリケーションのインターフェースを構築するためのオープンソースJavaScriptライブラリです。

Ruby on Rails 7

Ruby on Rails 7は、2021年12月に正式リリースされました。Ruby on Railsのバージョン7であり、フロントエンド開発環境を大幅に刷新。Node.jsを用いない構成がデフォルトになっています。

Q&A

解決済

1回答

2248閲覧

本番環境で起きるrailsの{Don't know how to build task 'assets:precompile'}エラーを解決したい。

senseIY

総合スコア281

docker-compose

docker-composeとは、複数のコンテナで構成されるサービスを提供する手順を自動的し管理を簡単にするツール。composeファイルを使用しコマンド1回で設定した全サービスを作成・起動することが可能です。

AWS(Amazon Web Services)

Amazon Web Services (AWS)は、仮想空間を機軸とした、クラスター状のコンピュータ・ネットワーク・データベース・ストーレッジ・サポートツールをAWSというインフラから提供する商用サービスです。

React.js

Reactは、アプリケーションのインターフェースを構築するためのオープンソースJavaScriptライブラリです。

Ruby on Rails 7

Ruby on Rails 7は、2021年12月に正式リリースされました。Ruby on Railsのバージョン7であり、フロントエンド開発環境を大幅に刷新。Node.jsを用いない構成がデフォルトになっています。

0グッド

0クリップ

投稿2022/08/24 06:55

編集2022/08/26 08:42

前提

現在こちらのサイトを参考にdocker-compose を使用してバックエンド側(Rails APIモード)とフロントエンド側(React TypeScript)で開発したポートフォリオをAWSにデプロイしようとしています。現在サイトの通りに、一旦ローカル環境でRailsが本番環境で正常に作動するかチェックをしています。しかし、以下のエラーが発生してしまいます。

発生している問題・エラーメッセージ

$ docker-compose -f docker-compose.production.yml run api rails assets:precompile RAILS_ENV=production --trace Starting redictum_ver-20_db_1 ... done rails aborted! Don't know how to build task 'assets:precompile' (See the list of available tasks with `rails --tasks`) /usr/local/lib/ruby/gems/3.1.0/gems/rake-13.0.6/lib/rake/task_manager.rb:59:in `[]' /usr/local/lib/ruby/gems/3.1.0/gems/rake-13.0.6/lib/rake/application.rb:159:in `invoke_task' /usr/local/lib/ruby/gems/3.1.0/gems/rake-13.0.6/lib/rake/application.rb:116:in `block (2 levels) in top_level' /usr/local/lib/ruby/gems/3.1.0/gems/rake-13.0.6/lib/rake/application.rb:116:in `each' /usr/local/lib/ruby/gems/3.1.0/gems/rake-13.0.6/lib/rake/application.rb:116:in `block in top_level' /usr/local/lib/ruby/gems/3.1.0/gems/rake-13.0.6/lib/rake/application.rb:125:in `run_with_threads' /usr/local/lib/ruby/gems/3.1.0/gems/rake-13.0.6/lib/rake/application.rb:110:in `top_level' /usr/local/bundle/gems/railties-7.0.3.1/lib/rails/commands/rake/rake_command.rb:24:in `block (2 levels) in perform' /usr/local/lib/ruby/gems/3.1.0/gems/rake-13.0.6/lib/rake/application.rb:186:in `standard_exception_handling' /usr/local/bundle/gems/railties-7.0.3.1/lib/rails/commands/rake/rake_command.rb:24:in `block in perform' /usr/local/lib/ruby/gems/3.1.0/gems/rake-13.0.6/lib/rake/rake_module.rb:59:in `with_application' /usr/local/bundle/gems/railties-7.0.3.1/lib/rails/commands/rake/rake_command.rb:18:in `perform' /usr/local/bundle/gems/railties-7.0.3.1/lib/rails/command.rb:51:in `invoke' /usr/local/bundle/gems/railties-7.0.3.1/lib/rails/commands.rb:18:in `<main>' /usr/local/bundle/gems/bootsnap-1.13.0/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:32:in `require' /usr/local/bundle/gems/bootsnap-1.13.0/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:32:in `require' bin/rails:4:in `<main>'

エラーが発生してしまうのは

wsl

1docker-compose -f docker-compose.production.yml run rails rails assets:precompile RAILS_ENV=production

このコマンドを打った時です

docker-compose.production.yml

yml

1version: "3.3" 2services: 3 api: 4 build: 5 context: ./backend/ 6 dockerfile: Dockerfile 7 command: bash -c "rm -f tmp/pids/server.pid && bundle exec puma -C ./backend/config/puma.rb -e production" 8 volumes: 9 - ./backend:/myapp 10 - ./backend/vendor/bundle:/myapp/vendor/bundle 11 environment: 12 TZ: Asia/Tokyo 13 RAILS_ENV: production 14 # ports: 15 # - "3001:3000" 16 depends_on: 17 - db 18 stdin_open: true 19 tty: true 20 env_file: 21 - .env 22 user: root 23 24 front: 25 build: 26 context: ./frontend/ 27 dockerfile: Dockerfile 28 volumes: 29 - ./frontend/app:/usr/src/app 30 command: sh -c "yarn && yarn start" 31 ports: 32 - "4000:3000" 33 environment: 34 - WDS_SOCKET_PORT=0 35 36 db: 37 image: mysql:8.0 38 environment: 39 MYSQL_ROOT_PASSWORD: ${DB_PASSWORD} 40 command: --default-authentication-plugin=mysql_native_password 41 volumes: 42 - mysql-data:/var/lib/mysql 43 - /tmp/dockerdir:/etc/mysql/conf.d/ 44 ports: 45 - "3306:3306" 46 cap_add: 47 - SYS_NICE 48 49 web: 50 build: 51 context: backend/containers/nginx 52 volumes: 53 - public-data:/myapp/public 54 - tmp-data:/myapp/tmp 55 ports: 56 - 80:80 57 depends_on: 58 - api 59 60volumes: 61 db: 62 driver: local 63 public-data: 64 tmp-data: 65 log-data: 66 mysql-data:

Dockerfile(Rails)

Docker

1FROM ruby:3.1.2-buster 2 3RUN apt-get update -qq && apt-get install -y build-essential libpq-dev nodejs default-mysql-client 4 5ENV RAILS_ENV=production 6 7ENV APP_PATH /myapp 8 9RUN mkdir $APP_PATH 10WORKDIR $APP_PATH 11 12COPY Gemfile $APP_PATH/Gemfile 13COPY Gemfile.lock $APP_PATH/Gemfile.lock 14RUN bundle install 15 16COPY . $APP_PATH 17 18# Add a script to be executed every time the container starts. 19COPY entrypoint.sh /usr/bin/ 20RUN chmod +x /usr/bin/entrypoint.sh 21ENTRYPOINT ["entrypoint.sh"] 22EXPOSE 3000 23 24# Start the main process. 25RUN mkdir -p tmp/sockets 26RUN mkdir -p tmp/pids 27 28CMD ["rails", "server", "-b", "0.0.0.0"]

Dockerfile(nginx)

FROM nginx:alpine # インクルード用のディレクトリ内を削除 RUN rm -f /etc/nginx/conf.d/* # Nginxの設定ファイルをコンテナにコピー ADD nginx.conf /etc/nginx/conf.d/myapp.conf # ビルド完了後にNginxを起動 CMD /usr/sbin/nginx -g 'daemon off;' -c /etc/nginx/nginx.conf

nginx.conf

# プロキシ先の指定 # Nginxが受け取ったリクエストをバックエンドのpumaに送信 upstream myapp { # ソケット通信したいのでpuma.sockを指定 server unix:///myapp/tmp/sockets/puma.sock; } server { listen 80; # ドメインもしくはIPを指定 server_name localhost; access_log /var/log/nginx/access.log; error_log /var/log/nginx/error.log; # ドキュメントルートの指定 root /myapp/public; client_max_body_size 100m; error_page 404 /404.html; error_page 505 502 503 504 /500.html; try_files $uri/index.html $uri @myapp; keepalive_timeout 5; # リバースプロキシ関連の設定 location @myapp { proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header Host $http_host; proxy_pass http://myapp; } }

試して見たこと

・まず、私はAPIモードを使用しているのでassets:precompileをする必要がないのではないかと考えて、コマンドを実行しましたが、以下のエラーが発生してしまいました。

/usr/bin/entrypoint.sh: line 8: exec: RAILS_ENV=production: not found

・assets:precompileに関するエラー情報の多くはrailsのバージョンがかなり古いものが多いためあまり参考になりませんでした。
このサイトはまあまあ新しそうな情報だったため、applicationlrbに以下を書き込みました。

rb

1require 'capistrano/bundler' 2require 'capistrano/rails/migrations'

しかし、エラーが発生してしまいました。

$ docker-compose -f docker-compose.production.yml run api rails assets:precompile RAILS_ENV=production Starting redictum_ver-20_db_1 ... done rails aborted! LoadError: cannot load such file -- capistrano/bundler /usr/local/bundle/gems/bootsnap-1.13.0/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:17:in `require' /usr/local/bundle/gems/bootsnap-1.13.0/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:17:in `require' /myapp/config/application.rb:4:in `<main>' /myapp/Rakefile:4:in `require_relative' /myapp/Rakefile:4:in `<main>' /usr/local/bundle/gems/railties-7.0.3.1/lib/rails/commands/rake/rake_command.rb:20:in `block in perform' /usr/local/bundle/gems/railties-7.0.3.1/lib/rails/commands/rake/rake_command.rb:18:in `perform' /usr/local/bundle/gems/railties-7.0.3.1/lib/rails/command.rb:51:in `invoke' /usr/local/bundle/gems/railties-7.0.3.1/lib/rails/commands.rb:18:in `<main>' /usr/local/bundle/gems/bootsnap-1.13.0/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:32:in `require' /usr/local/bundle/gems/bootsnap-1.13.0/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:32:in `require' bin/rails:4:in `<main>' (See full trace by running task with --trace)

いろいろ調べてみるとCapistranoは自動デプロイに使うものだと判明しました。ですが、私はそのような機能を入れる予定はないため、あまりエラーの解決策にはならないように思えました。
・今使っているrailsは7系です

なぜこのようなエラーが発生してしまうのでしょうか?何かしらアドバイスがあればよろしくお願いいたします。欠けている情報があれば追記いたします。

追記

このGithubの議論を参考に以下を試しました
以下をGemfileに追記

gem 'sass-rails' gem 'uglifier' gem 'coffee-rails'

application.rbに追記

require 'sprockets/railtie'

これで再度buildしましたが、

api_1 | For more information see: https://github.com/rails/sprockets/blob/070fc01947c111d35bb4c836e9bb71962a8e0595/UPGRADING.md#manifestjs api_1 | bundler: failed to load command: puma (/usr/local/bundle/bin/puma) api_1 | /usr/local/bundle/gems/sprockets-rails-3.4.2/lib/sprockets/railtie.rb:110:in `block in <class:Railtie>': Expected to find a manifest file in `app/assets/config/manifest.js` (Sprockets::Railtie::ManifestNeededError) api_1 | But did not, please create this file and use it to link any assets that need api_1 | to be rendered by your app: api_1 | api_1 | Example: api_1 | //= link_tree ../images api_1 | //= link_directory ../javascripts .js api_1 | //= link_directory ../stylesheets .css api_1 | and restart your server api_1 |

以下のエラーが発生してしまいました。私の予想ですが、やはりrailsのAPIモードを使用する際には本番環境であってrails assets:precompile はいらないように思えます。ちなみにそのままupをすると、

! Unable to load application: I18n::InvalidLocaleData: can not load translations from /usr/local/bundle/bundler/gems/devise_token_auth-1a0483fbd125/config/locales/ja.yml: #<Psych::SyntaxError: (<unknown>): did not find expected key while parsing a block mapping at line 20 column 7> api_1 | bundler: failed to load command: puma (/usr/local/bundle/bin/puma)

このようなエラーになります。このエラーが出るのは本番環境のときだけです。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

API モードなら assets:precompile は不要です。
(そもそもアセットが存在しないはずです)

投稿2022/08/25 01:41

shinoharat

総合スコア1674

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

senseIY

2022/08/25 01:53

回答して頂きありがとうございます。まだエラーのデバッグは終わっていませんが、タイトルの「本番環境で起きるrailsの{Don't know how to build task 'assets:precompile'}エラーを解決したい。」というのは解決したので一旦この投稿は解決済みにさせていただきます。またデバッグが困難になった場合改めて質問を作成しようと思います。
shinoharat

2022/08/25 02:13

ごめんなさい。 https://teratail.com/questions/olzwzrckx7bpmt と間違えて回答を投稿してしまいました。 解決で良かったんでしょうか・・・? > またデバッグが困難になった場合改めて質問を作成しようと思います。 了解です。 ちなみにですが、 gem devise_token_auth のコミット 1a0483fbd125 の ja.yml には構文ミスがあり、8月20日に修正されています。 https://github.com/lynndylanhurley/devise_token_auth/pull/1550/files I18n::InvalidLocaleData はこれが原因かと思います。
senseIY

2022/08/25 04:17

追記ありがとうございます。「ちなみにですが、 gem devise_token_auth のコミット 1a0483fbd125 の ja.yml には構文ミスがあり、8月20日に修正されています。」 について確認しました。確かにそのようですね。しかし、私の場合、 ``` api_1 | ! Unable to load application: I18n::InvalidLocaleData: can not load translations from /usr/local/bundle/bundler/gems/devise_token_auth-1a0483fbd125/config/locales/ja.yml: #<Psych::SyntaxError: (<unknown>): did not find expected key while parsing a block mapping at line 20 column 7> api_1 | bundler: failed to load command: puma (/usr/local/bundle/bin/puma) api_1 | /usr/local/bundle/gems/i18n-1.12.0/lib/i18n/backend/base.rb:254:in `rescue in load_yml': can not load translations from /usr/local/bundle/bundler/gems/devise_token_auth-1a0483fbd125/config/locales/ja.yml: #<Psych::SyntaxError: (<unknown>): did not find expected key while parsing a block mapping at line 20 column 7> (I18n::InvalidLocaleData) ``` というエラーが直りませんでした。githubの修正箇所を確認しましたが、24行目の修正がされているようです。 私のエラーメッセージを見ると、「line 20 column 7」とあるので関係性が分かりませんでした。 また、このエラーが解決出来なかったため、新しく質問を投稿させていただきました。 https://teratail.com/questions/5lv3p6tjh26sa3
senseIY

2022/08/25 23:42

追記失礼します。 ``` docker-compose -f docker-compose.production.yml build --no-cache ``` で再度ビルドしたところエラーがなくなりました。--no-cacheをつけていなかったため、新しくgemが再インストールされなかったようです。貴重なお時間を割いて頂きありがとうございました。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問