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

I'm a Docker Rocker and So Can You!

I'm a Docker Rocker and So Can You!

Introducing the basics of Docker, what it is, and how to adopt to the Docker paradigm and the "docker way".

Avatar for Buster "Silver Eagle" Neece

Buster "Silver Eagle" Neece

August 10, 2018

Other Decks in Technology

Transcript

  1. I’m a Docker Rocker and So Can You I Can

    Hardly Containerize My Excitement Hosted by Buster “Silver Eagle” Neece
  2. What Wikipedia Says Docker is • Initially released in 2013

    • Builds on top of existing Linux technologies like LXC, cgroups, etc • Used to run software packages called "containers"
  3. Containers • Standalone software packages configured by “images” • Isolated

    from each other and use their own set of tools and libraries • Communicate with each other through well-defined channels (mostly TCP/IP) • Share the same kernel • More lightweight than virtual machines
  4. What This Means for You • Better dev and production

    parity: You can use the same Docker image(s) to power both your local dev environment and your production servers! • Easier spin-up of new machines/servers/team members: Images are all configured in predictable text files Pre-built images can be uploaded to a “hub” and pulled down quickly • Go “web-scale” in a jiffy: If you build your app right, you can go from one container doing one thing to a hundred doing that thing
  5. Where we are (or were): Vagrant and Virtual Machines Pros:

    • Everything’s in one big honkin’ VM • You can SSH into the VM and goof around to your heart’s content • Can use existing infrastructure automation tools (like Ansible, Chef, or Puppet) Cons: • Everything’s in one big honkin’ VM • Changes you’ve made inside the VM after the provision aren’t saved or documented anywhere • Small and subtle differences between local dev and production servers add up to pain points down the road
  6. Splitting the App Up PHP Web Application (php-fpm) Web Server

    Database Cache Virtual Machine PHP Web Application Database Web Server Cache
  7. The Docker Way • Each container has one single command

    that it runs • When the command exits, the container stops • Daemons aren’t a thing; some containers just have really long-running commands and don’t exit • Commands can be anything you’d run in a shell • If you absolutely need to run more than one thing in a Docker container, you can use tools like Supervisor: http://supervisord.org/ Each container should only do one thing
  8. The Docker Hub (hub.docker.com) Prebuilt Images for: • Nginx •

    Apache • PHP • MySQL/MariaDB • MongoDB • and much more For PHP Developers: • Wordpress • Drupal • Magento • MediaWiki • etc
  9. Roll Your Own Images • Defined by a Dockerfile •

    FROM scratch ◦ FROM debian:stretch-slim ▪ FROM php:7.2-stretch ◦ FROM ubuntu:bionic ▪ FROM ◦ FROM alpine:latest ▪ FROM php:7.2-alpine
  10. Example Dockerfile FROM ubuntu:bionic MAINTAINER Foo Barrington version: 0.1 RUN

    apt-get update && apt-get install -y apache2 && apt-get clean && rm -rf /var/lib/apt/lists/* ENV APACHE_RUN_USER www-data EXPOSE 80 CMD ["/usr/sbin/apache2", "-D", "FOREGROUND"] https://docs.docker.com/engine/reference/builder/
  11. Does Size Matter? Try Alpine • Alpine Linux • Built

    for containers • Base image size of ~3MB • Uses musl instead of glibc • Lots of prebuilt images: ◦ nginx:alpine ◦ php:7.2-alpine ◦ redis:alpine
  12. Where to Store Stuff • Temporary files may not need

    to be stored outside the container! • Flat files: Share a volume between containers (volumes don’t go away) • Databases: Store sessions and other persistent data in your DB • Dedicated Cache: Store caches and sessions in a dedicated key/value store, like Redis or Memcached ^ Handy library for abstracting caching: Stash http://www.stashphp.com/
  13. Environment Variables Are Your Friend • Can be passed into

    Docker containers easily: ◦ From the docker run command ◦ Inside docker-compose.yml ◦ If using Docker Compose, they can also be in .env at the project root • Can easily customize for each environment you’re running in • Helpful tool for importing env files if you’re not in Docker: DotEnv https://github.com/symfony/dotenv Caveat: By default, PHP-FPM doesn’t pull environment variables from where it’s running, instead has them sent over by the web server (nginx, Apache, etc) You can add: clear_env = no ...to php-fpm.conf to change this behavior
  14. Docker for Linux • Native support on many distros (CentOS,

    Ubuntu, Debian, etc) • Nowadays split into Enterprise Edition ($$$) and Community Edition (free!) • Packages often named docker-ce • May need to install a PPA for latest version • Very high performance and low overhead
  15. Docker for Windows and MacOS Docker for Windows • Uses

    HyperV • Requires Windows 10 Pro • Kubernetes support out of the box • Some known issues Docker for MacOS • Uses’s MacOS’s Hypervisor API / xhyve Docker Toolkit • Considered “legacy” version • Uses VirtualBox • Includes docker-compose
  16. docker-compose • Python utility script, originally called “Fig” • One

    single file, docker-compose.yml, that defines how multiple services interact, exposed ports, volumes, networks, etc. • Turns what could be a 40-line Docker bash command into: docker-compose up -d • Comes with Docker for Windows and Mac, can be installed by itself too: https://docs.docker.com/compose/install/#install-compose
  17. Example docker-compose.yml version: '3.3' services: db: image: mysql:5.7 volumes: -

    db_data:/var/lib/mysql restart: always environment: MYSQL_ROOT_PASSWORD: somewordpress MYSQL_DATABASE: wordpress MYSQL_USER: wordpress MYSQL_PASSWORD: wordpress wordpress: depends_on: - db image: wordpress:latest ports: - "8000:80" restart: always environment: WORDPRESS_DB_HOST: db:3306 WORDPRESS_DB_USER: wordpress WORDPRESS_DB_PASSWORD: wordpress volumes: db_data:
  18. docker-app • Brand new, experimental program • Extends upon Docker

    Compose to create a .dockerapp file that contains: ◦ Project metadata ◦ Docker-compose-style services ◦ Environment-specific settings • Docker App can be distributed on the Docker Hub just like a Docker image • Works directly with Kubernetes and can generate Helm charts for you • Check it out at https://github.com/docker/app