Early Praise for Docker for Rails Developers With the avalanche of DevOps tools in use, this text has definitely cleared up the mystery. I’ve been waiting for a Docker book aimed at Rails projects, and I’m now convinced Docker is the way to go. ➤ Nigel Lowry Company Director and Principal Consultant, Lemmata Docker for Rails Developers is a wonderful book that allows you to jump in and start converting your existing apps to run in containers. It is well written, easy to follow, and makes you want to keep reading. I recommend this book for anyone with a little Rails experience who wants to get a jump start on using Docker. ➤ Chris Johnson Operations Manager, healthfinch With this book at my side, I was able to help my team move our largest, highest- revenue service into containers. This migration made disaster recovery much faster and more reliable, and made it possible to open a data center in a whole new market. ➤ Erin Dees Lead Software Engineer, New Relic This is much more than a how-to book, with the best technical writing I’ve seen recently. Isenberg’s excellent guide provides clear and understandable explanations of how to solve Rails-specific Docker DevOps issues. This is the kind of thing I wish Docker had published a long time ago. ➤ David L. Bean, PhD Director of Data Science, PayClip, Inc. Docker for Rails Developers is more than just a fantastic resource for Ruby and Rails developers looking to get up to speed with Docker. It’s a great, no-frills guide for how to use the technology in practical, real-world situations, and I’d have no hesitation in recommending this to Python or Node developers either. I’ve been waiting since 2014 for a go-to book to hand to the Docker curious, and this might just be it. ➤ Alexander Lynham Owner, envoys.io Docker for Rails Developers Build, Ship, and Run Your Applications Everywhere Rob Isenberg The Pragmatic Bookshelf Raleigh, North Carolina Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in this book, and The Pragmatic Programmers, LLC was aware of a trademark claim, the designations have been printed in initial capital letters or in all capitals. The Pragmatic Starter Kit, The Pragmatic Programmer, Pragmatic Programming, Pragmatic Bookshelf, PragProg and the linking g device are trade- marks of The Pragmatic Programmers, LLC. Every precaution was taken in the preparation of this book. However, the publisher assumes no responsibility for errors or omissions, or for damages that may result from the use of information (including program listings) contained herein. Our Pragmatic books, screencasts, and audio books can help you and your team create better software and have more fun. Visit us at https://pragprog.com. The team that produced this book includes: Publisher: Andy Hunt VP of Operations: Janet Furlow Managing Editor: Susan Conant Development Editor: Adaobi Obi Tulton Copy Editor: Nicole Abramowitz Indexing: Potomac Indexing, LLC Layout: Gilson Graphics For sales, volume licensing, and support, please contact [email protected]. For international rights, please contact [email protected]. Copyright © 2019 The Pragmatic Programmers, LLC. All 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, photocopying, recording, or otherwise, without the prior consent of the publisher. ISBN-13: 978-1-68050-273-2 Book version: P1.0—February 2019 Ruth. In hindsight, writing a book whilst hav- ing a baby and renovating a house probably wasn’t the best idea—who knew? Thank you for your patience, love, and support. None of this would have been possible without you. Sammy. I couldn’t have imagined the joy and love you’d bring into our life. Be kind, be brave, and be willing to take risks in pursuit of your happiness and passions. I love you so much. Mum and Dad. Thank you for everything. Contents Acknowledgements . . . . . . . . . . . xi Introduction . . . . . . . . . . . . xiii Part I — Development 1. A Brave New World . . . . . . . . . . . 3 Installing Docker 3 Verifying Your Install 5 Before We Begin 6 Running a Ruby Script Without Ruby Installed 7 Generating a New Rails App Without Ruby Installed 10 Quick Recap 15 2. Running a Rails App in a Container . . . . . . . 17 How Do We Run Our Rails App? 17 Defining Our First Custom Image 18 Building Our Image 23 Running a Rails Server with Our Image 25 Reaching the App: Publishing Ports 26 Binding the Rails Server to IP Addresses 28 Quick Recap 29 3. Fine-Tuning Our Rails Image . . . . . . . . . 31 Naming and Versioning Our Image 31 A Default Command 33 Ignoring Unnecessary Files 34 The Image Build Cache 36 Caching Issue 1: Updating Packages 37 Caching Issue 2: Unnecessary Gem Installs 38 Contents • viii The Finishing Touch 41 Quick Recap 42 4. Describing Our App Declaratively with Docker Compose . . 45 Getting Started with Compose 45 Launching Our App 47 Mounting a Local Volume 50 Starting and Stopping Services 51 Other Common Tasks 53 Quick Recap 56 5. Beyond the App: Adding Redis . . . . . . . . 59 Starting a Redis Server 60 Manually Connecting to the Redis Server 62 How Containers Can Talk to Each Other 63 Our Rails App Talking to Redis 64 Starting the Entire App with Docker Compose 67 Quick Recap 68 6. Adding a Database: Postgres . . . . . . . . . 71 Starting a Postgres Server 71 Connecting to Postgres from a Separate Container 74 Connecting Our Rails App to Postgres 75 Using the Database in Practice 80 Decoupling Data from the Container 81 Quick Recap 85 7. Playing Nice with JavaScript . . . . . . . . . 87 The JavaScript Front-End Options 87 Rails JavaScript Front End with Webpacker 88 Compiling Assets with Webpacker 90 A Hello World React App 92 Quick Recap 94 8. Testing in a Dockerized Environment . . . . . . 95 Setting Up RSpec 96 Our First Test 97 Setting Up Rails System Tests 99 Running Tests That Rely on JavaScript 101 Debugging 109 Quick Recap 110