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

docker for rubyists

docker for rubyists

miamirb 10/16/2017

Bryce "BonzoESC" Kerley

October 17, 2017
Tweet

More Decks by Bryce "BonzoESC" Kerley

Other Decks in Programming

Transcript

  1. Rails in 2005 • Compile Ruby from source • Install

    Rubygems separately • Choose your own adventure: • CGI (slow) • FastCGI (quite miserable to set up) • App server (fast, low-grade miserable to set up) • Configure database • Figure out gem dependencies
  2. Rails in 2017 • Choose your own install Ruby adventure

    • rbenv • chruby • rvm • PPA • source • Configure database • Install dependencies
  3. Vagrant • Makes setting up a VM repeatable • Good

    but slow for development • Less common in production
  4. Docker • Good for development • Good and common in

    production • Plenty fast • Lots of work is done
  5. Greatjobify 1. Pick base image 2. Add OS packages 3.

    Create and change to app directory 4. Add Gemfile and Gemfile.lock 5. Bundle 6. Add rest of app 7. Run it
  6. Dockerfile FROM ruby:2.4.1 
 RUN apt-get update -qq && \

    
 apt-get install -y build-essential imagemagick 
 RUN mkdir /greatjobify 
 WORKDIR /greatjobify 
 ADD Gemfile* /greatjobify/ 
 RUN bundle install 
 ADD . /greatjobify 
 EXPOSE 9292 
 CMD ["unicorn", "-p", "9292"]
  7. Dockerfile FROM ruby:2.4.1 
 RUN apt-get update -qq && \

    
 apt-get install -y build-essential imagemagick 
 RUN mkdir /greatjobify 
 WORKDIR /greatjobify 
 ADD Gemfile* /greatjobify/ 
 RUN bundle install 
 ADD . /greatjobify 
 EXPOSE 9292 
 CMD ["unicorn", "-p", "9292"] Base image
  8. Dockerfile FROM ruby:2.4.1 
 RUN apt-get update -qq && \

    
 apt-get install -y build-essential imagemagick 
 RUN mkdir /greatjobify 
 WORKDIR /greatjobify 
 ADD Gemfile* /greatjobify/ 
 RUN bundle install 
 ADD . /greatjobify 
 EXPOSE 9292 
 CMD ["unicorn", "-p", "9292"] OS packages
  9. Dockerfile FROM ruby:2.4.1 
 RUN apt-get update -qq && \

    
 apt-get install -y build-essential imagemagick 
 RUN mkdir /greatjobify 
 WORKDIR /greatjobify 
 ADD Gemfile* /greatjobify/ 
 RUN bundle install 
 ADD . /greatjobify 
 EXPOSE 9292 
 CMD ["unicorn", "-p", "9292"] Create and change to app directory
  10. Dockerfile FROM ruby:2.4.1 
 RUN apt-get update -qq && \

    
 apt-get install -y build-essential imagemagick 
 RUN mkdir /greatjobify 
 WORKDIR /greatjobify 
 ADD Gemfile* /greatjobify/ 
 RUN bundle install 
 ADD . /greatjobify 
 EXPOSE 9292 
 CMD ["unicorn", "-p", "9292"] Add Gemfile and Gemfile.lock
  11. Dockerfile FROM ruby:2.4.1 
 RUN apt-get update -qq && \

    
 apt-get install -y build-essential imagemagick 
 RUN mkdir /greatjobify 
 WORKDIR /greatjobify 
 ADD Gemfile* /greatjobify/ 
 RUN bundle install 
 ADD . /greatjobify 
 EXPOSE 9292 
 CMD ["unicorn", "-p", "9292"] Bundle
  12. Dockerfile FROM ruby:2.4.1 
 RUN apt-get update -qq && \

    
 apt-get install -y build-essential imagemagick 
 RUN mkdir /greatjobify 
 WORKDIR /greatjobify 
 ADD Gemfile* /greatjobify/ 
 RUN bundle install 
 ADD . /greatjobify 
 EXPOSE 9292 
 CMD ["unicorn", "-p", "9292"] Add rest of the app
  13. Dockerfile FROM ruby:2.4.1 
 RUN apt-get update -qq && \

    
 apt-get install -y build-essential imagemagick 
 RUN mkdir /greatjobify 
 WORKDIR /greatjobify 
 ADD Gemfile* /greatjobify/ 
 RUN bundle install 
 ADD . /greatjobify 
 EXPOSE 9292 
 CMD ["unicorn", "-p", "9292"] Declare a listening port
  14. Dockerfile FROM ruby:2.4.1 
 RUN apt-get update -qq && \

    
 apt-get install -y build-essential imagemagick 
 RUN mkdir /greatjobify 
 WORKDIR /greatjobify 
 ADD Gemfile* /greatjobify/ 
 RUN bundle install 
 ADD . /greatjobify 
 EXPOSE 9292 
 CMD ["unicorn", "-p", "9292"] Command to run
  15. Dockerfile FROM ruby:2.4.1 
 RUN apt-get update -qq && \

    
 apt-get install -y build-essential imagemagick 
 RUN mkdir /greatjobify 
 WORKDIR /greatjobify 
 ADD Gemfile* /greatjobify/ 
 RUN bundle install 
 ADD . /greatjobify 
 EXPOSE 9292 
 CMD ["unicorn", "-p", "9292"]
  16. Running it docker run --rm -p 9292:9292 -it greatjobify Remove

    the instance afterwards Forward instance port 9292 to host port 9292 Keep stdio open and hook it up here The image to run
  17. External Services • Instances can run multiple processes • Instances

    should be single-purpose • Horizontal scalability • Orchestrate several instances to work together
  18. docker-compose.yml • Describe each instance that you need • base

    image or build directory • mounted volumes • dependencies • open ports • commands • environment variables
  19. web.rb (snippet) memcache_address = ENV['MEMCACHIER_SERVERS'] || 'localhost:11211' 
 memcache =

    [ 
 memcache_address, 
 { 
 username: ENV['MEMCACHIER_USERNAME'] || nil, 
 password: ENV['MEMCACHIER_PASSWORD'] || nil 
 } 
 ] 
 dalli = Dalli::Client.new *memcache 
 
 use Rack::Cache, 
 :metastore => dalli, 
 :entitystore => dalli
  20. docker-compose.yml • Web service • Build the current directory •

    Expose 9292 • Depend on memcached service • Set MEMCACHIER_SERVERS environment variable • Memcached service • Use the "memcached" image
  21. docker-compose.yml version: '3' 
 services: 
 web: 
 build: .

    
 ports: 
 - "9292:9292" 
 depends_on: 
 - memcached 
 environment: 
 - MEMCACHIER_SERVERS=memcached 
 memcached: 
 image: memcached
  22. Rails App • Similar to Sinatra • Web process •

    Db processes • Support processes • Database console • App console