Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dockerfile failed to build #184

Open
jmanteau opened this issue Nov 11, 2024 · 5 comments
Open

Dockerfile failed to build #184

jmanteau opened this issue Nov 11, 2024 · 5 comments

Comments

@jmanteau
Copy link

on Mac OS 14.7 with podman, I failed to build the docker due to mitmproxy-rs requires a newer version of rustc ( [email protected] requires rustc 1.80
[email protected] requires rustc 1.80)

Building wheels for collected packages: aioquic, mitmproxy-rs, pylsqpack, pyperclip, ruamel-yaml-clib
  Building wheel for aioquic (pyproject.toml): started
  Building wheel for aioquic (pyproject.toml): finished with status 'done'
  Created wheel for aioquic: filename=aioquic-1.2.0-cp38-abi3-linux_aarch64.whl size=125235 sha256=2261884cdc281fed219293f53a204a0b096bbf513b0c7aaad478aa4606e8ef30
  Stored in directory: /tmp/pip-ephem-wheel-cache-bm1j_apq/wheels/c0/66/1d/0acd9f790b5e41f102fdb97cd4cfa1598840bf7eade5aef454
  Building wheel for mitmproxy-rs (pyproject.toml): started
  Building wheel for mitmproxy-rs (pyproject.toml): finished with status 'error'
  error: subprocess-exited-with-error

  × Building wheel for mitmproxy-rs (pyproject.toml) did not run successfully.
  │ exit code: 1
  ╰─> [14 lines of output]
      Running `maturin pep517 build-wheel -i /venv/bin/python --compatibility off`
      📦 Including license file "/tmp/pip-install-gt12w0ub/mitmproxy-rs_72815ce09aad49899835caf1bad1bd73/LICENSE"
      🍹 Building a mixed python/rust project
      🔗 Found pyo3 bindings with abi3 support for Python ≥ 3.10
      🐍 Not using a specific python interpreter
      📡 Using build options features from pyproject.toml
      error: rustc 1.78.0 is not supported by the following packages:
        [email protected] requires rustc 1.80
        [email protected] requires rustc 1.80

      💥 maturin failed
        Caused by: Failed to build a native library through cargo
        Caused by: Cargo build finished with "exit status: 101": `env -u CARGO CARGO_ENCODED_RUSTFLAGS="-C\u{1f}target-feature=-crt-static" PYO3_ENVIRONMENT_SIGNATURE="cpython-3.12-64bit" PYO3_PYTHON="/venv/bin/python" PYTHON_SYS_EXECUTABLE="/venv/bin/python" "cargo" "rustc" "--features" "pyo3/extension-module" "--message-format" "json-render-diagnostics" "--manifest-path" "/tmp/pip-install-gt12w0ub/mitmproxy-rs_72815ce09aad49899835caf1bad1bd73/mitmproxy-rs/Cargo.toml" "--release" "--lib" "--crate-type" "cdylib"`
      Error: command ['maturin', 'pep517', 'build-wheel', '-i', '/venv/bin/python', '--compatibility', 'off'] returned non-zero exit status 1
      [end of output]

I changed the dockerfile to get latest rust release and it worked fine afterward:

# Base stage for the final image
FROM python:3.12-alpine AS base
ENV PYTHONFAULTHANDLER=1 \
    PYTHONHASHSEED=random \
    PYTHONUNBUFFERED=1
RUN apk update && apk upgrade && \
    apk add --no-cache libgcc

# Builder stage for compiling dependencies
FROM python:3.12-alpine AS builder
ENV PIP_DEFAULT_TIMEOUT=100 \
    PIP_DISABLE_PIP_VERSION_CHECK=1 \
    PIP_NO_CACHE_DIR=1

WORKDIR /app

# Install build dependencies
RUN apk update && apk upgrade && \
    apk add --no-cache \
    gcc \
    libc-dev \
    libffi-dev \
    alpine-sdk \
    bsd-compat-headers \
    openssl-dev \
    python3-dev \
    curl

# Install rustup to get the latest Rust
RUN curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y && \
    source $HOME/.cargo/env && \
    rustup update stable && \
    rustup default stable

# Update PATH to include Rust binaries
ENV PATH="/root/.cargo/bin:${PATH}"

# Install Python dependencies
RUN python -m pip install --upgrade pip && \
    pip install poetry

# Create a virtual environment
RUN python -m venv /venv

