be a happier developer with docker: tricks of the trade

58
Nicola Paolucci Developer Advocate / Evangelist with Happier Developer with Be a

Upload: docker-inc

Post on 14-Jun-2015

1.048 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Be a Happier Developer with Docker: Tricks of the Trade

Nicola Paolucci!Developer Advocate / Evangelist

with!

Happier Developerwith

Be a

Page 2: Be a Happier Developer with Docker: Tricks of the Trade

Nicola Paolucci@durdn

Bio pictures: the subtle pleasure of embarrassing yourself in front of hundreds of people

Page 3: Be a Happier Developer with Docker: Tricks of the Trade

Tools, Tips and Hacks

2

3

The plan for this session:

Notes on Workflows and Techniques

1 Why does Docker make Developers happy?

Page 4: Be a Happier Developer with Docker: Tricks of the Trade

Why?

Page 5: Be a Happier Developer with Docker: Tricks of the Trade

Compulsion to have clean and perfect

environments

© www.elephantlifestyle.com

Page 6: Be a Happier Developer with Docker: Tricks of the Trade

the need for speed of every developer with an idea

Page 7: Be a Happier Developer with Docker: Tricks of the Trade

Fast application mobility, Real repeatability

Page 8: Be a Happier Developer with Docker: Tricks of the Trade

Great for development team collaboration

Page 9: Be a Happier Developer with Docker: Tricks of the Trade

Building blocks for your Micro-services Architecture

Page 10: Be a Happier Developer with Docker: Tricks of the Trade

Micro-services Architecture Blueprint

@crichardson

!!!!!

Traditional Server-side web apps

View Controller

Model

API Gateway

!!!!!

Browser/Native App

View Controller

Model

REST

REST

REST

Product Info Service

Recommendation Service

Review Service

Single entry point

Protocol translationClient specific APIs

Page 11: Be a Happier Developer with Docker: Tricks of the Trade

Workflows & Techniques

Page 12: Be a Happier Developer with Docker: Tricks of the Trade

Workflow 1: Develop inside a single running container

Page 13: Be a Happier Developer with Docker: Tricks of the Trade

Workflow 1: Develop inside a single running container

As you would in a single VM

Page 14: Be a Happier Developer with Docker: Tricks of the Trade

Container

Start a shell in container docker run -i -t ubuntu /bin/bash

Folder from host:!/src

Docker host

Host folder!/path/to/code

-v /path/to/code:/src

Page 15: Be a Happier Developer with Docker: Tricks of the Trade

To use a container as a full Development Environment

phusion/baseimage

Page 16: Be a Happier Developer with Docker: Tricks of the Trade

Workflow 2: Leverage containers, modularise

Page 17: Be a Happier Developer with Docker: Tricks of the Trade

Techniques: Embrace Reusability in Dockerfiles

Page 18: Be a Happier Developer with Docker: Tricks of the Trade

Techniques: Embrace Reusability in Dockerfiles

Write general requirements early, commit and name relevant checkpoints, leave customisations last

Page 19: Be a Happier Developer with Docker: Tricks of the Trade

add + build routine magicTechniques:

Page 20: Be a Happier Developer with Docker: Tricks of the Trade

docker add <src> <dest>

Page 21: Be a Happier Developer with Docker: Tricks of the Trade

docker add <src> <dest>The ADD instruction copies new files from host’s <src> to container’s <dest>

Page 22: Be a Happier Developer with Docker: Tricks of the Trade

docker build your image with updated code

1

2

add + build routine magic?!

Update code in local app folder (git pull?)

Distribute and profit!3

Page 23: Be a Happier Developer with Docker: Tricks of the Trade

Sharing data in containersTechniques:

Page 24: Be a Happier Developer with Docker: Tricks of the Trade

Container

Folder from host:!/app

Docker host

Host folder!/opt/test-app

-v /opt/test-app:/app

host to containersshare folder from

Page 25: Be a Happier Developer with Docker: Tricks of the Trade

host to containers

docker run -v /opt/test-app:/app \ -i -t ubuntu /bin/bash

Use the run -v (volume option) to specify host/container folder to be synced

From

is simple

Page 26: Be a Happier Developer with Docker: Tricks of the Trade

Same pattern using Dockerfile

FROM busybox VOLUME ["/var/volume1", "/var/volume2"] CMD ["/bin/true"]

Page 27: Be a Happier Developer with Docker: Tricks of the Trade

Common pattern: Data in containers

Docker host

/var/volume1

DATA

/var/volume2

Page 28: Be a Happier Developer with Docker: Tricks of the Trade

Common pattern: data in containers

docker run -v /var/volume1 \ -v /var/volume2 \ --name DATA busybox true

Switched off, named, data container which exposes a folder

Page 29: Be a Happier Developer with Docker: Tricks of the Trade

Data in containers pattern

Docker host

/var/volume1

DATA

/var/volume2

Page 30: Be a Happier Developer with Docker: Tricks of the Trade

Data in containers pattern

Docker host

/var/volume1

DATA

