title: What is the purpose of building a docker image inside a compose file?
body: <p>I was reading <a href="https://docs.docker.com/compose/django/" rel="nofollow noreferrer">Quickstart: Compose and Django</a> when I came across &quot;defining a build in a compose file&quot;. Well I've seen it before but what I'm curious about here is what's the purpose of it? I just can't get it.</p>
  <p>Why we just don't build the image once (or update it whenever we want) and use it multiple times in different <code>docker-compose</code> files?</p>
  
  <p>Here is the <code>Dockerfile</code>:</p>
  <pre class="lang-sh prettyprint-override"><code>FROM python:3
  ENV PYTHONUNBUFFERED 1
  RUN mkdir /code
  WORKDIR /code
  COPY requirements.txt /code/
  RUN pip install -r requirements.txt
  COPY . /code/
  </code></pre>
  <p>And here is <code>docker-compose.yml</code>:</p>
  <pre class="lang-sh prettyprint-override"><code>version: '3'
   web:
    # &lt;&lt;&lt;&lt; 
    # Why not building the image and using it here like &quot;image: my/django&quot;?
    # &lt;&lt;&lt;&lt;
    build: .
    command: python manage.py runserver 0.0.0.0:8000
    volumes:
      - .:/code
    ports:
      - &quot;8000:8000&quot;
  </code></pre>
  <p>You might say: &quot;well, do as you wish!&quot; Why I'm asking is because I think there might be some benefits that I'm not aware of.</p>
  <p>PS:</p>
  <ul>
  <li>I mostly use Docker for bringing up some services (DNS, Monitoring, etc. Never used it for
  development).</li>
  <li>I have already read this <a href="https://stackoverflow.com/questions/50230399/what-is-the-difference-between-docker-compose-build-and-docker-build">What is the difference between `docker-compose build` and `docker build`?</a></li>
  </ul>