Docker 容器内的 Micromamba

ant*_*ton 3 docker micromamba

我有一个基本的 Docker 镜像:

FROM ubuntu:21.04

WORKDIR /app

RUN apt-get update && apt-get install -y wget bzip2 \
    && wget -qO-  https://micromamba.snakepit.net/api/micromamba/linux-64/latest | tar -xvj bin/micromamba \
    && touch /root/.bashrc \
    && ./bin/micromamba shell init -s bash -p /opt/conda  \
    && cp /root/.bashrc /opt/conda/bashrc \
    && apt-get clean autoremove --yes \
    && rm -rf /var/lib/{apt,dpkg,cache,log}

SHELL ["bash", "-l" ,"-c"]
Run Code Online (Sandbox Code Playgroud)

并从中派生出另一个:

ARG BASE
FROM $BASE

RUN source /opt/conda/bashrc && micromamba activate \
    && micromamba create --file environment.yaml -p /env
Run Code Online (Sandbox Code Playgroud)

在构建第二个映像时,我收到以下错误:micromamba: command not found对于 RUN 部分。

  1. 如果我手动运行第一个基本映像,我可以启动 micromamba,它运行正常
  2. 我可以运行为第二个映像构建创建的临时映像,micromamba 可通过 CLI 获得,并且运行正确。
  3. 例如,如果我继承 debian:buster 或 alpine,那么它的构建就非常完美。

Ubuntu有什么问题吗?micromamba为什么在第二个 Docker 镜像构建过程中看不到?

PS使用脚手架进行构建,因此可以正确理解它在哪里$BASE以及它是什么。

Wil*_*ltz 10

ubuntu:21.04 映像附带一个/root/.bashrc以以下内容开头的文件:

# ~/.bashrc: executed by bash(1) for non-login shells.
# see /usr/share/doc/bash/examples/startup-files (in the package bash-doc)
# for examples

# If not running interactively, don't do anything
[ -z "$PS1" ] && return
Run Code Online (Sandbox Code Playgroud)

当第二个 Dockerfile 执行时RUN source /opt/conda/bashrc,PS1 未设置,因此 bashrc 文件的其余部分不会执行。bashrc 文件的其余部分是进行初始化的地方,包括用于激活 micromamba 环境的 bash 函数micromamba的设置。micromamba

debian:buster 镜像较小/root/.bashrc,没有类似的行[ -z "$PS1" ] && return,因此该micromamba函数被加载。

alpine 图像没有附带 a /root/.bashrc,因此它也不包含提前退出文件的代码。

如果你想使用 ubuntu:21.04 镜像,你可以先修改 Dockerfile,如下所示:

FROM ubuntu:21.04

WORKDIR /app

RUN apt-get update && apt-get install -y wget bzip2 \
    && wget -qO-  https://micromamba.snakepit.net/api/micromamba/linux-64/latest | tar -xvj bin/micromamba \
    && touch /root/.bashrc \
    && ./bin/micromamba shell init -s bash -p /opt/conda  \
    && grep -v '[ -z "\$PS1" ] && return' /root/.bashrc  > /opt/conda/bashrc   # this line has been modified \
    && apt-get clean autoremove --yes \
    && rm -rf /var/lib/{apt,dpkg,cache,log}

SHELL ["bash", "-l" ,"-c"]
Run Code Online (Sandbox Code Playgroud)

这将删除导致提前终止的一行。

或者,您可以使用现有的mambaorg/micromamba docker 镜像。它mambaorg/micromamba:latest基于 debian:slim,但mambaorg/micromamba:jammy会给你一个基于 ubuntu 的镜像(披露:我维护这个镜像)。