docker for developers - sunshine php
TRANSCRIPT
Sunshine PHP 2017 1
Docker for PHP DevelopersChris Tankersley@dragonmantankSunshine PHP 2017
Sunshine PHP 2017 2
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.”
https://www.docker.com/whatisdocker/
Sunshine PHP 2017 3
Containers
Sunshine PHP 2017 4
Normal Bare-Metal Server
CPU RAM HD Network
Operating System
nginx PHP DB
Sunshine PHP 2017 5
Virtual Machines
CPU RAM HD Network
Operating System
nginx PHP DB
Operating System
nginx PHP DB
Operating System
Hypervisor
Sunshine PHP 2017 6
Containers
CPU RAM HD Network
Operating System
nginxnginx PHP DB PHP DB
Sunshine PHP 2017 7
Containers Are Not New• LXC (Linux Containers)• OpenVZ• Systemd-nspawn• Qemu/kvm• BSD Jails• Solaris Zones• chroot
Sunshine PHP 2017 8
Docker is an Ecosystem
Docker Engine
Sunshine PHP 2017 9
Docker is an Ecosystem
Docker ComposeDocker Machine Docker Swarm
Sunshine PHP 2017 10
How does it work?
Uses a variety of existingContainer technologies
Server ContainersHyper-V Containers xhyve Virtualization
Sunshine PHP 2017 11
Sorry OSX < 10.10 and Windows < 10 Users
Docker Toolbox
Sunshine PHP 2017 12
Let’s use Docker
Sunshine PHP 2017 13
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 14
Running a simple shell
Sunshine PHP 2017 15
Running a simple shell
Sunshine PHP 2017 16
Running a simple shell
Sunshine PHP 2017 17
What’s Going On?
Ubuntu Kernel
/+ bin/+ etc/+ dev/+ home/+ usr/+ var/+ lib/+ …
nginx
bash
/+ bin/+ etc/+ dev/+ home/+ usr/+ var/+ lib/+ …
php
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
Volumes
Sunshine PHP 2017 28
Modifying a running container• `docker exec` can run a command inside of an existing container• Use Volumes to share data
Sunshine PHP 2017 29
Persistent Data with Volumes• You can designate a volume with –v• Create a named volume with `volume create`• Volumes can be shared amongst containers• Volumes can mount data from the host system
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 machine
Sunshine PHP 2017 35
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 36
Named Data Volumes• Creates a space that becomes persistent• Can be mounted anywhere inside your images• Have our app containers use the data volume to store data• Use ‘editor containers’ to go in and modify data when needed
Sunshine PHP 2017 37
vim Tutorial• vim is a Modal text editor• ESC will drop you back to default mode• :new /opt/webconfig/default to create a new file• In default mode, i will get us into interactive (edit) mode• :w to save a file• :q will quit
Sunshine PHP 2017 38
Mounting Data Volumes
Sunshine PHP 2017 39
Mounting Data Volumes
Sunshine PHP 2017 40
Mounting Data Volumes
Sunshine PHP 2017 41
Mounting Data Volumes
Sunshine PHP 2017 42
Mounting Data Volumes
Sunshine PHP 2017 43
Mounting Data Volumes
Sunshine PHP 2017 44
Why go through the hassle?• Data volumes are portable, depending on the driver• Data volumes are safer• Separates the app containers from data• Production can use a data volume, dev can use a host volume
• Our app containers stay small• Works directly with other tools
Sunshine PHP 2017 45
Networking
Sunshine PHP 2017 46
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 47
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 48
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 49
Create a network
Sunshine PHP 2017 50
Attach to a network
Sunshine PHP 2017 51
Ping the web container
Sunshine PHP 2017 52
Add another web and kill web1
Sunshine PHP 2017 53
BREAK TIME! WOO!
Sunshine PHP 2017 54
Other Helpful Commands
Sunshine PHP 2017 55
Inspect a containerdocker inspect [options] CONTAINER_NAME
• Returns a JSON string with data about the container• Can also query• docker inspect -f “{{ .NetworkSettings.IPAddress }}” web_server
• Really handy for scripting out things like reverse proxies
Sunshine PHP 2017 56
Work with images• docker pull IMAGE – Pulls down an image before using• docker images – Lists all the images that are downloaded• docker rmi IMAGE – Deletes an image if it’s not being used
Sunshine PHP 2017 57
Containerizing An Application
Sunshine PHP 2017 58
Our Goals• Not change our workflow (much)• Run PHP 7, Unit Tests, and webserver• Deploy “easily”
Sunshine PHP 2017 59
Just try and run itdocker run -d --name d4dapp \ -v C:\drago\Projects\dockerfordevs-app:/var/www/ \ -p 8080:80 php:apache
Sunshine PHP 2017 60
Sunshine PHP 2017 61
Checking Logs• Containers log to stdout/stderr• Docker aggregates the logs• Can be viewed with docker logs
Sunshine PHP 2017 62
Oops
Sunshine PHP 2017 63
Custom Images• PHP images are pretty bare• Lots of times need to install extensions
Sunshine PHP 2017 64
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 65
docker/DockerfileFROM php:apache
RUN a2enmod rewrite
Sunshine PHP 2017 66
Build itdocker 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 67
Build itdocker build -t d4dapp docker/
Sunshine PHP 2017 68
Sunshine PHP 2017 69
Use the new imagedocker run -d --name d4dapp \ -v C:\drago\Projects\dockerfordevs-app:/var/www/ \ -p 8080:80 d4dapp
Sunshine PHP 2017 70
Use the new image
Sunshine PHP 2017 71
Slightly better
Sunshine PHP 2017 72
Install Dependencies
Sunshine PHP 2017 73
Running Composerdocker 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 74
Better!
Sunshine PHP 2017 75
Look at queues!
Sunshine PHP 2017 76
docker/DockerfileFROM php:apache
RUN a2enmod rewrite\ && docker-php-ext-install pdo_mysql
Sunshine PHP 2017 77
Rebuild itdocker build -t d4dapp docker/
Sunshine PHP 2017 78
Rebuild the imagedocker build -t d4dapp docker/
Sunshine PHP 2017 79
Rebuild the container$ docker rm -f d4dapp$ docker run -d --name d4dapp \ -v C:\drago\Projects\dockerfordevs-app:/var/www/ \ -p 8080:80 d4dapp
Sunshine PHP 2017 80
Progress!
Sunshine PHP 2017 81
Docker Compose
Sunshine PHP 2017 82
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 83
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 84
No longer use docker run$ docker rm –f d4dapp$ docker-compose up -d
Sunshine PHP 2017 85
Now we have 2 containers
Sunshine PHP 2017 86
Config for DB now points to the service name<?php
return [ 'debug' => true,
'config_cache_enabled' => false,
'db' => [ 'driver' => 'Pdo_Mysql', 'hostname' => 'mysqlserver', 'port' => '3306', 'database' => 'dockerfordevs', 'user' => 'root', 'password' => 's3curep@assword', ],];
Sunshine PHP 2017 87
Yay!
Sunshine PHP 2017 88
Install our DB Migration Softwaredocker 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 \ require robmorgan/phinx
Sunshine PHP 2017 89
Set up phinxdocker run --rm \ -v C:\Users\drago\Projects\dockerfordevs-app\:/app \ -w /app \ php:cli php vendor/bin/phinx init
Sunshine PHP 2017 90
Run the migrationdocker run --rm \ -v C:\Users\drago\Projects\dockerfordevs-app\:/app \ -w /app \ --network dockerfordevsapp_default \ php:cli php vendor/bin/phinx migrate
Sunshine PHP 2017 91
Oops
Sunshine PHP 2017 92
Let’s use the existing containerdocker run --rm \ -v C:\Users\drago\Projects\dockerfordevs-app\:/app \ -w /app \ --network dockerfordevsapp_default \ dockerfordevsapp_d4dapp php vendor/bin/phinx migrate
Sunshine PHP 2017 93
Good…
Sunshine PHP 2017 94
It Lives!
Sunshine PHP 2017 95
Other Tools
Sunshine PHP 2017 96
Docker Ecosystem• Docker Machine• Docker Swarm
Sunshine PHP 2017 97
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