title: Docker Compose Set Linux Docker Host IP Address in Container's /etc/hosts
body: <p>When using Docker Compose, how can we determine and set the IP address of the Docker host in the <code>/etc/hosts</code> file of the Docker container when the host is running Linux? I will like a hostname like <code>DOCKER_HOST</code> in the container to refer to the Docker host.</p>
  
  <p>Docker for Linux does not yet support <code>host.docker.internal</code>, unlike Docker for Windows and Docker for Mac, so we need an alternative to this.</p>
  
  <p><code>--network host</code> cannot be used here because for my purpose <code>localhost</code> in the container must still refer to the container itself.</p>
  
  <p>Docker Compose supports using <code>extra_hosts</code> to add a hostname-ip mapping in <code>/etc/hosts</code>, but I am unable to figure out how to automatically determine the host IP address to be used.</p>
  
  <pre><code>version: '3'
  services:
      api:
          build: .
          ports:
              - "8080:8080"
          extra_hosts:
              - "DOCKER_HOST:X.X.X.X"   # &lt;=== How do we automatically set this IP address?
  </code></pre>
  
  <p>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?</p>
  
  <pre><code>DOCKER_HOST=`command_to_get_host_ip` docker-compose up -d 
  </code></pre>
  
  <p>Or can we set a static IP for the Docker host in <code>docker-compose.yml</code>?</p>