Skip to content

Instantly share code, notes, and snippets.

@a-recknagel
Created May 14, 2021 12:22
Show Gist options
  • Save a-recknagel/b055a41a7a0a942b644b2d66a8723c9a to your computer and use it in GitHub Desktop.
Save a-recknagel/b055a41a7a0a942b644b2d66a8723c9a to your computer and use it in GitHub Desktop.
If your workstation and your Dockerfile have different archs
FROM python:slim
COPY wheelhouse/* wheelhouse/
RUN pip install wheelhouse/*
#!/usr/bin/env bash
set -euf -o pipefail
# --------------------------------------------------------------------------- #
# Build a debian wheelhouse so that `docker build .` works #
# #
# This is only necessary if this project 1) has dependencies with #
# arch-specific requirements (the c-headers of numpy are a usual suspect) #
# and 2) it needs to be deployed with a debian based docker container. #
# In that case, local tests with docker need to build the right wheelhouse #
# first, which is what this script does. #
# --------------------------------------------------------------------------- #
# get location of project root
PROJECT_DIR="$(dirname "$(dirname "$(readlink -f "${0}")")")"
# store dockerfile in temp dir
echo "Creating temporary workspace and writing Dockerfile..."
TMP=$(mktemp -d)
trap "{ rm -rf ${TMP}; }" EXIT
cat << EOF > ${TMP}/Dockerfile
FROM python:slim
COPY src src
COPY README.rst .
COPY pyproject.toml .
RUN apt-get update && apt-get install -y gcc make
RUN pip install poetry && \
poetry lock && \
poetry build -f wheel && \
poetry export -f requirements.txt --without-hashes > requirements.txt && \
pip wheel -w wheels -r requirements.txt && \
mv dist/* wheels
EOF
# build image, run container, and copy wheelhouse to project root on host
echo "Building image..."
docker build -f ${TMP}/Dockerfile -t 'wheelhouse_builder' ${PROJECT_DIR}
echo "Running container..."
docker run --cidfile ${TMP}/wheelhouse.cid 'wheelhouse_builder'
echo "Cleaning up former wheelhouse and copying over new one from container..."
rm -fr ${PROJECT_DIR}/wheelhouse
docker cp $(cat ${TMP}/wheelhouse.cid):/wheels ${PROJECT_DIR}/wheelhouse
echo "Done."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment