質問編集履歴

2

タイトル変更

2020/01/16 06:18

投稿

HayatoOkuma
HayatoOkuma

スコア12

test CHANGED
@@ -1 +1 @@
1
- Rails on Docker Herokuへデプロイ時に db:createが通らない
1
+ Rails on Docker Heroku へデプロイ時に db:create が通らない
test CHANGED
File without changes

1

重複する内容の削除

2020/01/16 06:18

投稿

HayatoOkuma
HayatoOkuma

スコア12

test CHANGED
File without changes
test CHANGED
@@ -1,329 +1,325 @@
1
+ 現在docker-composeを用いてRailsアプリケーションを開発しており,
2
+
3
+ 本番環境へのリリースとしてHerokuへのデプロイを行っている途中でエラーが発生している状態です.
4
+
5
+
6
+
7
+ ## やったこと
8
+
9
+ [こちらのQiita記事](https://qiita.com/take18k_tech/items/25a1147c481a37b423fc)や[herokuのdocs](https://devcenter.heroku.com/articles/cleardb#configuring-your-ruby-application-to-use-cleardb)を参照しながら以下のコマンドを実行していきました.
10
+
11
+ ここまでは何のエラーもなく通っています.
12
+
13
+ (herokuへログイン済み)
14
+
15
+ ```bash
16
+
17
+ $ heroku container:push web
18
+
19
+
20
+
21
+ $ heroku container:release web
22
+
23
+
24
+
25
+ $ heroku addons:create cleardb:ignite
26
+
27
+
28
+
29
+ $ heroku config
30
+
31
+ === myapp Config Vars
32
+
33
+ CLEARDB_DATABASE_URL: mysql://xxx
34
+
35
+
36
+
37
+ $ heroku config:set DATABASE_URL='mysql2://xxx'
38
+
39
+ Setting DATABASE_URL and restarting ⬢ myapp... done, v21
40
+
41
+ DATABASE_URL: mysql2://xxx
42
+
43
+
44
+
45
+ $ heroku config:set RAILS_ENV='production'
46
+
47
+ Setting RAILS_ENV and restarting ⬢ myapp... done, v22
48
+
49
+ RAILS_ENV: production
50
+
51
+ ```
52
+
53
+
54
+
55
+ ## 発生している問題
56
+
57
+ 以上のコマンドに引き続き`db:create`を実行した際に以下のエラーが発生しています.
58
+
59
+ ```
60
+
61
+ $ heroku run rails db:create
62
+
63
+ Running rails db:create on ⬢ myapp... up, run.9235 (Free)
64
+
65
+ rails aborted!
66
+
67
+ NameError: Cannot load database configuration:
68
+
69
+ undefined local variable or method `root' for main:Object
70
+
71
+ (erb):19:in `block in <main>'
72
+
73
+ (erb):19:in `fetch'
74
+
75
+ (erb):19:in `<main>'
76
+
77
+ /usr/local/bundle/gems/railties-5.2.4.1/lib/rails/application/configuration.rb:172:in `database_configuration'
78
+
79
+ /usr/local/bundle/gems/activerecord-5.2.4.1/lib/active_record/railtie.rb:39:in `block (3 levels) in <class:Railtie>'
80
+
81
+ /usr/local/bundle/gems/railties-5.2.4.1/lib/rails/commands/rake/rake_command.rb:23:in `block in perform'
82
+
83
+ /usr/local/bundle/gems/railties-5.2.4.1/lib/rails/commands/rake/rake_command.rb:20:in `perform'
84
+
85
+ /usr/local/bundle/gems/railties-5.2.4.1/lib/rails/command.rb:48:in `invoke'
86
+
87
+ /usr/local/bundle/gems/railties-5.2.4.1/lib/rails/commands.rb:18:in `<main>'
88
+
89
+ /usr/local/bundle/gems/bootsnap-1.4.5/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:22:in `require'
90
+
91
+ /usr/local/bundle/gems/bootsnap-1.4.5/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:22:in `block in require_with_bootsnap_lfi'
92
+
93
+ /usr/local/bundle/gems/bootsnap-1.4.5/lib/bootsnap/load_path_cache/loaded_features_index.rb:92:in `register'
94
+
95
+ /usr/local/bundle/gems/bootsnap-1.4.5/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:21:in `require_with_bootsnap_lfi'
96
+
97
+ /usr/local/bundle/gems/bootsnap-1.4.5/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:30:in `require'
98
+
99
+ /usr/local/bundle/gems/activesupport-5.2.4.1/lib/active_support/dependencies.rb:291:in `block in require'
100
+
101
+ /usr/local/bundle/gems/activesupport-5.2.4.1/lib/active_support/dependencies.rb:257:in `load_dependency'
102
+
103
+ /usr/local/bundle/gems/activesupport-5.2.4.1/lib/active_support/dependencies.rb:291:in `require'
104
+
105
+ /katagami-ant/bin/rails:9:in `<top (required)>'
106
+
107
+ /usr/local/bundle/gems/spring-2.1.0/lib/spring/client/rails.rb:28:in `load'
108
+
109
+ /usr/local/bundle/gems/spring-2.1.0/lib/spring/client/rails.rb:28:in `call'
110
+
111
+ /usr/local/bundle/gems/spring-2.1.0/lib/spring/client/command.rb:7:in `call'
112
+
113
+ /usr/local/bundle/gems/spring-2.1.0/lib/spring/client.rb:30:in `run'
114
+
115
+ /usr/local/bundle/gems/spring-2.1.0/bin/spring:49:in `<top (required)>'
116
+
117
+ /usr/local/bundle/gems/spring-2.1.0/lib/spring/binstub.rb:11:in `load'
118
+
119
+ /usr/local/bundle/gems/spring-2.1.0/lib/spring/binstub.rb:11:in `<top (required)>'
120
+
121
+ /katagami-ant/bin/spring:15:in `<top (required)>'
122
+
123
+ bin/rails:3:in `load'
124
+
125
+ bin/rails:3:in `<main>'
126
+
127
+ ```
128
+
129
+
130
+
131
+ ## 考えうる原因
132
+
133
+ ```
134
+
135
+ /usr/local/bundle/gems/railties-5.2.4.1/lib/rails/application/configuration.rb:172:in `database_configuration'
136
+
137
+ ```
138
+
139
+ エラーが発生したコードは以上の部分ということだったので, `database_configuration`について[調べてみると](https://www.rubydoc.info/docs/rails/4.1.7/Rails%2FApplication%2FConfiguration:database_configuration)
140
+
141
+ > Loads and returns the entire raw configuration of database from values stored in `config/database.yml`.
142
+
143
+
144
+
145
+ ということで, どうも`config/database.yml`が怪しいと分かりました.
146
+
147
+ `heroku config`で設定した環境変数が読み込まれているかを確認するために,
148
+
149
+ `initializers/`の適当なファイルに以下を置いてみましたが問題ないようでした.
150
+
151
+
152
+
153
+ ```ruby
154
+
155
+ p ENV['DATABASE_URL']
156
+
157
+ p ENV['RAILS_ENV']
158
+
159
+ ```
160
+
161
+
162
+
163
+ ```bash
164
+
165
+ $ heroku run rails db:create
166
+
167
+ Running rails db:create on ⬢ myapp... up, run.2733 (Free)
168
+
169
+ "mysql2://xxx"
170
+
171
+ "production"
172
+
173
+ rails aborted!
174
+
175
+ NameError: Cannot load database configuration:
176
+
177
+ undefined local variable or method `root' for main:Object
178
+
179
+ ...
180
+
181
+ ```
182
+
183
+
184
+
185
+
186
+
187
+ ## 関連のあるコード
188
+
189
+
190
+
191
+ - docker-compose.yml
192
+
193
+
194
+
195
+ ```yml
196
+
197
+ version: "3"
198
+
199
+ services:
200
+
201
+ web:
202
+
203
+ build: .
204
+
205
+ command: /bin/sh -c "rm -f /myapp/tmp/pids/server.pid && bundle exec rails s -p '3001' -b '0.0.0.0'"
206
+
207
+ tty: true
208
+
209
+ stdin_open: true
210
+
211
+ # environment:
212
+
213
+ # - DATABASE=myapp_development
214
+
215
+ # - DATABASE_USER=root
216
+
217
+ # - DATABASE_PASSWORD=password
218
+
219
+ # - DATABASE_HOST=db
220
+
221
+ volumes:
222
+
223
+ - .:/katagami-ant
224
+
225
+ ports:
226
+
227
+ - 3001:3001
228
+
229
+ depends_on:
230
+
231
+ - db
232
+
233
+ - redis
234
+
235
+
236
+
237
+ db:
238
+
239
+ image: mysql:5.7
240
+
241
+ command: mysqld --character-set-server=utf8 --collation-server=utf8_unicode_ci
242
+
243
+ environment:
244
+
245
+ - MYSQL_DATABASE=myapp_development
246
+
247
+ - MYSQL_ROOT_USER=root
248
+
249
+ - MYSQL_ROOT_PASSWORD=password
250
+
251
+ volumes:
252
+
253
+ - mysql_vol:/var/lib/mysql
254
+
255
+ ports:
256
+
257
+ - 3306:3306
258
+
259
+
260
+
261
+ redis:
262
+
263
+ image: redis
264
+
265
+ ports:
266
+
267
+ - 6379:6379
268
+
269
+ volumes:
270
+
271
+ - "./app/redis:/data"
272
+
273
+
274
+
275
+ volumes:
276
+
277
+ mysql_vol:
278
+
279
+ ```
280
+
281
+ - config/database.yml
282
+
283
+ ```yml
284
+
285
+ default: &default
286
+
287
+ adapter: mysql2
288
+
289
+ encoding: utf8
290
+
291
+ pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
292
+
293
+
294
+
295
+ development:
296
+
297
+ <<: *default
298
+
299
+ # username: <%= ENV.fetch("DATABASE_USER") { root } %>
300
+
301
+ # password: <%= ENV.fetch("DATABASE_PASSWORD") { password } %>
302
+
303
+ # host: <%= ENV.fetch("DATABASE_HOST") { db } %>
304
+
305
+ # database: <%= ENV.fetch("DATABASE") { katagami-ant_development } %>
306
+
307
+
308
+
309
+ production:
310
+
311
+ <<: *default
312
+
313
+ url: <%= ENV['DATABASE_URL'] %>
314
+
315
+
316
+
317
+ ```
318
+
319
+
320
+
1
321
  ## 実現したいこと
2
322
 
3
-
4
-
5
- 現在docker-composeを用いてRailsアプリケーションを開発しており,
6
-
7
- 本番環境へのリリースとしてHerokuへのデプロイを行っている途中でエラーが発生している状態です.
8
-
9
-
10
-
11
- ## やったこと
12
-
13
- [こちらのQiita記事](https://qiita.com/take18k_tech/items/25a1147c481a37b423fc)や[herokuのdocs](https://devcenter.heroku.com/articles/cleardb#configuring-your-ruby-application-to-use-cleardb)を参照しながら以下のコマンドを実行していきました.
14
-
15
- ここまでは何のエラーもなく通っています.
16
-
17
- (herokuへログイン済み)
18
-
19
- ```bash
20
-
21
- $ heroku container:push web
22
-
23
-
24
-
25
- $ heroku container:release web
26
-
27
-
28
-
29
- $ heroku addons:create cleardb:ignite
30
-
31
-
32
-
33
- $ heroku config
34
-
35
- === myapp Config Vars
36
-
37
- CLEARDB_DATABASE_URL: mysql://xxx
38
-
39
-
40
-
41
- $ heroku config:set DATABASE_URL='mysql2://xxx'
42
-
43
- Setting DATABASE_URL and restarting ⬢ myapp... done, v21
44
-
45
- DATABASE_URL: mysql2://xxx
46
-
47
-
48
-
49
- $ heroku config:set RAILS_ENV='production'
50
-
51
- Setting RAILS_ENV and restarting ⬢ myapp... done, v22
52
-
53
- RAILS_ENV: production
54
-
55
- ```
56
-
57
-
58
-
59
- ## 発生している問題
60
-
61
- 以上のコマンドに引き続き`db:create`を実行した際に以下のエラーが発生しています.
62
-
63
- ```
64
-
65
- $ heroku run rails db:create
66
-
67
- Running rails db:create on ⬢ myapp... up, run.9235 (Free)
68
-
69
- rails aborted!
70
-
71
- NameError: Cannot load database configuration:
72
-
73
- undefined local variable or method `root' for main:Object
74
-
75
- (erb):19:in `block in <main>'
76
-
77
- (erb):19:in `fetch'
78
-
79
- (erb):19:in `<main>'
80
-
81
- /usr/local/bundle/gems/railties-5.2.4.1/lib/rails/application/configuration.rb:172:in `database_configuration'
82
-
83
- /usr/local/bundle/gems/activerecord-5.2.4.1/lib/active_record/railtie.rb:39:in `block (3 levels) in <class:Railtie>'
84
-
85
- /usr/local/bundle/gems/railties-5.2.4.1/lib/rails/commands/rake/rake_command.rb:23:in `block in perform'
86
-
87
- /usr/local/bundle/gems/railties-5.2.4.1/lib/rails/commands/rake/rake_command.rb:20:in `perform'
88
-
89
- /usr/local/bundle/gems/railties-5.2.4.1/lib/rails/command.rb:48:in `invoke'
90
-
91
- /usr/local/bundle/gems/railties-5.2.4.1/lib/rails/commands.rb:18:in `<main>'
92
-
93
- /usr/local/bundle/gems/bootsnap-1.4.5/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:22:in `require'
94
-
95
- /usr/local/bundle/gems/bootsnap-1.4.5/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:22:in `block in require_with_bootsnap_lfi'
96
-
97
- /usr/local/bundle/gems/bootsnap-1.4.5/lib/bootsnap/load_path_cache/loaded_features_index.rb:92:in `register'
98
-
99
- /usr/local/bundle/gems/bootsnap-1.4.5/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:21:in `require_with_bootsnap_lfi'
100
-
101
- /usr/local/bundle/gems/bootsnap-1.4.5/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:30:in `require'
102
-
103
- /usr/local/bundle/gems/activesupport-5.2.4.1/lib/active_support/dependencies.rb:291:in `block in require'
104
-
105
- /usr/local/bundle/gems/activesupport-5.2.4.1/lib/active_support/dependencies.rb:257:in `load_dependency'
106
-
107
- /usr/local/bundle/gems/activesupport-5.2.4.1/lib/active_support/dependencies.rb:291:in `require'
108
-
109
- /katagami-ant/bin/rails:9:in `<top (required)>'
110
-
111
- /usr/local/bundle/gems/spring-2.1.0/lib/spring/client/rails.rb:28:in `load'
112
-
113
- /usr/local/bundle/gems/spring-2.1.0/lib/spring/client/rails.rb:28:in `call'
114
-
115
- /usr/local/bundle/gems/spring-2.1.0/lib/spring/client/command.rb:7:in `call'
116
-
117
- /usr/local/bundle/gems/spring-2.1.0/lib/spring/client.rb:30:in `run'
118
-
119
- /usr/local/bundle/gems/spring-2.1.0/bin/spring:49:in `<top (required)>'
120
-
121
- /usr/local/bundle/gems/spring-2.1.0/lib/spring/binstub.rb:11:in `load'
122
-
123
- /usr/local/bundle/gems/spring-2.1.0/lib/spring/binstub.rb:11:in `<top (required)>'
124
-
125
- /katagami-ant/bin/spring:15:in `<top (required)>'
126
-
127
- bin/rails:3:in `load'
128
-
129
- bin/rails:3:in `<main>'
130
-
131
- ```
132
-
133
-
134
-
135
- ## 考えうる原因
136
-
137
- ```
138
-
139
- /usr/local/bundle/gems/railties-5.2.4.1/lib/rails/application/configuration.rb:172:in `database_configuration'
140
-
141
- ```
142
-
143
- エラーが発生したコードは以上の部分ということだったので, `database_configuration`について[調べてみると](https://www.rubydoc.info/docs/rails/4.1.7/Rails%2FApplication%2FConfiguration:database_configuration)
144
-
145
- > Loads and returns the entire raw configuration of database from values stored in `config/database.yml`.
146
-
147
-
148
-
149
- ということで, どうも`config/database.yml`が怪しいと分かりました.
150
-
151
- `heroku config`で設定した環境変数が読み込まれているかを確認するために,
152
-
153
- `initializers/`の適当なファイルに以下を置いてみましたが問題ないようでした.
154
-
155
-
156
-
157
- ```ruby
158
-
159
- p ENV['DATABASE_URL']
160
-
161
- p ENV['RAILS_ENV']
162
-
163
- ```
164
-
165
-
166
-
167
- ```bash
168
-
169
- $ heroku run rails db:create
170
-
171
- Running rails db:create on ⬢ myapp... up, run.2733 (Free)
172
-
173
- "mysql2://xxx"
174
-
175
- "production"
176
-
177
- rails aborted!
178
-
179
- NameError: Cannot load database configuration:
180
-
181
- undefined local variable or method `root' for main:Object
182
-
183
- ...
184
-
185
- ```
186
-
187
-
188
-
189
-
190
-
191
- ## 関連のあるコード
192
-
193
-
194
-
195
- - docker-compose.yml
196
-
197
-
198
-
199
- ```yml
200
-
201
- version: "3"
202
-
203
- services:
204
-
205
- web:
206
-
207
- build: .
208
-
209
- command: /bin/sh -c "rm -f /myapp/tmp/pids/server.pid && bundle exec rails s -p '3001' -b '0.0.0.0'"
210
-
211
- tty: true
212
-
213
- stdin_open: true
214
-
215
- # environment:
216
-
217
- # - DATABASE=myapp_development
218
-
219
- # - DATABASE_USER=root
220
-
221
- # - DATABASE_PASSWORD=password
222
-
223
- # - DATABASE_HOST=db
224
-
225
- volumes:
226
-
227
- - .:/katagami-ant
228
-
229
- ports:
230
-
231
- - 3001:3001
232
-
233
- depends_on:
234
-
235
- - db
236
-
237
- - redis
238
-
239
-
240
-
241
- db:
242
-
243
- image: mysql:5.7
244
-
245
- command: mysqld --character-set-server=utf8 --collation-server=utf8_unicode_ci
246
-
247
- environment:
248
-
249
- - MYSQL_DATABASE=myapp_development
250
-
251
- - MYSQL_ROOT_USER=root
252
-
253
- - MYSQL_ROOT_PASSWORD=password
254
-
255
- volumes:
256
-
257
- - mysql_vol:/var/lib/mysql
258
-
259
- ports:
260
-
261
- - 3306:3306
262
-
263
-
264
-
265
- redis:
266
-
267
- image: redis
268
-
269
- ports:
270
-
271
- - 6379:6379
272
-
273
- volumes:
274
-
275
- - "./app/redis:/data"
276
-
277
-
278
-
279
- volumes:
280
-
281
- mysql_vol:
282
-
283
- ```
284
-
285
- - config/database.yml
286
-
287
- ```yml
288
-
289
- default: &default
290
-
291
- adapter: mysql2
292
-
293
- encoding: utf8
294
-
295
- pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
296
-
297
-
298
-
299
- development:
300
-
301
- <<: *default
302
-
303
- # username: <%= ENV.fetch("DATABASE_USER") { root } %>
304
-
305
- # password: <%= ENV.fetch("DATABASE_PASSWORD") { password } %>
306
-
307
- # host: <%= ENV.fetch("DATABASE_HOST") { db } %>
308
-
309
- # database: <%= ENV.fetch("DATABASE") { katagami-ant_development } %>
310
-
311
-
312
-
313
- production:
314
-
315
- <<: *default
316
-
317
- url: <%= ENV['DATABASE_URL'] %>
318
-
319
-
320
-
321
- ```
322
-
323
-
324
-
325
- ## 実現したいこと
326
-
327
323
  `heroku run db:create`を実行したい.
328
324
 
329
325