Docker Intro

Deni Bertovic / @denibertovic

About me

Deni Bertovic

initeQ d.o.o.

denibertovic.com

About this talk

  • Docker 101
  • Images/Containers/etc
  • Development Environments
  • Automation
  • Deployment
  • Orchestration/Scheduling
  • Service Discovery
  • Questions at the end
  • What is Docker?

    What is Docker?

    • "Open platform for developers and sysadmins to build, ship, and run distributed applications. "
    • Light weight containers
    • Available on most linux distros

    What's a container?

    Traditional VM vs. Docker

    Images vs. Containers?

    Images vs. Containers

    Why should you care?

    • It's fast
    • Minimal overhead/resource usage
    • Run thousands of containers
    • Easy to run your whole production stack locally

    Architecture

    What we've learned so far?

    • Container - Light weight virtualization
    • Image - Immutable snapshot of a container
    • Hub/Index- Central hub for sharing images

    Docker CLI example

    user@host:/$ docker pull postgres
    user@host:/$ docker run -d -p 5432:5432 postgres

    Manipulating Containers

    user@host:/$ docker ps
    
    CONTAINER ID  IMAGE      COMMAND   CREATED        STATUS
    d03ba4afb47c  debian:7.5 /bin/bash 10 seconds ago Up 9 seconds
    
    user@host:/$ docker logs d03ba4afb47c
    user@host:/$ docker stop d03ba4afb47c
    user@host:/$ docker kill d03ba4afb47c
    user@host:/$ docker start d03ba4afb47c

    Images - Dockerfiles

    
    FROM      debian:wheezy
    ENV DEBIAN_FRONTEND noninteractive
    
    ...
    
    RUN apt-get -qq update && apt-get -qq -y install postgresql-9.3 \
        postgresql-client-9.3 postgresql-contrib-9.3
    ADD postgresql.conf /etc/postgresql/9.3/main/postgresql.conf
    ...
    
    CMD /usr/local/bin/start_postgres.sh
                        

    Dockerfiles

    user@host:/$ docker build -t username/postgres .

    Hub

    
    user@host:/$ docker login
                            
    
    user@host:/$ docker push username/postgresql
                            
    
    co-worker@workstation2:/$ docker pull username/postgresql
                            

    Recap

    • How to work with containers
    • Use Dockerfiles for building images

    How does docker fit into your dev environment?

    Requirements

  • Let's say we have to build a website for a conference called Webcamp Zagreb
  • We're going to use Python/Django
  • For starters we're going to need a database (PostgreSQL)
  • We want potential contributors to be able to bring up a dev env fast
  • Docker Compose

    https://docs.docker.com/compose/

    
    # docker-compose.yml
    db:
        image: postgres:9.3
        ports:
            - "5432:5432"
    
    web:
        build: ./
        command: python manage.py runserver 0.0.0.0:8000
        volumes:
            - ./:/code
        links:
            - db
        ports:
            - "8000:8000"
    
    

    Container links

    
    root@web:/$ cat /etc/hosts
    
    172.17.0.9  db
                        
    
                        root@f9e4e78c60c6:/# export
    DATABASE_PORT="tcp://172.17.0.9:5432"
    DATABASE_PORT_5432_TCP="tcp://172.17.0.9:5432"
    DATABASE_PORT_5432_TCP_ADDR="172.17.0.9"
    DATABASE_PORT_5432_TCP_PORT="5432"
    DATABASE_PORT_5432_TCP_PROTO="tcp"
                        

    Dockerfile

    
    # Dockerfile
    FROM python:2.7
    
    ENV PYTHONUNBUFFERED 1
    
    RUN mkdir /code
    WORKDIR /code
    
    ADD requirements /code/requirements
    RUN pip install -r /code/requirements/dev.txt
    
    

    Benefits

    • Simplify runtime
    • Make sure everyone runs the same version of deps
    • What about virtualenv? --> C libraries!

    Everyone on the team runs the same version of database, cache, amqp, openssl, C libraries...etc

    Makes it easy to maintain a project that you haven't touched in a year!

    "docker-compose up" and do hotfix in 3 minutes, push to github, deploy

    Automation

    
    IMAGE_NAME="denibertovic/myimage"
    
    ## Build image from Dockerfile.
    build:
        docker build --rm -t $(IMAGE_NAME) .
    
    ## Push image to hub
    push:
        docker push $(IMAGE_NAME)
    
    ## Run container with default command.
    run: build_env
        @docker run -it \
            --log-driver=syslog \
            --env-file=/tmp/envfile \
            $(IMAGE_NAME)
    
    ## Spawn a bash prompt in a container.
    bash: build_env
        @docker run -it \
            --log-driver=syslog \
            --env-file=/tmp/env \
            $(IMAGE_NAME) /bin/bash
    
                        

    Hosting images

  • Docker Hub (has private repos)
  • Self hosted docker registry
  • DTR
  • Automate builds

  • Docker Hub has webhook support for github/bitbucket
  • Or just use Jenkins
  • Yeah but I have a Mac/Windows!

    Use Docker Toolbox!

    https://www.docker.com/toolbox

    Deployment

  • I'm talking about legacy stuff...
  • For single machine...Use Docker Compose
  • For multi node...
  • Use what you know!
  • Ansible/Chef/Puppet/Salt
  • Deployment

    
    - name: redis container
      docker:
        name: myredis
        image: redis
        command: redis-server --appendonly yes
        state: started
        expose:
        - 6379
    
                        

    Secrets

  • Hashicorp Vault
  • Shopify/ejson
  • Logging

  • Docker Logging Drivers:
  • Syslog
  • Journald
  • Fluentd
  • Gelf
  • Orchestration/Scheduling

  • Docker Swarm
  • Kubernetes
  • Mesos
  • Nomad
  • Service discovery

  • Consul
  • etcd
  • Zookeeper
  • Whatever/HA/key/value/store
  • In case it wasn't clear

    The last 6+ slides are ideas for future talks you should submit...

    Some more ideas...

    Docker Networking Plugins, Docker storage drivers...

    Thanks!

    Questions