docker 101

28
Docker 101 An Introduction to Containerizing Apps Benjamin Schmidt kWantera.com CTO

Upload: schmidtbt

Post on 13-Jul-2015

111 views

Category:

Technology


0 download

TRANSCRIPT

Page 1: Docker 101

Docker 101

An Introduction to Containerizing Apps

Benjamin Schmidt

kWantera.com CTO

Page 2: Docker 101

Agenda

● What is Docker?

● Using Docker

● What can you do with Docker?

● Examples

● Tech that extends Docker

Page 3: Docker 101

Talk’s Purpose

● While simple, Docker is expansive in it’s

capabilities

● I’d like to cover enough so people can

understand the concepts

● I leave it to the docs and SO for becoming

“experts”

Page 4: Docker 101

What is Docker?

“open platform … to build, ship and run

distributed applications”

Linux virtualization made easy (and light-

weight)

Page 5: Docker 101

A Container For Your App

● You’ve got code

● Docker is a self-contained, customizable

linux environment to run that code

● It is a virtual machine without the full OS

Page 6: Docker 101

Origins (-ish)

● Linux containers (LXC) :: sort of like chroot

● Create a kernel separated environment for

security purposes

● Since it’s part of kernel = does not require a

full OS

● Can run multiple containers on same system

● Sprinkle in resource constraints

● Add a dash of environment scripting

Page 7: Docker 101

Representation

You have an “App” you want

to run:● Mongo server

● HTTP server

● Long-running script

● An ad-hoc backup script

image source: docker.io

Page 8: Docker 101

Representation

Traditionally you run it on a

“full server”

image source: docker.io

The physical

hardware

Required

libraries

Page 9: Docker 101

Representation

Virtualization solved the

multiple-app / library

problem● Still a single physical hardware server

image source: docker.io

Use Virtualization to

mirror the “physical”

hardware at OS level

Page 10: Docker 101

Representation

Some inefficiencies pop-up● Architecture duplication

● Setting up “fully systems”

=> Leads to resource waste

image source: docker.io

HEAVY copying &

redundancy

Potentially

redundant

Page 11: Docker 101

Representation

Docker Solution● There is only one running “full OS”

o You use a dedicated portion of kernel

resources

● Much lighter weight leads to:

o Easily cloneable containers

o Sharing system libraries versus program

libraries (i.e. a portion of the host kernel)

image source: docker.io

Page 12: Docker 101

Terminology

● Containero Runs your code using a specified environment

(Image) and the command you wish to execute in

that environment

● Imageo An environment complete with the type of OS and

any setup instructions for generating that

environment

Page 13: Docker 101

Using Docker

● Docker has a pretty simple command line

API*o run: converts images and commands into containers

o start/stop: starts or stops containers

o ps: inspect the available containers and their status

o images: list all available images (locally

downloaded)

o build: builds image (e.x from a Dockerfile)

*Other commands exist for more fine-grained control and inspection

Page 14: Docker 101

At this point...

● I want to give a flavor for Docker usage

without all the detailso See the Docs for extensive coverage

o This is just a 101 course :)

Page 15: Docker 101

Let’s start a docker container...

$ sudo docker run ubuntu:14.04 /bin/echo ‘Hello World’

Hello World

Page 16: Docker 101

Let’s start a docker container...

$ sudo docker run ubuntu:14.04 /bin/echo ‘Hello World’

Hello World

Accesses LXC

under the hood

(needs root)

Convert from

Image to

Container

The type of

ImageThe command to

run in that image

type

Page 17: Docker 101

And if you wanted to test in multiple

OS’s...$ sudo docker run ubuntu:14.04 /bin/echo ‘Hello World’

Hello World

$ sudo docker run ubuntu /bin/echo ‘Hello World’

Hello World

$ sudo docker run centos /bin/echo ‘Hello World’

Hello World

$ sudo docker run debian /bin/echo ‘Hello World’

Hello World

Page 18: Docker 101

A few notes

● Program printed to STDOUT (the console)

● If you run `sudo docker ps` you won’t see

any currently running containers (it stopped)

● Running `sudo docker ps -a` will show a

stopped container

Page 19: Docker 101

And if you want an interactive shell...

$ sudo docker run -t -i ubuntu:14.04 /bin/bash

root@ae23f43: |

-t = pseudo-tty

terminal-i = interactive (STDIN is

piped into process)

Page 20: Docker 101

And if you want an interactive shell...

$ sudo docker run -t -i ubuntu:14.04 /bin/bash

root@ae23f43: |

And to know you’re not in a full OS, try

`top`... only two processes

Page 21: Docker 101

And throw a process into

background...

$ sudo docker run -d ubuntu /bin/sh -c "while true; do echo hello world; sleep 1;

done"

1e5535038e285177d5214659a068137486f96ee5c2e85a4ac52dc83f2ebe4147

-d = daemonize

leaving off :14.04 provides

the “latest” version

Returns the

container ID

Page 22: Docker 101

Bind ports inside container to host...

$ sudo docker run -d -p 5000:5000 ubuntu python myserver.py

-p = port mapping.

(lower case)

Inside:Outside

Page 23: Docker 101

DockerfileCreate Images

# Put this in a file called Dockerfile

FROM ubuntu

MAINTAINER Ben Schmidt <[email protected]>

RUN apt-get update

$ cd ~/myserver

$ sudo docker build -t=”myimage” .

$ sudo docker run -i -it myimage /bin/bash

A Dockerfile in ~/myimage/

-t = Set the name of the

image

Page 24: Docker 101

Things to be Aware of...

● Containers run in their own environmento Files, ports, and host data must be “mapped” to the

container

o EXPOSE: exposes ports to host

o ADD: copies local host files into container

● Volumes allow exposure of host OS

filesystem to containers

o Can get very tricky! But allows shared storage and

even containers that act as storage only

Page 25: Docker 101

Things to be Aware of...

● Images are stored with a VCS-isho Each line in a Dockerfile creates a new image

o FROM command allows stacking of images

o Can create parent-child relationships

o sudo docker commit or pull

● Docker Hubo Store your images in the cloud

o This is default how we access many of the common

images (ubuntu, centos etc.)

o Can also create your own private repository

Page 26: Docker 101

Things to be Aware of...

● Resource limitso Can set CPU and RAM limits

● Networkingo Docker creates an elaborate networking

infrastructure via clever use of /etc/hosts

o Do not modify /etc/hosts!! or do so at peril

Page 27: Docker 101

Neat tech out there today...

Docker swarm:

https://github.com/docker/swarm

CoreOS Rocket:

https://github.com/coreos/rocket

Page 28: Docker 101

Thanks!!

Contact:

Ben Schmidt

kwantera.com