前提
dockerで開発したポートフォリオをHerokuにデプロイしようとしているのですが、エラーが発生してしまい、上手くデプロイできません。また、開発は、バックエンド側とフロントエンド側に分けて行っています。
発生している問題・エラーメッセージ
: /usr/local/lib/ruby/3.1.0/psych.rb:455:in `parse': (<unknown>): did not find expected key while parsing a block mapping at line 20 column 7 (Psych::SyntaxError)
database.yml
# MySQL. Versions 5.5.8 and up are supported.
#
# Install the MySQL driver
# gem install mysql2
#
# Ensure the MySQL gem is defined in your Gemfile
# gem "mysql2"
#
# And be sure to use new-style password hashing:
# https://dev.mysql.com/doc/refman/5.7/en/password-hashing.html
#
default: &default
adapter: mysql2
encoding: utf8mb4
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
username: root
password: password
host: db
development:
<<: *default
database: myapp_development
# Warning: The database defined as "test" will be erased and
# re-generated from your development database when you run "rake".
# Do not set this db to the same as development or production.
test:
<<: *default
database: myapp_test
# As with config/credentials.yml, you never want to store sensitive information,
# like your database password, in your source code. If your source code is
# ever seen by anyone, they now have access to your database.
#
# Instead, provide the password or a full connection URL as an environment
# variable when you boot the app. For example:
#
# DATABASE_URL="mysql2://myuser:mypass@localhost/somedatabase"
#
# If the connection URL is provided in the special DATABASE_URL environment
# variable, Rails will automatically merge its configuration values on top of
# the values provided in this file. Alternatively, you can specify a connection
# URL environment variable explicitly:
#
# production:
# url: <%= ENV["MY_APP_DATABASE_URL"] %>
#
# Read https://guides.rubyonrails.org/configuring.html#configuring-a-database
# for a full overview on how database connection configuration can be specified.
#
# production:
# <<: *default 調整
# database: myapp_production
# username: myapp
# password: <%= ENV["MYAPP_DATABASE_PASSWORD"] %>
production:
<<: *default
database: <%= ENV['APP_DATABASE'] %>
username: <%= ENV['APP_DATABASE_USERNAME'] %>
password: <%= ENV['APP_DATABASE_PASSWORD'] %>
host: <%= ENV['APP_DATABASE_HOST'] %>
url: <%= ENV['JAWSDB_DATABASE_URL']&.sub(/\Amysql/, 'mysql2') %>
試したことと自身の考察
・このエラーが発生する時は、多くの場合、ymlファイルのインデントが誤っていることが原因だとわかりました。しかし、恐らく私の場合は当てはまらないように思えます。理由は1 ローカル環境では正常に動く2以下のようにheroku側でmigrateが出来る
/backend$ heroku run rake db:migrate
Running rake db:migrate on ⬢ dictum-app-ver-3... up, run.4759 (Free)
== 20220816085742 DeviseTokenAuthCreateUsers: migrating =======================
-- create_table(:users)
-> 0.0247s
-- add_index(:users, :email, {:unique=>true})
-> 0.0218s
-- add_index(:users, [:uid, :provider], {:unique=>true})
-> 0.0260s
-- add_index(:users, :reset_password_token, {:unique=>true})
-> 0.0226s
-- add_index(:users, :confirmation_token, {:unique=>true})
-> 0.0198s
== 20220816085742 DeviseTokenAuthCreateUsers: migrated (0.1163s) ==============
== 20220816090705 CreateLearningMaterials: migrating ==========================
-- create_table(:learning_materials)
-> 0.0354s
-- add_index(:learning_materials, [:user_id, :created_at])
-> 0.0294s
== 20220816090705 CreateLearningMaterials: migrated (0.0650s) =================
== 20220816091236 CreateActiveStorageTables: migrating ========================
-- create_table(:active_storage_blobs, {:id=>:primary_key})
-> 0.0511s
-- create_table(:active_storage_attachments, {:id=>:primary_key})
-> 0.0331s
-- create_table(:active_storage_variant_records, {:id=>:primary_key})
-> 0.0312s
== 20220816091236 CreateActiveStorageTables: migrated (0.115
Heroku側でマイグレーションがはしるならymlファイルのインデントはあっているのではないかというのが私の見解です。
・私はローカルではdocker-composeを使用してコンテナ管理をしていましたが、herokuにデプロイする場合は、バックエンド側とフロントエンド側に分けてデプロイしなければならないようです。私の認識では、herokuではdocker-composeを使用できないため、バックエンド側とフロントエンド側それぞれを別のアプリとしてデプロイし、双方で通信を行うという認識をしています。そのため、まだフロントエンド側をデプロイしていなくてもバックエンド側にアクセス可能だと認識しているので、恐らく今回のエラーはフロントエンド側は関係がないと予想しています。
・ログを確認しましたが、この他にエラーは発生していないと思います。
・初めはyoutubeの動画を参考にしていたのですが、バックエンド側とフロントエンド側に分けてデプロイする際にはこのやり方では上手くいかないと考え、途中からこの記事を参考にして進めていました。
・不備などがあれば追記いたします。何かしらアドバイスがあればよろしくお願いいたします。
追記
私が何かしら編集をしたymlファイルを一応載せておきます
docker-compose.yml
version: "3"
services:
db:
image: mysql:8.0
environment:
MYSQL_ROOT_PASSWORD: password
command: --default-authentication-plugin=mysql_native_password
volumes:
- mysql-data:/var/lib/mysql
- /tmp/dockerdir:/etc/mysql/conf.d/
ports:
- "3306:3306"
cap_add:
- SYS_NICE
api:
build:
context: ./backend/
dockerfile: Dockerfile
command: bash -c "rm -f tmp/pids/server.pid && bundle exec rails s -p 3000 -b '0.0.0.0'"
volumes:
- ./backend:/myapp
- ./backend/vendor/bundle:/myapp/vendor/bundle
environment:
TZ: Asia/Tokyo
RAILS_ENV: development
ports:
- "3001:3000"
depends_on:
- db
stdin_open: true
tty: true
front:
build:
context: ./frontend/
dockerfile: Dockerfile
volumes:
- ./frontend/app:/usr/src/app
command: sh -c "yarn && yarn start"
ports:
- "4000:3000"
environment:
- WDS_SOCKET_PORT=0
volumes:
mysql-data:
heroku.yml
build:
docker:
web: Dockerfile
run:
web: bundle exec puma -C config/puma.rb
・rubyのバージョンによるエラーの可能性を考えたので、rubyのバージョンを3.0.4に落としましたが、エラーは変わりませんでした。
*マルチポストをしています。不快に思われたら申し訳ございません。
https://teratail.com/questions/hf1il24rf6xbp3