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

Docker Introduction

88b8138397e1d1441f4e4a54348bde09?s=47 ubermuda
March 28, 2014

Docker Introduction

88b8138397e1d1441f4e4a54348bde09?s=128

ubermuda

March 28, 2014
Tweet

Transcript

  1. Geoffrey Bachelet – @ubermuda What it is. What it can

    do for you.
  2. Who am I? • Geoffrey Bachelet • @ubermuda • Then:

    Lagardère, SensioLabs, KnpLabs, and PMSIPilot. • Now: Stage1, Gustave, Freelance.
  3. Go!

  4. "Container Engine" Based on the Linux kernel's LXC instructions Processes

    and resources isolation "chroot" on steroids / super lightweight VMs
  5. Containers vs VMs LXC / Jails / etc Use the

    host's kernel Boots in seconds 0 overhead, almost Easy to pass around Hypervisor Boots a complete OS Boots in... more time. All an OS' overhead Several Go images
  6. Docker vs Vagrant Less mature (1 year) 379 contributors Manages

    containers Dockerfile (homegrown DSL) DIY Mature (4 years) 338 contributors Manages... VMs. Vagrantfile (Ruby) Chef, Puppet, etc
  7. $  echo  "deb  https://get.docker.io/ubuntu  docker  main"  \     >

     /etc/apt/sources.list.d/docker.list   $  apt-­‐get  update   $  apt-­‐get  install  lxc-­‐docker
  8. Create a container

  9. $  docker  search  ubuntu   ! $  docker  run  -­‐i

     -­‐t  stackbrew/ubuntu  /bin/bash   Unable  to  find  image  'stackbrew/ubuntu'  locally   Pulling  repository  stackbrew/ubuntu   ...   ! root@21d86a0b8387:/#
  10. $  docker  search  ubuntu   ! $  docker  run  -­‐i

     -­‐t  stackbrew/ubuntu  /bin/bash   Unable  to  find  image  'stackbrew/ubuntu'  locally   Pulling  repository  stackbrew/ubuntu   ...   ! root@21d86a0b8387:/#
  11. credit: http:/ /docs.docker.io/en/latest/terms/image/

  12. $  docker  search  ubuntu   ! $  docker  run  -­‐i

     -­‐t  stackbrew/ubuntu  /bin/bash   Unable  to  find  image  'stackbrew/ubuntu'  locally   Pulling  repository  stackbrew/ubuntu   ...   ! root@21d86a0b8387:/#
  13. credit: http:/ /docs.docker.io/en/latest/terms/image/

  14. $  docker  search  ubuntu   ! $  docker  run  -­‐i

     -­‐t  stackbrew/ubuntu  /bin/bash   Unable  to  find  image  'stackbrew/ubuntu'  locally   Pulling  repository  stackbrew/ubuntu   ...   ! root@21d86a0b8387:/#
  15. Commit the container

  16. root@21d86a0b8387:/#  apt-­‐get  install  nginx   root@21d86a0b8387:/#  exit   ! $

     docker  ps  -­‐q  -­‐n  1   9cc9c762a0eb   ! $  docker  commit  9cc9c762a0eb  m6web/nginx   240198b750c3cc950c60005d6d24cae4fc2dbcc6c31e274574af68d4a2e8   ! $  docker  images   REPOSITORY            TAG                  IMAGE  ID   m6web/nginx          latest            240198b750c3
  17. root@21d86a0b8387:/#  apt-­‐get  install  nginx   root@21d86a0b8387:/#  exit   ! $

     docker  ps  -­‐q  -­‐n  1   9cc9c762a0eb   ! $  docker  commit  9cc9c762a0eb  m6web/nginx   240198b750c3cc950c60005d6d24cae4fc2dbcc6c31e274574af68d4a2e8   ! $  docker  images   REPOSITORY            TAG                  IMAGE  ID   m6web/nginx          latest            240198b750c3
  18. root@21d86a0b8387:/#  apt-­‐get  install  nginx   root@21d86a0b8387:/#  exit   ! $

     docker  ps  -­‐q  -­‐n  1   9cc9c762a0eb   ! $  docker  commit  9cc9c762a0eb  m6web/nginx   240198b750c3cc950c60005d6d24cae4fc2dbcc6c31e274574af68d4a2e8   ! $  docker  images   REPOSITORY            TAG                  IMAGE  ID   m6web/nginx          latest            240198b750c3
  19. root@21d86a0b8387:/#  apt-­‐get  install  nginx   root@21d86a0b8387:/#  exit   ! $

     docker  ps  -­‐q  -­‐n  1   9cc9c762a0eb   ! $  docker  commit  9cc9c762a0eb  m6web/nginx   240198b750c3cc950c60005d6d24cae4fc2dbcc6c31e274574af68d4a2e8   ! $  docker  images   REPOSITORY            TAG                  IMAGE  ID   m6web/nginx          latest            240198b750c3
  20. root@21d86a0b8387:/#  apt-­‐get  install  nginx   root@21d86a0b8387:/#  exit   ! $

     docker  ps  -­‐q  -­‐n  1   9cc9c762a0eb   ! $  docker  commit  9cc9c762a0eb  m6web/nginx   240198b750c3cc950c60005d6d24cae4fc2dbcc6c31e274574af68d4a2e8   ! $  docker  images   REPOSITORY            TAG                  IMAGE  ID   m6web/nginx          latest            240198b750c3
  21. Run the image

  22. $  docker  run  -­‐p  80  m6web/nginx  nginx  -­‐g  'daemon  off;'

  23. $  docker  ps   CONTAINER  ID    ...    PORTS

      923cb190dbc3    ...    0.0.0.0:49155-­‐>80/tcp   ! $  curl  http://localhost:49155   <html>   <head>   <title>Welcome  to  nginx!</title>   ...
  24. Dockerfile

  25. credit: http:/ /docs.docker.io/en/latest/terms/image/

  26. FROM  stackbrew/ubuntu   ! RUN  apt-­‐get  update  -­‐y   RUN

     apt-­‐get  install  nginx  -­‐y   RUN  echo  "\ndaemon  off;"  >>  /etc/nginx/nginx.conf   ! EXPOSE  80   ENTRYPOINT  ["nginx"]
  27. FROM  stackbrew/ubuntu   ! RUN  apt-­‐get  update  -­‐y   RUN

     apt-­‐get  install  nginx  -­‐y   RUN  echo  "\ndaemon  off;"  >>  /etc/nginx/nginx.conf   ! EXPOSE  80   ENTRYPOINT  ["nginx"]
  28. FROM  stackbrew/ubuntu   ! RUN  apt-­‐get  update  -­‐y   RUN

     apt-­‐get  install  nginx  -­‐y   RUN  echo  "\ndaemon  off;"  >>  /etc/nginx/nginx.conf   ! EXPOSE  80   ENTRYPOINT  ["nginx"]
  29. FROM  stackbrew/ubuntu   ! RUN  apt-­‐get  update  -­‐y   RUN

     apt-­‐get  install  nginx  -­‐y   RUN  echo  "\ndaemon  off;"  >>  /etc/nginx/nginx.conf   ! EXPOSE  80   ENTRYPOINT  ["nginx"]
  30. FROM  stackbrew/ubuntu   ! RUN  apt-­‐get  update  -­‐y   RUN

     apt-­‐get  install  nginx  -­‐y   RUN  echo  "\ndaemon  off;"  >>  /etc/nginx/nginx.conf   ! EXPOSE  80   ENTRYPOINT  ["nginx"]
  31. $  docker  run  \     -­‐p  8000:80  \  

      -­‐v  $(pwd):/usr/share/nginx/www  \     m6web/nginx   ! $  echo  'Hello,  world!'  >  index.html   ! $  curl  http://localhost:8000   Hello,  world!
  32. $  docker  run  \     -­‐p  8000:80  \  

      -­‐v  $(pwd):/usr/share/nginx/www  \     m6web/nginx   ! $  echo  'Hello,  world!'  >  index.html   ! $  curl  http://localhost:8000   Hello,  world!
  33. $  docker  run  \     -­‐p  8000:80  \  

      -­‐v  $(pwd):/usr/share/nginx/www  \     m6web/nginx   ! $  echo  'Hello,  world!'  >  index.html   ! $  curl  http://localhost:8000   Hello,  world!
  34. $  docker  run  \     -­‐p  8000:80  \  

      -­‐v  $(pwd):/usr/share/nginx/www  \     m6web/nginx   ! $  echo  'Hello,  world!'  >  index.html   ! $  curl  http://localhost:8000   Hello,  world!
  35. Now what?

  36. credit: http:/ /robtiffany.com/5-steps-to-containerize-your-apps/

  37. FROM  stackbrew/ubuntu   ! ENV  DEBIAN_FRONTEND  noninteractive   ! RUN

     apt-­‐get  update  -­‐y   RUN  apt-­‐get  install  -­‐y  daemontools  curl  nginx  \     php5-­‐fpm  php5-­‐cli  php5-­‐mysqlnd  php5-­‐intl  \     mysql-­‐server   ! RUN  curl  -­‐sS  https://getcomposer.org/installer  |  php   RUN  mv  composer.phar  /usr/local/bin/composer   ! RUN  echo  "daemonize=no"  >  /etc/php5/fpm/pool.d/daemonize.conf   RUN  echo  "\ndaemon  off;"  >>  /etc/nginx/nginx.conf   ! ADD  services  /srv/services   RUN  find  /srv/services  -­‐name  run  -­‐exec  chmod  +x  {}  \;   ! ADD  nginx.conf  /etc/nginx/sites-­‐enabled/default   ADD  php.ini  /etc/php5/fpm/php.ini   ADD  php.ini  /etc/php5/cli/php.ini   ADD  entrypoint.sh  /usr/local/bin/entrypoint.sh   ! EXPOSE  80   ENTRYPOINT  ["/usr/local/bin/entrypoint.sh"]
  38. FROM  stackbrew/ubuntu   ENV  DEBIAN_FRONTEND  noninteractive   ! RUN  apt-­‐get

     update  -­‐y   RUN  apt-­‐get  install  -­‐y  daemontools  curl  nginx  \     php5-­‐fpm  php5-­‐cli  php5-­‐mysqlnd  php5-­‐intl  \     mysql-­‐server   ! RUN  echo  "daemonize=no"  \      >  /etc/php5/fpm/pool.d/daemonize.conf   RUN  echo  "\ndaemon  off;"  \     >>  /etc/nginx/nginx.conf
  39. FROM  stackbrew/ubuntu   ENV  DEBIAN_FRONTEND  noninteractive   ! RUN  apt-­‐get

     update  -­‐y   RUN  apt-­‐get  install  -­‐y  daemontools  curl  nginx  \     php5-­‐fpm  php5-­‐cli  php5-­‐mysqlnd  php5-­‐intl  \     mysql-­‐server   ! RUN  echo  "daemonize=no"  \      >  /etc/php5/fpm/pool.d/daemonize.conf   RUN  echo  "\ndaemon  off;"  \     >>  /etc/nginx/nginx.conf
  40. RUN  curl  -­‐sS  https://getcomposer.org/installer  |  php   RUN  mv  composer.phar

     /usr/local/bin/composer
  41. ADD  services  /srv/services   RUN  find  /srv/services  -­‐name  run  -­‐exec

     chmod  +x  {}  \; =>  services/mysqld/run   #!/bin/bash   exec  mysqld =>  services/nginx/run   #!/bin/bash   exec  nginx =>  services/php5-­‐fpm/run   #!/bin/bash   exec  php5-­‐fpm
  42. ADD  nginx.conf  /etc/nginx/sites-­‐enabled/default   ADD  php.ini  /etc/php5/fpm/php.ini   ADD  php.ini

     /etc/php5/cli/php.ini   ADD  entrypoint.sh  /usr/local/bin/entrypoint.sh
  43. server  {                

     listen              80;   !                server_name    m6web;                  access_log      /var/log/nginx/access.log;                  error_log        /var/log/nginx/error.log;   !                root  /var/www/web/;                  index  app_dev.php;   !                location  /  {                                  try_files  $uri  $uri/  /app_dev.php?$query_string;                  }   !                location  ~  [^/]\.php(/|$)  {                                  fastcgi_pass  127.0.0.1:9000;                                  include  fastcgi_params;                  }   }
  44. #!/bin/bash   cd  /var/www   composer  install   exec  svscan

     /srv/services
  45. EXPOSE  80   ENTRYPOINT  ["/usr/local/bin/entrypoint.sh"]

  46. docker  run  \     -­‐p  8000:80  \    

    -­‐v  /project:/var/www  \     m6web/symfony2
  47. alias  drun='docker  run  -­‐t  \     -­‐p  8000:80  \

        -­‐v  $(pwd):/var/www  \     m6web/symfony2'
  48. $  cd  /project   $  drun   Loading  composer  repositories

     with  package  information   Installing  dependencies  (including  require-­‐dev)  from  lock  file   ...   ! $  curl  http://localhost:8000
  49. credit: http:/ /www.goinflippincrazy.com/2014/03/10/anger-or-happiness-which-is-more-motivating/

  50. Container Links

  51. credit: http:/ /www.goinflippincrazy.com/2014/03/10/anger-or-happiness-which-is-more-motivating/

  52. $  docker  run  -­‐name  redis  m6web/redis   $  docker  run

     ...  \     -­‐link  redis:redis  \     m6web/symfony2
  53. $  docker  run  -­‐name  redis  m6web/redis   $  docker  run

     ...  \     -­‐link  redis:redis  \     m6web/symfony2
  54. REDIS_PORT=tcp://172.17.0.44:6379   REDIS_PORT_6379_TCP_PROTO=tcp   REDIS_PORT_6379_TCP_PORT=6379   REDIS_PORT_6379_TCP=tcp://172.17.0.44:6379   REDIS_NAME=/crimson_squirrel9/redis  

    REDIS_PORT_6379_TCP_ADDR=172.17.0.44
  55. REDIS_PORT=tcp://172.17.0.44:6379   REDIS_PORT_6379_TCP_PROTO=tcp   REDIS_PORT_6379_TCP_PORT=6379   REDIS_PORT_6379_TCP=tcp://172.17.0.44:6379   REDIS_NAME=/crimson_squirrel9/redis  

    REDIS_PORT_6379_TCP_ADDR=172.17.0.44
  56. imports:     -­‐  {  resource:  parameters.php  } app/config/config.yml

  57. <?php   ! $container-­‐>setParameter(     'redis_host',     getenv('REDIS_PORT_6379_TCP_ADDR')

      ); app/config/parameters.php
  58. <?php   ! $container-­‐>setParameter(     'redis_host',     getenv('REDIS_PORT_6379_TCP_ADDR')

      ); app/config/parameters.php
  59. Volume sharing

  60. FROM  stackbrew/ubuntu   VOLUME  ["/var/lib/mysql"]   ENTRYPOINT  ["true"] data  container

  61. FROM  stackbrew/ubuntu   ENV  DEBIAN_FRONTEND  noninteractive   RUN  apt-­‐get  update

     -­‐y   RUN  apt-­‐get  install  -­‐y  mysql-­‐server  mysql-­‐client   ADD  entrypoint.sh  /usr/local/bin/entrypoint.sh   ENTRYPOINT  ["/usr/local/bin/entrypoint.sh"] mysql  container
  62. #!/bin/bash   ! if  [  !  -­‐f  /var/lib/mysql/ibdata1  ];  then

             mysql_install_db  >  /dev/null  2>  /dev/null   fi   ! mysqld_safe  >  /dev/null  2>  /dev/null  &   ! while  !  mysqladmin  -­‐s  ping;  do      echo  -­‐n  .;      sleep  1;   done;   ! exec  mysql
  63. #!/bin/bash   ! if  [  !  -­‐f  /var/lib/mysql/ibdata1  ];  then

             mysql_install_db  >  /dev/null  2>  /dev/null   fi   ! mysqld_safe  >  /dev/null  2>  /dev/null  &   ! while  !  mysqladmin  -­‐s  ping;  do      echo  -­‐n  .;      sleep  1;   done;   ! exec  mysql
  64. #!/bin/bash   ! if  [  !  -­‐f  /var/lib/mysql/ibdata1  ];  then

             mysql_install_db  >  /dev/null  2>  /dev/null   fi   ! mysqld_safe  >  /dev/null  2>  /dev/null  &   ! while  !  mysqladmin  -­‐s  ping;  do      echo  -­‐n  .;      sleep  1;   done;   ! exec  mysql
  65. #!/bin/bash   ! if  [  !  -­‐f  /var/lib/mysql/ibdata1  ];  then

             mysql_install_db  >  /dev/null  2>  /dev/null   fi   ! mysqld_safe  >  /dev/null  2>  /dev/null  &   ! while  !  mysqladmin  -­‐s  ping;  do      echo  -­‐n  .;      sleep  1;   done;   ! exec  mysql
  66. $  docker  build  -­‐t  m6web/data  data/   $  docker  build

     -­‐t  m6web/mysql  mysql/
  67. $  docker  run  -­‐name  mysql-­‐data  m6web/data   $  docker  run

     -­‐i  -­‐t  \     -­‐-­‐volumes-­‐from  mysql-­‐data  \     m6web/mysql   ...   mysql>  create  database  'symfony';
  68. #!/bin/bash   ! if  [  !  -­‐f  /var/lib/mysql/ibdata1  ];  then

             mysql_install_db   fi   ! exec  mysqld_safe entrypoint.sh
  69. <?php   ! $container-­‐>setParameter('redis_host',     getenv('REDIS_PORT_6379_TCP_ADDR')   );  

    ! $container-­‐>setParameter('database_host',     getenv('MYSQL_PORT_3306_TCP_ADDR')   ); app/config/parameters.php
  70. $  docker  run  -­‐name  mysql-­‐data  m6web/data   $  docker  run

     -­‐name  redis  m6web/redis   $  docker  run  -­‐-­‐volumes-­‐from  mysql-­‐data  -­‐name  mysql  m6web/mysql   $  docker  run  ...  \     -­‐link  redis:redis  \     -­‐link  mysql:mysql  \     m6web/symfony2
  71. $  docker  run  -­‐name  mysql-­‐data  m6web/data   $  docker  run

     -­‐name  redis  m6web/redis   $  docker  run  -­‐-­‐volumes-­‐from  mysql-­‐data  -­‐name  mysql  m6web/mysql   $  docker  run  ...  \     -­‐link  redis:redis  \     -­‐link  mysql:mysql  \     m6web/symfony2
  72. None
  73. And more! • docker run -d, attach, logs, top, ...

    • Orchestration (https://github.com/marmelab/gaudi) • Docker Index / local Index • Docker Remote API • Dockerfile: USER, WORKDIR, ONBUILD, ... • ...
  74. Concepts recap. • An image is like a VM image,

    it contains the hard- drive and some configuration. • Images can be pushed and pulled from an index. • A container is a running instance of an image.
  75. Concepts recap. • You can commit a terminated container, and

    you get a reusable image representing the state of that container. • Volumes are like shared directories. Containers can share zero or many volumes. • Containers can be linked to one another.
  76. Thanks! https://github.com/ubermuda/m6web-docker/ Geoffrey Bachelet – @ubermuda