Thanks to tools like vagrant, puppet/chef, and Platform as a Service services like Heroku, developers are extremely used to being able to spin up a development environment that is the same every time. What if we could go a step further and make sure our development environment is not only using the same software, but 100% configured and set up like production. Docker will let us do that, and so much more. We'll look at what Docker is, why you should look into using it, and all of the features that developers can take advantage of.
1. Dockerize All The Things!
Chris Tankersley
@dragonmantank
SunshinePHP 2015
SunshinePHP 2015 1
2. Who Am I
โข PHP Programmer for over 10 years
โข Sysadmin/DevOps for around 8 years
โข Using Linux for more than 15 years
โข https://github.com/dragonmantank
SunshinePHP 2015 2
4. 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.โ
SunshinePHP 2015 4
https://www.docker.com/whatisdocker/
5. What is it from a technical standpoint?
โข Docker is a wrapper around Containers
โข Docker Engine is the packaging portion that builds and runs the
containers
โข Docker Hub allows you to publish images for others to use
โข Docker Machine is a bare-metal provisioning tool
โข Docker Swarm is an load-balancing deployment tool
โข Docker Compose is a multi-container build system
SunshinePHP 2015 5
10. Docker can use many different containers
โข Since 0.9.0 it supports:
โข LXC (Linux Containers) โ Started with LXC when it was released
โข OpenVZ
โข Systemd-nspawn
โข libvert-sandbox
โข Qemu/kvm
โข BSD Jails
โข Solaris Zones
โข chroot
SunshinePHP 2015 10
11. Still regulated to Linux, BSD, and Solaris
โข No native container drivers for OSX or Windows, as they donโt have
their own container architecture
โข Microsoft is helping with working on a Hyper-V container driver
though
โข I donโt think there is anything native planned for OSX
SunshinePHP 2015 11
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
SunshinePHP 2015 13
16. 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
SunshinePHP 2015 16
18. Modifying a running container
โข `docker exec` can run a command inside of an existing container
โข Use Volumes to share data
SunshinePHP 2015 18
19. Persistent Data with Volumes
โข You can designate a volume with -v
โข Volumes can be shared amongst containers
โข Volumes can mount data from the host system
SunshinePHP 2015 19
21. 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
SunshinePHP 2015 21
22. Container Data Volumes
โข Uses a small container that does nothing but stores data
โข Have our app containers use the data volume to store data
โข Use โeditor containersโ to go in and modify data when needed
SunshinePHP 2015 22
24. Why not run SSH inside of the container?
โข Well, you canโฆ
โข Docker is designed for one command per container
โข If you need to modify data, then you need to change your setup
โข If you have to run SSH, then you need a way to run SSH and your
command
SunshinePHP 2015 24
25. Why go through the hassle?
โข Data volumes are portable
โข 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
SunshinePHP 2015 25
27. Docker Links
โข Allows containers to โseeโ each other over the network
โข Each container thinks the other one is just another machine
โข Containers all have an internal network address, so we donโt need to
expose everything through the host
SunshinePHP 2015 27
30. More Notes!
โข We can now rebuild sections of the app as needed
โข We can restart nginx without impacting PHP
โข We can extend much easier
โข Linked containers will not update if they are stopped/started
โข If we upgrade PHP, we have to destroy/create the web_server container again
SunshinePHP 2015 30
32. 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
SunshinePHP 2015 32
34. 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
SunshinePHP 2015 34
36. Inspect a container
docker inspect [options] CONTAINER_NAME
โข Returns a JSON string with data about the container
โข Can also query
โข docker inspect -f โ{{ .NetworkSettings.IPAddres }}โ web_server
โข Really handy for scripting out things like reverse proxies
SunshinePHP 2015 36
37. 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
SunshinePHP 2015 37