Base class for container images to run functions in.
Do not construct this class directly; instead use one of its static factory methods, such as modal.Image.debian_slim, modal.Image.from_registry, or modal.Image.micromamba.
Factory methods
Image.debian_slim
modal.Image.debian_slim(
python_version: Optional[str] = None,
)
Default image, based on the official Debian slim image.
Python version to use (e.g., “3.11”, “3.12”). Defaults to the version you’re currently running locally.
Example:
image = modal.Image.debian_slim(python_version="3.11")
Image.from_registry
modal.Image.from_registry(
tag: str,
*,
secret: Optional[Secret] = None,
add_python: Optional[str] = None,
**kwargs,
)
Create an image from a public or private registry.
Full image tag (e.g., “ubuntu:22.04” or “nvidia/cuda:12.2.0-devel-ubuntu22.04”).
Optional secret for pulling from private registries.
Optionally install a Python version onto the image (e.g., “3.11”).
Example:
image = modal.Image.from_registry(
"nvidia/cuda:12.2.0-devel-ubuntu22.04",
add_python="3.11"
)
Image.micromamba
modal.Image.micromamba(
python_version: Optional[str] = None,
)
Create an image using micromamba for fast package installation.
Python version to use. Defaults to your current version.
Example:
image = modal.Image.micromamba(python_version="3.11")
Image.from_dockerfile
modal.Image.from_dockerfile(
path: Union[str, Path],
*,
context_dir: Optional[Union[str, Path]] = None,
ignore: Union[Sequence[str], Callable[[Path], bool]] = [],
)
Create an image from a Dockerfile.
Directory to use as build context. Defaults to the directory containing the Dockerfile.
ignore
Union[Sequence[str], Callable]
Files to ignore in the context (dockerignore syntax or callable).
Example:
image = modal.Image.from_dockerfile("./Dockerfile")
Image modification methods
image.pip_install
image.pip_install(
*packages: Union[str, list[str]],
find_links: Optional[str] = None,
index_url: Optional[str] = None,
extra_index_url: Optional[str] = None,
pre: bool = False,
extra_options: str = "",
force_build: bool = False,
env: Optional[dict[str, Optional[str]]] = None,
secrets: Optional[Collection[Secret]] = None,
gpu: GPU_T = None,
) -> Image
Install a list of Python packages using pip.
List of packages to install (e.g., “numpy”, “pandas>=1.0”).
Passes -f (—find-links) to pip install.
Passes -i (—index-url) to pip install.
Passes —extra-index-url to pip install.
Additional options to pass to pip install (e.g., “—no-build-isolation”).
Environment variables to set during build.
Secrets to inject during build.
Example:
image = (
modal.Image.debian_slim()
.pip_install("numpy", "pandas>=1.0")
)
image.pip_install_from_requirements
image.pip_install_from_requirements(
requirements_txt: str,
*,
find_links: Optional[str] = None,
index_url: Optional[str] = None,
extra_index_url: Optional[str] = None,
pre: bool = False,
extra_options: str = "",
force_build: bool = False,
env: Optional[dict[str, Optional[str]]] = None,
secrets: Optional[Collection[Secret]] = None,
gpu: GPU_T = None,
) -> Image
Install Python packages from a local requirements.txt file.
Path to requirements.txt file.
Example:
image = (
modal.Image.debian_slim()
.pip_install_from_requirements("requirements.txt")
)
image.run_commands
image.run_commands(
*commands: Union[str, list[str]],
force_build: bool = False,
secrets: Collection[Secret] = [],
gpu: GPU_T = None,
) -> Image
Run shell commands during image build.
Secrets to inject during build.
Example:
image = (
modal.Image.debian_slim()
.run_commands(
"apt-get update",
"apt-get install -y git"
)
)
image.add_local_file
image.add_local_file(
local_path: Union[str, Path],
remote_path: str,
*,
copy: bool = False,
) -> Image
Adds a local file to the image at remote_path within the container.
Absolute path in the container where file will be placed.
If True, copy into image layer at build time. If False, add on container startup.
Example:
image = (
modal.Image.debian_slim()
.add_local_file("./config.json", "/app/config.json")
)
image.add_local_dir
image.add_local_dir(
local_path: Union[str, Path],
remote_path: str,
*,
copy: bool = False,
ignore: Union[Sequence[str], Callable[[Path], bool]] = [],
) -> Image
Adds a local directory’s contents to the image at remote_path.
Local path to the directory.
Absolute path in the container.
If True, copy into image layer. If False, add on startup.
ignore
Union[Sequence[str], Callable]
Files to exclude (dockerignore syntax or callable).
Example:
image = (
modal.Image.debian_slim()
.add_local_dir("./src", "/app/src", ignore=["*.pyc", "__pycache__"])
)
image.add_local_python_source
image.add_local_python_source(
*modules: str,
copy: bool = False,
ignore: Union[Sequence[str], Callable[[Path], bool]] = NON_PYTHON_FILES,
) -> Image
Adds locally available Python packages/modules to containers.
Names of Python packages or modules to include.
If True, copy into image layer. If False, add on startup.
ignore
Union[Sequence[str], Callable]
Files to exclude. Defaults to non-Python files.
Example:
image = (
modal.Image.debian_slim()
.add_local_python_source("mypackage", "mymodule")
)
image.env
image.env(
env_dict: dict[str, str],
) -> Image
Set environment variables in the image.
Dictionary of environment variable name-value pairs.
Example:
image = (
modal.Image.debian_slim()
.env({"MY_VAR": "value", "DEBUG": "true"})
)
image.workdir
image.workdir(
path: str,
) -> Image
Set the working directory for subsequent commands and when the container starts.
Absolute path to use as working directory.
Example:
image = (
modal.Image.debian_slim()
.workdir("/app")
)