问题描述
我必须使用奇异性来打包我的代码以在学校服务器上运行。
我试图仅从python3.6-slim映像构建一个基本容器,然后在%post
部分中安装了python以取得良好的效果。
我也尝试了其他一些基础,例如debian buster和ubuntu 20.04。
当前,定义文件如下所示:
Bootstrap: docker
From: ubuntu:20.04
%post
# Downloads the latest package lists (important).
apt-get update -y
# Runs apt-get while ensuring that there are no user prompts that would
# cause the build process to hang.
apt-get update && apt-get install -y --no-install-recommends \
python3.6
每次生成映像后,我都会尝试运行python --version
,但始终坚持:python: not found
我觉得我在文档中遗漏了一些明显的内容,但是无法弄清楚我所缺少的是什么。如何在官方Python docker映像上构建的容器...未安装python?
解决方法
我尝试了以下定义文件:
Bootstrap: docker
From: ubuntu:20.04
%post
apt-get update
apt-get install -y --no-install-recommends python3
apt-get -y clean
rm -rf rm -rf /var/lib/apt/lists/*
,然后在外壳程序中运行以下命令:
sudo singularity build ubuntu-python.sif ubuntu-python.def
singularity run ubuntu-python.sif
并在容器中(结果添加到命令下方):
Singularity> python3 -V
Python 3.8.2
将我的工作示例与您的示例进行比较,我看到了几个问题:
- 您两次运行
apt-get update
-一次就足够 - 您尝试安装python3.6软件包-Ubuntu是基于Debian的发行版,就像Debian仅有一个
python3
软件包,而不是每个Python 3版本的单独软件包一样。 - 您运行
python --version
而非python3 --version
-在Ubuntupython
中是python2
的别名,有关更多信息,请参见here和{{3 }}。
您还提到了映像python3.6-slim
-没有这样的Docker映像(我假设您是在谈论Docker映像,因为稍后将使用Docker Ubuntu映像)。但是,有python:3.6-slim
张图片。这是我将它与奇点结合使用的快速测试:
singularity shell docker://python:3.6-slim
Singularity> python --version
Python 3.6.12
Singularity> python3 --version
Python 3.6.12
注意:我将singularity run
与基于Ubuntu的映像一起使用,因为其默认命令为bash
。对于python:3.6-slim
,我必须使用singularity shell
作为其默认命令来启动交互式Python Shell。
编辑:如果您正在使用已安装Python的映像,则应该从不手动安装Python-您很可能在同一个容器中以两个版本的Python 3结尾,导致难以调试的问题。