Introduction to Docker (as presented at December 2013 Global Hackathon)

62
a Gentle Introduction to Docker and All Things Containers December 2013—updated for Docker 0.7

description

Not on board of the Docker ship yet? This presentation will get you up to speed, and explain everything you want to know about Linux Containers and Docker, including the new features of the latest 0.7 version (which brings support for all Linux distros and kernels).

Transcript of Introduction to Docker (as presented at December 2013 Global Hackathon)

Page 1: Introduction to Docker (as presented at December 2013 Global Hackathon)

aGentle Introduction

to

Dockerand

All Things Containers

December 2013—updated for Docker 0.7

Page 2: Introduction to Docker (as presented at December 2013 Global Hackathon)

Outline

● Whom is this for?● What's the problem?● What's a Container?● Docker 101● Docker images● Docker deployment● Docker future

Page 3: Introduction to Docker (as presented at December 2013 Global Hackathon)

Outline

● Whom is this for?● What's the problem?● What's a Container?● Docker 101● Docker images● Docker deployment● Docker future

Page 4: Introduction to Docker (as presented at December 2013 Global Hackathon)

Devs

● all languages● all databases● all O/S● targetting Linux systems

Docker will eventually be able to target FreeBSD, Solaris, and maybe OS X.

Page 5: Introduction to Docker (as presented at December 2013 Global Hackathon)

Ops

● any distro● any cloud● any machine (physical, virtual...)● recent kernels

– at least 3.8

– or the one that comes with RHEL 6.5

Page 6: Introduction to Docker (as presented at December 2013 Global Hackathon)

CFO, CIO, CTO, ...

● LESS overhead!● MOAR consolidation!● MOAR agility!● LESS costs!

Page 7: Introduction to Docker (as presented at December 2013 Global Hackathon)

Outline

● Whom is this for?● What's the problem?● What's a Container?● Docker 101● Docker images● Docker deployment● Docker future

Page 8: Introduction to Docker (as presented at December 2013 Global Hackathon)

The Matrix From Hell

djangoweb frontend ? ? ? ? ? ?

node.jsasync API ? ? ? ? ? ?

background workers ? ? ? ? ? ?

SQL database

? ? ? ? ? ?

distributed DB, big data

? ? ? ? ? ?

message queue

? ? ? ? ? ?

mylaptop

your laptop

QA staging prod on cloud VM

prod on bare metal

Page 9: Introduction to Docker (as presented at December 2013 Global Hackathon)

Another Matrix from Hell? ? ? ? ? ? ?

? ? ? ? ? ? ?

? ? ? ? ? ? ?

? ? ? ? ? ? ?

? ? ? ? ? ? ?

? ? ? ? ? ? ?

Page 10: Introduction to Docker (as presented at December 2013 Global Hackathon)

Solution:the intermodal shipping container

Page 11: Introduction to Docker (as presented at December 2013 Global Hackathon)

Solved!

Page 12: Introduction to Docker (as presented at December 2013 Global Hackathon)

Solution to the deployment problem: the Linux container

Page 13: Introduction to Docker (as presented at December 2013 Global Hackathon)

Linux containers...

Units of software delivery (ship it!)● run everywhere

– regardless of kernel version

– regardless of host distro

– (but container and host architecture must match*)

● run anything– if it can run on the host, it can run in the container

– i.e., if it can run on a Linux kernel, it can run

*Unless you emulate CPU with qemu and binfmt

Page 14: Introduction to Docker (as presented at December 2013 Global Hackathon)

Outline

● Whom is this for?● What's the problem?● What's a Container?● Docker 101● Docker images● Docker deployment● Docker future

Page 15: Introduction to Docker (as presented at December 2013 Global Hackathon)

High level approach:it's a lightweight VM

● own process space● own network interface● can run stuff as root● can have its own /sbin/init

(different from the host)

« Machine Container »

Page 16: Introduction to Docker (as presented at December 2013 Global Hackathon)

Low level approach:it's chroot on steroids

● can also not have its own /sbin/init● container = isolated process(es)● share kernel with host● no device emulation (neither HVM nor PV)

« Application Container »

Page 17: Introduction to Docker (as presented at December 2013 Global Hackathon)

Separation of concerns:Dave the Developer

● inside my container:– my code

– my libraries

– my package manager

– my app

– my data

Page 18: Introduction to Docker (as presented at December 2013 Global Hackathon)

Separation of concerns:Oscar the Ops guy

● outside the container:– logging

– remote access

– network configuration

– monitoring

Page 19: Introduction to Docker (as presented at December 2013 Global Hackathon)

How does it work?Isolation with namespaces

● pid● mnt● net● uts● ipc● user

