'Is there a conda (mamba) equivalent to pip install's `--root` path option?

Background

I have a multi-stage Dockerfile for a JupyterLab image. There are three stages:

  • server
  • kernel: mamba create -y -n /tmp/kernel-packages/myenv ...
  • runner:
FROM ... as runner
...

COPY --from=kernel /tmp/kernel-packages/ /opt/conda/envs
RUN conda config --append envs_dirs /opt/conda/envs/

...

Problem

In the resulting image, the preferred python -m pip works, but pip gives:

bash: /opt/conda/envs/myenv/bin/pip: /tmp/kernel-packages/myenv/bin/python3.9: bad interpreter: No such file or directory

The reason is that pip has #!/tmp/kernel-packages/myenv/bin/python3.9 as its shebang.

Expected

A behaviour like pip install --root /tmp/server-packages ..., which works perfectly fine in a COPY --from=server /tmp/server-packages /.

Additional information

Additionally, some other binaries, like curl or papermill also have wrong paths hardcoded by conda. I've read about Anaconda | Moving Conda Environments, but it seems like overkill to use conda-pack and conda-unpack.

Workaround

Simply create an env by name: mamba create -y -n myenv and then in the runner stage COPY --from=kernel /opt/conda/envs /opt/conda/envs.

Question

Is there a Conda (Mamba) equivalent to pip install's --root option? I'm not looking for a solution to the stated problem as I have I have already found a way that works for me. My interest is purely in the functionality of the conda binary.



Solution 1:[1]

The --prefix argument is the equivalent - just that some Conda packages use hardcoded paths, hence the issue you encounter.

conda-prefix-replacement

To properly move a Conda environment to a new prefix via a COPY operation one would need to run the conda-prefix-replacement tool (a.k.a., cpr) to ensure that all files with hardcoded paths get updated to the new location. Presumably, conda-pack is doing a similar thing, just under-the-hood.

For your purposes, you might consider pre-running cpr on the environment(s) in the kernel image so that they are ready to work in the deployed location. Though that would mean always COPYing to the same location.

See the cpr repository for details on use.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1