liveBook · Manning

Compose service

As explained in Road to Kubernetes

A Docker Compose service is a named application component declared in a Compose configuration and instantiated as one or more containers.

A Compose service is a named containerized application declared under `services:` in `compose.yaml`. It gives Docker Compose a repeatable definition for building or obtaining a container image and running the application. Instead of retyping options for `docker run`, put settings such as ports and environment variables in the service definition. section 6.2.2

Listing 6.3 Docker Compose for our Python app
services:
  web:
    build:
      context: .
      dockerfile: Dockerfile
    ports:
      - "8080:8080"
    environment:
      - PORT=8080

This `web` service builds the Python application from the current directory’s Dockerfile, maps host port 8080 to container port 8080, and sets `PORT=8080`. section 6.2.2

From the project directory, run `docker compose build` to build the image, `docker compose up` to run the service, and `docker compose down` to stop it. These commands use the declared configuration rather than requiring the port and environment options on each run. section 6.2.2

A Compose file can declare more than one service. Compose provides a network between their containers, and a service name works as a hostname on that network. For example, a Python `web` container can request `http://my-nginx-service:80` from an NGINX service. The NGINX service needs no host port mapping for that container-to-container request. section 6.4.1

Listing 6.9 NGINX service in Docker Compose
services:
  my-nginx-service:
    image: nginx
  web:
    build:
      context: .
      dockerfile: Dockerfile
    ports:
      - "8080:8080"
    environment:
      - PORT=8080
    volumes:
      - mywebdata:/app/data

volumes:
  mywebdata:

Compare the two services: `web` is built locally and publishes a port, while `my-nginx-service` uses the public NGINX image without publishing one. section 6.4.1

For a load-balancing setup, separate service names can run the same Python image. NGINX can then address those names as upstream destinations and distribute requests among them. This example duplicates service declarations rather than introducing an automatic scaling command. section 7.4.1

Do not confuse a Compose service with a Kubernetes `Service` resource. The Compose definition describes a containerized application; a Kubernetes Service has its own manifest and can direct traffic to containers selected by labels. section 6.2.2 section 8.3.4

How Road to Kubernetes covers it

How deep it goes
Works through service names, build settings, ports, environment variables, commands, and inter-service requests; leaves other configuration options aside.
Examples
Uses a Python `web` service, an NGINX image service, and repeated Python services as load-balancing destinations.
What it assumes
Starts with a working Python application, Dockerfile, and environment variables, while explaining Compose settings through equivalent `docker run` arguments.
What you can do after
Operate/configure it with `docker compose build`, `up`, and `down`, then address another service by name from a container.
How it connects
Connects Dockerfiles, container images, port mapping, environment variables, Compose networking, and NGINX load balancing.

Also explained in

Compare how 2 books explain Compose service →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Road to Kubernetes. The section links point to the parts of the book it draws on.