简体   繁体   中英

Pass current local ip to dnsmasq command in docker-compose

Setup

I have a setup with multiple containers, using dnsmasq as a nameserver for my virtual hosts. I want the containers to be accessible within my local network so I need to resolve all requests to the current local ip of the machine on which the containers are running on (here 192.168.178.21 )

version: "3"

services:
  dnsmasq:
    image: andyshinn/dnsmasq
    ports:
      - 53:53/tcp
      - 53:53/udp
    cap_add:
      - NET_ADMIN
    command: [
      "--log-queries",
      "--log-facility=-",
      "--address=/.test/192.168.178.21"
    ]

  apache:
    ...

  gulp:
    ...

  nginx-proxy:
   ...

Issue

What I would like to do is to 'add' the current ip dynamically, in concept like a variable, that gets the current ip, when I start docker-compose:

...
"--address=/.test/current_local_ip"
...

This way I can start a project with this setup on every development machine in the network and make it reachable for others without manually changing things in the docker-compose file. Thanks for your suggestions

You can use .env file and add

env_file=.env
environment:
  - IP_ADDR

and modify the command to "--address=/.test/$IP_ADDR"

OR

map conf file like

    volumes:
      - .docker/dnsmasq.conf:/etc/dnsmasq.conf

I solved it using a makefile to pass an environment variable to docker-compose like this:

Makefile

LOCAL_IP := $(shell ipconfig getifaddr en0)

all:
    make docker-start
docker-start:
    LOCAL_IP=$(LOCAL_IP) docker-compose -f dev-docker-compose.yml up --detach

dev-docker-compose.yml

version: "3"

services:
  dnsmasq:
    image: andyshinn/dnsmasq
    ports:
      - 53:53/tcp
      - 53:53/udp
    cap_add:
      - NET_ADMIN
    command: [
      "--log-queries",
      "--log-facility=-",
      "--address=/.test/${LOCAL_IP}"
    ]
...

The only issue I run into is that en0 is not always the desired ethernet adapter. Does anyone know a command that always gets the local ip regardless of the active adapter?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM