Docker container fails with "ModuleNotFoundError: No module named 'magic'" when importing python-magic library

Problem Overview:
I’m getting a “No module named ‘magic’” error when trying to run my Python application that uses the python-magic package inside a Docker container on Windows 10.

What I tried:
Locally, I fixed this by switching from python-magic to python-magic-bin in my requirements. But when I build and run the Docker image, the same ModuleNotFoundError comes back.

My Docker setup:

# Base image for the application
FROM python:3.11.0 as foundation

# Install system packages
RUN apt-get update && apt-get install -y \
    pdf-tools \
    ocr-engine \
    opengl-libs \
    file-magic \
    python3-libmagic

# Create application user
RUN groupadd --gid 1001 appuser && adduser --disabled-password --gecos '' --uid 1001 --gid 1001 appuser
WORKDIR /home/appuser
USER appuser

# Development environment
FROM foundation as development
USER root
COPY --chown=appuser:appuser ./deps.txt ./
RUN pip install --no-cache-dir -r deps.txt
COPY --chown=appuser:appuser ./app /home/appuser/app
COPY --chown=appuser:appuser ./tests /home/appuser/tests
COPY --chown=appuser:appuser config.env /home/appuser/config.env
USER appuser
CMD ["python", "-m", "uvicorn", "app.server:application", "--host", "0.0.0.0", "--port", "8000", "--reload", "--env-file", "/home/appuser/config.env"]

# Testing environment
FROM foundation as testing
COPY --chown=appuser:appuser ./deps.txt ./
RUN pip install --no-cache-dir -r deps.txt
USER root
COPY --chown=appuser:appuser ./app /home/appuser/app
COPY --chown=appuser:appuser ./tests /home/appuser/tests
RUN mkdir /home/appuser/.cache && chown appuser:appuser -R /home/appuser/.cache
RUN pip install pytest-runner
CMD ["pytest-runner"]
USER appuser

# Main stage
FROM development

Current situation:
Even after installing the system dependencies and switching Python packages, the magic module import still fails in the containerized environment. Has anyone encountered this before? What am I missing here?

Check your deps.txt file to make sure you’re actually using python-magic-bin instead of python-magic in the container build. I’ve seen cases where the local requirements file gets out of sync with what’s being copied into the Docker image. Also worth noting that the multi-stage build approach might be causing issues - you’re copying deps.txt multiple times across different stages. Try simplifying by consolidating the package installation into a single stage first to isolate the problem. Another thing to verify is that your base Python image architecture matches your host system, especially on Windows with Docker Desktop. Sometimes the magic library has different dependencies between x86 and ARM builds.

had this exact issue last week! the problem is your dockerfile is installing file-magic but you actually need libmagic1 and libmagic-dev packages. also remove python3-libmagic since it conflicts with pip installed python-magic. try this: RUN apt-get update && apt-get install -y libmagic1 libmagic-dev then stick with python-magic-bin in your requirements. worked for me on ubuntu containers

I ran into something similar a few months back and the issue was actually with the libmagic installation order. The problem is you’re installing the system packages as root but then switching to the appuser before installing the Python dependencies. Try moving your pip install step to run as root right after the system package installation, then switch to appuser for the application files. Also, you might need to explicitly install libmagic-dev instead of just file-magic and python3-libmagic. In my case, I had to add libmagic1 to the apt-get install line as well. The key thing is ensuring the Python magic library can find the underlying libmagic shared library, which sometimes gets missed when you install packages in the wrong order or with different user permissions.

windows docker can be tricky with magic libs. i’ve seen this happen when the container architecture doesnt match - try adding --platform=linux/amd64 to your docker build command. also your multi-stage setup might be losing the libmagic binaries between stages, consider flattening it temporarilly to test

The issue might be related to your multi-stage build setup where you’re not consistently carrying over the system dependencies between stages. When you use FROM foundation as development and then FROM development for the main stage, make sure the libmagic system libraries are properly inherited. I’ve found that sometimes the Python magic module gets installed correctly but can’t locate the underlying C libraries at runtime due to permission issues or missing library paths. Try adding ldconfig after your apt-get install step to refresh the library cache, and consider setting the LD_LIBRARY_PATH environment variable explicitly. Another approach that worked for me was installing everything in a single stage first to verify it works, then optimizing the multi-stage build afterwards. The user switching between root and appuser across different stages can sometimes cause library linking problems that aren’t immediately obvious during the build process.