Containers are meant to provide component isolation in a modern software stack. Put your database in one container, your web application in another, and they can all be scaled, managed, restarted, and swapped out independently. But developing and testing a multi-container application isn’t anything like working with a single container at a time.
Docker Compose was created by Docker to simplify the process of developing and testing multi-container applications. It’s a command-line tool, reminiscent of the Docker client, that takes in a specially formatted descriptor file to assemble applications out of multiple containers and run them in concert on a single host. (Tools like Docker Swarm or Kubernetes deploy multi-container apps in production across multiple hosts.)
In this tutorial, we’ll walk through the steps needed to define and deploy a simple multi-container web service app. While Docker Compose is normally used for development and testing, it can also be used for deploying production applications. For the sake of this discussion, we will concentrate on dev-and-test scenarios.
Docker Compose example
A minimal Docker Compose application consists of three components:
- A Dockerfile for each container image you want to build.
- A YAML file, docker-compose.yml, that Docker Compose will use to launch containers from those images and configure their services.
- The files that comprise the application itself.
In our example, we’re going to create a toy web messaging system, written in Python using the Bottle web framework and configured to store data in Redis. If used in production app, it would be horrendously insecure and impractical (not to mention underpowered!). But the point is to show how the pieces fit together, and to provide you with a skeleton you could flesh out further on your own.
To obtain all the pieces at once, download and unpack this docker-compose-example.zip file into a working directory. You will need to have the most recent version of Docker installed; I used version 17.12.01, which was the latest stable version at the time of writing. Note that this tutorial should work as-is on Docker for Windows, Docker for Mac, and the conventional Linux incarnation of Docker.
The Dockerfile included in the bundle is simple enough:
This set of commands describes an image that uses the stock python:3 image as its base, and uses two files (both included in the .zip bundle): requirements.txt and app.py. The former is used by Python to describe the dependencies of an application; the latter is the Python application itself.
The elements of the docker-compose.yml file are worth examining in detail:
The version line specifies the version of the Docker Compose file format to use. Docker Compose has been through a number of revisions, each tied to specific versions of the Docker engine; version 3 is the most recent as of this writing.
The services section defines the various services used by this particular container stack. Here, we’re defining two services: redis (which uses the redis container image to run the Redis service), and web (our Python application). Each service descriptor provides details about the service:
- build: Describes configurations applied at build time. It can be just a pathname, as shown here, or it can provide details such as a Dockerfile to use (instead of the default Dockerfile in the directory) or arguments to pass to the Dockerfile during the build process.
- command: A command to run when starting the container. This overrides the CMD statement supplied in the container’s Dockerfile, if any.
- volumes: Any paths to volumes to be mounted for this service. You can also specify volumes as a top-level configuration option and re-use any volumes defined there across multiple containers in the docker-compose.yml file.
- ports: Port mappings for the container. You can use a simple format, as shown here, or a more detailed format that describes which protocols to use.
- depends_on: Describes the order of dependencies between services. Here, because web depends on redis, redis must be brought up first when Docker Compose starts the app.
There are many more options available in services, but these few are enough to get a basic project started.
Continue here: goo.gl/6u4vHv
www.infoworld.com
No comments:
Post a Comment