現在Docker内に環境を構築するため学習をしています。
https://www.youtube.com/watch?v=uQf9968RWWo
参考にしている動画は上記です
herokuにアプリを作成し、DBを追加設定し、Dockerファイルを本番環境用に設定、Dockerイメージをビルドしてコンテナレジストリーにプッシュするしてリリースまでしました。
得に何も機能を追加していませんが、db:migrateを実行してみたところ以下のエラーが発生しておりました。
iTerm
1Mysql2::Error::ConnectionError: Unknown MySQL server host '‘us-cdbr-east-05.cleardb.net’' (-2)
hostの環境変数の入力を間違えたのかと思い、確認致しましたが
user@yuzanoMacBook-puro rails_docker % heroku config -a rails-docker-datiko1115 === rails-docker-datiko1115 Config Vars APP_DATABASE: ‘heroku_02586e9c5f5ae42’ APP_DATABASE_HOST: ‘us-cdbr-east-05.cleardb.net’ APP_DATABASE_PASSWORD: ‘198a933b’ APP_DATABASE_USERNAME: ‘b5d57f44b8c971’ CLEARDB_DATABASE_URL: mysql://b5d57f44b8c971:198a933b@us-cdbr-east-05.cleardb.net/heroku_02586e9c5f5ae42?reconnect=true RAILS_SERVE_STATIC_FILES: ture
問題なく設定できてる様に見えます。
database.yml
1production: 2 <<: *default 3 database: <%= ENV['APP_DATABASE'] %> 4 username: <%= ENV['APP_DATABASE_USERNAME'] %> 5 password: <%= ENV['APP_DATABASE_PASSWORD'] %> 6 host: <%= ENV['APP_DATABASE_HOST'] %>
database.yml内の本番環境の設定も確認して変数と実際に入力した値を確認しましたが問題なさそうです。
学習始めたばかりでなぜこのエラーが出ているのかわかりません、、力及ばず悔しいですが、どなたか原因がわかる方がいらっしゃいましたらご回答お願い致します。
以下関係のありそうなファイルです
dockercompose.yml
1version: '3' 2#dbとwebのサービスを定義 3services: 4 db: #msql 5 image: mysql:8.0 6 command: --default-authentication-plugin=mysql_native_password #認証形式の設定8.0から 7 volumes: 8 - ./src/db/mysql_data:/var/lib/mysql #./src/db/mysql_data(ローカル)を/var/lib/mysql(docker)側に共有する設定(コンテナを作り直すたびにデータが消えてしまうが、これを設定しておけばローカルのデータを持ってこれる) 9 environment: 10 MYSQL_ROOT_PASSWORD: password #(環境変数でパスワードを指定) 11 web: #rails 12 build: . #ベースのイメージとしてDocker fileを参照する設定 13 command: bundle exec rails s -p 3000 -b '0.0.0.0' #rails サーバーを起動 14 volumes: 15 - ./src:/app #./src(ローカル)内を/app(Docker)側に共有する 16 ports: 17 - "3000:3000" #ローカルの3000ポートをDockerの3000ポートに接続 18 depends_on: #webはdbサービスに依存しているという設定 19 - db
Dockerfile
1FROM ruby:2.7 2 3ENV RAILS_ENV=production 4 5RUN curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add - \ 6 && echo "deb https://dl.yarnpkg.com/debian/ stable main" | tee /etc/apt/sources.list.d/yarn.list \ 7 && apt-get update -qq \ 8 && apt-get install -y nodejs yarn 9 #必要なライブラリをインストール 10WORKDIR /app 11#作業ディレクトリを指定/app 12COPY ./src /app 13#./srcローカル側を/appにコピー 14RUN bundle config --local set path 'vendor/bundle' \ 15 && bundle install 16#bundle installをする 17 18COPY start.sh /start.sh 19RUN chmod 744 /start.sh 20CMD ["sh", "/start.sh"]
start.sh
1#!/bin/sh 2 3if [ "${RAILS_ENV}" = "production"] 4then 5 bundle exec rails rails assets:precompile 6fi 7 8bundle exec rails s -p ${PORT:-3000} -b 0.0.0.0
# 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: app_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: app_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: <%= ENV['APP_DATABASE'] %> username: <%= ENV['APP_DATABASE_USERNAME'] %> password: <%= ENV['APP_DATABASE_PASSWORD'] %> host: <%= ENV['APP_DATABASE_HOST'] %>
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。