Docker

and

Docker Compose

Making your development workflows better

What is Docker?

  • Speed
  • Consistency
  • Portability
  • Flexibility
  • Scalability

Open platform for building, shipping and running distributed applications.

Linux Containers

OS level virtualization environment for running multiple isolated Linux systems on a single Linux host.

Used LXC, but now runs on libcontainer

VMs vs. Containers

Virtual Machine

Docker

Installing Docker

sudo apt-get install docker.io

Mac OSX / Windows

Linux

debian

wget -qO- https://get.docker.com/ | sh

ubuntu

Docker Image

Container

Base Image (Debian, CentOS, Alpine)

Apache Installed

PHP Installed

Apache config file added

Application files added

Runtime file system (read-write)

Dockerfile

FROM ubuntu

MAINTAINER George Costanza <george@vandalayindustries.com>

RUN apt-get update && apt-get install -y apache2 php

COPY configs/apache.conf /etc/apache/conf

WORKDIR /app

COPY . /app

CMD ./run.sh
docker build -t my_first_image .

Docker Hub

Public repository or docker images

~100

Official Images

45k+

User Images

https://quay.io/ - private repos

Running an image

docker run --rm my_first_image

Runs the image inside a new container

Common arguments:

  • -e    set environment variables
  • --expose    expose ports to linked containers
  • -p    bind ports to host
  • -v    bind mount a volume
  • -d    run in detached mode
  • lots more...

Managing images/containers

docker images

View all images on your system

docker ps [-a]

View running or all containers

docker rm [tag/hash]

Delete a container

docker rmi [hash]

Delete an image

Multi-container apps

PHP​

MySQL

Nginx

Redis

Containers are linked together by Docker daemon

  • /etc/hosts file*
  • environment vars

Multi-container apps

MySQL

docker run --expose 3306 -d mysql

Redis

docker run --expose 6379 -d redis

PHP

docker run --expose 9000 -d --link /redis:redis
--link /mysql:mysql -v /app:/app my_first_image

Nginx

docker run -p 80:80 -d --link /app:app nginx

Docker Compose

  • Define containers in a single file
  • Spin them up with one command
  • Formerly known as Fig

A tool for defining and running multi-container applications with Docker

Installing

curl -L https://github.com/docker/compose/releases/download/1.3.2/docker-compose-`uname -s`-`uname -m` > /usr/local/bin/docker-compose

chmod +x /usr/local/bin/docker-compose
sudo pip install -U docker-compose

docker-compose.yml

web:
  build: .
  expose:
   - "9000"
  volumes:
   - .:/app
  links:
   - redis
   - mysql
redis:
  image: redis
mysql:
  image: mysql
nginx:
  image: nginx
  ports:
   - 80:80
  links:
   - web
docker-compose up [-d]
  • Download any images
  • Build images if needed
  • Launch containers in order
  • Link containers together
  • Attach stdout to console

Managing images

docker-compose ps

View current containers for project

docker-compose stop [app]

Stop any or all running containers

docker-compose rm [app]

Delete a container

docker-compose build [app]

Force a re-build of an image

DEMO

Running in Production

  • Kubernetes
  • Mesosphere
  • Deis
  • Dokku
  • Panamax
  • Digital Ocean
  • AWS EC2 Container Service
  • Google Container Engine
  • Tutum
  • So many more...

Tips / Tricks

  • Single process per image
  • Keep images lightweight
    • ​Use small base images
    • Install minimum packages
    • Combine commands
  • Use a .dockerignore file
  • Configuration via environment variables
  • Containers should be immutable
  • Treat containers as ephemeral entities
  • Version images with tags
  • Mount code folder during development

Thanks

Twitter - @davesters

Github - davesters

Website - http://www.lovesmesomecode.com

More resources...

Made with Slides.com