/var/volume2

/var/volume1

client1

/var/volume2

--volumes-from DATA--volumes-from DATA

Page 31: Be a Happier Developer with Docker: Tricks of the Trade

Common pattern: data in containers

docker run -t -i -rm --volumes-from DATA \ --name client1 ubuntu bash

Then mount the data container in your application containers

Page 32: Be a Happier Developer with Docker: Tricks of the Trade

What if you use docker in a VM?

Page 33: Be a Happier Developer with Docker: Tricks of the Trade

Container

You have an extra layer

Container folder!/src

Docker in a VM

VM folder!/path/to/code

-v /path/to/code:/src

Host OS

Host OS folder!/path/to/code

?

Page 34: Be a Happier Developer with Docker: Tricks of the Trade

Simplest way: Use Guest Additions

And a VM that supports shared folders. But what if you want to keep using the

lightest weight genius of boot2docker?

Page 35: Be a Happier Developer with Docker: Tricks of the Trade

boot2docker is great!

Page 36: Be a Happier Developer with Docker: Tricks of the Trade

Patched boot2docker with vboxsf support

Page 37: Be a Happier Developer with Docker: Tricks of the Trade

Patched boot2docker with vboxsf support

Pull Request #282-4 add the feature

Page 38: Be a Happier Developer with Docker: Tricks of the Trade

Patched boot2docker with vboxsf support

https://vagrantcloud.com/dduportal/boot2docker

Page 39: Be a Happier Developer with Docker: Tricks of the Trade

The UNIX way: NFS, Samba, plan9, etc.

Page 40: Be a Happier Developer with Docker: Tricks of the Trade

Access via NFS

docker pull cpuguy83/nfs-server

Pull the right image, share a data folder in it, share it with other containers AND the host

docker run -d --name nfs --privileged \ cpuguy83/nfs-server /path/to/share

Page 41: Be a Happier Developer with Docker: Tricks of the Trade

Access via Samba

docker pull svendowideit/samba

And if you prefer Samba, here’s a shortcut ready made for you

docker run svendowideit/samba data

Page 42: Be a Happier Developer with Docker: Tricks of the Trade

--links: simple service connections for docker

Page 43: Be a Happier Developer with Docker: Tricks of the Trade

linked containers

Docker host

/var/volume1

postgresql

/var/volume2

client

--link postgresql:pg

can refer to hostname pg in commands

Page 44: Be a Happier Developer with Docker: Tricks of the Trade

Sample access to linked container

docker build -t postresql .

Build the image, run it with a name, !link in child container

docker run -rm -P --name pg postgresql

docker run -rm -t -i --link pg:pg postgresql bash !psql -h pg -d docker -U docker --password

Page 45: Be a Happier Developer with Docker: Tricks of the Trade

Tools, Tips & Hacks

Page 46: Be a Happier Developer with Docker: Tricks of the Trade

workhorse with a secret weapon

Page 47: Be a Happier Developer with Docker: Tricks of the Trade

Tiny Core Linux

workhorse with a secret weapon

Page 48: Be a Happier Developer with Docker: Tricks of the Trade

Tiny Core Aside Awesomeness!

Every time it boots, it is brand-spanking new

Minimal nomadic X desktop for 12MB

Page 49: Be a Happier Developer with Docker: Tricks of the Trade

Customise your boot2docker/Tiny Core

Tailor the boot sequence in!/opt/bootlocal.sh

List personal settings folders and files !/opt/.filetool.lst

Page 50: Be a Happier Developer with Docker: Tricks of the Trade

Tunnels, tunnels… and ports

Page 51: Be a Happier Developer with Docker: Tricks of the Trade

Tunnels and ports

Docker in a VMHost OS

8000 8000

Container

8000 8000

-p outer:inner?

Page 52: Be a Happier Developer with Docker: Tricks of the Trade

Port forwarding into a container

(boot2docker) ssh -L 8000:localhost:8000

Expose a container port to the host machine

Page 53: Be a Happier Developer with Docker: Tricks of the Trade

Mass-expose ports with VBoxManage

# vm must be powered off for i in {49000..49900}; do VBoxManage modifyvm "boot2docker-vm" --natpf1 "tcp-port$i,tcp,,$i,,$i"; VBoxManage modifyvm "boot2docker-vm" --natpf1 "udp-port$i,udp,,$i,,$i"; done

To expose all 49XXX ports you can run:

Workarounds section of boot2docker docs

Page 54: Be a Happier Developer with Docker: Tricks of the Trade

Open ports on live containers with force using iptables

sudo iptables-save

http://j.mp/force-port

Note down the command to open ports of another container

Page 55: Be a Happier Developer with Docker: Tricks of the Trade

One more thing…

Page 56: Be a Happier Developer with Docker: Tricks of the Trade
Page 57: Be a Happier Developer with Docker: Tricks of the Trade

Nicola Paolucci@durdn

Thank you!

Page 58: Be a Happier Developer with Docker: Tricks of the Trade

Nicola Paolucci@durdn

Thank you!