ModuleNotFoundError when using Dolfinx_MPC Docker Image

I am trying to use DOLFINx_MPC via Docker image, running

docker run -ti -v $(pwd):/root/shared -w /root/shared ghcr.io/jorgensd/dolfinx_mpc:v0.9.0

as suggested in the docs

My script starts with some imports

import numpy as np
from mpi4py import MPI
import gmsh
import ufl
from dolfinx import fem, io
import dolfinx.fem.petsc
from dolfinx.io.gmshio import model_to_mesh
import dolfinx_mpc.utils
from dolfinx_mpc import LinearProblem

and the next from last triggers the error

Traceback (most recent call last):
  File "/root/shared/periodic_elasticity.py", line 97, in <module>
    import dolfinx_mpc.utils
  File "/usr/local/dolfinx-real/lib/python3.12/dist-packages/dolfinx_mpc/utils/__init__.py", line 20, in <module>
    from .test import (
  File "/usr/local/dolfinx-real/lib/python3.12/dist-packages/dolfinx_mpc/utils/test.py", line 24, in <module>
    import pytest
ModuleNotFoundError: No module named 'pytest'

I installed pytest (and next, scipy) to get going, and it worked but I am wondering if I am doing anything wrong, I understand very little Docker, thanks

I’d watch a quick docker tutorial on youtube. In 30 minutes you’ll understand most of it.

Essentially, the docker image is a stand-alone linux machine with pre-specified installs. The image ghcr.io/jorgensd/dolfinx_mpc:v0.9.0 apparently does not have pytest installed.

You can manually install it when you have the container running, but that is not very useful; each time you create a new container it spins up a blank version of ghcr.io/jorgensd/dolfinx_mpc:v0.9.0 (that is precisely the point).

Luckily, though. It is incredibly easy to create your own container images building on top of others. In an empty folder, you’d create a Dockerfile looking something like:

FROM ghcr.io/jorgensd/dolfinx_mpc:v0.10.0

# Pip installs side-packages
RUN python3 -m pip install pytest
RUN python3 -m pip install scipy

# Working directory
WORKDIR /app

# Creates a non-root user with an explicit UID and adds permission to access the /app folder
RUN chown -R ubuntu /app
USER ubuntu

Or maybe with some additional supporting packages:

FROM ghcr.io/jorgensd/dolfinx_mpc:v0.10.0

# Pip installs side-packages
RUN python3 -m pip install pytest
RUN python3 -m pip install pytest-cov
RUN python3 -m pip install scipy

# Pip installs dolfinx packages
RUN python3 -m pip install adios4dolfinx
RUN python3 -m pip install scifem --no-build-isolation
RUN python3 -m pip install pyvista4dolfinx

# Default visualization variables
ENV XDG_RUNTIME_DIR=/tmp
ENV PYVISTA_OFF_SCREEN=true

# Working directory
WORKDIR /app

# Creates a non-root user with an explicit UID and adds permission to access the /app folder
RUN chown -R ubuntu /app
USER ubuntu

Then, with terminal command while in that empty folder you’d run docker build . -t <my_image_name>.
Then you can run with docker run -ti -v $(pwd):/app <my_image_name>