$30 off During Our Annual Pro Sale. View Details »

Docker: Contain All The Things

Docker: Contain All The Things

An introduction to Docker and Containers

Davey Shafik

June 16, 2015
Tweet

More Decks by Davey Shafik

Other Decks in Programming

Transcript

  1. Proprietary and Confidential •Community Engineer at Engine Yard •Author of

    Zend PHP 5 Certification Study Guide, Sitepoints PHP Anthology: 101 Essential Tips, Tricks & Hacks & PHP Master: Write Cutting Edge Code •A contributor to Zend Framework 1 & 2, phpdoc, & PHP internals • Original creator of PHAR/PHP_Archive •@dshafik Davey Shafik
  2. Docker is a tool that can package an application and

    its dependencies in a virtual container that can run on any Linux server. This helps enable flexibility and portability on where the application can run, whether on premise, public cloud, private cloud, bare metal, etc. “ ” Source: 451 Research (Emphasis Mine)
  3. [Docker] automates the deployment of applications inside software containers, by

    providing an additional layer of abstraction and automation of operating- system-level virtualization on Linux. “ ” Source: Wikipedia
  4. • Docker is not the container technology • Docker is

    an abstraction and automation framework for deploying applications on Linux containers (LXC) • Provides process isolation (sandboxing) • Does not require a virtualized environment, runs on the host OS What is Docker?
  5. Container What is Docker? Host OS (Linux) Server (Real or

    Virtual) Docker Daemon binaries/libs Container binaries/libs Container binaries/libs Container binaries/libs
  6. • You can build an image from scratch: don’t •

    Extend from a base image – Ubuntu, Debian – CentOS, RHEL, Fedora – ArchLinux – OpenSUSE – Gentoo – CoreOS Extending Images
  7. • Minimal Distro (based on Gentoo) • Automatic Updates (Atomic

    + Rollbacks) • Container Support • Cluster Management (fleet) • Service Discovery (etcd) • Everything is a service, accessed via an API CoreOS
  8. • Manages Container • Systemd for the cluster • Schedules

    tasks automatically • Resolving conflicts • Automatically handles machine failure Fleet
  9. • Key-Value Store • Handles service discovery • Configuration Storage

    • Guaranteed Consistency – Useful for implementing things like distributed locking etcd
  10. • Flannel: Container Networking Layer • Rkt: CoreOS backed container

    format (alternative to Docker) • Locksmith: Reboot Manager, allows you to smartly reboot segments of a cluster and ensure zero interruptions • Many more… Other Tools
  11. • Create a Dockerfile • Have Docker Hub build it

    for you by linking to a Github/Bitbucket repo • Build it locally • Build it on deploy with Deis Building an Image
  12. • Github for Docker Images – Sign up with Github

    (or with bespoke credentials) – Supports organizations – Private images (one free) – Automatic builds on push to Github/Bitbucket – Images: <username or organization>/<image> Docker Hub
  13. Proprietary and Confidential FROM ubuntu:wily MAINTAINER Davey Shafik <[email protected]> RUN

    apt-get update -qq RUN apt-get install -q -y memcached CMD ["memcached", "-u", "daemon"] EXPOSE 11211 Dockerfile Example: memcached
  14. • Must start with FROM (first non-comment), defines the base

    image • Creates images after each step as required • Caches and will re-use any step that it can • The container will continue running for as long as the CMD is running the foreground. Will only run the last CMD • With Deis, you may only EXPOSE one port Dockerfile Example: memcached
  15. • The entire CWD is available to the Dockerfile: This

    is the build Context • Use .dockerignore file to ignore files in the CWD. Users Go’s filepath.Match pattern matching • Use WORKDIR to change CWD • Use ADD to add additional files, directories, or remote files o ADD <src> <dest> o # Required for paths with whitespace
 ADD ["src", “dest”] o Supports wildcards Context
  16. • RUN: Run commands to build the final container image

    • CMD: The default process, or arguments the container is going to run when run – ENTRYPOINT: A default command to which default arguments from CMD, or those passed in via docker run, are passed. • Relative to the WORKDIR • Runs as root unless changed with USER Running Commands
  17. • All three take two forms (at least): – exec

    form: [“executable”, “param1”, “param…”] – shell form: command param1 param… • CMD also takes just arguments to pass to the ENTRYPOINT: – [“param1”, “param…”] • exec and param form do not perform shell interpolation of params (e.g. $USER or `hostname`) Running Commands
  18. Proprietary and Confidential RUN apt-get install -y memcached RUN [“apt-get”,

    “install”, “-y”, “memcached”] # This is NOT the same: 
 RUN [“apt-get”, “install -y memcached”] Running Commands: RUN
  19. Proprietary and Confidential ENTRYPOINT memcached CMD [“-u”, “daemon”] ENTRYPOINT memcached

    $ docker run -u daemon $ docker exec -u daemon -p 11212 Running Commands: ENTRYPOINT
  20. • Commands to run when using the image as the

    base for another image • Allows you to call any other Dockerfile instruction (some may not make sense however) • For example: the base ubuntu image could ensure that apt-get update is always run whenever you build upon that base image. – ONBUILD RUN apt-get update -qq Deferred Commands
  21. • Similar to ADD but instead of adding files to

    the context, it copies it from the context into the resulting image • Two syntaxes: – COPY <src> <dest> – COPY [“src”, “dest”] • Supports wildcards • Relative to the WORKDIR Copying Files
  22. • Volumes create a mount point within the container •

    Volumes are shared with the host, or other containers • Set at runtime • Files created within the VOLUME path prior to running are copied over to the mounted share at runtime Sharing Files
  23. • Associate meta-data using LABEL • Each LABEL creates a

    new image! •LABEL version=“1.0” •Read meta-data using docker inspect Meta-data
  24. • docker run -d -p 11211:11211 dshafik/memcached o -d: daemonizes

    the container o -p: bind container and host port o <image>: the image to launch • docker ps: shows currently running containers • telnet <host> 11211: telnet to the mecached daemon • docker stop <hash or name>: stop the container Demo
  25. • EXPOSE: In the Dockerfile • --expose with docker run

    (useful for with custom run commands) • Bind to host: o -p: bind host port to container port: -p <host>:<container> o -P: bind all exposed ports to a random ports on the host – Find ports: docker port <container> <container port> Ports
  26. • Intra-Container Communication (TCP and/or UDP) • Linked by container

    name • Sets ENVironment variables and • Updates /etc/hosts file • Doesn’t require ports be exposed to the outside (e.g. using -p or -P) Linking Containers
  27. Proprietary and Confidential $ docker run -d -P --name <name>

    <image> $ docker run -d -P --link <name>:<alias> <image> Linking Containers
  28. • Exposes all ENV vars from source container • Creates

    ENV vars: - <alias>_PORT_<port>_<protocol>_ADDR = <IP> - <alias>_PORT_<port>_<protocol>_PORT=<port> - <alias>_PORT_<port>_<protocol>_PROTO=<protocol> - <alias>_PORT=<first EXPOSEd port> - <alias>_ENV_<environment vars> = <value> • Add <alias> to hosts file: ping <alias>: <container IP> Linking Containers
  29. • Using docker hub – docker push <image> – docker

    pull <image> • Without docker hub – docker save -o <image>.tar <image> – docker load -i <image>.tar Sharing Images