在 Dockerfile 中设置别名不起作用:找不到命令

Ren*_*mas 3 python docker dockerfile

我的 Dockerfile 中有以下内容:

...
USER $user

# Set default python version to 3
RUN alias python=python3
RUN alias pip=pip3

WORKDIR /app

# Install local dependencies
RUN pip install --requirement requirements.txt --user
Run Code Online (Sandbox Code Playgroud)

构建图像时,我得到以下信息:

 Step 13/22 : RUN alias pip=pip3
 ---> Running in dc48c9c84c88
Removing intermediate container dc48c9c84c88
 ---> 6c7757ea2724
Step 14/22 : RUN pip install --requirement requirements.txt --user
 ---> Running in b829d6875998
/bin/sh: pip: command not found
Run Code Online (Sandbox Code Playgroud)

pip如果我在其上设置别名,为什么无法识别?

Ps:我不想.bashrc用于加载别名。

C.N*_*ivs 5

问题是别名只存在于图像中的中间层。请尝试以下操作:

FROM ubuntu

RUN apt-get update && apt-get install python3-pip -y

RUN alias python=python3
Run Code Online (Sandbox Code Playgroud)

在这里测试:

?mm92400?~/sample??? docker build . -t testimage
...
Successfully tagged testimage:latest

?mm92400?~/sample??? docker run -it testimage bash
root@78e4f3400ef4:/# python
bash: python: command not found
root@78e4f3400ef4:/#
Run Code Online (Sandbox Code Playgroud)

这是因为每一层都会启动一个新的 bash 会话,所以别名会在接下来的层中丢失。

为了保持一个稳定的别名,你可以像 python 在他们的官方镜像中那样使用符号链接:

FROM ubuntu

RUN apt-get update && apt-get install python3-pip -y

RUN alias python=python3
Run Code Online (Sandbox Code Playgroud)

注意使用python3-pip包来捆绑 pip。调用 时pip,最好使用python -m pip语法,因为它确保您调用的 pip 是与您安装的 python 相关的那个:

?mm92400?~/sample??? docker build . -t testimage
...
Successfully tagged testimage:latest

?mm92400?~/sample??? docker run -it testimage bash
root@78e4f3400ef4:/# python
bash: python: command not found
root@78e4f3400ef4:/#
Run Code Online (Sandbox Code Playgroud)