logo资料库

TheDockerBook官方最新版.pdf

第1页 / 共400页
第2页 / 共400页
第3页 / 共400页
第4页 / 共400页
第5页 / 共400页
第6页 / 共400页
第7页 / 共400页
第8页 / 共400页
资料共400页,剩余部分请下载后查看
Foreword
Who is this book for?
A note about versions
Credits and Acknowledgments
Technical Reviewers
Scott Collier
John Ferlito
Pris Nasrat
Technical Illustrator
Proofreader
Author
Conventions in the book
Code and Examples
Colophon
Errata
Version
Introduction
Introducing Docker
An easy and lightweight way to model reality
A logical segregation of duties
Fast, efficient development life cycle
Encourages service oriented architecture
Docker components
Docker client and server
Docker images
Registries
Containers
Compose, Swarm and Kubernetes
What can you use Docker for?
Docker with configuration management
Docker's technical components
What's in the book?
Docker resources
Installing Docker
Requirements
Installing on Ubuntu and Debian
Checking for prerequisites
Installing Docker
Docker and UFW
Installing on Red Hat and family
Checking for prerequisites
Installing Docker
Starting the Docker daemon on the Red Hat family
Docker for Mac
Installing Docker for Mac
Testing Docker for Mac
Docker for Windows installation
Installing Docker for Windows
Testing Docker for Windows
Using Docker on OSX and Windows with this book
Docker installation script
Binary installation
The Docker daemon
Configuring the Docker daemon
Checking that the Docker daemon is running
Upgrading Docker
Docker user interfaces
Summary
Getting Started with Docker
Ensuring Docker is ready
Running our first container
Working with our first container
Container naming
Starting a stopped container
Attaching to a container
Creating daemonized containers
Seeing what's happening inside our container
Docker log drivers
Inspecting the container's processes
Docker statistics
Running a process inside an already running container
Stopping a daemonized container
Automatic container restarts
Finding out more about our container
Deleting a container
Summary
Working with Docker images and repositories
What is a Docker image?
Listing Docker images
Pulling images
Searching for images
Building our own images
Creating a Docker Hub account
Using Docker commit to create images
Building images with a Dockerfile
Building the image from our Dockerfile
What happens if an instruction fails?
Dockerfiles and the build cache
Using the build cache for templating
Viewing our new image
Launching a container from our new image
Dockerfile instructions
Pushing images to the Docker Hub
Automated Builds
Deleting an image
Running your own Docker registry
Running a registry from a container
Testing the new registry
Alternative Indexes
Quay
Summary
Testing with Docker
Using Docker to test a static website
An initial Dockerfile for the Sample website
Building our Sample website and Nginx image
Building containers from our Sample website and Nginx image
Editing our website
Using Docker to build and test a web application
Building our Sinatra application
Creating our Sinatra container
Extending our Sinatra application to use Redis
Connecting our Sinatra application to the Redis container
Docker internal networking
Docker networking
Connecting containers summary
Using Docker for continuous integration
Build a Jenkins and Docker server
Create a new Jenkins job
Running our Jenkins job
Next steps with our Jenkins job
Summary of our Jenkins setup
Multi-configuration Jenkins
Create a multi-configuration job
Testing our multi-configuration job
Summary of our multi-configuration Jenkins
Other alternatives
Drone
Shippable
Summary
Building services with Docker
Building our first application
The Jekyll base image
Building the Jekyll base image
The Apache image
Building the Jekyll Apache image
Launching our Jekyll site
Updating our Jekyll site
Backing up our Jekyll volume
Extending our Jekyll website example
Building a Java application server with Docker
A WAR file fetcher
Fetching a WAR file
Our Tomcat 7 application server
Running our WAR file
Building on top of our Tomcat application server
A multi-container application stack
The Node.js image
The Redis base image
The Redis primary image
The Redis replica image
Creating our Redis back-end cluster
Creating our Node container
Capturing our application logs
Summary of our Node stack
Managing Docker containers without SSH
Summary
Docker Orchestration and Service Discovery
Docker Compose
Installing Docker Compose
Getting our sample application
The docker-compose.yml file
Running Compose
Using Compose
Compose in summary
Consul, Service Discovery and Docker
Building a Consul image
Testing a Consul container locally
Running a Consul cluster in Docker
Starting the Consul bootstrap node
Starting the remaining nodes
Running a distributed service with Consul in Docker
Docker Swarm
Understanding the Swarm
Installing Swarm
Setting up a Swarm
Running a service on your Swarm
Orchestration alternatives and components
Fleet and etcd
Kubernetes
Apache Mesos
Helios
Centurion
Summary
Using the Docker API
The Docker APIs
First steps with the Engine API
Testing the Docker Engine API
Managing images with the API
Managing containers with the API
Improving the TProv application
Authenticating the Docker Engine API
Create a Certificate Authority
Create a server certificate signing request and key
Configuring the Docker daemon
Creating a client certificate and key
Configuring our Docker client for authentication
Summary
Getting help and extending Docker
Getting help
The Docker forums
Docker on IRC
Docker on GitHub
Reporting issues for Docker
Setting up a build environment
Install Docker
Install source and build tools
Check out the source
Contributing to the documentation
Build the environment
Running the tests
Use Docker inside our development environment
Submitting a pull request
Merge approval and maintainers
Summary
List of Figures
List of Listings
Index
Version: v18.09 (6172afc) 1
The Docker Book James Turnbull December 2, 2018 Version: v18.09 (6172afc) Website: The Docker Book
Some rights reserved. No part of this publication may be reproduced, stored in a mechanical or photocopying, recording, or otherwise, for commercial purposes retrieval system, or transmitted in any form or by any means, electronic, 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
Contents Foreword Who is this book for? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . A note about versions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Credits and Acknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . Technical Reviewers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Scott Collier . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . John Ferlito . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Pris Nasrat . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Technical Illustrator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Proofreader Author . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Conventions in the book . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Code and Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Colophon . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Errata . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Version . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Chapter 1 Introduction Introducing Docker . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . An easy and lightweight way to model reality . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . A logical segregation of duties Fast, efficient development life cycle . . . . . . . . . . . . . . . . . . Encourages service oriented architecture . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Docker client and server . . . . . . . . . . . . . . . . . . . . . . . . . . Docker components Page 1 1 1 1 2 2 3 3 3 4 4 4 5 5 5 5 6 7 8 8 9 9 9 10 i
Contents Docker images . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Registries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Containers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Compose, Swarm and Kubernetes . . . . . . . . . . . . . . . . . . . . What can you use Docker for? . . . . . . . . . . . . . . . . . . . . . . . . . Docker with configuration management . . . . . . . . . . . . . . . . . . . Docker’s technical components . . . . . . . . . . . . . . . . . . . . . . . . . What’s in the book? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Docker resources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Chapter 2 Installing Docker Requirements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Installing on Ubuntu and Debian . . . . . . . . . . . . . . . . . . . . . . . Checking for prerequisites . . . . . . . . . . . . . . . . . . . . . . . . . Installing Docker . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Docker and UFW . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Installing on Red Hat and family . . . . . . . . . . . . . . . . . . . . . . . Checking for prerequisites . . . . . . . . . . . . . . . . . . . . . . . . . Installing Docker . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Starting the Docker daemon on the Red Hat family . . . . . . . . . . Docker for Mac . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Installing Docker for Mac . . . . . . . . . . . . . . . . . . . . . . . . . Testing Docker for Mac . . . . . . . . . . . . . . . . . . . . . . . . . . . Docker for Windows installation . . . . . . . . . . . . . . . . . . . . . . . . Installing Docker for Windows . . . . . . . . . . . . . . . . . . . . . . Testing Docker for Windows . . . . . . . . . . . . . . . . . . . . . . . . Using Docker on OSX and Windows with this book . . . . . . . . . . . . Docker installation script . . . . . . . . . . . . . . . . . . . . . . . . . . . . Binary installation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . The Docker daemon . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Configuring the Docker daemon . . . . . . . . . . . . . . . . . . . . . Checking that the Docker daemon is running . . . . . . . . . . . . . Upgrading Docker . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Docker user interfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Version: v18.09 (6172afc) 12 12 13 14 14 15 16 17 18 19 21 21 22 24 26 27 28 28 31 32 33 34 34 35 36 36 37 38 39 40 42 44 44 45 ii
Chapter 3 Getting Started with Docker Contents Ensuring Docker is ready . . . . . . . . . . . . . . . . . . . . . . . . . . . . Running our first container . . . . . . . . . . . . . . . . . . . . . . . . . . . Working with our first container . . . . . . . . . . . . . . . . . . . . . . . . Container naming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Starting a stopped container . . . . . . . . . . . . . . . . . . . . . . . . . . Attaching to a container . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Creating daemonized containers . . . . . . . . . . . . . . . . . . . . . . . . Seeing what’s happening inside our container . . . . . . . . . . . . . . . Docker log drivers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Inspecting the container’s processes . . . . . . . . . . . . . . . . . . . . . . Docker statistics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Running a process inside an already running container . . . . . . . . . . Stopping a daemonized container . . . . . . . . . . . . . . . . . . . . . . . Automatic container restarts . . . . . . . . . . . . . . . . . . . . . . . . . . Finding out more about our container . . . . . . . . . . . . . . . . . . . . Deleting a container . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46 46 48 51 54 55 56 57 58 60 61 62 63 64 65 66 69 70 71 72 What is a Docker image? . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74 Listing Docker images . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78 Pulling images . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80 Searching for images . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82 Building our own images . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82 Creating a Docker Hub account . . . . . . . . . . . . . . . . . . . . . . 84 Using Docker commit to create images . . . . . . . . . . . . . . . . . 87 Building images with a Dockerfile . . . . . . . . . . . . . . . . . . . . 91 Building the image from our Dockerfile . . . . . . . . . . . . . . . . . 94 What happens if an instruction fails? . . . . . . . . . . . . . . . . . . 96 Dockerfiles and the build cache . . . . . . . . . . . . . . . . . . . . . . 97 Using the build cache for templating . . . . . . . . . . . . . . . . . . . 98 Viewing our new image . . . . . . . . . . . . . . . . . . . . . . . . . . 99 Launching a container from our new image . . . . . . . . . . . . . . Dockerfile instructions . . . . . . . . . . . . . . . . . . . . . . . . . . . 104 Chapter 4 Working with Docker images and repositories Version: v18.09 (6172afc) iii
Contents Chapter 5 Testing with Docker Pushing images to the Docker Hub . . . . . . . . . . . . . . . . . . . . . . 127 Automated Builds . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 130 Deleting an image . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 133 Running your own Docker registry . . . . . . . . . . . . . . . . . . . . . . 135 Running a registry from a container . . . . . . . . . . . . . . . . . . . 136 Testing the new registry . . . . . . . . . . . . . . . . . . . . . . . . . . 136 Alternative Indexes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 138 Quay . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 138 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 138 140 Using Docker to test a static website . . . . . . . . . . . . . . . . . . . . . 141 An initial Dockerfile for the Sample website . . . . . . . . . . . . . . 141 Building our Sample website and Nginx image . . . . . . . . . . . . 145 Building containers from our Sample website and Nginx image . . 147 Editing our website . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 150 Using Docker to build and test a web application . . . . . . . . . . . . . 152 Building our Sinatra application . . . . . . . . . . . . . . . . . . . . . 152 . . . . . . . . . . . . . . . . . . . . . . 154 Creating our Sinatra container Extending our Sinatra application to use Redis . . . . . . . . . . . . 159 Connecting our Sinatra application to the Redis container . . . . . 164 Docker internal networking . . . . . . . . . . . . . . . . . . . . . . . . 165 Docker networking . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 171 Connecting containers summary . . . . . . . . . . . . . . . . . . . . . 182 Using Docker for continuous integration . . . . . . . . . . . . . . . . . . . 183 Build a Jenkins and Docker server . . . . . . . . . . . . . . . . . . . . 184 Create a new Jenkins job . . . . . . . . . . . . . . . . . . . . . . . . . . 190 Running our Jenkins job . . . . . . . . . . . . . . . . . . . . . . . . . . 196 Next steps with our Jenkins job . . . . . . . . . . . . . . . . . . . . . . 198 Summary of our Jenkins setup . . . . . . . . . . . . . . . . . . . . . . 199 Multi-configuration Jenkins . . . . . . . . . . . . . . . . . . . . . . . . . . . 199 Create a multi-configuration job . . . . . . . . . . . . . . . . . . . . . 199 Testing our multi-configuration job . . . . . . . . . . . . . . . . . . . 204 Summary of our multi-configuration Jenkins . . . . . . . . . . . . . . 206 Other alternatives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 207 Version: v18.09 (6172afc) iv
Contents Chapter 6 Building services with Docker Drone . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 207 Shippable . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 207 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 207 208 Building our first application . . . . . . . . . . . . . . . . . . . . . . . . . . 208 The Jekyll base image . . . . . . . . . . . . . . . . . . . . . . . . . . . 209 Building the Jekyll base image . . . . . . . . . . . . . . . . . . . . . . 210 The Apache image . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 212 Building the Jekyll Apache image . . . . . . . . . . . . . . . . . . . . 214 Launching our Jekyll site . . . . . . . . . . . . . . . . . . . . . . . . . . 215 Updating our Jekyll site . . . . . . . . . . . . . . . . . . . . . . . . . . 218 Backing up our Jekyll volume . . . . . . . . . . . . . . . . . . . . . . . 220 Extending our Jekyll website example . . . . . . . . . . . . . . . . . . 222 Building a Java application server with Docker . . . . . . . . . . . . . . . 222 A WAR file fetcher . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 223 Fetching a WAR file . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 225 Our Tomcat 7 application server . . . . . . . . . . . . . . . . . . . . . 226 Running our WAR file . . . . . . . . . . . . . . . . . . . . . . . . . . . 228 Building on top of our Tomcat application server . . . . . . . . . . . 229 A multi-container application stack . . . . . . . . . . . . . . . . . . . . . . 233 The Node.js image . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 234 The Redis base image . . . . . . . . . . . . . . . . . . . . . . . . . . . . 237 The Redis primary image . . . . . . . . . . . . . . . . . . . . . . . . . . 239 The Redis replica image . . . . . . . . . . . . . . . . . . . . . . . . . . 240 Creating our Redis back-end cluster . . . . . . . . . . . . . . . . . . . 241 Creating our Node container . . . . . . . . . . . . . . . . . . . . . . . . 248 Capturing our application logs . . . . . . . . . . . . . . . . . . . . . . 249 Summary of our Node stack . . . . . . . . . . . . . . . . . . . . . . . . 253 Managing Docker containers without SSH . . . . . . . . . . . . . . . . . . 254 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 255 256 Docker Compose . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 257 Installing Docker Compose . . . . . . . . . . . . . . . . . . . . . . . . . 258 Chapter 7 Docker Orchestration and Service Discovery Version: v18.09 (6172afc) v
分享到:
收藏