简体   繁体   English

Docker 组合设置 Linux Docker 主机 ZA12A3079E14CED46E69BA52B8A90B2 中的地址

[英]Docker Compose Set Linux Docker Host IP Address in Container's /etc/hosts

When using Docker Compose, how can we determine and set the IP address of the Docker host in the /etc/hosts file of the Docker container when the host is running Linux? When using Docker Compose, how can we determine and set the IP address of the Docker host in the /etc/hosts file of the Docker container when the host is running Linux? I will like a hostname like DOCKER_HOST in the container to refer to the Docker host.我希望容器中的主机名如DOCKER_HOST来指代 Docker 主机。

Docker for Linux does not yet support host.docker.internal , unlike Docker for Windows and Docker for Mac, so we need an alternative to this. Docker for Linux does not yet support host.docker.internal , unlike Docker for Windows and Docker for Mac, so we need an alternative to this.

--network host cannot be used here because for my purpose localhost in the container must still refer to the container itself. --network host不能在这里使用,因为出于我的目的,容器中的localhost仍然必须引用容器本身。

Docker Compose supports using extra_hosts to add a hostname-ip mapping in /etc/hosts , but I am unable to figure out how to automatically determine the host IP address to be used. Docker Compose 支持使用extra_hosts/etc/hosts添加主机名-ip 映射,但我无法弄清楚如何自动确定要使用的主机 IP 地址。

version: '3'
services:
    api:
        build: .
        ports:
            - "8080:8080"
        extra_hosts:
            - "DOCKER_HOST:X.X.X.X"   # <=== How do we automatically set this IP address?

Is it possible to do something like the following, where we do not have to manually define the Docker host IP address when starting the containers?是否可以执行以下操作,在启动容器时我们不必手动定义 Docker 主机 IP 地址?

DOCKER_HOST=`command_to_get_host_ip` docker-compose up -d 

Or can we set a static IP for the Docker host in docker-compose.yml ?或者我们可以为 Docker 主机设置 static docker-compose.yml吗?

You can try:你可以试试:

DOCKER_HOST=$(python -c 'import socket;print(socket.gethostbyname(socket.gethostname()))') docker-compose up -d

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

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