1

Lets suppose that I have got a ten services in docker-compose and every this service need the same extra_hosts with a four records. I would like to define extra_hosts once and only include it to every service.

Is it possible?

version: '3.7'
services:

  web:
    build:
      context: ./apache
      dockerfile: dockerfile_apache2
    image: debian:latest
    container_name: hsthttp1
    extra_hosts:
     - "somehost1:162.242.195.82"
     - "somehost2:162.242.195.83"
     - "somehost3:162.242.195.84"
     - "somehost4:162.242.195.85"
  web2:
    build:
      context: ./apache
      dockerfile: dockerfile_apache2
    image: debian:latest
    container_name: hsthttp2
    extra_hosts:
     - "somehost1:162.242.195.82"
     - "somehost2:162.242.195.83"
     - "somehost3:162.242.195.84"
     - "somehost4:162.242.195.85"
  web3:
    build:
      context: ./apache
      dockerfile: dockerfile_apache2
    image: debian:latest
    container_name: hsthttp3
    extra_hosts:
     - "somehost1:162.242.195.82"
     - "somehost2:162.242.195.83"
     - "somehost3:162.242.195.84"
     - "somehost4:162.242.195.85"
1
  • Are you perhaps looking like a DNS service (BIND, Consul, AWS Route 53, ...)? That would let you define these host names centrally in one place and not manually copy them around everywhere. Commented Jul 7, 2019 at 17:15

1 Answer 1

10

Yes, it's possible to use Extension fields to define reusable fragments since compose version 3.4:

For your situation, you can use next:

docker-compose.yaml:

version: '3.7'

x-extra_hosts:
  &default-extra_hosts
  - "somehost1:162.242.195.82"
  - "somehost2:162.242.195.83"
  - "somehost3:162.242.195.84"
  - "somehost4:162.242.195.85"

services:
  web:
    image: debian:latest
    container_name: hsthttp1
    extra_hosts: *default-extra_hosts
  web2:
    image: debian:latest
    container_name: hsthttp2
    extra_hosts: *default-extra_hosts
  web3:
    image: debian:latest
    container_name: hsthttp3
    extra_hosts: *default-extra_hosts

Above, we define a global &default-extra_hosts which later in every service we can reference it with *default-extra_hosts.

You can use docker-compose config to quick check the effect as next:

shubuntu1@shubuntu1:~/try$ docker-compose config
services:
  web:
    container_name: hsthttp1
    extra_hosts:
    - somehost1:162.242.195.82
    - somehost2:162.242.195.83
    - somehost3:162.242.195.84
    - somehost4:162.242.195.85
    image: debian:latest
  web2:
    container_name: hsthttp2
    extra_hosts:
    - somehost1:162.242.195.82
    - somehost2:162.242.195.83
    - somehost3:162.242.195.84
    - somehost4:162.242.195.85
    image: debian:latest
  web3:
    container_name: hsthttp3
    extra_hosts:
    - somehost1:162.242.195.82
    - somehost2:162.242.195.83
    - somehost3:162.242.195.84
    - somehost4:162.242.195.85
    image: debian:latest
version: '3.7'
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.