Simplify Your Docker Setup with Extensions in 'compose.yml'
Docker compose extension

Simplify Your Docker Setup with Extensions in 'compose.yml'

To write an extension in a compose.yml file for Docker, you can use the x- prefix to define reusable fragments of configuration. Here's an example demonstrating how to use extensions in compose.yml:

  • Define the Extension: Use the x- prefix to define reusable configurations.
  • Reference the Extension: Use << to include the extension in your service definitions.

Example compose.yml with Extensions

version: '3.8'

# Define extensions
x-common-environment: &common-environment
  environment:
    - NODE_ENV=production
    - LOG_LEVEL=info

x-common-volumes: &common-volumes
  volumes:
    - data:/data

services:
  web:
    image: my-web-app:latest
    <<: *common-environment  # Include common environment variables
    <<: *common-volumes      # Include common volumes
    ports:
      - "80:80"

  worker:
    image: my-worker-app:latest
    <<: *common-environment  # Include common environment variables
    <<: *common-volumes      # Include common volumes
    environment:
      - WORKER_TYPE=background

volumes:
  data:

        

Key Points:

  • Extensions with x- Prefix: Define common configurations with x- prefix.
  • Reference Extensions: Use <<: *extension-name to include them in services.
  • Reuse Configurations: Simplifies and DRYs up the compose.yml.

This method helps maintain a clean and maintainable compose.yml file by avoiding repetition.

To view or add a comment, sign in

More articles by Hamza Waheed

  • Apache Kafka

    Apache Kafka Introduction: What is event Streaming? Event is basically any any change in logs of your app. When we…

    2 Comments
  • Streamlining Development with Docker Compose

    When it comes to orchestrating Docker environments, developers often debate the merits of declarative versus imperative…

    2 Comments

Explore content categories