Slide 1

Slide 1 text

ì   15  Docker  Tips  in  5  Minutes   Brian  Morearty                                                                                                                                                                                                                      Nov  5,  2013  

Slide 2

Slide 2 text

1.  Getting  the  id  of  the  last-­‐run   container     15  Docker  Tips  in  5  Minutes   2  

Slide 3

Slide 3 text

•  You  could  do  this:       •  But  you  have  to  keep  assigning  IDs.   •  Try  this  instead:   ! $ ID=$(docker run ubuntu echo hello world)! hello world! $ docker commit $ID helloworld! fd08a884dc79! 15  Docker  Tips  in  5  Minutes   3  

Slide 4

Slide 4 text

! $ alias dl='docker ps -l -q'! $ docker run ubuntu echo hello world! hello world! $ dl! 1904cf045887! $ docker commit `dl` helloworld! fd08a884dc79! 15  Docker  Tips  in  5  Minutes   4  

Slide 5

Slide 5 text

2.  Why  you  always  want  a  Dockerfile,  even   if  you  install  everything  in  the  shell   15  Docker  Tips  in  5  Minutes   5  

Slide 6

Slide 6 text

$ docker run -i -t ubuntu bash! root@db0c3978abf8:/# apt-get install postgresql! Reading package lists... Done! Building dependency tree... Done! etc., etc., etc.! root@db0c3978abf8:/# exit! $ docker commit \! -run='{"Cmd":["postgres", 
 "-too -many -opts"]}' \! `dl` postgres! 5061b88e4cf0! 15  Docker  Tips  in  5  Minutes   6  

Slide 7

Slide 7 text

  •  Instead,  make  a  wee  liEle   Dockerfile  that  is  FROM  the  image   you  made  interacGvely.   •  There  you  can  set  CMD,   ENTRYPOINT,  VOLUME,  etc.   15  Docker  Tips  in  5  Minutes   7  

Slide 8

Slide 8 text

3.  Su-­‐su-­‐sudo   15  Docker  Tips  in  5  Minutes   8  

Slide 9

Slide 9 text

TOMATO       TOMAHTO   15  Docker  Tips  in  5  Minutes   9  

Slide 10

Slide 10 text

# Add the docker group.! $ sudo groupadd docker! ! # Add self to the docker group. ! $ sudo gpasswd -a myusername docker! ! # Restart the docker daemon! $ sudo service docker restart! ! # logout and in again.! $ exit! ! 15  Docker  Tips  in  5  Minutes   10  

Slide 11

Slide 11 text

4.  Take  out  the  garbage   15  Docker  Tips  in  5  Minutes   11  

Slide 12

Slide 12 text

! ! $ docker rm $(docker ps -a -q)!   •  Deletes  all  stopped  containers.   •  (Tries,  but  conveniently  fails,  to  delete  sGll-­‐ running  containers.)   15  Docker  Tips  in  5  Minutes   12  

Slide 13

Slide 13 text

5.  jq  -­‐  the  gangsta  way  to  parse     docker inspect’s  output ! 15  Docker  Tips  in  5  Minutes   13  

Slide 14

Slide 14 text

! $ docker inspect `dl` | \! grep IPAddress | cut -d '"' -f 4! 172.17.0.52! ! $ docker inspect `dl` | \! jq -r '.[0].NetworkSettings.IPAddress'! 172.17.0.52! !     •  JSON  is  for  JavaScript.   15  Docker  Tips  in  5  Minutes   14  

Slide 15

Slide 15 text

6.  What  environment  variables  does   an  image  have?     15  Docker  Tips  in  5  Minutes   15  

Slide 16

Slide 16 text

! $ docker run ubuntu env! HOME=/! PATH=/usr/local/sbin:/usr/local/bin:/ usr/sbin:/usr/bin:/sbin:/bin! container=lxc! HOSTNAME=5e1560b7f757!     •  This  is  nice  when  using  docker run
 -link  to  connect  containers.  (Later  slide.)   15  Docker  Tips  in  5  Minutes   16  

Slide 17

Slide 17 text

7.  RUN  vs.  CMD  instruction   15  Docker  Tips  in  5  Minutes   17  

Slide 18

Slide 18 text

! FROM thelanddownunder! MAINTAINER crocdundee! ! # docker build will execute these:! RUN apt-get update! RUN apt-get install softwares! ! # docker run runs this cmd by default:! CMD ["softwares"]!     •  A  Dockerfile.   15  Docker  Tips  in  5  Minutes   18  

Slide 19

Slide 19 text

8.  CMD  vs.  ENTRYPOINT  instruction   15  Docker  Tips  in  5  Minutes   19  

Slide 20

Slide 20 text

! $ cat Dockerfile! FROM ubuntu! CMD ["echo"]! ! $ docker run \
 imagename \
 echo hello! hello! ! 15  Docker  Tips  in  5  Minutes   20   ! $ cat Dockerfile! FROM ubuntu! ENTRYPOINT ["echo"]! ! $ docker run \
 imagename \
 echo hello! echo hello! !     •  Overrideable.       •  Not  overrideable.  

