rails applications with docker

40
Building Rails Applications with Docker Laura Frank @rhein_wein Software En g ineer @codeship

Upload: laura-frank

Post on 21-Jan-2018

7.308 views

Category:

Technology


0 download

TRANSCRIPT

Page 1: Rails Applications with Docker

Building  Rails  Applications  with  

DockerLaura Frank @rhein_wein

Software Engineer @codeship

Page 2: Rails Applications with Docker

Today’s  Agenda

• An introduction to containers: what are they, and what makes them different from virtual machines?

• The Docker ecosystem

• Working with Docker on a Rails project

2

Page 3: Rails Applications with Docker

An  Introduction  to  Containerization

Page 4: Rails Applications with Docker
Page 5: Rails Applications with Docker

Docker  !=  containers

Page 6: Rails Applications with Docker

Docker  manages  containers

• Builds images to run as containers

• Can manage entire applications with docker-compose

• Provision machines with docker-machine

6

Page 7: Rails Applications with Docker

A container is a virtualization layer — sort of like a VM — but with some fundamental differences

Page 8: Rails Applications with Docker

Containers

• Run in a self-contained execution environment

• Share the kernel of host system

• Are isolated from other containers

• Have fast boot times & low overhead

8

Page 9: Rails Applications with Docker

infrastructure

host OS

hypervisor

$ guest OS

libraries

service 0

$ guest OS $ guest OS

libraries

service 2

libraries

service 1

Page 10: Rails Applications with Docker

libraries librarieslibraries

infrastructure

host OS

service 0 service 2service 1

Page 11: Rails Applications with Docker

host OS

libraries librarieslibraries

infrastructure

container runtime engine

service 0 service 2service 1

Page 12: Rails Applications with Docker

Getting started with containers may initially seem more complex…

Page 13: Rails Applications with Docker

…But they greatly reduce the amount of time and space needed

to run your application

Page 14: Rails Applications with Docker

Spend less time provisioning, rebooting, and fighting with

dependencies, and more time building what you want.

TL;DR

Page 15: Rails Applications with Docker

The  Docker  Ecosystem  

Page 16: Rails Applications with Docker

Build Ship Run

Page 17: Rails Applications with Docker

Docker  Images

Page 18: Rails Applications with Docker

Are images containers?No… an image is like a class, and a container is an

instance of that class

Page 19: Rails Applications with Docker

The  Docker  Hub

19

Page 20: Rails Applications with Docker

• ~15,000 images that you can pull down and use in your own projects

• Also includes additional features like webhooks, build triggers, authentication, and private repositories

• Use either the web GUI or the CLI — familiar workflow of login, push, pull, search, etc.

The  Docker  Hub

Page 21: Rails Applications with Docker

Different  Types  of  Images

• Service: self-contained images that provide a self-contained service out-of-the-box. Postgres, MySQL.

• Project Base: intended to serve as a base for your own project; do not directly provide a service. Ruby, golang.

• Official Images: maintained by the organization/company themselves (i.e. the official Rails image)

Page 22: Rails Applications with Docker

• Build them yourself by creating a Dockerfile

• docker  build  -­‐t  image_name  .  \  #  don’t  forget  the  dot!  

• run docker  images  to see all of the images you’ve downloaded or built

Your  Own  Docker  Images

Page 23: Rails Applications with Docker

A  Rails  DockerfileFROM rails:4.2.4 MAINTAINER Laura Frank <[email protected]> RUN mkdir -p /var/app COPY . /var/app WORKDIR /var/app RUN bundle install CMD rails s -b 0.0.0.0

set versions here

Page 24: Rails Applications with Docker

Use the Docker Hub to find and store images to use in your project.

Use Docker Compose to build it.

Page 25: Rails Applications with Docker

Docker  Compose

25

Page 26: Rails Applications with Docker

Building  Your  First  Rails  App  with  Docker

Page 27: Rails Applications with Docker

Installing  Docker• The old way: boot2docker

• The new way: Docker Toolbox

• Docker Client, Machine, Engine, Compose (on a Mac), Kitematic, and Virtual Box

• You can migrate your boot2docker VM to a Docker Machine machine — check the docs

Page 28: Rails Applications with Docker

Installing  Docker

🐳  docker.com/toolbox

Page 29: Rails Applications with Docker

Development  Goals

Run a Rails app in a container, so that a developer can:

• view the app running in the browser • edit files in a local environment and see the changes • run rake tasks like migrations • see log output

Page 30: Rails Applications with Docker

Remember  This?FROM rails:4.2.4 MAINTAINER Laura Frank <[email protected]> RUN mkdir -p /var/app COPY Gemfile /var/app/Gemfile WORKDIR /var/app RUN bundle install CMD rails s -b 0.0.0.0

Page 31: Rails Applications with Docker
Page 32: Rails Applications with Docker

🚉 🐘

A  Multi-­‐Container  Rails  App

Rails  App Postgres❤

Page 33: Rails Applications with Docker

How can I get containerized services to communicate with one another?container linking, environment variables…

and of course, a little config sugar

Page 34: Rails Applications with Docker

Docker Compose makes this easy.

Page 35: Rails Applications with Docker

db:      image:  postgres  web:      build:  .      command:  bundle  exec  rails  s  -­‐p  3000  -­‐b  '0.0.0.0'      volumes:          -­‐  .:/var/app      ports:          -­‐  '3333:3000'      links:          -­‐  db

docker-­‐compose.yml

Page 36: Rails Applications with Docker

development:  &default      adapter:  postgresql      encoding:  unicode      database:  postgres      pool:  5      username:  postgres      password:      host:  db

config/database.yml

Page 37: Rails Applications with Docker
Page 38: Rails Applications with Docker

Ship  It

A simple CI/CD workflow would include

• running tests • building a new Docker image • pushing that image to a registry • firing up containers with the new image

Page 39: Rails Applications with Docker

#alwayskeepshipping

Page 40: Rails Applications with Docker

thanks!