Containerized
Ruby Applications
with Docker
Laura Frank
rheinwein
@rhein_wein
Slide 2
Slide 2 text
No content
Slide 3
Slide 3 text
+
Slide 4
Slide 4 text
Containerization: What even is it?
How do I use Docker within my applications?
How can I use Docker with Ruby?
Three Questions
Slide 5
Slide 5 text
No content
Slide 6
Slide 6 text
Docker may seem trendy right now…
but there’s a reason it’s popular.
Slide 7
Slide 7 text
Docker != containers
Slide 8
Slide 8 text
A tool for managing containers
• Executing and running code
• Managing code
Docker
Slide 9
Slide 9 text
First, let’s talk about containers.
Slide 10
Slide 10 text
• Run in a self-contained execution environment
• Share the kernel of host system
• Are isolated from other containers
• Have fast boot times & low overhead
Containers
A container is a virtualization layer — sort of
like a VM — but with some fundamental
differences
Containers can work in conjunction with or in
place of virtual machines (VMs).
Slide 13
Slide 13 text
hardware
host OS
hypervisor
$ guest OS
libraries
web
$ guest OS $ guest OS
libraries
DB
libraries
web
Slide 14
Slide 14 text
hardware
host OS
libraries
web
libraries
DB
libraries
web
Slide 15
Slide 15 text
hardware
host OS
container runtime engine
libraries libraries
web web DB
Slide 16
Slide 16 text
Containers have slightly more complexity
but
They reduce the amount of time/space
resources needed to run your application
Slide 17
Slide 17 text
No content
Slide 18
Slide 18 text
A tool for managing containers
• Executing and running code
• Managing code
Docker
Anything else? You need to use a lightweight VM.
Pro Tip: Boot2Docker (OSX and Windows)
Installing Docker
Linux? Install Docker with official packages.
Kitematic (OSX)
Slide 25
Slide 25 text
CLI
REST API
docs.docker.com
Interacting with Docker
Slide 26
Slide 26 text
No content
Slide 27
Slide 27 text
Docker Images
An image is controlled by a Dockerfile
docker build -t foo/bar .
docker pull foo/bar
Slide 28
Slide 28 text
• Static: all files and code are contained in
image
• Dynamic: link folders to actively modify
code (development only)
Sehr wichtig/very important/¡muy importante!
Docker Images
Slide 29
Slide 29 text
FROM centurylink/ruby-base:2.1.2
MAINTAINER Laura Frank
EXPOSE 4567
RUN mkdir -p /usr/src/app
ADD . /usr/src/app
WORKDIR /usr/src/app
RUN bundle install
CMD “ruby hello_world.rb"
Dockerfile
Slide 30
Slide 30 text
FROM centurylink/ruby-base:2.1.2
MAINTAINER Laura Frank
EXPOSE 4567
RUN mkdir -p /usr/src/app
ADD . /usr/src/app
WORKDIR /usr/src/app
RUN bundle install
CMD “ruby hello_world.rb"
Dockerfile
Slide 31
Slide 31 text
FROM centurylink/ruby-base:2.1.2
MAINTAINER Laura Frank
EXPOSE 4567
RUN mkdir -p /usr/src/app
ADD . /usr/src/app
WORKDIR /usr/src/app
RUN bundle install
CMD “ruby hello_world.rb"
Dockerfile
Slide 32
Slide 32 text
FROM centurylink/ruby-base:2.1.2
MAINTAINER Laura Frank
EXPOSE 4567
RUN mkdir -p /usr/src/app
ADD . /usr/src/app
WORKDIR /usr/src/app
RUN bundle install
CMD “ruby hello_world.rb"
Dockerfile
Slide 33
Slide 33 text
FROM centurylink/ruby-base:2.1.2
MAINTAINER Laura Frank
EXPOSE 4567
RUN mkdir -p /usr/src/app
ADD . /usr/src/app
WORKDIR /usr/src/app
RUN bundle install
CMD “ruby hello_world.rb"
Dockerfile
Slide 34
Slide 34 text
FROM centurylink/ruby-base:2.1.2
MAINTAINER Laura Frank
EXPOSE 4567
RUN mkdir -p /usr/src/app
ADD . /usr/src/app
WORKDIR /usr/src/app
RUN bundle install
CMD “ruby hello_world.rb"
Dockerfile
Slide 35
Slide 35 text
FROM centurylink/ruby-base:2.1.2
MAINTAINER Laura Frank
EXPOSE 4567
RUN mkdir -p /usr/src/app
ADD . /usr/src/app
WORKDIR /usr/src/app
RUN bundle install
CMD “ruby hello_world.rb"
Dockerfile
Slide 36
Slide 36 text
ENV KEY value
Declare any environment variables that will be passed
to any RUN command.
VOLUME /var/foo
VOLUME [“/var/foo”, “/var/bar”]
creates a mount point for volumes from host or other
containers
Slide 37
Slide 37 text
FROM centurylink/ruby-base:2.1.2
Dockerfile
No need for gemset or version management,
just use a different base image.
Base images are great!
Slide 38
Slide 38 text
Official Ruby Images
https://registry.hub.docker.com/_/ruby/
Support for 1.9.3+
docker pull ruby:2.1.X
Repository includes instructions for
bootstrapping
Slide 39
Slide 39 text
Gems
From Swipely:
docker-api: for interacting with the Docker API
from your Ruby application
Over 30 other gems
Slide 40
Slide 40 text
(Sorry.)
…is kind of a pain.
Debugging in a Container…
Slide 41
Slide 41 text
Debugging in a Container
Use Pry.
require ‘pry’
class Thing
def some_method
binding.pry
#some brilliant broken code
end
end
Slide 42
Slide 42 text
Debugging in a Container
No need for a remote session*
Alternatively, use pry command:
docker run centurylink/panamax-api pry
Slide 43
Slide 43 text
No content
Slide 44
Slide 44 text
Application Architecture
with Docker
Slide 45
Slide 45 text
Simply put, Docker architecture is
service-oriented architecture.
If a service fails, new containers can be
spun up in milliseconds.
Slide 46
Slide 46 text
DB
Web
One Service, One Container
Slide 47
Slide 47 text
Bind 8080:4567 Expose 3306
link
One Service, One Container
DB
Web
Slide 48
Slide 48 text
Configuration can happen in two places:
The Dockerfile, by baking config
options into the service’s base image
The docker run string, by specifying
configuration options with various flags
Configuring an Application
Slide 49
Slide 49 text
Bind 8080:4567 Expose 3306
One Service, One Container
link DB
Web
Slide 50
Slide 50 text
Bind 8080:4567
The Dockerfile
Link: DB
Web
FROM centurylink/ruby-base:2.1.2
MAINTAINER Laura Frank
RUN mkdir -p /usr/src/app
ADD . /usr/src/app
WORKDIR /usr/src/app
RUN bundle install
CMD ["ruby", "hello_world.rb"]
Slide 51
Slide 51 text
Bind 8080:4567
FROM centurylink/ruby-base:2.1.2
MAINTAINER Laura Frank
ENV APIKEY=superSeCrEt01234567abcdef!
RUN mkdir -p /usr/src/app
ADD . /usr/src/app
WORKDIR /usr/src/app
RUN bundle install
CMD ["ruby", "hello_world.rb"]
The Dockerfile
Link: DB
Web
Slide 52
Slide 52 text
Bind 8080:4567
Link: DB
The Docker Run String
Web
docker run -p 8080:4567
-e “APIKEY=superSeCrEt01234567abcdef!”
—-link db:db my-image
Slide 53
Slide 53 text
The Docker Run String
Each container has own docker run string
Can only start one container at a time via CLI
Multiple containers can run from the same image
Find balance between docker run and Dockerfile
Slide 54
Slide 54 text
…that sounds like a lot of tedious work.
Slide 55
Slide 55 text
No content
Slide 56
Slide 56 text
Application Templating
Use your own images, or images from
the Docker Registry
Specify config options beforehand
Run applications with one or two simple
commands
Slide 57
Slide 57 text
Standards tightly coupled with Docker
docs.docker.com/compose
Dump requirements into docker-compose.yml and
run with docker-compose up
Docker Compose
Slide 58
Slide 58 text
Docker workflow tool
Itself a containerized application
panamax.io
Uses CoreOS, Fleet, and etcd for orchestration
and service discovery
Slide 59
Slide 59 text
Templating language similar to docker compose
Supports remote deployments
panamax.io/get-panamax
Parting Thoughts
Extract commonalities from images in order
to share them
Don’t include any user- or org-specific
configurations in an image or application template
Boot2Docker
Use an application template