Answers: <p>The solution is to use <a href="https://docs.docker.com/compose/cli-command/" rel="nofollow noreferrer">Compose V2</a> and then:</p>
  <pre class="lang-bash prettyprint-override"><code>docker compose build --no-cache &amp;&amp; docker compose up
  </code></pre>
  <p>(note dashes replaced with spaces).</p>
  <p>Here's <a href="https://github.com/docker/compose/blob/v2/pkg/compose/build.go#L259" rel="nofollow noreferrer">specific line</a> in implementation.</p>
  
Answers: CC BY-SA 4.0
Answers: 2022-01-29T13:10:07.897Z
Answers: 2022-01-29T13:10:07.897Z
Answers: dzieciou
Body: <p>When building an image of a docker container I need to access a service on host localhost.</p>
  <p>It looks like <code>--add-host</code> option for <code>docker</code> command updates <code>/etc/hosts</code> both at image build time and container runtime.</p>
  <p>The analogous option for <code>docker-compose</code> command, <code>extra_hosts</code> seems to work only at container runtime. How do I enable it at docker image build time?</p>
  <p>Looks like there feature request for that: <a href="https://github.com/docker/compose/issues/7323" rel="nofollow noreferrer">https://github.com/docker/compose/issues/7323</a>, but not implemented yet. What would be workaround until this is solved?</p>
  <h1>Minimal working example</h1>
  <p>Here's my <code>Dockerfile</code>:</p>
  <pre><code>FROM python:3.8-slim-buster
  RUN grep &quot;host.docker.internal&quot; /etc/hosts || echo &quot;Missing&quot;
  CMD grep &quot;host.docker.internal&quot; /etc/hosts || echo &quot;Missing&quot;
  </code></pre>
  <p>and <code>docker-compose.yml</code>:</p>
  <pre class="lang-yaml prettyprint-override"><code>version: &quot;3.9&quot;
  services:
    test:
      build: .
      extra_hosts:
        - &quot;host.docker.internal:host-gateway&quot;
  </code></pre>
  <p>Running docker:</p>
  <pre class="lang-bash prettyprint-override"><code>docker build . --add-host host.docker.internal:host-gateway  --no-cache -t test \
      &amp;&amp; docker run --add-host host.docker.internal:host-gateway test
  </code></pre>
  <p>prints:</p>
  <pre><code>172.17.0.1      host.docker.internal
  ...
  172.17.0.1      host.docker.internal
  ...
  </code></pre>
  <p>while:</p>
  <pre class="lang-bash prettyprint-override"><code>docker-compose build --no-cache &amp;&amp; docker-compose up
  </code></pre>
  <p>returns</p>
  <pre><code>Missing
  ...
  test_1  | 172.17.0.1    host.docker.internal
  ...
  </code></pre>
  <h1>Motivation</h1>
  <p>I have a Flask app that depends on my private Python package that I install during docker image build. I store my private Python packages in my local <a href="https://github.com/devpi/devpi" rel="nofollow noreferrer">devpi</a> instance hosted on the same machine I use for building docker image. Hence, my Flask app <code>pyproject.toml</code> points to the devpi instance:</p>
  <pre><code>url = &quot;http://localhost:3141/testuser/dev/+simple&quot;
  </code></pre>
  <p>or rather</p>
  <pre><code>url = &quot;http://host.docker.internal:3141/testuser/dev/+simple&quot;
  </code></pre>
  
Tags: <docker><docker-compose>
Title: docker-compose does not update /etc/hosts at build time
UserDisplayName: dzieciou
ThreadText: # docker-compose does not update /etc/hosts at build time
  
  (Asked by: username_0 on 2022-01-23)
  
  When building an image of a docker container I need to access a service on host localhost.
  
  It looks like `--add-host` option for `docker` command updates `/etc/hosts` both at image build time and container runtime.
  
  The analogous option for `docker-compose` command, `extra_hosts` seems to work only at container runtime. How do I enable it at docker image build time?
  
  Looks like there feature request for that: <https://github.com/docker/compose/issues/7323>, but not implemented yet. What would be workaround until this is solved?
  
  # Minimal working example
  
  Here's my `Dockerfile`:
  
  ```
  FROM python:3.8-slim-buster
  RUN grep "host.docker.internal" /etc/hosts || echo "Missing"
  CMD grep "host.docker.internal" /etc/hosts || echo "Missing"
  
  ```
  
  and `docker-compose.yml`:
  
  ```yaml
  version: "3.9"
  services:
    test:
      build: .
      extra_hosts:
        - "host.docker.internal:host-gateway"
  
  ```
  
  Running docker:
  
  ```bash
  docker build . --add-host host.docker.internal:host-gateway  --no-cache -t test \
      && docker run --add-host host.docker.internal:host-gateway test
  
  ```
  
  prints:
  
  ```
  172.17.0.1      host.docker.internal
  ...
  172.17.0.1      host.docker.internal
  ...
  
  ```
  
  while:
  
  ```bash
  docker-compose build --no-cache && docker-compose up
  
  ```
  
  returns
  
  ```
  Missing
  ...
  test_1  | 172.17.0.1    host.docker.internal
  ...
  
  ```
  
  # Motivation
  
  I have a Flask app that depends on my private Python package that I install during docker image build. I store my private Python packages in my local [devpi](https://github.com/devpi/devpi) instance hosted on the same machine I use for building docker image. Hence, my Flask app `pyproject.toml` points to the devpi instance:
  
  ```
  url = "http://localhost:3141/testuser/dev/+simple"
  
  ```
  
  or rather
  
  ```
  url = "http://host.docker.internal:3141/testuser/dev/+simple"
  
  ```
  
  **Tags:** docker, docker-compose
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_0 on 2022-01-29. Score: 0 | Accepted answer
  
  The solution is to use [Compose V2](https://docs.docker.com/compose/cli-command/) and then:
  
  ```bash
  docker compose build --no-cache && docker compose up
  
  ```
  
  (note dashes replaced with spaces).
  
  Here's [specific line](https://github.com/docker/compose/blob/v2/pkg/compose/build.go#L259) in implementation.
  
  
  ---
  
  