Slide 21

Slide 21 text

9.  Does  a  Docker  container  have  its   own  IP  address?     15  Docker  Tips  in  5  Minutes   21  

Slide 22

Slide 22 text

! $ ip -4 -o addr show eth0! 2: eth0 inet 162.243.139.222/24! ! $ docker run ubuntu \! ip -4 -o addr show eth0! 149: eth0 inet 172.17.0.43/16!   •  Yep.  It’s  like  a  process  with  an  IP  address.   15  Docker  Tips  in  5  Minutes   22  

Slide 23

Slide 23 text

10.  Docker’s  architecture:  thin  CLI  client,   REST  server  daemon  over  UNIX  socket   15  Docker  Tips  in  5  Minutes   23  

Slide 24

Slide 24 text

! # Connect to the UNIX socket and make! # like an HTTP client.! $ nc -U //var/run/docker.sock! GET /images/json HTTP/1.1! ! HTTP/1.1 200 OK! Content-Type: application/json! Date: Tue, 05 Nov 2013 23:18:09 GMT! Transfer-Encoding: chunked! ! 16aa! [{"Repository":"postgres","Tag":”.........!     •  Orange  is  what  I  typed.   15  Docker  Tips  in  5  Minutes   24  

Slide 25

Slide 25 text

11.  Graph  the  dependencies  among   your  images     15  Docker  Tips  in  5  Minutes   25  

Slide 26

Slide 26 text

! # Generate an image dependency diagram! $ docker images -viz | \! dot -Tpng -o docker.png! ! # To see it, run this on the host:! $ python -m SimpleHTTPServer! ! # then browse to:! # http://machinename:8000/docker.png!     15  Docker  Tips  in  5  Minutes   26  

Slide 27

Slide 27 text

15  Docker  Tips  in  5  Minutes   27   •  And  hope  yours  doesn’t  look  like  this.  

Slide 28

Slide 28 text

15  Docker  Tips  in  5  Minutes   28  

Slide 29

Slide 29 text

12.  Where  does  Docker  store   everything?   15  Docker  Tips  in  5  Minutes   29  

Slide 30

Slide 30 text

! $ sudo su! # cd /var/lib/docker! # ls -F! containers/ graph/ repositories volumes/!     •  Explore!   •  “graph”  means  images.   •  Filesystem  layers  are  in  graph/imageid/layer.   15  Docker  Tips  in  5  Minutes   30  

Slide 31

Slide 31 text

13.  Docker  source  code:  Go,  Go,  Go,   Grabote.   15  Docker  Tips  in  5  Minutes   31  

Slide 32

Slide 32 text

15  Docker  Tips  in  5  Minutes   32  

Slide 33

Slide 33 text

15  Docker  Tips  in  5  Minutes   33   commands.go   The  CLI.     api.go   The  REST  API  router.     server.go   ImplementaGon  of  much  of  the  the  REST  API.   buildfile.go   The  Dockerfile  parser.  

Slide 34

Slide 34 text

14.  RUN  some  daemons,  exit  the   container.  What  happens?   15  Docker  Tips  in  5  Minutes   34  

Slide 35

Slide 35 text

$ cat Dockerfile! FROM ubuntu:12.04! MAINTAINER Brian Morearty! ...! RUN pg_ctl start ...! ! $ docker run -i -t postgresimage bash! root@08d363f57161:/# ps aux! # Doesn’t show postgres daemon!         •  Don’t  run  a  daemon  in  your  Dockerfile.   15  Docker  Tips  in  5  Minutes   35  

Slide 36

Slide 36 text

15.  Letting  containers  talk  to  each   other   15  Docker  Tips  in  5  Minutes   36  

Slide 37

Slide 37 text

15  Docker  Tips  in  5  Minutes   37   • I  saved  the  best  for   last.  

Slide 38

Slide 38 text

# Run a container. Give it a name.! $ docker run -d -name loldb loldbimage! ! # Run another. Link it to the first,! # using an alias.! $ docker run -link /loldb:cheez \! otherimage env! CHEEZ_PORT=tcp://172.17.0.8:6379! CHEEZ_PORT_1337_TCP=tcp://172.17.0.8:6379! CHEEZ_PORT_1337_TCP_ADDR=172.17.0.12! CHEEZ_PORT_1337_TCP_PORT=6379! CHEEZ_PORT_1337_TCP_PROTO=tcp! !         •  This  sets  up  a  bridge.  2nd  container  needs  to   know  aliased  name  (CHEEZ)  and  port  (1337).   15  Docker  Tips  in  5  Minutes   38  

Slide 39

Slide 39 text

Extracted  from  my   intro-­‐level   Hands  on  with   Docker  class.     (In  partnership   with  Docker,  Inc.)     Beta  class  this   Monday,  6-­‐9pm.   handsonwith.com   15  Docker  Tips  in  5  Minutes   39