Skip to content

Chapter 6 of 7

Docker Compose and Orchestration with Swarm

Most real applications are not a single container but a collection of cooperating services such as a web server, an application server, and a database. Docker Compose is a tool for describing such multi-container applications declaratively in a YAML file called docker-compose.yml, which lists each service along with its image, ports, environment, networks, and volumes. Compose keeps the configuration in source control so the whole application stack can be brought up, torn down, or recreated in a single step.

To launch the defined application you run docker-compose up, which builds or pulls the necessary images, creates the required networks and volumes, and starts all the services together. Adding the -d flag runs everything in detached mode so the terminal remains free. To observe what is happening, docker-compose logs service_name prints the logs for a named service, or omits the name to see logs from every service at once. Compose effectively turns a multi-container application into one command-line operation.

When you need to run containers across many machines rather than one, Docker Swarm provides clustering and orchestration. You initialize a cluster with docker swarm init, which turns the current node into a manager and prints a join token for worker nodes. Inside a Swarm, a Docker service describes the desired state of a group of containers: which image to run, how many replicas, what networks to attach, and what resources to allocate. You deploy one with docker service create --name name --replicas N image and inspect the cluster with docker service ls. Swarm also provides Docker secrets, which are encrypted objects for storing sensitive data such as passwords. Secrets are mounted into services through in-memory tmpfs filesystems so the data never touches the container image or disk. Overlay networks complement Swarm by enabling multi-host container communication using VXLAN encapsulation, allowing containers on different physical nodes to talk to one another securely and transparently.

All chapters
  1. 1Foundations of Containers and Docker
  2. 2Docker Architecture, Installation, and Core CLI
  3. 3Building Images with Dockerfile
  4. 4Running and Managing Containers
  5. 5Data Persistence and Networking
  6. 6Docker Compose and Orchestration with Swarm
  7. 7Security and Troubleshooting

Drill it

Reading is not remembering. These come from the Docker And Containers deck:

Q

What is a container in computing?

A container is a lightweight, standalone, executable package that includes everything needed to run a piece of software, such as code, runtime, libraries, and d...

Q

What is containerization?

Containerization is a virtualization method that packages an application and its dependencies into a container, enabling consistent deployment across different...

Q

What is Docker?

Docker is an open-source platform that automates the deployment, scaling, and management of applications inside containers using containerization technology.

Q

What is the difference between a container and a virtual machine?

Containers share the host OS kernel and are lightweight, starting in seconds, while virtual machines include a full guest OS, making them heavier and slower to...