Page 20: Introduction to Docker (as presented at December 2013 Global Hackathon)

pid namespace

jpetazzo@tarrasque:~$ ps aux | wc -l212

jpetazzo@tarrasque:~$ sudo docker run -t -i ubuntu bashroot@ea319b8ac416:/# ps auxUSER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMANDroot 1 0.0 0.0 18044 1956 ? S 02:54 0:00 bashroot 16 0.0 0.0 15276 1136 ? R+ 02:55 0:00 ps aux

(That's 2 processes)

Page 21: Introduction to Docker (as presented at December 2013 Global Hackathon)

mnt namespace

jpetazzo@tarrasque:~$ wc -l /proc/mounts

32 /proc/mounts

root@ea319b8ac416:/# wc -l /proc/mounts

10 /proc/mounts

Page 22: Introduction to Docker (as presented at December 2013 Global Hackathon)

net namespace

root@ea319b8ac416:/# ip addr

1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo valid_lft forever preferred_lft forever inet6 ::1/128 scope host valid_lft forever preferred_lft forever

22: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 2a:d1:4b:7e:bf:b5 brd ff:ff:ff:ff:ff:ff inet 10.1.1.3/24 brd 10.1.1.255 scope global eth0 valid_lft forever preferred_lft forever inet6 fe80::28d1:4bff:fe7e:bfb5/64 scope link valid_lft forever preferred_lft forever

Page 23: Introduction to Docker (as presented at December 2013 Global Hackathon)

uts namespace

jpetazzo@tarrasque:~$ hostnametarrasque

root@ea319b8ac416:/# hostnameea319b8ac416

Page 24: Introduction to Docker (as presented at December 2013 Global Hackathon)

ipc namespace

jpetazzo@tarrasque:~$ ipcs------ Shared Memory Segments --------key shmid owner perms bytes nattch status0x00000000 3178496 jpetazzo 600 393216 2 dest 0x00000000 557057 jpetazzo 777 2778672 0 0x00000000 3211266 jpetazzo 600 393216 2 dest

root@ea319b8ac416:/# ipcs------ Shared Memory Segments --------key shmid owner perms bytes nattch status------ Semaphore Arrays --------key semid owner perms nsems ------ Message Queues --------key msqid owner perms used-bytes messages

Page 25: Introduction to Docker (as presented at December 2013 Global Hackathon)

user namespace

● no « demo » for this one... Yet!● UID 0→1999 in container C1 is mapped to

UID 10000→11999 in host;UID 0→1999 in container C2 is mapped toUID 12000→13999 in host; etc.

● required lots of VFS and FS patches (esp. XFS)

● what will happen with copy-on-write?– double translation at VFS?

– single root UID on read-only FS?

Page 26: Introduction to Docker (as presented at December 2013 Global Hackathon)

How does it work?Isolation with cgroups

● memory● cpu● blkio● devices

Page 27: Introduction to Docker (as presented at December 2013 Global Hackathon)

How does it work?Isolation with cgroups

● memory● cpu● blkio● devices

Page 28: Introduction to Docker (as presented at December 2013 Global Hackathon)

memory cgroup

● keeps track pages used by each group:– file (read/write/mmap from block devices; swap)

– anonymous (stack, heap, anonymous mmap)

– active (recently accessed)

– inactive (candidate for eviction)

● each page is « charged » to a group● pages can be shared (e.g. if you use any COW FS)

● Individual (per-cgroup) limits and out-of-memory killer

Page 29: Introduction to Docker (as presented at December 2013 Global Hackathon)

cpu and cpuset cgroups

● keep track of user/system CPU time● set relative weight per group● pin groups to specific CPU(s)

– Can be used to « reserve » CPUs for some apps

– This is also relevant for big NUMA systems

Page 30: Introduction to Docker (as presented at December 2013 Global Hackathon)

blkio cgroups

● keep track IOs for each block device– read vs write; sync vs async

● set relative weights● set throttle (limits) for each block device

– read vs write; bytes/sec vs operations/sec

Note: earlier versions (<3.8) didn't account async correctly.3.8 is better, but use 3.10 for best results.

Page 31: Introduction to Docker (as presented at December 2013 Global Hackathon)

devices cgroups

● controls read/write/mknod permissions● typically:

– allow: /dev/{tty,zero,random,null}...

– deny: everything else

– maybe: /dev/net/tun, /dev/fuse

Page 32: Introduction to Docker (as presented at December 2013 Global Hackathon)

If you're serious about security,you also need…

● capabilities– okay: cap_ipc_lock, cap_lease, cap_mknod, cap_net_admin,

cap_net_bind_service, cap_net_raw

– troublesome: cap_sys_admin (mount!)

● think twice before granting root● grsec is nice● seccomp (very specific use cases); seccomp-bpf● beware of full-scale kernel exploits!

Page 33: Introduction to Docker (as presented at December 2013 Global Hackathon)

How does it work?Copy-on-write storage

● unioning filesystems(AUFS, overlayfs)

● snapshotting filesystems(BTRFS, ZFS)

● copy-on-write block devices(thin snapshots with LVM or device-mapper)

Since 0.7, Docker has a storage plugin system!

Page 34: Introduction to Docker (as presented at December 2013 Global Hackathon)

Efficiency

Page 35: Introduction to Docker (as presented at December 2013 Global Hackathon)

Compute efficiency:almost no overhead

● processes are isolated,but run straight on the host

● CPU performance = native performance

● memory performance = a few % shaved off for (optional) accounting

● network performance = small overhead; can be reduced to zero

Page 36: Introduction to Docker (as presented at December 2013 Global Hackathon)

Storage efficiency:many options!

Union Filesystems

Snapshotting Filesystems

Copy-on-writeblock devices

Provisioning SuperfastSupercheap

FastCheap

FastCheap

Changingsmall files

SuperfastSupercheap

FastCheap

FastCostly

Changinglarge files

Slow (first time)Inefficient (copy-up!)

FastCheap

FastCheap

Diffing Superfast Superfast (ZFS)Kinda meh (BTRFS)

Slow

Memory usage Efficient Efficient Inefficient(at high densities)

Drawbacks Random quirksAUFS not mainline!AUFS more quirks

ZFS not mainlineBTRFS not as nice

Higher disk usageGreat performance (except diffing)

Bottom line Ideal for PAAS and high density things

This might be the Future

Dodge Ram 3500

Page 37: Introduction to Docker (as presented at December 2013 Global Hackathon)

Outline

● Whom is this for?● What's the problem?● What's a Container?● Docker 101● Docker images● Docker deployment● Docker future

Page 38: Introduction to Docker (as presented at December 2013 Global Hackathon)
Page 39: Introduction to Docker (as presented at December 2013 Global Hackathon)

Docker-what?

● Open Source engine to commoditize LXC● using copy-on-write for quick provisioning

STOP!

HAMMER DEMO TIME.

Page 40: Introduction to Docker (as presented at December 2013 Global Hackathon)
Page 41: Introduction to Docker (as presented at December 2013 Global Hackathon)

Yes, but...

● « I don't need Docker; I can do all that stuff with LXC tools, rsync,and some scripts! »

● correct on all accounts;but it's also true for apt, dpkg, rpm, yum, etc.

● the whole point is to commoditize,i.e. make it ridiculously easy to use

Page 42: Introduction to Docker (as presented at December 2013 Global Hackathon)

Containers before Docker

Page 43: Introduction to Docker (as presented at December 2013 Global Hackathon)

Containers after Docker

Page 44: Introduction to Docker (as presented at December 2013 Global Hackathon)

What this really means…

● instead of writing « very small shell scripts » tomanage containers, write them to do the rest:– continuous deployment/integration/testing

– orchestration

● = use Docker as a building block● re-use other people images (yay ecosystem!)

Page 45: Introduction to Docker (as presented at December 2013 Global Hackathon)

Docker-what?The Big Picture

● Open Source engine to commoditize LXC● using copy-on-write for quick provisioning● allowing to create and share images● standard format for containers

(stack of layers; 1 layer = tarball+metadata)● standard, reproducible way to easily build

trusted images (Dockerfile, Stackbrew...)

Page 46: Introduction to Docker (as presented at December 2013 Global Hackathon)

Docker-what?Under The Hood

● rewrite of dotCloud internal container engine– original version: Python, tied to dotCloud's internal stuff

– released version: Go, legacy-free

● the Docker daemon runs in the background– manages containers, images, and builds– HTTP API (over UNIX or TCP socket)– embedded CLI talking to the API

● Open Source (GitHub public repository + issue tracking)● user and dev mailing lists● FreeNode IRC channels #docker, #docker-dev

Page 47: Introduction to Docker (as presented at December 2013 Global Hackathon)

Outline

● Whom is this for?● What's the problem?● What's a Container?● Docker 101● Docker images● Docker deployment● Docker future

Page 48: Introduction to Docker (as presented at December 2013 Global Hackathon)

Authoring imageswith run/commit

1) docker run ubuntu bash

