Docker Image Size Discrepancy: linux/amd64 6x Larger than linux/arm64
Problem Description
I’m building a Docker image for my Python Flask application on my Mac. I’ve noticed a significant size discrepancy between the linux/amd64
and linux/arm64
targeted images:
linux/amd64
image size: ~6GBlinux/arm64
image size: ~1GB
This 6x size difference persists even when building from Docker Cloud or GitHub, ruling out any local environment issues.
Environment
- Host: MacOS (Apple Silicon)
- Python version: 3.11
- Base image:
python:3.11-slim
Dockerfile
# Stage 1: Builder
FROM python:3.11-slim AS compile-image
ENV PYTHONDONTWRITEBYTECODE=1
ENV PYTHONUNBUFFERED=1
WORKDIR /app
# Copy requirements first for better cache utilization
COPY ./chatbot/chatbot_requirements.txt .
# Create virtual environment and install dependencies
RUN python -m venv /opt/venv
ENV PATH="/opt/venv/bin:$PATH"
RUN pip install --upgrade pip &&
pip install --no-cache-dir -r chatbot_requirements.txt &&
rm -rf /root/.cache/pip chatbot_requirements.txt
# Stage 2: Final
FROM python:3.11-slim AS runtime-image
ENV PYTHONDONTWRITEBYTECODE=1
ENV PYTHONUNBUFFERED=1
ENV PYTHONPATH="/app/:/app/chatbot/"
ENV PATH="/opt/venv/bin:$PATH"
# Create a non-root user
RUN useradd -m appuser
WORKDIR /app
# Copy the virtual environment from the builder stage
COPY --from=compile-image /opt/venv /opt/venv
# Copy application files
COPY --chown=appuser:appuser ./chatbot/ /app/chatbot/
COPY --chown=appuser:appuser .env .
# Switch to non-root user
USER appuser
EXPOSE 8000
CMD ["python", "chatbot/api/chatbot_endpoint.py"]
Requirements
python-dotenv
pydantic
pydantic-settings
pinecone
langchain
langchain-pinecone
langchain-openai
langchain-huggingface
langserve
langgraph
fastapi
uvicorn
sse_starlette
Questions
- Why is there such a significant size difference between the
linux/amd64
andlinux/arm64
images? - How can I reduce the size of the
linux/amd64
image to be closer to that of thelinux/arm64
image?
Attempted Solutions
- Tried building on different platforms (local Mac, Docker Cloud, GitHub) with the same results.
Any insights or suggestions would be greatly appreciated!
1