Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Hand_on_Swift_Docker_Bluemix

 Hand_on_Swift_Docker_Bluemix

Swiftコードを使ったWebアプリケーションの新しい開発方法 - Bluemix Swift runtime -
https://bmxug.doorkeeper.jp/events/41438

にて発表した、オープンソースのSwiftとDockerとBluemixを使って、Linux上でSwiftアプリを作成するハンズオン資料です。
オープンソースのSwiftやDockerを触ったことがない方の導入にピッタリです。ぜひご覧ください

53e2d354b3299d64a54af680865516d5?s=128

Sato Takeshi

March 26, 2016
Tweet

More Decks by Sato Takeshi

Other Decks in Programming

Transcript

  1. SWIFT DOCKERΠϝʔδ࡞੒ & IBM BLUEMIX CONTAINERS 2016೥3݄25೔(ۚ) ༗ݶձࣾΧΠΧΠ ࠤ౻߶࢜ 1

  2. ࣗݾ঺հ ࠤ౻߶࢜(͞ͱ͏͚ͨ͠) ༗ݶձࣾΧΠΧΠͰCTO&iOSΤϯ δχΞ ϦϦʔεΞϓϦ ເ࣮ݱԠԉΞϓϦʮYumehoshiʯ ޿ࠂϒϩοΫΞϓϦ ʮLillyBlocker -޿ࠂϒϩοΫͰ αΫαΫշదSafariʹʂʯ

    2 —
  3. SWIFTͷษڧձΛओ࠵ɺӡӦ͠ ͍ͯ·͢ʂ > beezϓϩάϥϛϯάษڧձ https://www.facebook.com/groups/ 407821455988195/ > Swiftಡॻษڧձ https://www.facebook.com/groups/ 1543718659272111/

    3 —
  4. 2015೥12݄ SWIFT͕Φʔϓϯιʔεʹͳ ͬͨʂ 4 —

  5. WEBϑϨʔϜϫʔΫ΋ͨ͘͞Μ Ͱ͖ͯͨ > Kitura(IBM) > Vapor > SwiftExpress > Blackfish

    > Swifton > Slimane > Perfect 5 —
  6. 2016೥3݄22೔ SWIFT2.2΋ϦϦʔεʂ 6 —

  7. ࠓͦ͜SWIFTΛ࢖ ͓͏ʂ 7 —

  8. Ͱ΋೉ͦ͠͏(T_T) 8 —

  9. Docker࢖͑͹؆୯ͩΑ(ɾ˲ɾ) 9 —

  10. ࠓ೔ͷϋϯζΦϯ͸ΦʔϓϯιʔεͷSwift ͱ஥ྑ͘ͳΔͷ͕໨తͰ͢ɻ 10 —

  11. ࠓ೔΍Δ͜ͱ > Docker Hub͔ΒSwiftͷDockerΠϝʔδΛऔಘ > SwiftͰ͓Έ͘͡ΞϓϦΛ࡞੒ > DockerΠϝʔδΛBluemix΁Ξοϓϩʔυ 11 —

  12. ࣮ߦ؀ڥ > OSX El Capitan > Docker version 1.10.3 >

    docker-machine version 0.6.0 12 —
  13. Ͱ͸͸͡Ί·͠ΐ͏ 13 —

  14. OSXͷ৔߹ DOCKER QUICKSTART TERMINALΛ࣮ߦ͢Δ 14 —

  15. λʔϛφϧ͕ىಈ ؀ڥม਺Λઃఆ͢Δ eval $(docker-machine env) 15 —

  16. λʔϛφϧΛ։͘ ࡞ۀϑΥϧμΛ࡞੒͢Δ mkdir firstSwift cd firstSwift 16 —

  17. Docker HubͷswiftΠϝʔδΛऔಘ͢Δ docker pull swiftdocker/swift Πϝʔδͷμ΢ϯϩʔυʹ͸͕͔͔࣌ؒΔ Πϝʔδͷ֬ೝΛ͢Δ docker images REPOSITORY

    TAG IMAGE ID CREATED SIZE swiftdocker/swift latest 6ee180026200 2 weeks ago 870.8 MB 17 —
  18. ίϯςφΛ࡞੒ͯ͠ϩάΠϯ docker run -it --name swiftcontainer swiftdocker/swift:latest /bin/bash root@e907eeb3e669:/# 18

  19. ࡞ۀϑΥϧμomikujiSwiftΛ࡞੒͢Δ mkdir omikujiSwift cd omikujiSwift mkdir sources cd sources 19

  20. ΞϓϦέʔγϣϯϑΝΠϧ main.swiftΛ࡞੒ swiftΛϏϧυͨ͠৔߹ʹɺ·࣮ͣߦ͞ΕΔͷ͕ ͜ͷmain.swiftϑΝΠϧ vi main.swift 20 —

  21. ԼهͷΑ͏ʹϑΝΠϧΛ࡞Δ //main.swift import Foundation import Glibc //To display the words

    to start a 'omikuji' print("Omikuji start!!") for count in 1...3 { print(count) sleep(1) } //To create a seed for the random number from the number of seconds of the current time srandom(UInt32(NSDate().timeIntervalSince1970)) let player = ["Daikichi", "Kichi", "Cyukichi", "Kichi", "Kyou"] print(player[random() % player.count]) 21 —
  22. ࣮ߦ͢Δͨͼʹ͓Έ͘͡ ͕Ҿ͚ΔΞϓϦͰ͢ʂ 22 —

  23. main.swift͕͔͚ͨΒɺswiftίϚϯυͰ࣮ߦ ͢Δ swift main.swift Omikuji start!! 1 2 3 Daikichi

    23 —
  24. ඪ४ग़ྗͰग़ྗ͞Ε͍ͯΕ͹OK! ϥϯμϜͰग़͍ͯ͠ΔͷͰ࣮ߦ͞ΕΔͨͼ ʹ͓Έ͘͡ͷ݁Ռ͕ҟͳΓ·͢ swift main.swift Omikuji start!! 1 2 3

    Kyou 24 —
  25. SWIFTΛϏϧυ͢ Δʂ 25 —

  26. ಉ͡֊૚ͰPackage.swiftΛ࡞੒͢Δ vi Package.swift 26 —

  27. Package.swift͸ҎԼͷΑ͏ʹ͢Δ //Package.swift import PackageDescription let package = Package( name: "OmikujiSwift"//project

    name ) 27 —
  28. SWIFTΛϏϧυ͢Δ swift build Compiling Swift Module 'OmikujiSwift' (1 sources) Linking

    OmikujiSwift 28 —
  29. Ϗϧυ࣮ͨ͠ߦϑΝΠϧΛ࣮ߦ͢Δ .build/debug/OmikujiSwift Omikuji start!! 1 2 3 Kichi 29 —

  30. ݱࡏͷσΟϨΫτϦʔߏ଄ tree -a . . |-- .build | |-- build.db

    | |-- debug | | |-- OmikujiSwift | | |-- OmikujiSwift.build | | | |-- main.d | | | |-- main.swift.o | | | |-- main.swiftdeps | | | |-- main~partial.swiftdoc | | | |-- main~partial.swiftmodule | | | |-- master.swiftdeps | | | `-- output-file-map.json | | |-- OmikujiSwift.swiftdoc | | `-- OmikujiSwift.swiftmodule | `-- debug.yaml |-- Package.swift `-- main.swift 30 —
  31. ͜͜·ͰͰ͖ͨΒɺίϯςφ͔Βൈ͚Δ exit 31 —

  32. ࠓ࡞ͬͨίϯςφ͔ΒΠϝʔδΛ࡞Δ docker commit swiftcontainer satotakeshi/omikuji_swift 32 —

  33. ࡞੒ΠϝʔδΛ֬ೝ͢Δ docker images REPOSITORY TAG IMAGE ID CREATED SIZE satotakeshi/omikuji_swift

    latest d63340ea7b64 6 seconds ago 897.7 MB swiftdocker/swift latest 6ee180026200 2 weeks ago 870.8 MB 33 —
  34. BLUEMIXͷૢ࡞ 34 —

  35. BluemixʹϩάΠϯ͢Δɻ ஍ҬΛถࠃೆ෦ʹ͢Δɻ 35 —

  36. εϖʔεͷ࡞੒Λ͢Δ 36 —

  37. IBM CONTAINERS CLI ͷηοτΞοϓ > https://github.com/cloudfoundry/cli/releases ͔ΒCloud Foundry CLI όʔδϣϯ

    6.12.0 Ҏ߱ΛΠ ϯετʔϧ͢Δɻ 37 —
  38. Πϯετʔϧ͞Ε͍ͯΔ͜ͱΛ֬ೝ͢Δ cf -v 38 —

  39. Note cfͷίϚϯυͷॴ༗ऀ͸ σϑΥϧτͰrootɻ dockerίϚϯυͷॴ༗ऀͱ ߹ΘͤΔɻ 39 —

  40. ॴ༗ऀ͕ҧ͏৔߹͸ɺ Bluemixͷίϯςφ΁ϩʔΧ ϧͷDockerΠϝʔδΛϓο γϡͰ͖ͳ͍͜ͱ΋ɻ 40 —

  41. ͜ͷϋϯζΦϯͰ͸cfί Ϛϯυ͸ҰൠϢʔβʔΛ ॴ༗ऀʹมߋͯ͠ઌʹਐ Ί·͢ɻ 41 —

  42. OSXͷ৔߹͸ҎԼͷ༷ͳίϚϯυͰCFίϚϯ υΛҰൠϢʔβʔ΁มߋ sudo chown <ҰൠϢʔβʔ> /usr/local/bin/cf sudo chown -R <ҰൠϢʔβʔ>

    /usr/local/share/doc/cf-cli sudo chown -R <ҰൠϢʔβʔ> /Users/<ҰൠϢʔβʔ໊>/.cf 42 —
  43. IBM CONTAINERS ϓϥάΠϯ (cf ic) ωΠςΟϒͷ Docker CLI ίϚϯυΛ࣮ߦͯ͠ ϩʔΧϧͷίϯςφʔΛ؅ཧ͢Δʹ͸ɺIBM

    Containers ϓϥάΠϯ (cf ic) ΛΠϯετʔϧ ͢Δɻ 43 —
  44. OS XͷϓϥάΠϯΠϯετʔϧίϚϯυ cf install-plugin https://static-ice.ng.bluemix.net/ibm-containers-mac Windows 64 ϏοτͷΠϯετʔϧίϚϯυ cf install-plugin

    https://static-ice.ng.bluemix.net/ibm-containers-windows_x64.exe Windows 32 ϏοτͷΠϯετʔϧίϚϯυ cf install-plugin https://static-ice.ng.bluemix.net/ibm-containers-windows_x86.exe 44 —
  45. ΠϯετʔϧऴΘͬͨΒϓϥάΠϯ͕Πϯ ετʔϧͰ͖͍ͯΔ͔Λ֬ೝ͢Δ cf plugins 45 —

  46. Note cf icͰ࢖͏ূ໌ॻ͕μ΢ϯϩʔυ͞Ε· ͢ɻ ॴ༗ऀ͕rootʹͳ͍ͬͯΔ৔߹͸ҰൠϢʔβ ʔʹॴ༗ऀΛมߋ͠·͢ɻ sudo chown -R <ҰൠϢʔβʔ>

    /Users/<ҰൠϢʔβʔ>/.ice 46 —
  47. BLUEMIXʹ΋͏Ұ౓ϩάΠϯ ίϯςφʔͷ։࢝ΛΫϦοΫ 47 —

  48. ίϯςφʔɾΠϝʔδ ϚΠ૊৫ΛΫϦοΫ 48 —

  49. Bluemix΁ͷΠϝʔδͷϓογϡํ๏͕ දࣔ͞ΕΔ 49 —

  50. cf loginΛ࣮ߦ ίϚϯυϥΠϯͰBluemix ʹϩάΠϯ -aΦϓγϣϯΛࢦఆͯ͠ɺAPIͷϦʔδϣϯ ΛೆΞϝϦΧͷapi.ng.bluemix.netΛࢦఆ͢ Δ cf login -a

    api.ng.bluemix.net 50 —
  51. BluemixͷϩάΠϯ৘ใɺϝʔϧΞυϨεͱύ εϫʔυΛೖྗ͢Δ 51 —

  52. IBM CONTAINERS CF CLI ϓϥάΠϯΛ࣮ߦ͠·͢ɻ1 cf ic login 1 ໊લۭؒͷઃఆ͕·ͩͷ৔߹͸ઃఆΛ͠·͢ɻcf

    ic namespace set [YOUR NAMESPACE] ໊લۭؒ͸Ұ౓ઃఆ͢Δͱมߋ͕Ͱ͖·ͤΜɻ 52 —
  53. BLUEMIXʹࠓ࡞ͬͨ DOCKERΠϝʔδΛ ϓογϡ͢Δ 53 —

  54. ΠϝʔδΛ IBM CONTAINERS Ϩ δετϦʔ಺ͷϓϥΠϕ ʔτ໊લۭؒͰλά෇͚ ͠·͢ɻ 54 —

  55. ϩʔΧϧͷDOCKERΠϝʔδsatotakeshi/ omikuji_swiftΛswiftomikujiͱͯ͠ɺλά෇ ͚͢Δɻ docker tag satotakeshi/omikuji_swift registry.ng.bluemix.net/[ࣗ෼ͷ໊લۭؒ]/swiftomikuji 55 —

  56. ͜ͷΠϝʔδΛ IBM CONTAINERS ϨδετϦʔʹ ϓογϡ͠·͢ɻ docker push registry.ng.bluemix.net/[ࣗ෼ͷ໊લۭؒ]/swiftomikuji 56 —

  57. BLUEMIX্ͷΠϝʔδ͔ΒίϯςφΛ࡞੒͢Δ cf ic run -it --name container_swift registry.ng.bluemix.net/[ࣗ෼ͷ໊લۭؒ]/swiftomikuji /bin/bash 57

  58. ίϯςφʹϩάΠϯͯ͠ɺϏϧυ͓ͨ͠Έ ͘͡SWIFTΞϓϦΛ࣮ߦ͢Δ omikujiSwift/sources/.build/debug/OmikujiSwift Omikuji start!! 1 2 3 Daikichi 58

  59. ͜ΕͰ͋ͳͨ΋ʂ SWIFTͱ஥ྑ͘ͳΕΔʂ 59 —

  60. ࢀߟจݙ > IBM Containers CLI ͷηοτΞοϓ > Πϝʔδͷ؅ཧ > Getting

    Started with Swift on Linux > Docker Hub swiftdocker/swift 60 —