Why Docker?
Chris Tankersley
@dragonmantank
Dayton PHP, April 2017
Sunshine PHP 2017 1
What Is Docker?
āDocker is an open platform for developers and sysadmins to build, ship, and run distributed applications. Consisting of Docker Engine, a portable, lightweight runtime and packaging tool, and Docker Hub, a cloud service for sharing applications and automating workflows, Docker enables apps to be quickly assembled from components and eliminates the friction between development, QA, and production environments.ā
Sunshine PHP 2017 2
https://www.docker.com/whatisdocker/
Containers
Sunshine PHP 2017 3
Normal Bare-Metal Server
Sunshine PHP 2017 4
CPU RAM HD Network
Operating System
nginx PHP DB
Virtual Machines
Sunshine PHP 2017 5
CPU RAM HD Network
Operating System
nginx PHP DB
Operating System
nginx PHP DB
Operating System
Hypervisor
Containers
Sunshine PHP 2017 6
CPU RAM HD Network
Operating System
nginxnginx PHP DB PHP DB
Containers Are Not New
ā¢ LXC (Linux Containers)ā¢ OpenVZā¢ Systemd-nspawnā¢ Qemu/kvmā¢ BSD Jailsā¢ Solaris Zonesā¢ chroot
Sunshine PHP 2017 7
Docker is an Ecosystem
Sunshine PHP 2017 8
Docker Engine
Docker is an Ecosystem
Sunshine PHP 2017 9
Docker ComposeDocker Machine Docker Swarm
How does it work?
Sunshine PHP 2017 10
Uses a variety of existingContainer technologies
Server ContainersHyper-V Containers xhyve Virtualization
Sorry OSX < 10.10 and Windows < 10 Users
Docker Toolbox
Sunshine PHP 2017 11
Using Docker ā The Basics
Sunshine PHP 2017 12
Running a container
ā¢ `docker run` will run a containerā¢ This will not restart an existing container, just create a new oneā¢ docker run [options] IMAGE [command] [arguments]ā¢ [options ]modify the docker process for this containerā¢ IMAGE is the image to useā¢ [command] is the command to run inside the containerā¢ [arguments] are arguments for the command
Sunshine PHP 2017 13
Running a simple shell
Sunshine PHP 2017 14
Running a simple shell
Sunshine PHP 2017 15
Running a simple shell
Sunshine PHP 2017 16
Whatās Going On?
Sunshine PHP 2017 17
Ubuntu Kernel
/+ bin/+ etc/+ dev/+ home/+ usr/+ var/+ lib/+ ā¦
/+ bin/+ etc/+ dev/+ home/+ usr/+ var/+ lib/+ ā¦
php
nginx
bash
Running Multiple Copies of Software
Sunshine PHP 2017 18
Running Two Webservers
Sunshine PHP 2017 19
Running Two Webservers
Sunshine PHP 2017 20
Running Two Webservers
Sunshine PHP 2017 21
Running Two Webservers
Sunshine PHP 2017 22
Running Two Webservers
Sunshine PHP 2017 23
Running Two Webservers
Sunshine PHP 2017 24
Running Two Webservers
Sunshine PHP 2017 25
Running Two Webservers
Sunshine PHP 2017 26
Some Notes
ā¢ All three containers are 100% self containedā¢ Docker containers share common ancestors, but keep their own filesā¢ `docker run` parameters:ā¢ --rm ā Destroy a container once it exitsā¢ -d ā Run in the background (daemon mode)ā¢ -i ā Run in interactive modeā¢ --name ā Give the container a nameā¢ -p [local port]:[container port] ā Forward the local port to the container port
Sunshine PHP 2017 27
As a Dev Environment
Sunshine PHP 2017 28
Volumes
Sunshine PHP 2017 29
Mounting from the host machine
Sunshine PHP 2017 30
Mounting from the host machine
Sunshine PHP 2017 31
Mounting from the host machine
Sunshine PHP 2017 32
Mounting from the host machine
Sunshine PHP 2017 33
Mounting from the host machine
Sunshine PHP 2017 34
Mounting from the host isnāt perfect
ā¢ The container now has a window into your host machineā¢ Permissions can get screwy if you are modifying in the containerā¢ Most things it creates will be root by default, and you probably arenāt root on
the host machine
ā¢ Host-mounted volumes are not portable at allā¢ OSX and Hyper-V VMs have limited pathings to mountā¢ OSX has poor I/O performance
Sunshine PHP 2017 35
Simulating Networks
Sunshine PHP 2017 36
Networking
ā¢ Docker can create multiple network āpoolsāā¢ Each container gets an IP addressā¢ Containers can be attached to multiple networksā¢ Docker network allow service discovery inside networks
Sunshine PHP 2017 37
Legacy - Docker Links
ā¢ Legacy Links work with `--link`ā¢ Only works on the legacy ābridgeā networkā¢ Doesnāt support service discovery
ā¢ Not worth it to use anymore
Sunshine PHP 2017 38
Docker Networks
ā¢ Discreet IP pool for containersā¢ Containers can be added and removed to the network at whimā¢ Service discovery though ā--network-aliasāā¢ Can be set up to work across hosts
Sunshine PHP 2017 39
Create a network
Sunshine PHP 2017 40
Attach to a network
Sunshine PHP 2017 41
Ping the web container
Sunshine PHP 2017 42
Add another web and kill web1
Sunshine PHP 2017 43
Building Custom Environments
Sunshine PHP 2017 44
Custom Images
ā¢ PHP images are pretty bareā¢ Lots of times need to install extensions
Sunshine PHP 2017 45
Dockerfile
ā¢ Dockerfile is the configuration steps for an imageā¢ Can be created from scratch, or based on another imageā¢ Allows you to add files, create default volumes, ports, etcā¢ Can be used privately or pushed to Docker Hub
Sunshine PHP 2017 46
docker/Dockerfile
FROM php:apache
RUN a2enmod rewrite
Sunshine PHP 2017 47
Build it
docker build -t tag_name ./
ā¢ This runs through the Dockerfile and generates the imageā¢ We can now use the tag name to run the image
Sunshine PHP 2017 48
Build it
docker build -t d4dapp docker/
Sunshine PHP 2017 49
Sunshine PHP 2017 50
Use the new image
docker run -d --name d4dapp \
-v C:\drago\Projects\dockerfordevs-app:/var/www/ \
-p 8080:80
d4dapp
Sunshine PHP 2017 51
Use the new image
Sunshine PHP 2017 52
Containerizing Build Components
Sunshine PHP 2017 53
Running Composer
docker run --rm \
-v c:/Users/drago/.composer:/root/.composer \
-v c:/Users/drago/Projects/workshop:/app \
-v c:/Users/drago/.ssh:/root/.ssh \
composer/composer \
install
Sunshine PHP 2017 54
Run Multi-Tier Applications
Sunshine PHP 2017 55
What is Docker Compose?
ā¢ Multi-container orchestrationā¢ A single config file holds all of your container infoā¢ Works with Docker Swarm and a few other tools, like Rancher
Sunshine PHP 2017 56
Sample docker-compose.ymlversion: '2'
volumes: mysqldata: driver: local
services: d4dapp: build: ./docker/ volumes: - ./:/var/www/ ports: - 8080:80
mysqlserver: image: mysql environment: MYSQL_DATABASE: dockerfordevs MYSQL_ROOT_PASSWORD: 's3curep@assword' volumes: - mysqldata:/var/lib/mysql
Sunshine PHP 2017 57
No longer use docker run
$ docker rm āf d4dapp
$ docker-compose up -d
Sunshine PHP 2017 58
Now we have 2 containers
Sunshine PHP 2017 59
Other Tools
Sunshine PHP 2017 60
Docker Ecosystem
ā¢ Docker Machineā¢ Docker Swarm
Sunshine PHP 2017 61
Thank You!
ā¢ Software Engineer for InQuestā¢ Author of āDocker for Developersāā¢ https://leanpub.com/dockerfordevs
ā¢ Co-Host of āJerks Talk Gamesāā¢ http://jerkstalkgames
ā¢ http://ctankersley.comā¢ [email protected]ā¢ @dragonmantank
Sunshine PHP 2017 62