Table Of ContentVersion: v18.09.2(b71a7e7) 1
The Docker Book
James Turnbull
March 12, 2019
Version: v18.09.2 (b71a7e7)
Website: The Docker Book
Some rights reserved. No part of this publication may be reproduced, stored in a
retrieval system, or transmitted in any form or by any means, electronic,
mechanical or photocopying, recording, or otherwise, for commercial purposes
without the prior permission of the publisher.
This work is licensed under the Creative Commons
Attribution-NonCommercial-NoDerivs 3.0 Unported License. To view a copy of
this license, visit here.
© Copyright 2015 - James Turnbull <[email protected]>
Contents
Page
Foreword 1
Who is this book for? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
A note about versions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
Credits and Acknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . 1
Technical Reviewers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
Scott Collier . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
John Ferlito . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
Pris Nasrat . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
Technical Illustrator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
Proofreader . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
Author . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
Conventions in the book . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
Code and Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
Colophon . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
Errata . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
Version . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
Chapter 1 Introduction 6
Introducing Docker . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
An easy and lightweight way to model reality . . . . . . . . . . . . . 8
A logical segregation of duties . . . . . . . . . . . . . . . . . . . . . . 8
Fast, efficient development life cycle . . . . . . . . . . . . . . . . . . 9
Encourages service oriented architecture . . . . . . . . . . . . . . . . 9
Docker components . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
Docker client and server . . . . . . . . . . . . . . . . . . . . . . . . . . 10
i
Contents
Docker images . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
Registries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
Containers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
Compose, Swarm and Kubernetes . . . . . . . . . . . . . . . . . . . . 14
What can you use Docker for? . . . . . . . . . . . . . . . . . . . . . . . . . 14
Docker with configuration management . . . . . . . . . . . . . . . . . . . 15
Docker’s technical components . . . . . . . . . . . . . . . . . . . . . . . . . 16
What’s in the book? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
Docker resources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
Chapter 2 Installing Docker 19
Requirements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
Installing on Ubuntu and Debian . . . . . . . . . . . . . . . . . . . . . . . 21
Checking for prerequisites . . . . . . . . . . . . . . . . . . . . . . . . . 22
Installing Docker . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
Docker and UFW . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
Installing on Red Hat and family . . . . . . . . . . . . . . . . . . . . . . . 27
Checking for prerequisites . . . . . . . . . . . . . . . . . . . . . . . . . 28
Installing Docker . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
Starting the Docker daemon on the Red Hat family . . . . . . . . . . 31
Docker for Mac . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
Installing Docker for Mac . . . . . . . . . . . . . . . . . . . . . . . . . 33
Testing Docker for Mac . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
Docker for Windows installation . . . . . . . . . . . . . . . . . . . . . . . . 34
Installing Docker for Windows . . . . . . . . . . . . . . . . . . . . . . 35
Testing Docker for Windows . . . . . . . . . . . . . . . . . . . . . . . . 36
Using Docker on OSX and Windows with this book . . . . . . . . . . . . 36
Docker installation script . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
Binary installation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38
The Docker daemon . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39
Configuring the Docker daemon . . . . . . . . . . . . . . . . . . . . . 40
Checking that the Docker daemon is running . . . . . . . . . . . . . 42
Upgrading Docker . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44
Docker user interfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
Version: v18.09.2(b71a7e7) ii
Contents
Chapter 3 Getting Started with Docker 46
Ensuring Docker is ready . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46
Running our first container . . . . . . . . . . . . . . . . . . . . . . . . . . . 48
Working with our first container . . . . . . . . . . . . . . . . . . . . . . . . 51
Container naming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54
Starting a stopped container . . . . . . . . . . . . . . . . . . . . . . . . . . 55
Attaching to a container . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56
Creating daemonized containers . . . . . . . . . . . . . . . . . . . . . . . . 57
Seeing what’s happening inside our container . . . . . . . . . . . . . . . 58
Docker log drivers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60
Inspecting the container’s processes . . . . . . . . . . . . . . . . . . . . . . 61
Docker statistics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62
Running a process inside an already running container . . . . . . . . . . 63
Stopping a daemonized container . . . . . . . . . . . . . . . . . . . . . . . 64
Automatic container restarts . . . . . . . . . . . . . . . . . . . . . . . . . . 65
Finding out more about our container . . . . . . . . . . . . . . . . . . . . 66
Deleting a container . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70
Chapter 4 Working with Docker images and repositories 71
What is a Docker image? . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72
Listing Docker images . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74
Pulling images . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78
Searching for images . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80
Building our own images . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82
Creating a Docker Hub account . . . . . . . . . . . . . . . . . . . . . . 82
Using Docker commit to create images . . . . . . . . . . . . . . . . . 84
Building images with a Dockerfile . . . . . . . . . . . . . . . . . . . . 87
Building the image from our Dockerfile . . . . . . . . . . . . . . . . . 91
What happens if an instruction fails? . . . . . . . . . . . . . . . . . . 93
Dockerfiles and the build cache . . . . . . . . . . . . . . . . . . . . . . 95
Using the build cache for templating. . . . . . . . . . . . . . . . . . . 96
Viewing our new image . . . . . . . . . . . . . . . . . . . . . . . . . . 97
Launching a container from our new image . . . . . . . . . . . . . . 98
Dockerfile instructions . . . . . . . . . . . . . . . . . . . . . . . . . . . 103
Version: v18.09.2(b71a7e7) iii
Contents
Pushing images to the Docker Hub . . . . . . . . . . . . . . . . . . . . . . 126
Automated Builds . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129
Deleting an image . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 132
Running your own Docker registry . . . . . . . . . . . . . . . . . . . . . . 134
Running a registry from a container . . . . . . . . . . . . . . . . . . . 135
Testing the new registry . . . . . . . . . . . . . . . . . . . . . . . . . . 135
Alternative Indexes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 137
Quay . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 137
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 137
Chapter 5 Testing with Docker 139
Using Docker to test a static website . . . . . . . . . . . . . . . . . . . . . 140
An initial Dockerfile for the Sample website . . . . . . . . . . . . . . 140
Building our Sample website and Nginx image . . . . . . . . . . . . 144
Building containers from our Sample website and Nginx image . . 146
Editing our website . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 149
Using Docker to build and test a web application . . . . . . . . . . . . . 151
Building our Sinatra application . . . . . . . . . . . . . . . . . . . . . 151
Creating our Sinatra container . . . . . . . . . . . . . . . . . . . . . . 153
Extending our Sinatra application to use Redis . . . . . . . . . . . . 158
Connecting our Sinatra application to the Redis container . . . . . 163
Docker internal networking . . . . . . . . . . . . . . . . . . . . . . . . 164
Docker networking . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 170
Connecting containers summary . . . . . . . . . . . . . . . . . . . . . 181
Using Docker for continuous integration . . . . . . . . . . . . . . . . . . . 182
Build a Jenkins and Docker server . . . . . . . . . . . . . . . . . . . . 183
Create a new Jenkins job . . . . . . . . . . . . . . . . . . . . . . . . . . 189
Running our Jenkins job . . . . . . . . . . . . . . . . . . . . . . . . . . 195
Next steps with our Jenkins job . . . . . . . . . . . . . . . . . . . . . . 197
Summary of our Jenkins setup . . . . . . . . . . . . . . . . . . . . . . 198
Multi-configuration Jenkins. . . . . . . . . . . . . . . . . . . . . . . . . . . 198
Create a multi-configuration job . . . . . . . . . . . . . . . . . . . . . 198
Testing our multi-configuration job . . . . . . . . . . . . . . . . . . . 203
Summary of our multi-configuration Jenkins . . . . . . . . . . . . . . 205
Other alternatives. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 206
Version: v18.09.2(b71a7e7) iv
Contents
Drone . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 206
Shippable . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 206
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 206
Chapter 6 Building services with Docker 207
Building our first application . . . . . . . . . . . . . . . . . . . . . . . . . . 207
The Jekyll base image . . . . . . . . . . . . . . . . . . . . . . . . . . . 208
Building the Jekyll base image . . . . . . . . . . . . . . . . . . . . . . 209
The Apache image . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 211
Building the Jekyll Apache image . . . . . . . . . . . . . . . . . . . . 213
Launching our Jekyll site . . . . . . . . . . . . . . . . . . . . . . . . . . 214
Updating our Jekyll site . . . . . . . . . . . . . . . . . . . . . . . . . . 217
Backing up our Jekyll volume . . . . . . . . . . . . . . . . . . . . . . . 219
Extending our Jekyll website example . . . . . . . . . . . . . . . . . . 221
Building a Java application server with Docker . . . . . . . . . . . . . . . 221
A WAR file fetcher . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 222
Fetching a WAR file . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 224
Our Tomcat 7 application server . . . . . . . . . . . . . . . . . . . . . 225
Running our WAR file . . . . . . . . . . . . . . . . . . . . . . . . . . . 227
Building on top of our Tomcat application server . . . . . . . . . . . 228
A multi-container application stack . . . . . . . . . . . . . . . . . . . . . . 232
The Node.js image . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 233
The Redis base image . . . . . . . . . . . . . . . . . . . . . . . . . . . . 236
The Redis primary image . . . . . . . . . . . . . . . . . . . . . . . . . . 238
The Redis replica image . . . . . . . . . . . . . . . . . . . . . . . . . . 239
Creating our Redis back-end cluster . . . . . . . . . . . . . . . . . . . 240
Creating our Node container. . . . . . . . . . . . . . . . . . . . . . . . 246
Capturing our application logs . . . . . . . . . . . . . . . . . . . . . . 247
Summary of our Node stack . . . . . . . . . . . . . . . . . . . . . . . . 251
Managing Docker containers without SSH . . . . . . . . . . . . . . . . . . 252
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 253
Chapter 7 Docker Orchestration and Service Discovery 254
Docker Compose . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 255
Installing Docker Compose . . . . . . . . . . . . . . . . . . . . . . . . . 256
Version: v18.09.2(b71a7e7) v
Contents
Getting our sample application . . . . . . . . . . . . . . . . . . . . . . 257
The docker-compose.yml file . . . . . . . . . . . . . . . . . . . . . . . 261
Running Compose . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 264
Using Compose . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 266
Compose in summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . 270
Consul, Service Discovery and Docker . . . . . . . . . . . . . . . . . . . . 270
Building a Consul image . . . . . . . . . . . . . . . . . . . . . . . . . . 272
Testing a Consul container locally . . . . . . . . . . . . . . . . . . . . 276
Running a Consul cluster in Docker . . . . . . . . . . . . . . . . . . . 278
Starting the Consul bootstrap node . . . . . . . . . . . . . . . . . . . . 281
Starting the remaining nodes . . . . . . . . . . . . . . . . . . . . . . . 284
Running a distributed service with Consul in Docker . . . . . . . . . 292
Docker Swarm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 305
Understanding the Swarm . . . . . . . . . . . . . . . . . . . . . . . . . 306
Installing Swarm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 307
Setting up a Swarm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 307
Running a service on your Swarm . . . . . . . . . . . . . . . . . . . . 312
Orchestration alternatives and components . . . . . . . . . . . . . . . . . 317
Fleet and etcd . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 318
Kubernetes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 318
Apache Mesos . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 318
Helios . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 318
Centurion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 319
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 319
Chapter 8 Using the Docker API 320
The Docker APIs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 320
First steps with the Engine API . . . . . . . . . . . . . . . . . . . . . . . . . 321
Testing the Docker Engine API . . . . . . . . . . . . . . . . . . . . . . . . . 325
Managing images with the API . . . . . . . . . . . . . . . . . . . . . . 326
Managing containers with the API . . . . . . . . . . . . . . . . . . . . 328
Improving the TProv application . . . . . . . . . . . . . . . . . . . . . . . 333
Authenticating the Docker Engine API . . . . . . . . . . . . . . . . . . . . 338
Create a Certificate Authority . . . . . . . . . . . . . . . . . . . . . . . 339
Create a server certificate signing request and key . . . . . . . . . . 341
Version: v18.09.2(b71a7e7) vi
Contents
Configuring the Docker daemon . . . . . . . . . . . . . . . . . . . . . 345
Creating a client certificate and key . . . . . . . . . . . . . . . . . . . 346
Configuring our Docker client for authentication . . . . . . . . . . . 349
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 351
Chapter 9 Getting help and extending Docker 352
Getting help . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 353
The Docker forums . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 353
Docker on IRC . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 353
Docker on GitHub . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 353
Reporting issues for Docker . . . . . . . . . . . . . . . . . . . . . . . . . . . 354
Setting up a build environment . . . . . . . . . . . . . . . . . . . . . . . . 354
Install Docker . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 355
Install source and build tools . . . . . . . . . . . . . . . . . . . . . . . 355
Check out the source . . . . . . . . . . . . . . . . . . . . . . . . . . . . 355
Contributing to the documentation . . . . . . . . . . . . . . . . . . . . 356
Build the environment . . . . . . . . . . . . . . . . . . . . . . . . . . . 356
Running the tests . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 359
Use Docker inside our development environment . . . . . . . . . . . 361
Submitting a pull request. . . . . . . . . . . . . . . . . . . . . . . . . . 361
Merge approval and maintainers . . . . . . . . . . . . . . . . . . . . . 363
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 364
List of Figures 366
List of Listings 380
Index 381
Version: v18.09.2(b71a7e7) vii