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

10 Docker Tips in 10 Minutes

10 Docker Tips in 10 Minutes

A condensed version of 15 Docker Tips in 5 Minutes. Five fewer tips, five more minutes. :-) Lightning talk I gave at a Docker meetup at RelateIQ on Nov. 20, 2013.

Brian Morearty

November 20, 2013
Tweet

More Decks by Brian Morearty

Other Decks in Technology

Transcript

  1. ì   10  Docker  Tips  in  10  Minutes   Brian

     Morearty                          Hands  on  with  Docker  -­‐  handsonwith.com                              Nov  20,  2013  
  2. 1.  Getting  the  id  of  the  last-­‐run   container  

      15  Docker  Tips  in  5  Minutes   2  
  3. •  This  works  great  in  scripts:       • 

    But  is  annoying  interacCvely,  and  only   works  with  detached  containers.   •  Try  this  instead:   ! $ ID=$(docker run -d ubuntu myapp)! $ docker commit $ID helloworld! fd08a884dc79! 15  Docker  Tips  in  5  Minutes   3  
  4. ! $ 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  
  5. 2.  Why  you  always  want  a  Dockerfile,  even   if

     you  install  everything  in  the  shell   15  Docker  Tips  in  5  Minutes   5  
  6. $ 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  
  7.   •  Instead,  make  a  wee  liFle   Dockerfile  that

     is  FROM  the  image   you  made  interacCvely.   •  There  you  can  set  CMD,   ENTRYPOINT,  VOLUME,  etc.   15  Docker  Tips  in  5  Minutes   7  
  8. 3.  jq  -­‐  the  gangsta  way  to  parse    

    docker inspect’s  output ! 15  Docker  Tips  in  5  Minutes   8  
  9. ! $ 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   9  
  10. 4.  What  environment  variables  does   an  image  have?  

      15  Docker  Tips  in  5  Minutes   10  
  11. ! $ 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   11  
  12. ! $ cat Dockerfile! FROM ubuntu! CMD ["echo"]! ! $

    docker run \
 imagename \
 echo hello! hello! ! 15  Docker  Tips  in  5  Minutes   13   ! $ cat Dockerfile! FROM ubuntu! ENTRYPOINT ["echo"]! ! $ docker run \
 imagename \
 echo hello! echo hello! !     •  Overrideable.       •  Not  overridden  by   default.  
  13. 6.  Graph  the  dependencies  among   your  images    

    15  Docker  Tips  in  5  Minutes   14  
  14. ! # 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   15  
  15. 15  Docker  Tips  in  5  Minutes   16   • 

    And  hope  yours  doesn’t  look  like  this.  
  16. ! $ 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   19  
  17. 8.  Docker  source  code:  Go,  Go,  Go,   Grabote.  

    15  Docker  Tips  in  5  Minutes   20  
  18. 15  Docker  Tips  in  5  Minutes   22   commands.go

      The  CLI.     api.go   The  REST  API  router.     server.go   ImplementaCon  of  much  of  the  the  REST  API.   buildfile.go   The  Dockerfile  parser.  
  19. 9.  RUN  some  daemons,  exit  the   container.  What  happens?

      15  Docker  Tips  in  5  Minutes   23  
  20. $ cat Dockerfile! FROM ubuntu! MAINTAINER Brian Morearty! RUN apt-get

    install mysql-server! RUN /etc/init.d/mysql start ...! ! $ docker run -i -t mysqlimage bash! root@08d363f57161:/# ps aux! # Doesn’t show mysql daemon!         •  Don’t  run  a  daemon  in  your  Dockerfile.   15  Docker  Tips  in  5  Minutes   24  
  21. 10.  Letting  containers  talk  to  each   other   15

     Docker  Tips  in  5  Minutes   25  
  22. 15  Docker  Tips  in  5  Minutes   26   • I

     saved  the  best  for   last.  
  23. # 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   27  
  24. Extracted  from  our  1-­‐day,  intro-­‐level   Hands  on  with  Docker

     class.     Docker’s  only  training  partner.     Next  class:  Sat.,  December  14th.   handsonwith.com   15  Docker  Tips  in  5  Minutes   28