2) apt-get install this and that

3) docker commit <containerid> <imagename>

4) docker run <imagename> bash

5) git clone git://.../mycode

6) pip install -r requirements.txt

7) docker commit <containerid> <imagename>

8) repeat steps 4-7 as necessary

9) docker tag <imagename> <user/image>

10) docker push <user/image>

Page 49: Introduction to Docker (as presented at December 2013 Global Hackathon)

Authoring imageswith a Dockerfile

FROM ubuntu

RUN apt-get -y updateRUN apt-get install -y g++RUN apt-get install -y erlang-dev erlang-manpages erlang-base-hipe ...RUN apt-get install -y libmozjs185-dev libicu-dev libtool ...RUN apt-get install -y make wget

RUN wget http://.../apache-couchdb-1.3.1.tar.gz | tar -C /tmp -zxf-RUN cd /tmp/apache-couchdb-* && ./configure && make install

RUN printf "[httpd]\nport = 8101\nbind_address = 0.0.0.0" > /usr/local/etc/couchdb/local.d/docker.ini

EXPOSE 8101CMD ["/usr/local/bin/couchdb"]

docker build -t jpetazzo/couchdb .

Page 50: Introduction to Docker (as presented at December 2013 Global Hackathon)

Authoring Imageswith Trusted Builds

