Welcome to MLink Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
312 views
in Technique[技术] by (71.8m points)

Is it possible to set global settings for services in a docker compose file?

Is it possible to specify global settings for services in a Docker compose file?

For example, take this Docker Compose file:

version: "3.9"

services:
  test1:
    env_file: /path/to/env/file
    image: test
    container_name: test1
    ports:
      - "1234:22"
    networks:
      - dmz
    restart: always
  test2:
    env_file: /path/to/env/file
    image: test
    container_name: test2
    ports:
      - "2345:22"
    networks:
      - trust
    restart: always

networks:
  dmz:
    driver: bridge
  trust:
    driver: bridge

I don't want to have env_file: /path/to/env/file for every service and would like to make it apply to all services. I know I can pass it in the docker-compose command line but I'm hoping to do it from within the Docker compose file.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Try using extensions as fragments.

With the support for extension fields, Compose file can be written as follows to improve readability of reused fragments:

This is the example from the README.md:


x-logging: &default-logging
  options:
    max-size: "12m"
    max-file: "5"
  driver: json-file

services:
  frontend:
    image: awesome/webapp
    logging: *default-logging
  backend:
    image: awesome/database
    logging: *default-logging

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to MLink Developer Q&A Community for programmer and developer-Open, Learning and Share
...