Docker

 

Making your development workflows better

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

What is Docker ?

Docker allows you to package an application with all of its dependencies into a standardized unit for software development.

Why docker ?

  • Speed
  • Consistency
  • Portability
  • Flexibility
  • Scalability

Linux Containers

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

Used LXC, but now runs on libcontainer

Docker is not a Virtual Machine

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

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

.

 

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

Email - majua@mindfiresolutions.com
Twitter - @majuansari

Github - majuansari

Made with Slides.com