Sathyajith Bhat Practical Docker with Python Build, Release and Distribute your Python App with Docker Sathyajith Bhat Bangalore, Karnataka, India Any source code or other supplementary material referenced by the author in this book is available to readers on GitHub via the book's product page, located at www.apress.com/978-1-4842-3783-0 . For more detailed information, please visit http://www.apress.com/source-code . ISBN 978-1-4842-3783-0 e-ISBN 978-1-4842-3784-7 https://doi.org/10.1007/978-1-4842-3784-7 Library of Congress Control Number: 2018952361 © Sathyajith Bhat 2018 This work is subject to copyright. All rights are reserved by the Publisher, whether the whole or part of the material is concerned, specifically the rights of translation, reprinting, reuse of illustrations, recitation, broadcasting, reproduction on microfilms or in any other physical way, and transmission or information storage and retrieval, electronic adaptation, computer software, or by similar or dissimilar methodology now known or hereafter developed. Trademarked names, logos, and images may appear in this book. Rather than use a trademark symbol with every occurrence of a trademarked name, logo, or image we use the names, logos, and images only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark. The use in this publication of trade names, trademarks, service marks, and similar terms, even if they are not identified as such, is not to be taken as an expression of opinion as to whether or not they are subject to proprietary rights. While the advice and information in this book are believed to be true and accurate at the date of publication, neither the authors nor the editors nor the publisher can accept any legal responsibility for any errors or omissions that may be made. The publisher makes no warranty, express or implied, with respect to the material contained herein. the material contained herein. Distributed to the book trade worldwide by Springer Science+Business Media New York, 233 Spring Street, 6th Floor, New York, NY 10013. Phone 1-800- SPRINGER, fax (201) 348-4505, e-mail [email protected], or visit www.springeronline.com. Apress Media, LLC is a California LLC and the sole member (owner) is Springer Science + Business Media Finance Inc (SSBM Finance Inc). SSBM Finance Inc is a Delaware corporation. To my parents, Jayakar and Jyothika Bhat, who have unconditionally supported me throughout my entire life. Introduction Docker has exploded in popularity and has become the de facto target as a containerization image format as well as a containerization runtime. With modern applications getting more and more complicated, the increased focus on microservices has led to adoption of Docker, as it allows for applications along with their dependencies to be packaged into a file as a container that can run on any system. This allows for faster turnaround times in application deployment and less complexity and it negates the chances of the “it-works-on-my-server- but-not-on-yours” problem. Practical Docker with Python covers the fundamentals of containerization, gets you acquainted with Docker, breaks down terminology like Dockerfile and Docker Volumes, and takes you on a guided tour of building a chatbot using Python. You’ll learn how to package a traditional application as a Docker Image. Book Structure This book is divided into seven chapters—we start the first chapter with a brief introduction to Docker and containerization. We then take a 101 class of Docker, including installing, configuring, and understanding some Docker jargon. In Chapter 3 , we take a look at our project and look at how to configure our chatbot. In Chapters 4 to 6 , we dive into the meat of Docker, focusing on Dockerfiles, Docker Networks, and Docker Volumes. These chapters include practical exercises on how to incorporate each of these into the project. Finally, we take a look at Docker Compose and see how we can run multi-container applications. Acknowledgments Thank you to my wife, Jyothsna, for being patient and supporting me in my career and while writing this book. I would like to thank Nikhil Karkal, Siddhi Chavan, and Divya Modi from Apress for helping me immensely through all stages of the book. I would like to thank my technical reviewer, Swapnil Kulkarni, for providing pertinent feedback. I would also like to acknowledge the immense support provided by Saurabh Minni, Ninad Pundalik, Prashanth H. N., Mrityunjay Iyer, and Abhijith Gopal over the past couple of years. Table of Contents Chapter 1 : Introduction to Containerization What Is Docker? Docker the Company Docker the Software Technology Understanding Problems that Docker Solves Containerization Through the Years 1979: chroot 2000: FreeBSD Jails 2005: OpenVZ 2006: cgroups 2008: LXC Knowing the Difference Between Containers and Virtual Machines Summary Chapter 2 : Docker 101 Installing Docker Installing Docker on Windows Installing on MacOS Installing on Linux Understanding Jargon Around Docker Hands-On Docker Summary Chapter 3 : Building the Python App About the Project Setting Up Telegram Messenger BotFather: Telegram’s Bot Creation Interface Newsbot: The Python App Summary Chapter 4 : Understanding the Dockerfile Dockerfile Build Context Dockerignore Building Using Docker Build Dockerfile Instructions Guidelines and Recommendations for Writing Dockerfiles Multi-Stage Builds Dockerfile Exercises Summary Chapter 5 : Understanding Docker Volumes Data Persistence Example of Data Loss Within Docker Container Docker Volume Exercises Summary Chapter 6 : Understanding Docker Networks Why Do We Need Container Networking? Default Docker Network Drivers Working with Docker Networks Docker Networking Exercises Summary Chapter 7 : Understanding Docker Compose Overview of Docker Compose Installing Docker Compose Docker Compose Basics Docker Compose File Reference