Rails Applications with Docker

Post on 21-Jan-2018

7308 Views

Category:

Technology

0 Downloads

Preview:

Click to see full reader

Transcript

Building  Rails  Applications  with  

DockerLaura Frank @rhein_wein

Software Engineer @codeship

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

An  Introduction  to  Containerization

Docker  !=  containers

Docker  manages  containers

• Builds images to run as containers

• Can manage entire applications with docker-compose

• Provision machines with docker-machine

6

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

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

infrastructure

host OS

hypervisor

$ guest OS

libraries

service 0

$ guest OS $ guest OS

libraries

service 2

libraries

service 1

libraries librarieslibraries

infrastructure

host OS

service 0 service 2service 1

host OS

libraries librarieslibraries

infrastructure

container runtime engine

service 0 service 2service 1

Getting started with containers may initially seem more complex…

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

to run your application

Spend less time provisioning, rebooting, and fighting with

dependencies, and more time building what you want.

TL;DR

The  Docker  Ecosystem  

Build Ship Run

Docker  Images

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

instance of that class

The  Docker  Hub

19

• ~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

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)

• 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

A  Rails  DockerfileFROM rails:4.2.4 MAINTAINER Laura Frank <laura@codeship.com> 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

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

Use Docker Compose to build it.

Docker  Compose

25

Building  Your  First  Rails  App  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

Installing  Docker

🐳  docker.com/toolbox

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

Remember  This?FROM rails:4.2.4 MAINTAINER Laura Frank <laura@codeship.com> RUN mkdir -p /var/app COPY Gemfile /var/app/Gemfile WORKDIR /var/app RUN bundle install CMD rails s -b 0.0.0.0

🚉 🐘

A  Multi-­‐Container  Rails  App

Rails  App Postgres❤

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

and of course, a little config sugar

Docker Compose makes this easy.

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

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

config/database.yml

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

#alwayskeepshipping

thanks!

top related