Python [Errno 8] Exec format error: ‘/app/.venv/bin/python’

I have an error when building a Docker image. I didn’t have this error until a few days ago and I don’t really remember changing the Dockerfile, so I don’t know what happened. Also, I am using Poetry, running in the Dockerfile the following commands (following this other Integrating Python Poetry with Docker post):

source $HOME/.poetry/env && poetry export -f requirements.txt | /venv/bin/pip install -r /dev/stdin

source $HOME/.poetry/env && poetry build && /venv/bin/pip install dist/*.whl

I tried to repeat the same steps manually in a Docker container and I encountered the same error.

Whenever I run “poetry run” or “poetry build” I got this error:

[OSError]
[Errno 8] Exec format error: '/app/.venv/bin/python'

I don’t really know what to look at so I don’t even know what information share with you here. Let me know.

Thanks

Dockerfile ( I cut it on the line that causes the crash):

FROM python:3.7.4-slim-stretch AS builder

RUN apt-get update -qq && 
  apt-get install -y --no-install-recommends 
  build-essential 
  curl 
  git

ARG MY_ENV
ARG DOCKER_HOST

ENV MY_ENV=${MY_ENV} 
  PYTHONFAULTHANDLER=1 
  PYTHONUNBUFFERED=1 
  PYTHONHASHSEED=random 
  PIP_NO_CACHE_DIR=on 
  PIP_DISABLE_PIP_VERSION_CHECK=on 
  PIP_DEFAULT_TIMEOUT=100 
  POETRY_VERSION=1.0.0

RUN curl -sSL https://raw.githubusercontent.com/python-poetry/poetry/master/get-poetry.py | python /dev/stdin --version $POETRY_VERSION
RUN python -m venv /venv
RUN mkdir app
WORKDIR ./app
COPY pyproject.toml poetry.lock ./
RUN /bin/bash -c "source $HOME/.poetry/env && poetry export -f requirements.txt | /venv/bin/pip install -r /dev/stdin"
COPY . .
RUN /bin/bash -c "source $HOME/.poetry/env && poetry build && /venv/bin/pip install dist/*.whl"

pyproject.toml

[tool.poetry]
name = "cuppa-seeder"
version = "0.1.0"
description = "Python backend for the census app"
authors = ["Matteo Foglio <[email protected]>"]

[tool.poetry.dependencies]
python = "~3.7.4"
requests = "^2.23.0"
psycopg2-binary = "^2.8.4"

[tool.poetry.dev-dependencies]

[build-system]
requires = ["poetry>=0.12"]
build-backend = "poetry.masonry.api"

Source: StackOverflow