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

Docker - next big thing

Docker - next big thing

By @jwo for HoustonRB

I've been working in Docker for a couple of months, including an engagement to move a complicated multi-server setup to Docker, where you can `docker-compose up` and have a complete setup up and running.

It's useful for developing Rails 2.3 apps as well as orchestrating complicated deployments. I've found there's no need to use chef/ansible with Docker, but it can absolutely be done.

We'll create a Nginx+Rails(puma)+Postgres+Redis+Memcached server setup, and get it online on both our Macs and on a cheap'ish Digital Ocean droplet.

Jesse Wolgamott

June 09, 2015
Tweet

More Decks by Jesse Wolgamott

Other Decks in Technology

Transcript

  1. Docker - next big thing Jesse Wolgamott
    Docker - next big thing
    By @jwo for HoustonRB
    0

    View Slide

  2. View Slide

  3. Docker
    Run in linux, just like production
    It's like heroku, for development

    View Slide

  4. Laws of Docker
    1 Process per Container
    "Containers" are servers

    View Slide

  5. Standard Rails App
    Rails
    PostgreSQL
    Redis
    Memcached
    Sidekiq

    View Slide

  6. Startup - Development
    Clone
    bundle
    rake db:setup
    brew install postgresql
    createdb
    brew install redis
    brew install memcached
    foreman start

    View Slide

  7. Startup - Production
    install ruby from source. maybe rbenv?
    sudo apt-get install postgresql, redis, memcached
    bundle
    create database and user
    rake db:migrate
    configure nginx and unicorn

    View Slide

  8. ETOOMANYDIFFS
    New users, many dependencies, such frustration

    View Slide

  9. Startup, Dev and Live
    1. g
    i
    t c
    l
    o
    n
    e
    2. d
    o
    c
    k
    e
    r
    -
    c
    o
    m
    p
    o
    s
    e b
    u
    i
    l
    d
    3. d
    o
    c
    k
    e
    r
    -
    c
    o
    m
    p
    o
    s
    e u
    p

    View Slide

  10. Comparison vs Chef
    No need for chef or ansible for most cases
    Deploy and you're good

    View Slide

  11. Docker Compose
    Was "fig" (older documentation)
    Fancy term: "Coordination"
    Real World: connects your DB to your Rails

    View Slide

  12. docker-compose.yml
    m
    y
    s
    q
    l
    d
    a
    t
    a
    :
    i
    m
    a
    g
    e
    : c
    o
    g
    n
    i
    t
    e
    e
    v
    /
    e
    c
    h
    o
    v
    o
    l
    u
    m
    e
    s
    :
    - /
    v
    a
    r
    /
    l
    i
    b
    /
    m
    y
    s
    q
    l
    d
    b
    :
    b
    u
    i
    l
    d
    : m
    y
    s
    q
    l
    :
    l
    a
    t
    e
    s
    t
    e
    x
    p
    o
    s
    e
    :
    - "
    3
    3
    0
    6
    "
    v
    o
    l
    u
    m
    e
    s
    _
    f
    r
    o
    m
    :
    - m
    y
    s
    q
    l
    d
    a
    t
    a
    a
    p
    p
    :
    b
    u
    i
    l
    d
    : .
    c
    o
    m
    m
    a
    n
    d
    : b
    u
    n
    d
    l
    e e
    x
    e
    c r
    a
    i
    l
    s s -
    p 3
    0
    0
    0 -
    b '
    0
    .
    0
    .
    0
    .
    0
    '
    v
    o
    l
    u
    m
    e
    s
    :
    - .
    :
    /
    m
    y
    a
    p
    p
    p
    o
    r
    t
    s
    :
    - "
    3
    0
    0
    0
    :
    3
    0
    0
    0
    "
    l
    i
    n
    k
    s
    :
    - d
    b

    View Slide

  13. Boot2docker
    Have to have it so your Macs can run Linux in your Linux

    View Slide

  14. HOWTO Persist Data
    (Like Postgres, etc Data)
    Have your db data directory connect to a "data container"
    m
    y
    s
    q
    l
    d
    a
    t
    a
    :
    i
    m
    a
    g
    e
    : c
    o
    g
    n
    i
    t
    e
    e
    v
    /
    e
    c
    h
    o
    v
    o
    l
    u
    m
    e
    s
    :
    - /
    v
    a
    r
    /
    l
    i
    b
    /
    m
    y
    s
    q
    l
    d
    b
    :
    b
    u
    i
    l
    d
    : m
    y
    s
    q
    l
    :
    l
    a
    t
    e
    s
    t
    e
    x
    p
    o
    s
    e
    :
    - "
    3
    3
    0
    6
    "
    v
    o
    l
    u
    m
    e
    s
    _
    f
    r
    o
    m
    :
    - m
    y
    s
    q
    l
    d
    a
    t
    a

    View Slide

  15. HOWTO Deploy your
    docker
    SSH into your main box
    g
    i
    t c
    l
    o
    n
    e
    d
    o
    c
    k
    e
    r
    -
    c
    o
    m
    p
    o
    s
    e b
    u
    i
    l
    d
    d
    o
    c
    k
    e
    r
    -
    c
    o
    m
    p
    o
    s
    e s
    t
    o
    p &
    & d
    o
    c
    k
    e
    r
    -
    c
    o
    m
    p
    o
    s
    e s
    t
    a
    r
    t

    View Slide

  16. HOWTO Get a main box
    with docker
    1. Digital Ocean $5 plan
    2. Choose Docker
    3. Win.

    View Slide

  17. HOWTO Edit your Rails
    locally
    Do everything the same except:
    instead of
    http://192.168.59.103:3000/
    http://localhost:3000

    View Slide

  18. HOWTO run migrations
    d
    o
    c
    k
    e
    r
    -
    c
    o
    m
    p
    o
    s
    e r
    u
    n a
    p
    p r
    a
    k
    e d
    b
    :
    m
    i
    g
    r
    a
    t
    e

    View Slide

  19. HOWTO Environment files
    Add in a .env file like you normally would. Gitignore it.
    m
    y
    s
    q
    l
    :
    e
    n
    v
    _
    f
    i
    l
    e
    : D
    o
    c
    k
    e
    r
    C
    o
    n
    f
    i
    g
    /
    .
    e
    n
    v
    v
    o
    l
    u
    m
    e
    s
    _
    f
    r
    o
    m
    :
    - m
    y
    s
    q
    l
    d
    a
    t
    a
    c
    o
    n
    t
    a
    i
    n
    e
    r

    View Slide

  20. HOWTO database.yml
    p
    r
    o
    d
    u
    c
    t
    i
    o
    n
    :
    a
    d
    a
    p
    t
    e
    r
    : m
    y
    s
    q
    l
    2
    d
    a
    t
    a
    b
    a
    s
    e
    : y
    o
    l
    o
    _
    p
    r
    o
    d
    u
    c
    t
    i
    o
    n
    u
    s
    e
    r
    n
    a
    m
    e
    : <
    %
    = E
    N
    V
    [
    '
    M
    Y
    S
    Q
    L
    _
    U
    S
    E
    R
    '
    ]
    %
    >
    p
    a
    s
    s
    w
    o
    r
    d
    : <
    %
    = E
    N
    V
    [
    '
    M
    Y
    S
    Q
    L
    _
    P
    A
    S
    S
    '
    ]
    %
    >
    h
    o
    s
    t
    : m
    y
    s
    q
    l
    p
    o
    r
    t
    : 3
    3
    0
    6

    View Slide

  21. HOWTO SSH into your boxes
    1. Don't
    2. Instead, run a process like you would heroku
    d
    o
    c
    k
    e
    r
    -
    c
    o
    m
    p
    o
    s
    e r
    u
    n a
    p
    p /
    b
    i
    n
    /
    b
    a
    s
    h

    View Slide

  22. HOWTO Monitor your boxes
    Prometheus, by SoundCloud folk, rules.
    p
    r
    o
    m
    e
    t
    h
    e
    u
    s
    :
    b
    u
    i
    l
    d
    : d
    o
    c
    k
    e
    r
    _
    c
    o
    n
    f
    i
    g
    /
    p
    r
    o
    m
    e
    t
    h
    e
    u
    s
    p
    o
    r
    t
    s
    :
    - "
    9
    0
    9
    0
    :
    9
    0
    9
    0
    "
    l
    i
    n
    k
    s
    :
    - c
    o
    n
    t
    a
    i
    n
    e
    r
    e
    x
    p
    o
    r
    t
    e
    r
    c
    o
    n
    t
    a
    i
    n
    e
    r
    e
    x
    p
    o
    r
    t
    e
    r
    :
    i
    m
    a
    g
    e
    : p
    r
    o
    m
    /
    c
    o
    n
    t
    a
    i
    n
    e
    r
    -
    e
    x
    p
    o
    r
    t
    e
    r
    p
    o
    r
    t
    s
    :
    - "
    9
    1
    0
    4
    :
    9
    1
    0
    4
    "
    v
    o
    l
    u
    m
    e
    s
    :
    - /
    s
    y
    s
    /
    f
    s
    /
    c
    g
    r
    o
    u
    p
    :
    /
    c
    g
    r
    o
    u
    p
    - /
    v
    a
    r
    /
    r
    u
    n
    /
    d
    o
    c
    k
    e
    r
    .
    s
    o
    c
    k
    :
    /
    v
    a
    r
    /
    r
    u
    n
    /
    d
    o
    c
    k
    e
    r
    .
    s
    o
    c
    k

    View Slide

  23. DockerFile
    F
    R
    O
    M p
    h
    u
    s
    i
    o
    n
    /
    p
    a
    s
    s
    e
    n
    g
    e
    r
    -
    r
    u
    b
    y
    2
    2
    R
    U
    N a
    p
    t
    -
    g
    e
    t u
    p
    d
    a
    t
    e -
    q
    q &
    & a
    p
    t
    -
    g
    e
    t i
    n
    s
    t
    a
    l
    l -
    y b
    u
    i
    l
    d
    -
    e
    s
    s
    e
    n
    t
    i
    a
    l
    R
    U
    N m
    k
    d
    i
    r /
    m
    y
    a
    p
    p
    W
    O
    R
    K
    D
    I
    R /
    m
    y
    a
    p
    p
    A
    D
    D . /
    m
    y
    a
    p
    p
    R
    U
    N b
    u
    n
    d
    l
    e i
    n
    s
    t
    a
    l
    l

    View Slide

  24. Services
    Docker intentionally breaks upstart and most things that start
    systems
    The Dockerfile, instead, has a "CMD" (one cmd) that runs when
    started
    This is basically the docker way

    View Slide

  25. HOWTO Run more than one service
    (assuming you want to say, run Inspeqtor to notify you of
    crashes)
    Install runit (pronounced 'run-it')
    CMD is "/usr/sbin/runsvdir-start"
    # A
    d
    d r
    u
    n
    i
    t s
    t
    a
    r
    t
    u
    p
    s
    R
    U
    N m
    k
    d
    i
    r /
    e
    t
    c
    /
    s
    v
    /
    i
    n
    s
    p
    e
    q
    t
    o
    r
    A
    D
    D i
    n
    s
    p
    e
    q
    t
    o
    r
    .
    r
    u
    n
    i
    t /
    e
    t
    c
    /
    s
    v
    /
    i
    n
    s
    p
    e
    q
    t
    o
    r
    /
    r
    u
    n
    R
    U
    N m
    k
    d
    i
    r /
    e
    t
    c
    /
    s
    v
    /
    m
    y
    s
    q
    l
    A
    D
    D m
    y
    s
    q
    l
    .
    r
    u
    n
    i
    t /
    e
    t
    c
    /
    s
    v
    /
    m
    y
    s
    q
    l
    /
    r
    u
    n
    R
    U
    N c
    h
    m
    o
    d +
    x /
    e
    t
    c
    /
    s
    v
    /
    i
    n
    s
    p
    e
    q
    t
    o
    r
    /
    r
    u
    n
    R
    U
    N c
    h
    m
    o
    d +
    x /
    e
    t
    c
    /
    s
    v
    /
    m
    y
    s
    q
    l
    /
    r
    u
    n
    # L
    i
    n
    k r
    u
    n
    i
    t s
    t
    a
    r
    t
    u
    p s
    c
    r
    i
    p
    t
    s
    R
    U
    N l
    n -
    s /
    e
    t
    c
    /
    s
    v
    /
    m
    y
    s
    q
    l
    / /
    e
    t
    c
    /
    s
    e
    r
    v
    i
    c
    e
    /
    m
    y
    s
    q
    l
    R
    U
    N l
    n -
    s /
    e
    t
    c
    /
    s
    v
    /
    i
    n
    s
    p
    e
    q
    t
    o
    r
    / /
    e
    t
    c
    /
    s
    e
    r
    v
    i
    c
    e
    /
    i
    n
    s
    p
    e
    q
    t
    o
    r
    # r
    e
    m
    o
    v
    e t
    h
    e u
    p
    s
    t
    a
    r
    t c
    o
    n
    f
    i
    g d
    i
    r
    e
    c
    t
    o
    r
    y
    R
    U
    N r
    m -
    r
    f /
    e
    t
    c
    /
    i
    n
    i
    t

    View Slide

  26. View Slide

  27. Yeah
    So that part is rough around the edges
    Docker REALLY wants you to do 1 process per container
    It strips all else away besides what you start
    But runit can work

    View Slide

  28. Finding Images
    Images are on https://registry.hub.docker.com/

    View Slide

  29. HOWTO Reverse Engineer
    Dockerfiles
    1. Check out the Image Dockerfile
    2. Copy and Paste
    3. Make sure you're on same linux OS.
    Ubuntu. CoreOS. Debian. (likely more).

    View Slide

  30. JWO Rule of Docker Images
    What if I told you that using other people's
    (even official) docker images lead to more
    problems than they solve?
    — Jesse Wolgamott (@jwo) April 27, 2015

    View Slide

  31. HOWTO Scale number of app servers
    1. d
    o
    c
    k
    e
    r
    -
    c
    o
    m
    p
    o
    s
    e s
    c
    a
    l
    e a
    p
    p
    =
    2
    2. Use NGINX as reverse proxy
    3. Have ngnix server port 80, server to many upstream
    u
    p
    s
    t
    r
    e
    a
    m a
    p
    p
    _
    s
    e
    r
    v
    e
    r
    s {
    s
    e
    r
    v
    e
    r 1
    2
    7
    .
    0
    .
    0
    .
    1
    :
    8
    0
    8
    0
    ;
    s
    e
    r
    v
    e
    r 1
    2
    7
    .
    0
    .
    0
    .
    1
    :
    8
    0
    8
    1
    ;
    }
    Possible way to automate:
    http://jasonwilder.com/blog/2014/03/25/automated-nginx-
    reverse-proxy-for-docker/

    View Slide

  32. HOWTO Have a larger disk size on
    boot2docker
    b
    o
    o
    t
    2
    d
    o
    c
    k
    e
    r d
    e
    s
    t
    r
    o
    y
    b
    o
    o
    t
    2
    d
    o
    c
    k
    e
    r i
    n
    i
    t -
    -
    d
    i
    s
    k
    s
    i
    z
    e
    =
    2
    0
    0
    0
    0
    (Yeah, that deletes all stuff and you start over)

    View Slide

  33. HOWTO See what's running, and
    stop them
    d
    o
    c
    k
    e
    r p
    s
    C
    O
    N
    T
    A
    I
    N
    E
    R I
    D I
    M
    A
    G
    E C
    O
    M
    M
    A
    N
    D C
    R
    E
    A
    T
    5
    5
    5
    b
    3
    b
    4
    e
    3
    1
    f
    a f
    i
    v
    e
    h
    u
    n
    d
    r
    e
    d
    _
    a
    p
    p
    :
    l
    a
    t
    e
    s
    t "
    b
    u
    n
    d
    l
    e e
    x
    e
    c r
    a
    i
    l
    s s 2 m
    i
    n
    6
    a
    5
    b
    b
    7
    6
    6
    f
    7
    4
    5 p
    o
    s
    t
    g
    r
    e
    s
    :
    l
    a
    t
    e
    s
    t "
    /
    d
    o
    c
    k
    e
    r
    -
    e
    n
    t
    r
    y
    p
    o
    i
    n
    t
    .
    d
    o
    c
    k
    e
    r k
    i
    l
    l 5
    5
    5
    b
    3
    b
    4
    e
    3
    1
    f
    a

    View Slide

  34. Demo

    View Slide

  35. Conclusion
    1. Docker is extremely cool
    2. Fast, easy'ish (appropriate levels of easy)
    Try it out:
    GitHub / jwo / blocker

    View Slide