This is a Leanpub book. Leanpub empowers authors and publishers with the Lean Publishing process. Lean Publishing is the act of publishing an in-progress ebook using lightweight tools and many iterations to get reader feedback, pivot until you have the right book and build traction once you do.
To my wonderful wife Giana, who puts up with all the stuff I do, and my two boys who put up with all of my travel.
Thank you to the people that read this book early and provided valuable feedback, including Ed Finkler, Beau Simensen, Gary Hockin, and M. Craig Amiano.
Id also like to thank the entire PHP community, for without them I wouldnt be where I am today as a developer.
Preface
This book, for me, is a long time coming. Ive been following containerization for a long time, and got very interested in it in 2013 after seeing a presentation on dotCloud at the first MidwestPHP conference. Id had previously used things like chroots and jails in my career, but containers took most of that a step further.
It wasnt too long after that that the Docker project was announced, in fact it was only about a week later, and was started by the very same company I had just seen a presentation on. I started playing with it right away. It was primitive, but it made building Linux containers much, much easier than working in straight LXC.
About a year ago I started thinking about writing this book. In March 2015 I threw up a basic website just to see what sort of interest there would be and I got a pretty decent response. My only concern was the Docker ecosystem. It was, and still is, very Linux-centric and the tooling was well, it wasnt user friendly outside of Linux.
Over the last year there has been an explosion of software and Sofware-as-a-Services pop out of the woodwork to deal with Docker, and Docker finally announced what I thought were three major tools for working with Docker - being able to provision boxes easily, being able to cluster them, and being able to easily bring up mult-container environments. Yes, there were tools that did this made by third parties, but in this technological age I did not want to be bound to a particular vendor, and I did not want my readers to be bound by that as well. I wanted everything native.
Today we have almost everything a user needs in Docker, minus a few features that are still being worked on. I feel confident that I do not need to burden my readers with a specific vendor and that you, the reader of this book, can get Docker up and running for what you need.
Ive written plenty of articles and taught plenty of classes, but Im proud for this to be my first book. I hope you enjoy it.
Assumptions
I know, I know. Making assumptions makes well, you know the rest. In any event, Im writing this book for a developer who is familiar with PHP and is looking at Docker to either help with deployment issues, or development issues introduced through manually managing servers or keeping development environments in line. Maybe you are not happy with using a full virtualization stack like Vagrant, or are having issues with maintaining virtual machines across a wide variety of developers. This book will help you learn Docker, and possibly how it can help you deploy applications and make a better development environment for everyone.
This book will not teach you PHP. In fact, this book really doesnt care what kind of application you are building. I am only using PHP as an example since it is a well known and understood programming langauge that very heavily leans toward a multi-tiered application. Many modern PHP applications have a web tier, such as nginx, an application tier (PHP), and a database tier like MySQL. These types of applications are well suited for what we will be doing.
This book should work for any type of application that you want to set up in Docker, be it in Ruby, Python, or Go. This book focuses on full stack applications but will easily work with contained applications, such as C programs that are meant to be run from the command line. The ideas are all the same.
At the very least, read through this book if you want to know how to use Docker.
Style Conventions
Throughout the book there will be many times where a command is given as an example. Each command will have a $
preceeding the command, the command itself, and then possibly a result of the command output. These sections will be in a monospace
font, and look similiar to the following:
1
$
docker
-
v
2
Docker
version
1.9
.
1
,
build
a34a1d5
docker -v
is the command to run, so if you copy and paste the example into your own terminal make sure to leave off the preceding $
. The second line is the output of the command. Some commands may have more lines of output, some none at all or have been ommited for brevity.
Many commands will be too long to properly fit on a single line on the book page and will be broken up using the \
character. You can still copy and paste these commands into most terminals as they will properly process the \
character. If you are manually typing these commands, feel free to leave out the \
character and put the entire command on one like. For example:
1
$
docker
run
\
2
--
rm
-
ti
ubuntu
\
3
bash
is equivalent to running:
1
$
docker
run
--
rm
-
ti
ubuntu
bash
Containers
The development world goes through many different changes, just like technology in general. New ideas and concepts are brought out from various different places and we integrate them into our workflow. Some of these things are flashes in the pan, and other help revolutionize how we work and make our development lives easier.
About eight years ago virtualization started taking hold in the server room, and that eventually led to the need to run virtual machines locally. It was expensive and slow, and eventually technology caught up. Once virtual machines became disposable, we needed a way to maintain them, and we ended up with Vagrant.
Vagrant, like many things developers use, is a wrapper around an existing technology. Vagrant makes it easier to download, create, maintain, and destroy virtual machines, but its not virtualization itself. Thats handled by mature programs like Virtualbox or VMWare Workstation/Fusion. Vagrant puts a single interface on those technologies and allows us to share environments.
This is all well and good, but virtualization takes a heavy toll. Whenever we boot up a virtual machine, we are turning on a second computer (or third or fourth) inside our existing computer. Each virtual machine needs enough RAM and CPU to run that full little machine, and it takes it from your host machine. See Figure 1-1.