Technical skill

Docker compose

Describe an app made of several parts

A website may have one program that displays pages and another that stores information. It might also use a cache, a small place where often-used information is kept for quick access. Starting the programs by hand means remembering their settings and connections. The docker compose command reads a recipe and starts these parts together. The recipe is a YAML file, a plain-text format for settings. Each named part is a service; the project groups services and the things they need, such as a network or data storage.

A Dockerfile gives instructions for building one image. A Compose file describes how services run together and can use existing images, build them, or combine both. In this example, Redis is the separate cache program used by the web app:

services:
  web:
    build: .
    environment:
      CACHE_HOST: redis
    ports:
      - "127.0.0.1:8080:8080"
    depends_on:
      redis:
        condition: service_healthy

  redis:
    image: redis:7-alpine
    volumes:
      - cache-data:/data
    healthcheck:
      test: ["CMD", "redis-cli", "ping"]
      interval: 5s
      timeout: 3s
      retries: 10

volumes:
  cache-data:

Compose places the services on a project network, a private connection where they can find one another by name. The web container can connect to Redis using redis as its hostname. The web port is bound to the host's loopback address for local access. The named volume cache-data keeps Redis data outside its container. The redis:7-alpine image tag names a release line, but publishers can move a tag to newer contents. Choose tags according to the app's update policy.

Start the parts and check when they are ready

Run commands from the directory containing compose.yaml. docker compose up --build builds local images when needed and starts the project. docker compose ps shows service state; docker compose logs -f web follows the web service's output. After changing the Compose file or an image, run docker compose up --build again so Compose can update the project to match.

Starting a dependency does not mean it can already answer requests. A depends_on entry sets startup order. In this example, service_healthy asks Compose to wait until Redis passes its health check. The web application still needs retry and reconnection logic in case Redis becomes unavailable later.

Clean up without losing data by mistake

docker compose down stops and removes the project's containers and default network. Named volumes remain by default, so data can be used after a later up. Adding -v to down removes declared named volumes and their contents. Use that option only when deleting the stored data is intended.

Keep credentials out of a Compose file committed to a repository. Use a secret mechanism suited to the deployment and apply the repository's access rules to environment files. Compose helps make local projects repeatable; available features and deployment behavior depend on the Compose implementation.

Interactive exercises

Materials

Skillsets
Skillset Linux administrator preview
image descriptionLinux administrator
by Infinite Education
3080 users
Skillset Devops preview
image descriptionDevops
by Ajay Kumar
2004 users
Skillset Linux for backend developer preview
image descriptionLinux for backend developer
by Artem Naumenko (fb)
16 users
Skillset Devops preview
image descriptionDevops
by Dio
3 users
Do you know that ...?
Any useful or interesting facts about the skill. Any peace of information would be useful