COPY ["pyproject.toml", "./"]
COPY ["poetry.lock", "./"]
RUN poetry export -f requirements.txt | /venv/bin/pip install -r /dev/stdin

# Copy the source code and build the Python package
COPY . .
RUN poetry build && /venv/bin/pip install dist/*.whl

# Final stage with a minimal base
FROM base AS final
WORKDIR /app
COPY --from=builder /venv /venv
ENV PATH="/venv/bin:${PATH}"

ENTRYPOINT ["mitmproxy2swagger"]
@jmanteau jmanteau changed the title DockerFIle failed to build Dockerfile failed to build Nov 11, 2024
@alvarogonzalez-packlink

To make it easier, and keep using the Alpine repo, we could just pull cargo from the edge release:

FROM python:3.12-alpine AS base
ENV PYTHONFAULTHANDLER=1 \
    PYTHONHASHSEED=random \
    PYTHONUNBUFFERED=1
RUN apk update && \
    apk upgrade && \
    apk add --no-cache libgcc
FROM python:3.12-alpine AS builder
ENV PIP_DEFAULT_TIMEOUT=100 \
    PIP_DISABLE_PIP_VERSION_CHECK=1 \
    PIP_NO_CACHE_DIR=1
WORKDIR /app
RUN apk update && \
    apk upgrade && \
    echo "@edge https://dl-cdn.alpinelinux.org/alpine/edge/main" >> /etc/apk/repositories && \
    apk add cargo@edge && \
    apk add gcc libc-dev libffi-dev alpine-sdk bsd-compat-headers openssl-dev python3-dev && \
    python -m pip install --upgrade pip && \
    pip install poetry
RUN python -m venv /venv
COPY ["pyproject.toml", "./"]
COPY ["poetry.lock", "./"]
RUN poetry export -f requirements.txt | /venv/bin/pip install -r /dev/stdin
COPY . .
RUN poetry build && /venv/bin/pip install dist/*.whl

FROM base AS final
WORKDIR /app
COPY --from=builder /venv /venv
ENV PATH="/venv/bin:${PATH}"
# CMD [ "mitmproxy2swagger" ]

ENTRYPOINT [ "mitmproxy2swagger" ]

@alvarogonzalez-packlink

And now that I review it, mitm-proxy is being built on install because there aren't wheels available for muslbased images like Alpine. All the wheels available for Linux are for glibc.

So I've made a quick test and migrated the Dockerfile to use a Debian base image in combination with uv as a package manager. With that team, with no Docker Cache (just having the images already downloaded), the build has gone from taking around 2 minutes, to around 6 seconds. 🥳 Almost 20 times faster!

Of course, not all are advantages, the image gets a bit bigger. Not some crazy amount, from 160MB it grows to to 230MB. Still smaller than the mitmproxy Docker image 😇

This is it, just in case you wanna test it:

FROM python:3.12-slim-bookworm AS builder
COPY --from=ghcr.io/astral-sh/uv:latest /uv /uvx /bin/
ENV UV_HTTP_TIMEOUT=100 \
    UV_NO_CACHE=1
WORKDIR /app
RUN uv pip install --system poetry poetry-plugin-export
COPY pyproject.toml poetry.lock ./
RUN uv venv /venv && \
    poetry config warnings.export false && \
    poetry export -f requirements.txt -o requirements.txt && \
    VIRTUAL_ENV=/venv uv pip install -r requirements.txt
COPY . .
RUN poetry build && \
    VIRTUAL_ENV=/venv uv pip install dist/*.whl

FROM python:3.12-slim-bookworm AS final
ENV PYTHONFAULTHANDLER=1 \
    PYTHONHASHSEED=random \
    PYTHONUNBUFFERED=1
WORKDIR /app
COPY --from=builder /venv /venv
ENV PATH="/venv/bin:${PATH}"

ENTRYPOINT [ "mitmproxy2swagger" ]

@jmanteau
Copy link
Author

Indeed that's a good idea ! I didn't wanted to have a drastic change of behaviour in my initial proposal but I fully support your rework. It also works flawlessly on my side !

@alufers
Copy link
Owner

alufers commented Dec 1, 2024

Thanks guys for the feedback.

@alvarogonzalez-packlink Is it okay if I put your Dockerfile in the project?

@alvarogonzalez-packlink
Copy link

No prob, all yours!

If you want it as a PR just let me know, but feel free of just copy-pasting it as you want 😊

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants