简体   繁体   English

将当前本地 ip 传递给 docker-compose 中的 dnsmasq 命令

[英]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.我有一个包含多个容器的设置,使用 dnsmasq 作为我的虚拟主机的名称服务器。 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 )我希望容器可以在我的本地网络中访问,因此我需要将所有请求解析为运行容器的机器的当前本地 ip(此处为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:我想要做的是动态“添加”当前 ip,在概念上就像一个变量,它在我启动 docker-compose 时获取当前 ip:

...
"--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.通过这种方式,我可以在网络中的每台开发机器上使用此设置启动一个项目,并使其他人可以访问它,而无需手动更改 docker-compose 文件中的内容。 Thanks for your suggestions谢谢你的建议

You can use .env file and add您可以使用 .env 文件并添加

env_file=.env
environment:
  - IP_ADDR

and modify the command to "--address=/.test/$IP_ADDR"并将命令修改为"--address=/.test/$IP_ADDR"

OR或者

map conf file like映射 conf 文件,如

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

I solved it using a makefile to pass an environment variable to docker-compose like this:我使用 makefile 将环境变量传递给 docker-compose 解决了这个问题,如下所示:

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 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.我遇到的唯一问题是en0并不总是所需的以太网适配器。 Does anyone know a command that always gets the local ip regardless of the active adapter?有没有人知道一个无论活动适配器如何都始终获取本地 ip 的命令?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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