34,79 €
Docker is an open source tool used for creating, deploying, and running applications using containers. With more than 100 self-contained tutorials, this book examines common pain points and best practices for developers building distributed applications with Docker. Each recipe in this book addresses a specific problem and offers a proven, best practice solution with insights into how it works, so that you can modify the code and configuration files to suit your needs.
The Docker Cookbook begins by guiding you in setting up Docker in different environments and explains how to work with its containers and images. You’ll understand Docker orchestration, networking, security, and hosting platforms for effective collaboration and efficient deployment. The book also covers tips and tricks and new Docker features that support a range of other cloud offerings.
By the end of this book, you’ll be able to package and deploy end-to-end distributed applications with Docker and be well-versed with best practice solutions for common development problems.
Das E-Book können Sie in Legimi-Apps oder einer beliebigen App lesen, die das folgende Format unterstützen:
Seitenzahl: 263
Veröffentlichungsjahr: 2018
Copyright © 2018 Packt Publishing
All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews.
Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the author(s), nor Packt Publishing or its dealers and distributors, will be held liable for any damages caused or alleged to have been caused directly or indirectly by this book.
Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information.
Commissioning Editor: Gebin GeorgeAcquisition Editor: Shrilekha InaniContent Development Editor: Nithin George VargheseTechnical Editor:Prashant ChaudhariCopy Editor:Safis EditingProject Coordinator: Drashti PanchalProofreader: Safis EditingIndexer: Priyanka DhadkeGraphics: Tom ScariaProduction Coordinator: Aparna Bhagat
First published: June 2015 Second edition: August 2018
Production reference: 1300818
Published by Packt Publishing Ltd. Livery Place 35 Livery Street Birmingham B3 2PB, UK.
ISBN 978-1-78862-686-6
www.packtpub.com
Mapt is an online digital library that gives you full access to over 5,000 books and videos, as well as industry leading tools to help you plan your personal development and advance your career. For more information, please visit our website.
Spend less time learning and more time coding with practical eBooks and Videos from over 4,000 industry professionals
Improve your learning with Skill Plans built especially for you
Get a free eBook or video every month
Mapt is fully searchable
Copy and paste, print, and bookmark content
Did you know that Packt offers eBook versions of every book published, with PDF and ePub files available? You can upgrade to the eBook version at www.PacktPub.com and as a print book customer, you are entitled to a discount on the eBook copy. Get in touch with us at [email protected] for more details.
At www.PacktPub.com, you can also read a collection of free technical articles, sign up for a range of free newsletters, and receive exclusive discounts and offers on Packt books and eBooks.
Ken Cochrane is an IT professional with over 15 years of experience working with organizations to build large-scale applications and platforms. Ken was a founding team member at Docker, where he was the lead developer for the Docker Hub, and Docker for AWS products. As an early team member, he worked on everything from the Docker Engine to documentation. Ken also helped create Docker meetup groups in New York, Boston, and Portland Maine. Ken currently works for WEX, a global payments company, where he leads a team and helps modernize their technology stack by moving to the cloud and automating their development pipeline using containers. He currently resides in Southern Maine with his wife, Emily, and two sons, Zander and Maddox. His Twitter handle is @kencochrane.
Jeeva S. Chelladhurai has been working as a DevOps specialist at the IBM Global Cloud Center of Excellence (CoE) in India for the past 8 years. He has more than 20 years of experience in the IT industry. In various capacities, he has technically managed and mentored diverse teams across the globe in envisaging and building pioneering telecommunication products. He specializes in DevOps and cloud solution delivery, with a focus on data center optimization, software-defined environments (SDEs), and distributed application development, deployment, and delivery using the newest Docker technology. Jeeva is also a strong proponent of agile methodologies, DevOps, and IT automation. He holds a master's degree in computer science from Manonmaniam Sundaranar University and a graduate certificate in project management from Boston University. He has been instrumental in crafting reusable assets for IBM solution architects and consultants in Docker-inspired containerization technology.
Neependra K Khare is the founder and principal consultant at CloudYuga. CloudYuga provides training and consulting on Docker, Kubernetes, GO Programming, and so on. He is one of the Docker Captains and has run Docker Meetup Group in Bangalore for around 4 years. In 2015, he authored a book on Docker, Docker Cookbook. In 2016, he coauthored a course on Cloud Infrastructure Technologies at Edx for the Linux Foundation. Recently, he authored a course on Kubernetes on Edx, for the Linux Foundation.
Fabrizio Soppelsa works as a software engineer for Samsung Electronics, in the cloud computing department. He's also a Docker contributor and Docker community leader. He has extensive hands-on experience with open cloud infrastructures and container workloads in production, run by some of the most iconic companies in the world. He shares his life with Anna in Moscow, Russia.
He's the author of Docker Clustering with Swarm, edited by Packt Publishing (2017).
Vishnu Gopal is an engineer with strong product and user experience skills, with experience in product development, web development, and engineering management. He was part of the initial team that built SlideShare Inc., which then went on to be acquired by LinkedIn. He's been working in the web and mobile development field for over 10 years. He is currently the CTO of SV.CO, a product accelerator for students based in India. He lives in Kochi, India.
If you're interested in becoming an author for Packt, please visit authors.packtpub.com and apply today. We have worked with thousands of developers and tech professionals, just like you, to help them share their insight with the global tech community. You can make a general application, apply for a specific hot topic that we are recruiting an author for, or submit your own idea.
Title Page
Copyright and Credits
Docker Cookbook Second Edition
Dedication
Packt Upsell
Why subscribe?
PacktPub.com
Contributors
About the authors
About the reviewers
Packt is searching for authors like you
Preface
Who this book is for
What this book covers
To get the most out of this book
Download the example code files
Conventions used
Sections
Getting ready
How to do it...
How it works...
There's more...
See also
Get in touch
Reviews
Introduction and Installation
Introduction
Namespaces
The PID namespace
The net namespace
The IPC namespace
The mnt namespace
The UTS namespace
The user namespace
Cgroups
The union filesystem
The container format
Verifying requirements for Docker installation
Getting ready
How to do it...
How it works...
See also
Installing Docker on Ubuntu
Getting ready
How to do it...
How it works...
There's more...
See also
Installing Docker on CentOS
Getting ready
How to do it...
How it works...
There's more...
See also
Installing Docker on Linux with an automated script
Getting ready
How to do it
How it works...
There's more...
Installing Docker for Windows
Getting ready
How to do it
How it works...
There's more
See also
Installing Docker for Mac
Getting ready
How to do it
How it works...
There's more...
See also
Pulling an image and running a container
Getting ready
How to do it...
How it works...
There's more...
See also
Adding a nonroot user to administer Docker
Getting ready
How to do it...
How it works...
Finding help with the Docker command line
Getting ready
How to do it...
How it works...
See also
Working with Docker Containers
Introduction
Listing/searching for an image
Getting ready
How to do it...
How it works...
There's more...
See also
Pulling an image
Getting ready
How to do it...
How it works...
There's more...
See also
Listing images
Getting ready
How to do it...
How it works...
There's more...
See also
Starting a container
Getting ready
How to do it...
How it works...
There's more...
See also
Listing containers
Getting ready
How to do it...
How it works...
There's more...
See also
Looking at the container logs
Getting ready
How to do it...
How it works...
There's more...
See also
Stopping a container
Getting ready
How to do it...
How it works...
There's more...
See also
Removing a container
Getting ready
How to do it...
There's more...
How it works...
See also
Removing all stopped containers
Getting ready
How to do it...
There's more...
How it works...
See also
Setting the restart policy on a container
Getting ready
How to do it...
There's more...
See also
Getting privileged access inside a container
Getting ready
How to do it...
How it works...
There's more...
See also
Accessing the host device inside a container
Getting ready
How to do it...
How it works...
See also
Injecting a new process into a running container
Getting ready
How to do it...
How it works...
See also
Reading a container's metadata
Getting ready
How to do it...
How it works...
There's more...
See also
Labeling and filtering containers
Getting ready
How to do it...
How it works...
There's more...
See also
Reaping a zombie inside a container
Getting ready
How to do it...
There's more...
How it works...
See also
Working with Docker Images
Introduction
Creating an image from the container
Getting ready
How to do it...
How it works...
There's more...
See also
Creating an account with Docker Hub
Getting ready
How to do it...
How it works...
See also
Logging in and out of the Docker image registry
Getting ready
How to do it...
How it works...
There's more...
See also
Publishing an image to the registry
Getting ready
How to do it...
How it works...
There's more...
See also
Looking at the history of an image
Getting ready
How to do it...
How it works...
There's more...
See also
Removing an image
Getting ready
How to do it...
There's more...
See also
Exporting an image
Getting ready
How to do it...
There's more...
See also
Importing an image
Getting ready
How to do it...
There's more...
See also
Building an image using a Dockerfile
Getting ready
How to do it...
How it works...
There's more...
See also
Building an Apache image – a Dockerfile example
Getting ready
How to do it...
How it works...
There's more...
See also
Setting up a private index/registry
Getting ready
How to do it...
How it works...
There's more...
See also
Automated builds – with GitHub and Bitbucket
Getting ready
How to do it...
How it works...
There's more...
See also
Creating a custom base image
Getting ready
How to do it...
How it works...
See also
Creating a minimal image using a scratch base image
Getting ready
How to do it...
How it works...
There's more...
See also
Building images in multiple stages
Getting ready
How to do it...
How it works...
There's more...
See also
Visualizing the image hierarchy
Getting ready
How to do it...
How it works...
There's more...
See also
Network and Data Management for Containers
Introduction
Accessing containers from outside
Getting ready
How to do it...
How it works...
There's more...
See also
Attaching containers to a host network
Getting ready
How to do it...
How it works...
See also
Launching containers with no network
Getting ready
How to do it...
How it works...
See also
Sharing IP addresses with other containers
Getting ready
How to do it...
How it works...
There's more...
See also
Creating a user-defined bridge network
Getting ready
How to do it...
How it works...
There's more...
See also
Discovering and load balancing containers
Getting ready
How to do it...
How it works...
There's more...
See also
Persisting data using volumes
Getting ready
How to do it...
How it works...
There's more...
See also
Sharing data between the host and the container
Getting ready
How to do it...
How it works...
There's more...
See also
Docker Use Cases
Introduction
Testing with Docker
Getting ready
How to do it...
How it works...
There's more...
Performing CI/CD with Shippable and Heroku
Getting ready
How to do it...
How it works...
See also
Performing CI/CD with TravisCI
Getting ready
How to do it...
How it works...
There's more...
See also
Setting up PaaS with OpenShift origin
Getting ready
How to do it...
How it works...
There's more...
See also
Building and deploying an app on OpenShift from the source code
Getting ready
How to do it...
How it works...
There's more...
See also
Docker APIs and SDKs
Introduction
Working with images using APIs
How to do it...
How it works...
There's more...
See also
Building images using APIs
How to do it...
How it works...
There's more...
See also
Launching containers using APIs
How to do it...
How it works...
There's more...
See also
Performing container operations using APIs
How to do it...
How it works...
See also
Exploring Docker remote API client libraries
Getting ready
How to do it...
How it works...
See also
Configuring the Docker daemon for remote connectivity
How to do it...
How it works...
There's more...
See also
Securing the Docker daemon's remote connectivity
Getting ready
How to do it...
How it works...
There's more...
See also
Docker Performance
Introduction
Benchmarking CPU performance
Getting ready
How to do it...
How it works...
There's more...
See also
Benchmarking disk performance
Getting ready
How to do it...
How it works...
There's more...
See also
Benchmarking network performance
Getting ready
How to do it...
How it works...
There's more...
See also
Getting container resource usage using the stats feature
Getting ready
How to do it...
How it works...
See also
Setting up performance monitoring
Getting ready
How to do it...
How it works...
There's more...
See also
Docker Orchestration and Hosting a Platform
Introduction
Running applications with Docker Compose
Getting ready
How to do it
How it works...
There's more...
See also
Setting up a cluster with Docker Swarm
Getting ready
How to do it
How it works...
There's more...
See also
Using secrets with Docker Swarm
Getting ready
How to do it...
How it works...
There's more...
See also
Setting up a Kubernetes cluster
Getting ready
How to do it...
How it works...
There's more...
See also
Using secrets with Kubernetes
Getting ready
How to do it...
How it works...
There's more...
See also
Scaling up and down in Kubernetes cluster
Getting ready
How to do it...
How it works...
There's more...
Setting up WordPress with Kubernetes clusters
Getting ready
How to do it...
How it works...
There's more...
See also
Docker Security
Introduction
Setting Mandatory Access Control (MAC) with SELinux
Getting ready
How to do it...
How it works...
There's more...
See also
Allowing writes to volumes mounted from the host with SELinux ON
Getting ready
How to do it...
How it works...
See also
Removing capabilities to break down the power of a root user inside a container
Getting ready
How to do it...
How it works...
There's more...
See also
Sharing namespaces between the host and the container
Getting ready
How to do it...
How it works...
Getting Help and Tips and Tricks
Introduction
Starting Docker in debug mode
Getting ready
How to do it...
How it works...
See also
Building a Docker binary from the source
Getting ready
How to do it...
How it works...
There's more...
See also
Building images without using cached layers
Getting ready
How to do it...
How it works...
There's more...
Building your own bridge for container communication
Getting ready
How to do it...
How it works...
There's more...
See also
Changing the default OCI runtime
Getting ready
How to do it...
How it works...
See also
Selecting the logging driver for containers
Getting ready
How to do it
How it works...
There's more...
See also
Getting real-time Docker events for containers
Getting ready...
How to do it...
How it works...
There's more...
See also
Docker on the Cloud
Introduction
Docker for AWS
Getting ready
How to do it...
How it works...
There's more
See also
Deploying WordPress on Docker for AWS
Getting ready
How to do it...
How it works...
There's more...
Docker for Azure
Getting ready
How to do it...
How it works...
There's more...
See also
Deploying Joomla! on Docker for Azure
Getting ready
How to do it...
How it works...
See also
Other Books You May Enjoy
Leave a review - let other readers know what you think
With Docker, containers are becoming mainstream and enterprises are ready to use them in production. This book is specially designed to help you get up to speed with Docker and give you the confidence to use it in production. This book also covers Docker use cases, orchestration, clustering, hosting platforms, security, and performance, which will help you understand the different aspects of production deployment.
With step-by-step instructions to practical and applicable recipes,Docker Cookbookwill not only help you with the current version of Docker (18.06), but with the accompanying text, it will provide you with conceptual information to cope with the minor changes in future versions.
Dockeris a registered trademark of Docker, Inc.
This book targets developers, system administrators, and DevOps engineers who want to use Docker in their development, QA, or production environments.
Chapter 1, Introduction and Installation, compares containers with bare metal and virtual machines. It helps you understand Linux kernel features, that enable containerization; finally, we'll take a look at installation recipes.
Chapter 2, Working with Docker Containers, explains the different operations you can perform with containers, such as starting, stopping, listing, and deleting.
Chapter 3, Working with Docker Images, introduces you to Docker Hub and shows you how to share images through Docker Hub and how to host your own Docker registry. It will also show you the different ways to build your own image, along with a few Docker image housekeeping operations.
Chapter 4, Network and Data Management for Containers teaches you how to access the container from the outside world, share external storage within the container, communicate with containers running on other hosts, and more.
Chapter 5, Docker Use Cases, explains most of the Docker use cases, such as using Docker for testing, CI/CD, and setting up a PaaS.
Chapter 6,Docker APIs and Language Bindings, dives deep into the Docker API and shows how to work with Docker using the RESTful API and SDK. The curl command that ships with Ubuntu 18.04 has a bug; so, for this chapter, we are using Ubuntu 16.04 and Docker version 17.03.
Chapter 7, Docker Performance, explains the approach one can follow to compare the performance of containers with bare metal and VMs. It also covers monitoring tools.
Chapter 8, Docker Orchestration and Hosting Platforms, provides an introduction to Docker Compose and Swarm, and then we take a look at using Kubernetes for Docker Orchestration.
Chapter 9, Docker Security, explains general security guidelines, SELinux for mandatory access controls, and other security features such as changing capabilities and sharing namespaces.
Chapter 10, Getting Help and Tips and Tricks, provides tips and tricks and resources to help you in things related to Docker administration and development.
Chapter 11,Docker on Cloud, provides an introduction to Docker for AWS and Azure, along with how to install and deploy an application.
It is expected that the reader should have basic Linux/Unix skills, such as installing packages, editing files, and managing services.
Any experience in virtualization technologies, such as KVM, XEN, and VMware, would be an added advantage.
You can download the example code files for this book from your account at www.packtpub.com. If you purchased this book elsewhere, you can visit www.packtpub.com/support and register to have the files emailed directly to you.
You can download the code files by following these steps:
Log in or register at
www.packtpub.com
.
Select the
SUPPORT
tab.
Click on
Code Downloads & Errata
.
Enter the name of the book in the
Search
box and follow the onscreen instructions.
Once the file is downloaded, please make sure that you unzip or extract the folder using the latest version of:
WinRAR/7-Zip for Windows
Zipeg/iZip/UnRarX for Mac
7-Zip/PeaZip for Linux
The code bundle for the book is also hosted on GitHub athttps://github.com/PacktPublishing/Docker-Cookbook-Second-Edition. In case there's an update to the code, it will be updated on the existing GitHub repository.
We also have other code bundles from our rich catalog of books and videos available athttps://github.com/PacktPublishing/. Check them out!
There are a number of text conventions used throughout this book.
CodeInText: Indicates code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles. Here is an example: "Mount the downloaded WebStorm-10*.dmg disk image file as another disk in your system."
A block of code is set as follows:
{ "insecure-registries": [ "172.30.0.0/16" ] }
When we wish to draw your attention to a particular part of a code block, the relevant lines or items are set in bold:
{ "insecure-registries": [
"172.30.0.0/16"
] }
Any command-line input or output is written as follows:
$ docker image pull ubuntu
Bold: Indicates a new term, an important word, or words that you see onscreen. For example, words in menus or dialog boxes appear in the text like this. Here is an example: "Select System info from the Administration panel."
In this book, you will find several headings that appear frequently (Getting ready, How to do it..., How it works..., There's more..., and See also).
To give clear instructions on how to complete a recipe, use these sections as follows:
This section tells you what to expect in the recipe and describes how to set up any software or any preliminary settings required for the recipe.
This section contains the steps required to follow the recipe.
This section usually consists of a detailed explanation of what happened in the previous section.
This section consists of additional information about the recipe in order to make you more knowledgeable about the recipe.
This section provides helpful links to other useful information for the recipe.
Feedback from our readers is always welcome.
General feedback: Email [email protected] and mention the book title in the subject of your message. If you have questions about any aspect of this book, please email us at [email protected].
Errata: Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you have found a mistake in this book, we would be grateful if you would report this to us. Please visit www.packtpub.com/submit-errata, selecting your book, clicking on the Errata Submission Form link, and entering the details.
Piracy: If you come across any illegal copies of our works in any form on the internet, we would be grateful if you would provide us with the location address or website name. Please contact us at [email protected] with a link to the material.
If you are interested in becoming an author: If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, please visit authors.packtpub.com.
Please leave a review. Once you have read and used this book, why not leave a review on the site that you purchased it from? Potential readers can then see and use your unbiased opinion to make purchase decisions, we at Packt can understand what you think about our products, and our authors can see your feedback on their book. Thank you!
For more information about Packt, please visit packtpub.com.
In this chapter, we will cover the following recipes:
Verifying the requirements for Docker installation
Installing Docker on Ubuntu
Installing Docker on CentOS
Installing Docker on Linux with an automated script
Installing Docker for Windows
Installing Docker for Mac
Pulling an image and running a container
Adding a nonroot user to administer Docker
Finding help with the Docker command line
At the very start of the IT revolution, most applications were deployed directly on physical hardware, over the host OS. Because of that single user space, runtime was shared between applications. The deployment was stable, hardware-centric, and had a long maintenance cycle. It was mostly managed by an IT department, and gave much less flexibility to developers. In such cases, the hardware resources were underutilized most of the time. The following diagram depicts such a setup:
For flexible deployments, and in order to better utilize the resources of the host system, virtualization was invented. With hypervisors, such as KVM, XEN, ESX, Hyper-V, and so on, we emulated the hardware for virtual machines (VMs) and deployed a guest OS on each virtual machine. VMs can have a different OS than their host; this means that we are responsible for managing patches, security, and the performance of that VM. With virtualization, applications are isolated at VM level and are defined by the life cycle of VMs. This gives us a better return on our investment and higher flexibility at the cost of increased complexity and redundancy. The following diagram depicts a typical virtualized environment:
Since virtualization was developed, we have been moving towards more application-centric IT. We have removed the hypervisor layer to reduce hardware emulation and complexity. The applications are packaged with their runtime environment, and are deployed using containers. OpenVZ, Solaris Zones, and LXC are a few examples of container technology. Containers are less flexible compared to VMs; for example, we cannot run Microsoft Windows on Linux OS as of writing. Containers are also considered less secure than VMs, because with containers, everything runs on the host OS. If a container gets compromised, then it might be possible to get full access to the host OS. It can be a bit too complex to set up, manage, and automate. These are a few of the reasons why we have not seen the mass adoption of containers in the last few years, even though we had the technology. The following diagram shows how an application is deployed using containers:
With Docker, containers suddenly became first-class citizens. All big corporations, such as Google, Microsoft, Red Hat, IBM, and others, are now working to make containers mainstream.
Docker was started as an internal project by dotCloud founder Solomon Hykes. It was released as open source in March 2013 under the Apache 2.0 license. With dotCloud's platform as a service experience, the founders and engineers of Docker were aware of the challenges of running containers. So with Docker, they developed a standard way to manage containers.
