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

[トレタ社内勉強会 "Dockerの上でRailsアプリを動かす ハンズオン

[トレタ社内勉強会 "Dockerの上でRailsアプリを動かす ハンズオン

0ec58a040e1e4e959c8566484b4bba19?s=128

Yuichiro MASUI

April 13, 2016
Tweet

More Decks by Yuichiro MASUI

Other Decks in Programming

Transcript

  1. Docker hands-on in Toreta Yuichiro MASUI 
 masuidrive@toreta.in

  2. Dockerͱ͸ • Ծ૝Խٕज़ͷ૊Έ߹Θͤ • ܰྔ • Πϝʔδ഑෍ͷ࢓૊Έ͕੔͍ͬͯΔ • ࢖͍ࣺͯͷΠϯελϯεͱͷ૬ੑ͕ྑ͍

  3. ϑΝΠϧͱϓϩηε Host OS ϓϩηε ϓϩηε ϓϩηε FS ϓϩηε FS ϑΝΠϧγεςϜ

    Docker daemon Container Container Network Network
  4. Docker Container • ϓϩηεͷִ཭ • Dockerϓϩηε͔Βଞͷϓϩηε͸ݟ͑ͳ͍ • ϑΝΠϧγεςϜͷִ཭ • ϓϩηεຖʹݟ͑ΔϑΝΠϧ͸ҧ͏

  5. ϑΝΠϧͱϓϩηε

  6. Docker Machine Host OS ϓϩηε ϓϩηε ϑΝΠϧγεςϜ Docker daemon VirtualBox

    OSX / Windows Docker Machine ϓϩηε FS ϓϩηε FS Network Network
  7. DockerͰγΣϧىಈ • “Docker Quickstart Terminal”Λ3ຕىಈ • docker run -it ubuntu:14.04

    /bin/bash • ps aux • touch /abc123 • docker run -it ubuntu:14.04 /bin/bash • ls / • docker ps
  8. ͳʹ͕ى͍ͬͯ͜Δ͔ • docker run -it ubuntu:14.04 /bin/bash • “ubuntu:14.04” ࣮ߦ͢ΔϑΝΠϧΠϝʔδ

    • ϩʔΧϧʹͳ͍৔߹͸μ΢ϯϩʔυ • ΠϝʔδΛల։͠DockerͰىಈ • “-it” ίϯιʔϧΛܨ͛Δ
  9. ͳʹ͕ى͍ͬͯ͜Δ͔ • ىಈ͢ΔͨΊʹ৽͍͠ϑΝΠϧγεςϜΛ࡞Δ • docker ps -a • docker rm

    CONTAINER_ID
  10. Πϝʔδ • ΠϝʔδϑΝΠϧ͸Docker HubͰ഑෍ • ഑෍͢Δ࢓૊ΈΛDocker RegistryͱݺͿ • ֤छΠϝʔδ͕ެࣜʹ഑෍͞Ε͍ͯΔ •

    https://hub.docker.com/_/ubuntu/
  11. Πϝʔδ • OSҎ֎ʹΞϓϦͷΠϝʔδ΋ఏڙ͞Ε͍ͯΔ • docker run -it --rm ruby:2.3.0 /bin/bash

    • ruby -v • “--rm" ίϯςφΛऴྃ࣌ʹϑΝΠϧΛফ͢
  12. ࣗ෼ͷΠϝʔδΛ࡞Δ • ΠϝʔδʹมߋΛՃ͑Δ • Dockerfile FROM ubuntu:14.04 RUN apt-get update

    RUN apt-get install -y apache2 CMD ["/usr/sbin/apache2ctl","-D","FOREGROUND"]
  13. DockfileΛىಈ • docker build -t myapache ./ • “-t myapache”

    Πϝʔδ໊ • “./“ DockerfileͷσΟϨΫτϦ • docker run -p 8080:80 myapache • http://192.168.99.100:8080 • docker-machine ip
  14. docker-machine • docker-machine restart • $(docker-machine env)

  15. ڧ੍ఀࢭ • docker ps • docker stop CONTAINER_ID

  16. ϗετͷϑΝΠϧΛϚ΢ϯτ • docker run -v `pwd`:/var/www/html -p 8080:80 myapache

  17. Containerؒ௨৴ • Container͸ಠཱ͍ͯ͠ΔͷͰlocalhostͰ͸ΞΫ ηεͰ͖ͳ͍ • ͦͷลΛ؆୯ʹ͢Δͷ͕Docker Compose

  18. docker-compose.yml rails:
 build: .
 command: [“./bin/rails","server","-b","0.0.0.0"]
 mem_limit: 512m
 ports:
 -

    "3000:3000"
 volumes:
 - ".:/usr/src/app"
 links:
 - "mysql" mysql:
 image: mysql:5.6
 environment:
 MYSQL_ROOT_PASSWORD: "passwd"
  19. ΊΜͲ͍ͷͰϫϯϥΠφʔ • DockerͰRails։ൃ؀ڥΛ࡞ΔϫϯϥΠφʔ • http://qiita.com/masuidrive/items/7478fb9101652f2bbae1

  20. docker-compose ࣮ߦ • docker-compose build • docker-compose up • docker-compose

    rm
  21. database.yml • default: &default
 adapter: mysql2
 username: root
 password: "passwd"


    host: mysql • production:
 <<: *default
 host: <%= ENV[‘MYSQL_HOST'] %>
 ↑͜ͷߦΛ௥Ճ
  22. scaffold • docker-compose up -d • docker-compose run rails /bin/bash

    • rails g scaffold item key:string value:text • rake db:create db:migrate • http://192.168.99.100:3000/items • docker-compose stop
  23. Amazon ECS & ECR • Amazon EC2 Container System •

    Dockerͱecs-agentೖΓઐ༻ͷAMI • DockerΛૢ࡞͢Δίϯιʔϧ & API • Amazon EC2 Container Registry • Docker Registry
  24. ECRʹRepositoryΛ࡞੒ • ը໘ӈ্ͷRegion͸N.Virginia • Sample appͷνΣοΫΛ֎ͯ͠ • “sample1”ͷϦϙδτϦΛ࡞Δ

  25. ECRʹimageΛpush • $(aws ecr get-login --region us-east-1) • docker build

    -t sample1 . • docker tag sample1:latest 123456.dkr.ecr.us- east-1.amazonaws.com/sample1:latest • docker push 123456.dkr.ecr.us- east-1.amazonaws.com/sample1:latest
  26. RDS࡞Ζ͏ • ύεϫʔυ͸޷͖ͳϞϊΛ • Publicly access͸”NO”Ͱ

  27. ECS Cluster

  28. ECS instance • cluster࡞ͬͯ • ecs AMIͰΠϯελϯε࡞ΔɻIAM role๨Εͣʹ • http://docs.aws.amazon.com/AmazonECS/latest/

    developerguide/instance_IAM_role.html
  29. None
  30. None
  31. None
  32. None
  33. None
  34. Task defnition • docker-composeʹ૬౰ • ίϯςφͷͨΊͷ؀ڥΛઃఆ

  35. None
  36. None
  37. None
  38. ಉ͡Α͏ʹmigrate Task΋ • appͱ΄΅ಉ͡ • Port͸ઃఆ͠ͳ͍ • Command͸ɺ./bin/rake,db:migrate

  39. Service

  40. Service ͱ Task • Service͕TaskΛ্ཱͪ͛Δ • Task͸1ͭҎ্ͷContainerΛ༗͢Δ • Task͕མͪΔͱService͕ࣗಈతʹTaskΛىಈ •

    Service͔Βىಈ͞Εͳ͍λεΫ͸୯ൃىಈ • rake db:migrateͱ͔
  41. production.logΛݟΔ • sshͯ͠ • docker ps • docker exec -it

    CONTAINER_ID /bin/bash