Last active
October 10, 2022 19:35
-
-
Save basilfx/f415b0cacb2fd60d83b6db72ffb078fd to your computer and use it in GitHub Desktop.
Emulated-compilation of Docker images for another target platform with Python packages from source
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
# Requires binfmt to support linux/arm64 (or any other target). | |
docker buildx build --platform linux/arm64 . |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# syntax=docker/dockerfile:experimental | |
# Compile all dependencies in a separate build environment. Note that the | |
# cache is copied to /cache as a final step, because the actual cache folder | |
# is an external mount. | |
FROM --platform=$TARGETPLATFORM python:3.8-alpine3.13 as buildenv | |
COPY requirements_base.txt requirements.txt ./ | |
RUN --mount=type=cache,target=/root/.cache \ | |
apk update && \ | |
apk add --upgrade \ | |
build-base \ | |
ca-certificates \ | |
curl \ | |
freetype-dev \ | |
g++ \ | |
jpeg-dev \ | |
lapack-dev \ | |
less \ | |
libexecinfo-dev \ | |
libgcc \ | |
libgfortran \ | |
libgomp \ | |
libpng-dev \ | |
make \ | |
musl \ | |
openblas-dev \ | |
openssl \ | |
rsync \ | |
wget \ | |
zlib-dev && \ | |
pip install --upgrade pip wheel && \ | |
pip install -r requirements_base.txt && \ | |
pip install -r requirements.txt && \ | |
cp -r /root/.cache /cache | |
# Build the final image. | |
FROM --platform=$TARGETPLATFORM python:3.8-alpine3.13 as image | |
COPY --from=buildenv /cache /root/.cache | |
COPY requirements_base.txt requirements.txt ./ | |
RUN apk update && \ | |
apk add --upgrade \ | |
lapack \ | |
libgomp \ | |
libjpeg \ | |
libstdc++ \ | |
openblas && \ | |
pip install --upgrade pip wheel && \ | |
pip install -r requirements_base.txt && \ | |
pip install -r requirements.txt && \ | |
rm -f requirements_base.txt && \ | |
rm -f requirements.txt && \ | |
rm -rf /root/.cache && \ | |
rm -rf /var/cache | |
COPY . /src | |
WORKDIR /src |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Yes that makes sense, I'm using emulation now and it works (but slow as you mentioned). Just wanted to add a note here in case anyone else is looking for the same thing, since this is the closest example I could find!