Answers: <p>You're trying to copy just the <code>/venv</code> directory, but that's not going to work. Take a look at the <em>contents</em> of that directory, for example:</p>
  <pre><code>root@52bdcc57abd8:/# python3 -m venv /venv
  root@52bdcc57abd8:/# ls -l /venv/bin/
  total 48
  -rw-r--r-- 1 root root 8834 Oct 22 03:52 Activate.ps1
  -rw-r--r-- 1 root root 1880 Oct 22 03:52 activate
  -rw-r--r-- 1 root root  829 Oct 22 03:52 activate.csh
  -rw-r--r-- 1 root root 1969 Oct 22 03:52 activate.fish
  -rwxr-xr-x 1 root root  222 Oct 22 03:52 pip
  -rwxr-xr-x 1 root root  222 Oct 22 03:52 pip3
  -rwxr-xr-x 1 root root  222 Oct 22 03:52 pip3.9
  lrwxrwxrwx 1 root root    7 Oct 22 03:51 python -&gt; python3
  lrwxrwxrwx 1 root root   22 Oct 22 03:51 python3 -&gt; /usr/local/bin/python3
  lrwxrwxrwx 1 root root    7 Oct 22 03:51 python3.9 -&gt; python3
  </code></pre>
  <p>As you can see <code>/venv/bin/python3</code> isn't actually a <em>file</em>, it' just a symlink to whichever version of Python was used to create the virtual environment.</p>
  <p>You won't get away with just copying the binary, either, because you need the full set of runtime files from <code>/usr/local/lib/python3.9</code>, as well as any shared libraries that are by required by Python.</p>
  <hr />
  <blockquote>
  <p>But when replacing base image with debian:11-slim everything works as expected.</p>
  </blockquote>
  <p>In this case, you're installing a compatible version of Python...but if you're going to do that, you might as well just stick with the <code>python:3.9-slim</code> image.</p>
  
Answers: CC BY-SA 4.0
Answers: 2022-10-23T19:38:38.303Z
Answers: Thanks, I forgot about symlinks - I have listed a directory too and `python` executable was there, that's why I was even more confused. That totally makes sense.
Answers: GopherM
Answers: CC BY-SA 4.0
Answers: 2022-10-22T03:57:24.830Z
Answers: 2022-10-22T03:57:24.830Z
Answers: larsks
Body: <p>This is my Dockerfile for distroless image similar to <a href="https://github.com/GoogleContainerTools/distroless/blob/main/examples/python3-requirements/Dockerfile" rel="nofollow noreferrer">this example</a></p>
  <pre><code>FROM python:3.9-slim AS build-venv
  RUN python3 -m venv /venv 
  # other installation steps go here
  RUN /venv/bin/pip install --upgrade pip setuptools wheel
  # installing from requirements.txt etc.
  
  # Copy the virtualenv into a distroless image
  FROM gcr.io/distroless/python3-debian11
  COPY --from=build-venv /venv /venv
  ENTRYPOINT [&quot;/venv/bin/python3&quot;]
  </code></pre>
  <p>I'm trying to just get into Python shell (with all the dependencies installed), but <code>docker run -it my-distroless</code> gives me this error</p>
  <pre><code>docker: Error response from daemon: failed to create shim task: 
  OCI runtime create failed: runc create failed: 
  unable to start container process: exec: &quot;/venv/bin/python3&quot;: 
  stat /venv/bin/python3: no such file or directory: unknown.
  </code></pre>
  <p>But when replacing base image with <code>debian:11-slim</code> everything works as expected.</p>
  <pre><code>FROM debian:11-slim AS build
  RUN apt-get update &amp;&amp; \
      apt-get install --no-install-suggests --no-install-recommends --yes python3-venv gcc libpython3-dev &amp;&amp; \
      python3 -m venv /venv
  # the rest is the same
  </code></pre>
  <p>Are there only &quot;compatible&quot; base images for <em>distroless</em> that I should use for my builds or what is the possible reason?</p>
  
Tags: <docker><dockerfile><python><images>
Title: Multistage docker build for Python distroless image
UserDisplayName: GopherM
ThreadText: # Multistage docker build for Python distroless image
  
  (Asked by: username_0 on 2022-10-21)
  
  This is my Dockerfile for distroless image similar to [this example](https://github.com/GoogleContainerTools/distroless/blob/main/examples/python3-requirements/Dockerfile)
  
  ```
  FROM python:3.9-slim AS build-venv
  RUN python3 -m venv /venv 
  # other installation steps go here
  RUN /venv/bin/pip install --upgrade pip setuptools wheel
  # installing from requirements.txt etc.
  
  # Copy the virtualenv into a distroless image
  FROM gcr.io/distroless/python3-debian11
  COPY --from=build-venv /venv /venv
  ENTRYPOINT ["/venv/bin/python3"]
  
  ```
  
  I'm trying to just get into Python shell (with all the dependencies installed), but `docker run -it my-distroless` gives me this error
  
  ```
  docker: Error response from daemon: failed to create shim task: 
  OCI runtime create failed: runc create failed: 
  unable to start container process: exec: "/venv/bin/python3": 
  stat /venv/bin/python3: no such file or directory: unknown.
  
  ```
  
  But when replacing base image with `debian:11-slim` everything works as expected.
  
  ```
  FROM debian:11-slim AS build
  RUN apt-get update && \
      apt-get install --no-install-suggests --no-install-recommends --yes python3-venv gcc libpython3-dev && \
      python3 -m venv /venv
  # the rest is the same
  
  ```
  
  Are there only "compatible" base images for *distroless* that I should use for my builds or what is the possible reason?
  
  **Tags:** docker, dockerfile, python, images
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2022-10-22. Score: 2 | Accepted answer
  
  You're trying to copy just the `/venv` directory, but that's not going to work. Take a look at the *contents* of that directory, for example:
  
  ```
  root@52bdcc57abd8:/# python3 -m venv /venv
  root@52bdcc57abd8:/# ls -l /venv/bin/
  total 48
  -rw-r--r-- 1 root root 8834 Oct 22 03:52 Activate.ps1
  -rw-r--r-- 1 root root 1880 Oct 22 03:52 activate
  -rw-r--r-- 1 root root  829 Oct 22 03:52 activate.csh
  -rw-r--r-- 1 root root 1969 Oct 22 03:52 activate.fish
  -rwxr-xr-x 1 root root  222 Oct 22 03:52 pip
  -rwxr-xr-x 1 root root  222 Oct 22 03:52 pip3
  -rwxr-xr-x 1 root root  222 Oct 22 03:52 pip3.9
  lrwxrwxrwx 1 root root    7 Oct 22 03:51 python -> python3
  lrwxrwxrwx 1 root root   22 Oct 22 03:51 python3 -> /usr/local/bin/python3
  lrwxrwxrwx 1 root root    7 Oct 22 03:51 python3.9 -> python3
  
  ```
  
  As you can see `/venv/bin/python3` isn't actually a *file*, it' just a symlink to whichever version of Python was used to create the virtual environment.
  
  You won't get away with just copying the binary, either, because you need the full set of runtime files from `/usr/local/lib/python3.9`, as well as any shared libraries that are by required by Python.
  
  ---
  
  > But when replacing base image with debian:11-slim everything works as expected.
  
  In this case, you're installing a compatible version of Python...but if you're going to do that, you might as well just stick with the `python:3.9-slim` image.
  
  > **Comments:**
  > **username_0**: Thanks, I forgot about symlinks - I have listed a directory too and `python` executable was there, that's why I was even more confused. That totally makes sense.  
  
  ---
  
  