title: Running Django's collectstatic in Dockerfile produces empty directory
body: <p>I'm trying to run Django from a Docker container on Heroku, but to make that work, I need to run <code>python manage.py collectstatic</code> during my build phase. To achieve that, I wrote the following Dockerfile:</p>
  <pre><code># Set up image
  FROM python:3.10
  WORKDIR /usr/src/app
  ENV PYTHONDONTWRITEBYTECODE=1
  ENV PYTHONUNBUFFERED=1
  
  # Install poetry and identify Python dependencies
  RUN pip install poetry
  COPY pyproject.toml /usr/src/app/
  
  # Install Python dependencies
  RUN set -x \
      &amp;&amp; apt update -y \
      &amp;&amp; apt install -y \
          libpq-dev \
          gcc \
      &amp;&amp; poetry config virtualenvs.create false \
      &amp;&amp; poetry install --no-ansi
  
  # Copy source into image
  COPY . /usr/src/app/
  
  # Collect static files
  RUN python -m manage collectstatic -v 3 --no-input
  </code></pre>
  <p>And here's the docker-compose.yml file I used to run the image:</p>
  <pre><code>services:
      db:
          image: postgres
          env_file:
              - .env.docker.db
          volumes:
              - db:/var/lib/postgresql/data
          networks:
              - backend
          ports:
              - &quot;5433:5432&quot;
      web:
          build: .
          restart: always
          env_file:
              - .env.docker.web
          ports:
              - &quot;8001:$PORT&quot;
          volumes:
              - .:/usr/src/app
          depends_on:
              - db
          networks:
              - backend
          command: gunicorn --bind 0.0.0.0:$PORT myapp.wsgi
  volumes:
      db:
  networks:
      backend:
          driver: bridge
  </code></pre>
  <p>The Dockerfile builds just fine, and I can even see that <code>collectstatic</code> is running and collecting the appropriate files during the build. However, when the build is finished, the only evidence that <code>collectstatic</code> ran is an empty directory called <code>staticfiles</code>. If I run <code>collectstatic</code> again inside of my container, <code>collectstatic</code> works just fine, but since Heroku doesn't persist files created after the build stage, they disappear when my app restarts.</p>
  <p>I found a few SO answers discussing how to get <code>collectstatic</code> to run inside a Dockerfile, but that's not my problem; my problem is that it does run, but the collected files don't show up in the container. Anyone have a clue what's going on?</p>
  <p><strong>UPDATE:</strong> <a href="https://stackoverflow.com/a/71982071/6514229">This answer</a> did the trick. My <code>docker-compose.yml</code> was overriding the changes made by <code>collectstatic</code> with this line:</p>
  <pre><code>        volumes:
              - .:/usr/src/app
  </code></pre>
  <p>If, like me, you want to keep the bind mount for ease of local development (so that you don't need to re-build each time), you can edit the command for the web service as follows:</p>
  <pre><code>        command: bash -c &quot;python -m manage collectstatic &amp;&amp; gunicorn --bind 0.0.0.0:$PORT myapp.wsgi&quot;
  </code></pre>
  <p>Note that the image would have run just fine as-is had I pushed it to Heroku (since Heroku doesn't use the <code>docker-compose.yml</code> file), so this was just a problem affecting containers I created on my local machine.</p>