前提
現在こちらのサイトを参考に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)
このようなエラーになります。このエラーが出るのは本番環境のときだけです。

回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2022/08/25 01:53
2022/08/25 02:13
2022/08/25 04:17
2022/08/25 23:42