0) create a GitHub account

On index.docker.io:1) create a Docker account2) link it with your GitHub account3) enable Trusted Builds on any public repo

On your dev env:4) git add Dockerfile5) git commit6) git push

Page 51: Introduction to Docker (as presented at December 2013 Global Hackathon)

Authoring Imageswith Chef/Puppet/Ansible/Salt/...

Plan A: « my other VM is a container »● write a Dockerfile to install $YOUR_CM● start tons of containers● run $YOUR_CM in them

Good if you want a mix of containers/VM/metal

But slower to deploy, and uses more resources

Page 52: Introduction to Docker (as presented at December 2013 Global Hackathon)

Authoring Imageswith Chef/Puppet/Ansible/Salt/...

Plan B: « the revolution will be containerized »● write a Dockerfile to install $YOUR_CM● … and run $YOUR_CM as part of build process● deploy fully baked images

Faster to deploy

Easier to rollback

Page 53: Introduction to Docker (as presented at December 2013 Global Hackathon)

Outline

● Whom is this for?● What's the problem?● What's a Container?● Docker 101● Docker images● Docker deployment● Docker future

Page 54: Introduction to Docker (as presented at December 2013 Global Hackathon)

Running containers

● SSH to Docker host and manual pull+run● REST API (feel free to add SSL certs, OAUth...)● maestro, maestro-ng

(https://github.com/signalfuse/maestro-ng)● OpenStack Nova● OpenStack Heat● who's next? OpenShift, CloudFoundry?● multiple Open Source PAAS built on Docker

Page 55: Introduction to Docker (as presented at December 2013 Global Hackathon)

Orchestration & Service Discovery

● you can name your containers● they get a generated name by default

– 0.6: red_ant, gold_monkey...

– 0.7: angry_linus, naughty_tesla...

● you can link your containers

docker run -d -name frontdb mysqlimagedocker run -d -link frontdb:sql nginximage

→ container frontweb gets one bazillion environment vars

Page 56: Introduction to Docker (as presented at December 2013 Global Hackathon)

Orchestration & ServiceDiscovery roadmap

● currently single-host● problem:

how do I link with containers on other hosts?● solution:

ambassador pattern!– app container runs in its happy place

– other things (Docker, containers...) plumb it

Page 57: Introduction to Docker (as presented at December 2013 Global Hackathon)

Orchestration roadmap

● currently static● problem: what if I want to…

move a container?do a master/slave failover?WebScale my MangoDB cluster?

● solution:dynamic discovery!

Page 58: Introduction to Docker (as presented at December 2013 Global Hackathon)

Multi-host Docker deployments

I could give a full talkabout this topic

(and actually I did once)

Page 59: Introduction to Docker (as presented at December 2013 Global Hackathon)

Outline

● Whom is this for?● What's the problem?● What's a Container?● Docker 101● Docker images● Docker deployment● Docker future

Page 60: Introduction to Docker (as presented at December 2013 Global Hackathon)

Docker: the community

● Docker: >240 contributors● Docker inc.: <20 engineers ● latest milestone (0.7): >100 contributors● ~50% of all commits by external contributors● GitHub repository: >1000 forks

...and counting!

Page 61: Introduction to Docker (as presented at December 2013 Global Hackathon)

Docker long-term roadmap

Docker 1.0:● remove AUFS, THINP, LXC, etc.

– execution? chroot!

– storage? cp!

– we can run everywhere \o/

● re-add everything as plugins● discover and interconnect hosts and services

through docker.io

Page 62: Introduction to Docker (as presented at December 2013 Global Hackathon)

Thank you! Questions?

http://docker.io/

http://docker.com/

https://github.com/dotcloud/docker

@docker

@jpetazzo