concept Compose file in category docker

appears as: Compose file, The Compose file, Compose files
Learn Docker in a Month of Lunches

This is an excerpt from Manning's book Learn Docker in a Month of Lunches.

This is a good example of how to configure different types of services. The accesslog service doesn’t publish any ports or use any other properties you would capture from the docker container run command, so the only value recorded is the image name. The iotd service is the REST API--the Compose file records the image name and also publishes port 80 on the container to a random port on the host. The image-gallery service has the image name and a specific published port: 8010 on the host maps to port 80 in the container. It also has a depends_on section saying this service has a dependency on the other two services, so Compose should make sure those are running before it starts this one.

Figure 7.3 A more complex Compose file that specifies three services connected to the same network

Figure 7.3 shows the architecture of this app. I’ve generated the diagrams in this chapter from a tool that reads the Compose file and generates a PNG image of the architecture. That’s a great way to keep your documentation up to date--you can generate the diagram from the Compose file every time there’s a change. The diagram tool runs in a Docker container of course--you can find it on GitHub at https:// github.com/pmsipilot/docker-compose-viz .

Docker Compose is a very neat way of describing the setup for complex distributed apps in a small, clear file format. The Compose YAML file is effectively a deployment guide for your application, but it’s miles ahead of a guide written as a Word document. In the old days those Word docs described every step of the application release, and they ran to dozens of pages filled with inaccurate descriptions and out-of-date information. The Compose file is simple and it’s actionable--you use it to run your app, so there’s no risk of it going out of date.

Compose is a useful part of your toolkit when you start making more use of Docker containers. But it’s important to understand exactly what Docker Compose is for, and what its limitations are. Compose lets you define your application and apply the definition to a single machine running Docker. It compares the live Docker resources on that machine with the resources described in the Compose file, and it will send requests to the Docker API to replace resources that have been updated and create new resources where they are needed.

# load your machine's IP address into an environment variable - on Windows:
     $env:HOST_IP = $(Get-NetIPConfiguration | Where-Object {$_.IPv4DefaultGateway -ne $null }).IPv4Address.IPAddress
      
     # on Linux:
     export HOST_IP=$(ip route get 1 | awk '{print $NF;exit}')
      
     # run the app with a Compose file which includes Grafana:
     docker-compose -f ./docker-compose-with-grafana.yml up -d --scale accesslog=3
      
     # now send in some load to prime the metrics - on Windows:
     for ($i=1; $i -le 20; $i++) { iwr -useb http://localhost:8010 | Out-Null }
      
     # or on Linux:
     for i in {1..20}; do curl http://localhost:8010 > /dev/null; done
      
     # and browse to http://localhost:3000
sitemap

Unable to load book!

The book could not be loaded.

(try again in a couple of minutes)

manning.com homepage
test yourself with a liveTest