Docker on Amazon Web Services
上QQ阅读APP看书,第一时间看更新

Building Applications Using Docker

In the previous chapter, you were introduced to the sample application, and you were able to download and run the application locally. At present, your development environment is set up for local development; however, before you can get your application to production, you need to be able to package up your application and all of its dependencies, ensure the target production environment has the correct supporting operating system libraries and configuration, select an appropriate web server to host your application, and have a mechanism to be able to package this all together, ideally in a self-contained artifact that requires minimal external configuration. Traditionally, all of this has been very difficult to achieve reliably and consistently but this is where Docker has changed the landscape dramatically. With Docker and supporting tools, you now have the ability to achieve all of this and more in a much faster, more reliable, more consistent, and more portable fashion than ever before. 

In this chapter, you will learn how to create a comprehensive workflow that allows you to test, build, and publish your applications in a portable, repeatable, and consistent manner using Docker. The approach you will learn about has numerous benefits—for example, you will be able to perform all tasks by running a handful of simple, easy-to-remember commands, and you will be able to do so without needing to install any application-specific or operating-system-specific dependencies into your local development or build environment. This makes it very easy to move to another machine or configure a continuous-delivery service to perform the same workflow—as long as you have the core Docker-based environment you set up in the previous chapter, you will be able to run the workflow on any machine, regardless of the specifics of your application or programming language. 

You will learn how to define test and runtime environments for your application using a Dockerfile, configuring support for multi-stage builds that allow you to build application artifacts in an image that has all development tools and libraries available, and then copy those artifacts to other stages of your Dockerfile. You will leverage Docker Compose as a tool to orchestrate complex Docker environments with multiple containers, which allows you to test integration scenarios, such as your application interacting with a database, and also mimic how you would run your application in production environments.  An important concept that will be introduced is the concept of building a release image, which is a production-ready image that can be shipped to production, assuming any new application features and functionality work as expected. You will build and run this release image in your local Docker environment, connect your application to a database, and then create acceptance tests that verify the application works as expected, from the perspective of an external client connecting to your application.

Finally, you will bring all you have learned together using GNU Make to automate your workflow. Once finished, you will be able to run unit tests and build application artifacts by simply running make test, and then build your release image, start up a production-like environment, and run acceptance tests by running make release.  This will make it very simple to test and publish new application changes with confidence as they are developed, using a portable and consistent workflow that can be easily run in a local development environment and in any continuous-delivery environment that supports Docker and Docker Compose.

The following topics will be covered:

  • Testing and building applications using Docker
  • Creating multi-stage builds
  • Creating a test stage to build and test application artifacts
  • Creating a release stage to build and test a release image
  • Using Docker Compose to test and build applications
  • Creating acceptance tests
  • Automating the workflow