Docker Compose

repository·main·Indexed 12 days ago

https://github.com/docker/compose

A tool for defining and running multi-container Docker applications using a declarative Compose file format. It allows developers to manage the entire application lifecycle, including creating and starting containers, with a single command: `docker compose up`.

Tokens
52.8K
Snippets
187
Records
237
Agent score
96%

What's inside Docker Compose

  1. What is Docker Compose

    main
    Docker Compose is a tool for running multi-container applications on Docker. It uses the Compose file format to define how one or more containers that make up your application are configured. Once a Compose file is defined, you can manage the entire application lifecycle (creating and starting containers) with a single command: docker compose up.
  2. Manage container recreation with docker compose up

    main

    By default, docker compose up detects changes in a service's configuration or image and recreates the container (while preserving mounted volumes). You can control this behavior using the following flags:

    • Force recreation: Use --force-recreate to stop and recreate all containers even if their configuration and image haven't changed.
    • Prevent recreation: Use --no-recreate to prevent Compose from picking up changes and recreating existing containers.
    • Recreate dependencies: Use --always-recreate-deps to ensure dependent containers are also recreated (this is incompatible with --no-recreate).
  3. Control log attachment and output

    main

    When running docker compose up in attached mode, you can manage which service logs are streamed to your terminal:

    • Select specific services: Use --attach <service> to restrict log attachment to only the specified services.
    • Exclude specific services: Use --no-attach <service> to prevent logs from specific (e.g., very verbose) services from flooding your output.
    • Attach to dependencies: Use --attach-dependencies to automatically attach to the log output of dependent services.
    • Formatting: Use --no-log-prefix to suppress the service name prefix in logs and --timestamps to show timestamps.
  4. How EventProcessor works for tracking operations

    main

    The EventProcessor interface allows you to monitor Compose operations (like up, down, or build) in real-time. It provides hooks to react to the lifecycle of a Compose operation and the individual resource changes within it.

    An EventProcessor implements three main methods:

    • Start(ctx, operation): Triggered when a Compose operation begins.
    • On(events...): Triggered with progress events for individual resource changes (e.g., pulling an image, starting a container).
    • Done(operation, success): Triggered when the operation completes, providing the success/failure status.
  5. Implement a Docker Compose provider extension

    main

    Docker Compose allows extending the service abstraction to manage resources via third-party runtimes (e.g., cloud services) using the provider attribute.

    To create a valid extension, your provider binary must:

    1. Be accessible via the user's PATH or be a Docker CLI plugin.
    2. Implement a compose command with up and down subcommands.
    3. (Optional) Implement a stop subcommand to support docker compose stop.
    4. (Optional) Implement a metadata subcommand to describe its interface.
    5. (Required) Ensure the compose up command is idempotent.

    Use the project-name flag to tag resources so they can be correctly released during the down lifecycle.

      database:
        provider:
          type: awesomecloud
          options:
            type: mysql
            size: 256
            name: myAwesomeCloudDB
  6. Inject environment variables into dependent services

    main

    A service managed by a provider can pass configuration to dependent services using setenv or rawsetenv messages.

    Scenario:

    services:
      app:
        image: myapp
        depends_on:
           - database
    
      database:
        provider:
          type: awesomecloud
    1. Using setenv: If the provider emits {"type": "setenv", "message": "URL=https://cloud.com/db"}, the app service receives DATABASE_URL=https://cloud.com/db (prefixed with the service name).
    2. Using rawsetenv: If the provider emits {"type": "rawsetenv", "message": "SECRET_KEY=xxx"}, the app service receives SECRET_KEY=xxx exactly.

    Warning on rawsetenv: Since there is no prefixing, keys must be unique. If a rawsetenv key collides with an existing variable (including those in the environment section), the existing value is overwritten and Compose logs a warning.

    services:
      app:
        image: myapp 
        depends_on:
           - database
    
      database:
        provider:
          type: awesomecloud
  7. Use the dry-run command to test changes without applying them

    main

    The docker compose alpha dry-run command allows you to simulate the execution of a command. This is useful for testing your configuration and verifying what actions would be taken without actually modifying your containers, networks, or volumes.

    docker compose alpha dry-run <command>
  8. Quick Start with Docker Compose

    main

    Using Docker Compose follows a three-step process:

    1. Define the environment: Create a Dockerfile to ensure your application environment can be reproduced anywhere.
    2. Define the services: Create a compose.yaml file to define the services that make up your app so they can run together in an isolated environment.
    3. Run the application: Execute docker compose up to start and run your entire application.

    Example compose.yaml:

    services:
      web:
        build: .
        ports:
          - "5000:5000"
        volumes:
          - .:/code
      redis:
        image: redis
  9. Use `docker compose alpha watch` to monitor build context

    main

    The docker compose alpha watch command allows you to watch your build context for changes. When files in the context are updated, Docker Compose can automatically rebuild or refresh containers.

    Note: This is currently an alpha feature and may be subject to change.

    docker compose alpha watch
  10. Automatically remove containers with `--rm`

    main

    Use the --rm flag to automatically remove the container once the command finishes executing. This is particularly useful for one-off tasks like database migrations. Using --rm also overrides any restart policy defined in the service configuration, ensuring the container is removed even if it was configured to restart.

    $ docker compose run --rm web python manage.py db upgrade