Railsの開発環境をDockerで作った時にハマったところ Rails, Nginx, Redis, MySQL

docker-composeを使って構築する時にハマったところ


ホストマシン(Macのローカル環境)とDockerコンテナとのやりとり


 - ports ( -pオプション)でやりとりできるのはホストマシンのPortをマッピングする

なので

Dockerコンテナ同士のPortをマッピングしているわけではない

例えば、MySQLのコンテナ(Port:3306)にホストマシンからアクセスできるからといって、Railsを動かしているコンテナからアクセスはできない。


ホストマシン : mysql -h127.0.0.1 -uroot => OK
Railsコンテナ : mysql -h 127.0.0.1 -uroot => NG

linksを使って指定すれば、名前解決できるのでそれを使ってアクセスする

links:
    - mysql

Railsコンテナ : mysql -h mysql -uroot => OK


RailsアプリのMySQLへの接続、Redisへの接続などはホスト名の指定方法を変更する必要がある!!

修正前
database.yml
  host: localhost

development.rb
  config.session_store :redis_store,
                       servers: 'redis://localhost:6379/0',

修正後
database.yml
  url: mysql2://root:@mysql:3306

development.rb
  config.session_store :redis_store,
                       servers: 'redis://redis:6379/0',

Railsアプリの共有方法

ディレクトリ構成を以下のようにしていた

任意のディレクトリ/RailsApp/
  Gemfile
  app
  bin
  config
  db
  docker-compose/
    nginx/
      Dockerfile
      app.conf
    app/
      Dockerfile
    docker-compose.yml
    
そうすると、appのDockerfileに書くRailsAppのパスが親のパスになるため、Permissionエラーとなる

COPY .. /app
 
なのでdocker-compose.ymlとRailsアプリのDockerファイルはルートディレクトリに置くようにした



Xcode8 Swift3への対応

Swift3へのConvertでエラーとなって手動で直した部分


UICollectionDataSource

-    override func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
+    override func numberOfSections(in collectionView: UICollectionView) -> Int {


-    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
+    override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {


-    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
+    override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {



UITableViewDataSource


-    override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
+    override func numberOfSections(in tableView: UITableView) -> Int {


-    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
+    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {


-    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
+    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {



-    override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
+
+    override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {



CGGeometory

-        frame = CGRectMake(0.0, 0.0, self.view.frame.width, self.view.frame.height)
+       frame = CGRect(x: 0.0, y: 0.0, width: self.view.frame.width, height: self.view.frame.height)


UIVIewController

-     override func shouldAutomaticallyForwardAppearanceMethods() -> Bool {
+    override var shouldAutomaticallyForwardAppearanceMethods: Bool {



Cocoapods

"Use Legacy Swift Language Version"のエラーが出た場合、該当のフレームワークのBuild Settingsで「Use Legacy Swift Language Version」の設定をNOにする



Cocoapods 1.0.0にアップデートした時の対応

ターゲットの指定が必須

0.39.0の記述

platform :ios, "8.0"
use_frameworks!

pod 'Google/Analytics'
pod 'AFNetworking'
pod 'SSKeychain'
pod 'SVProgressHUD'


1.0.0の記述

platform :ios, "8.0"
use_frameworks!

target "Sample" do
  pod 'Google/Analytics'
  pod 'AFNetworking'
  pod 'SSKeychain'
  pod 'SVProgressHUD'
end



Acknowledgementsファイルのパスの変更

0.39.0
FileUtils.cp_r('Pods/Target Support Files/Pods-Sample/Pods-Sample-acknowledgements.plist', 'Sample/Settings.bundle/Acknowledgements.plist', :remove_destination => true)

1.0.0
FileUtils.cp_r('Pods/Target Support Files/Pods/Pods-acknowledgements.plist', 'Sample/Settings.bundle/Acknowledgements.plist', :remove_destination => true)

ActiveMailerのエラー

rails4.2 ActiveMailerを使っていて、メールが送られてそうなログは表示されるが、
実際にはメールが送られていない。


SystemMailer#system_email: processed outbound mail in 187.9ms


例えば、

mail(to: user.email, subject: "#{ message.creator.namename }さんから返信来てます" )

のようなコードがあると、実際にsubjectの文字列が作られるのはActiveJobの方?


なので、呼び出し側ではエラーはキャッチできない。

原因は調査中

bunder1.10.5を使い続ける><

rails + rspec + springを使ってて、bundlerを1.12にあげると以下のエラーが。

本当はもっとあったけど、ログに残ってない。

bundle/ruby/2.2.0/gems/guard-rspec-4.6.2/lib/guard/rspec_formatter.rb -f Guard::RSpecFormatter --failure-exit-code 2


原因がわかるまではbundlerを1.10.5からアップデートしない。

iOS既存プロジェクトにテストを追加する場合のハマりどころ

一番良いのは常に⌘Uを押してテストが通るか確認すること。
ある程度開発が進んでから、テストを書こうとするとどこに原因があるのか探すのに時間がかかる

Objective-Cのコードを使っている場合

テストTargetsに「Objective-C Bridging Header」の指定が漏れている


CocoaPodsを使ってる

ProjectのConfigurationsにPods.debugの指定が漏れている


CocoaPodsのresourceファイルがある

Build Phasesに「Copy Pods Resources」が指定されていない


上記の対応をするとうまくテストを実行する事ができました。
今回のプロジェクトはJSONのレスポンスをそのままCoreDataに保存するライブラリを使いました。
https://github.com/hrk-ys/HRKModelTransfer

iOS UIImage+ImageEffectsの調整をするサンプルコード

Blurを使ってぼかした画像を表示したい場合、iOS7もサポートしていると、iOS8から導入されたUIBlurEffectを使う事ができません。

ほかにもすでにCococaPodsなどでたくさん便利なものが出回っているので、実装自体そんなに大変ではないと思います。

が、デザイナーさんが表現したいぼかし具合を作るのがなかなか難しかったので、実際にパラメータをいじりながら調整できるサンプルコードを書きました。

今回対象にしているのは、Appleのサンプルコードです。

https://developer.apple.com/library/ios/samplecode/UIImageEffects/Introduction/Intro.html#//apple_ref/doc/uid/DTS40013396-Intro-DontLinkElementID_2


実際につくったものはここ

https://github.com/hrk-ys/BlurSample


実装 

UIImageEffects.h
+ (UIImage*)imageByApplyingBlurToImage:(UIImage*)inputImage
                            withRadius:(CGFloat)blurRadius
                             tintColor:(UIColor *)tintColor
                 saturationDeltaFactor:(CGFloat)saturationDeltaFactor
                             maskImage:(UIImage *)maskImage;

blurRadius、tintColor、saturationDeltaFactorをそれぞれSliderやColor PickerっぽいUIで設定できます





ReactNativeでAndroid対応する話

前提 ReactNativeでiOS版のアプリをリリースしていて、Android版をリリースする話 トラブルシューティング Build.VERSION_CODES.Q が存在しないエラー compileSdkVersionを29以上にすると解決 メモリー足りないエラー Execu...