Docker Cookbook
eBook - ePub

Docker Cookbook

Over 100 practical and insightful recipes to build distributed applications with Docker , 2nd Edition

  1. 352 pages
  2. English
  3. ePUB (mobile friendly)
  4. Available on iOS & Android
eBook - ePub

Docker Cookbook

Over 100 practical and insightful recipes to build distributed applications with Docker , 2nd Edition

Book details
Book preview
Table of contents
Citations

About This Book

Leverage Docker to deploying software at scale

Key Features

  • Leverage practical examples to manage containers efficiently
  • Integrate with orchestration tools such as Kubernetes for controlled deployments
  • Learn to implement best practices on improving efficiency and security of containers

Book Description

Docker is an open source platform for building, shipping, managing, and securing containers. Docker has become the tool of choice for people willing to work with containers. Since the market is moving toward containerization, Docker will definitely have a big role to play in the future tech market.

This book starts with setting up Docker in different environment, and helps you learn how to work with Docker images. Then, you will take a deep dive into network and data management for containers. The book explores the RESTful APIs provided by Docker to perform different actions, such as image/container operations. The book then explores logs and troubleshooting Docker to solve issues and bottlenecks. You will gain an understanding of Docker use cases, orchestration, security, ecosystems, and hosting platforms to make your applications easy to deploy, build, and collaborate on. The book covers the new features of Docker 18.xx (or later), such as working with AWS and Azure, Docker Engine, Docker Swarm, Docker Compose, and so on.

By the end of this book, you will have gained hands-on experience of finding quick solutions to different problems encountered while working with Docker.

What you will learn

  • Install Docker on various platforms
  • Work with Docker images and containers
  • Container networking and data sharing
  • Docker APIs and language bindings
  • Various PaaS solutions for Docker
  • Implement container orchestration using Docker Swarm and Kubernetes
  • Container security
  • Docker on various clouds

Who this book is for

Book is targeted towards developers, system administrators, and DevOps engineers who want to use Docker in his/her development, QA, or production environments.

It is expected that the reader has basic Linux/Unix skills such as installing packages, editing files, managing services, and so on.

Any experience in virtualization technologies such as KVM, XEN, and VMware will be an added advantage

Frequently asked questions

Simply head over to the account section in settings and click on “Cancel Subscription” - it’s as simple as that. After you cancel, your membership will stay active for the remainder of the time you’ve paid for. Learn more here.
At the moment all of our mobile-responsive ePub books are available to download via the app. Most of our PDFs are also available to download and we're working on making the final remaining ones downloadable now. Learn more here.
Both plans give you full access to the library and all of Perlego’s features. The only differences are the price and subscription period: With the annual plan you’ll save around 30% compared to 12 months on the monthly plan.
We are an online textbook subscription service, where you can get access to an entire online library for less than the price of a single book per month. With over 1 million books across 1000+ topics, we’ve got you covered! Learn more here.
Look out for the read-aloud symbol on your next book to see if you can listen to it. The read-aloud tool reads text aloud for you, highlighting the text as it is being read. You can pause it, speed it up and slow it down. Learn more here.
Yes, you can access Docker Cookbook by Ken Cochrane, Jeeva S. Chelladhurai, Neependra K Khare in PDF and/or ePUB format, as well as other popular books in Computer Science & Cloud Computing. We have over one million books available in our catalogue for you to explore.

Information

Year
2018
ISBN
9781788625982
Edition
2

Working with Docker Images

In this chapter, we will cover the following recipes:
  • Creating an image from a container
  • Creating an account with Docker Hub
  • Logging in and out of a Docker image registry
  • Publishing an image to a registry
  • Looking at the history of an image
  • Removing an image
  • Exporting an image
  • Importing an image
  • Building an image using a Dockerfile
  • Building an Apache image - a Dockerfile example
  • Setting up a private index/registry
  • Automated builds - with GitHub and Bitbucket
  • Creating a custom base image
  • Creating a minimal image using a scratch base image
  • Building images in multiple stages
  • Visualizing the image hierarchy

Introduction

Docker images are the essential building blocks of the Docker-inspired containerization paradigm. As you are already aware, Docker containers are created from Docker images. According to your application requirements, you can choose to create complex services using the Docker images that are built and offered by the folks at Docker or third parties. If the existing images don't fit your requirements, you can also extend the existing images or custom-build your own images.
In this chapter, we will introduce you to Docker Hub, and will show you how to share images through Docker Hub and how to host your own Docker registry. We will also show you different ways to build your own image, along with a few Docker image housekeeping operations.
We are using Ubuntu 18.04 as our primary environment on which to run the z=zx. They should also work with other environments.

Creating an image from the container

There are a couple of ways to create images. One is by manually making changes inside a container and then committing the image. The other is to build one using a Dockerfile. In this recipe, we'll look at the former recipe and look at Dockerfiles later in the chapter.
As we start a new container, a read/write layer gets attached to it. This layer will get destroyed if we do not save it. In this recipe, we will learn how to save that layer and make a new image from the running or stopped container using the docker container commit command. The following is the syntax for the docker container commit command:
 $ docker container commit [OPTIONS] CONTAINER [REPOSITORY[:TAG]]

Getting ready

Ensure that the Docker daemon is running and has access to the Ubuntu image.

How to do it...

Perform the following steps:
  1. Let's start a container from an ubuntu image using the docker container run command:
  1. Having launched the container, issue apt-get update from the container prompt to sync the package list, as shown in the following screenshot:
  1. Install the apache2 package using the apt-get install command:
  1. Now, open another terminal and create an image using the docker container run command:
As you can see, the new image is now being committed to the local repository with myapache2 as the name and latest as the tag.

How it works...

In Chapter 1, Introduction and Installation, we learned that Docker images are layered and that each layer is stacked on top of its parent image. When we start a container, a read/write ephemeral filesystem layer gets created. The changes to the filesystem (that is, the addition, modification, or deletion of files) through the apt-get update and install commands are preserved in this read/write ephemeral filesystem layer. If we stop and delete the container, the ephemeral layer associated with that container is removed and, essentially, we lose all the changes we applied to the container.
In this recipe, we persisted the container's ephemeral layer by using the docker container commit command. In effect, the commit operation creates another image layer and saves it along with other images in the Docker host.

There's more...

The docker container diff command lists all the changes to the container filesystem from its image, as shown in the following code:
 $ docker diff 6289e32373bf ...OUTPUT SNIPPED... C /var/log C /var/log/alternatives.log A /var/log/apache2 A /var/log/apache2/access.log A /var/log/apache2/error.log A /var/log/apache2/other_vhosts_access.log ... OUTPUT SNIPPED... 
We can see that there is a prefix before each entry of the output. The following is a list of those prefixes:
  • A: This is for when a file/directory has been added
  • C: This is for when a file/directory has been modified
  • D: This is for when a file/directory has been deleted
By default, a container gets paused while doing the co...

Table of contents

  1. Title Page
  2. Copyright and Credits
  3. Dedication
  4. Packt Upsell
  5. Contributors
  6. Preface
  7. Introduction and Installation
  8. Working with Docker Containers
  9. Working with Docker Images
  10. Network and Data Management for Containers
  11. Docker Use Cases
  12. Docker APIs and SDKs
  13. Docker Performance
  14. Docker Orchestration and Hosting a Platform
  15. Docker Security
  16. Getting Help and Tips and Tricks
  17. Docker on the Cloud
  18. Other Books You May Enjoy