title: Dockerfile Multistage Python Poetry - install
body: <p>I'm trying to dockerize a simple script (a command line utility). Here is a <em>working</em> Dockerfile:</p>
  <pre><code>FROM python:3.9-slim
  
  ARG user=tweaker project=tex-tweak src=tex_tweak
  
  # Non-root user.
  RUN useradd -ms /bin/bash &quot;$user&quot;
  USER $user
  WORKDIR /home/$user
  ENV PATH=/home/$user/.local/bin:$PATH
  
  # Source and project files.
  RUN mkdir /home/$user/$project
  WORKDIR /home/$user/$project
  COPY $src ./$src/
  COPY pyproject.toml ./
  #COPY poetry.lock ./
  
  # Build.
  RUN pip install poetry --user &amp;&amp; \
      poetry config virtualenvs.create false &amp;&amp; \
      poetry install --no-dev &amp;&amp; \
      poetry build
  
  CMD [&quot;bash&quot;]
  </code></pre>
  <p>Curiously, this is enough: the target utility somehow gets installed into <code>.local/bin</code>; I can't see why.</p>
  <p><code>python:slim</code> image is 115MB; the resulting image is 174MB. Not unacceptable, but relatively bloated. Multistage build is in order, u suppose. Unfortunately, I can't see how I should move the essentials into the second stage. The idea of selective copying of the <code>.local/bin</code> and <code>.local/lib</code> does not look particularly safe or inviting. Or maybe this is the way?</p>
  <p>Or is it possible/advisable to <code>pip install &lt;target&gt;.whl</code> into the second stage?</p>