Python script file missing in Singularity image

huangapple go评论67阅读模式
英文:

Python script file missing in Singularity image

问题

在AWS中,创建了一个包含Python脚本以打印字符串的Docker镜像(basicprint.py)。

Docker文件:

FROM python

COPY ./basicprint.py ./
CMD ["python", "basicprint.py"]

它正常工作,然后将Docker镜像保存为.tgz文件。

将该.tgz文件复制到本地。

我使用以下命令将Docker镜像(.tgz)转换为Singularity镜像:

singularity build sing_image.sif docker-archive://filename.tgz

成功创建了sing_image.sif

然后运行:

singularity run sing_image.sif

它报错:basicprint.py: No such file or directory

有关正确的转换方法而不会丢失文件的建议。

英文:

In AWS, created a docker image with a python script to print a string(basicprint.py)

docker file:

FROM python

COPY ./basicprint.py ./
CMD ["python", "basicprint.py"]

It works fine then saved docker image as .tgz file.

copy that .tgz file in to my local.

I converted docker image(.tgz) into singularity image by using

singularity build sing_image.sif docker-archive://filename.tgz

it was successfully created sing_image.sif

singularity run sing_image.sif

It throws an error : basicprint.py: No such file or directory

Any suggestions on correct method of conversion without missing the file.

答案1

得分: 2

请注意,您将文件放在了根目录下。singularity 在转换和清理过程中删除了您的文件已经有类似的问题报告,一般情况下,这是在使用容器时预期发生的

这稍微修改的镜像

FROM python:3.11-slim
COPY ./basicprint.py ./
CMD ["ls"]

演示了文件位于Linux根目录下:

(base) maciej.skorski@kaggle-cpu-maciej:~/docker-debug$ docker build . --tag test-sing
(base) maciej.skorski@kaggle-cpu-maciej:~/docker-debug$ docker run -it test-sing
root@ed8e3545a00b:/# ls
basicprint.py  bin  boot  dev  etc  home  lib  lib64  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var

现在,让我演示工作版本。重要的是,遵循最佳的Docker实践并使用工作目录

# Dockerfile
FROM python:3.11-slim-bullseye

RUN mkdir -p /usr/src
WORKDIR /usr/src
COPY ./basicprint.py /usr/src/

CMD ["python", "/usr/src/basicprint.py"]

其中示例Python脚本是

# basicprint.py
print('Welcome to my Docker!')

然后我在Debian GNU/Linux 10上使用Docker 20.10.17Singularity 3.11进行构建和测试:

(base) maciej.skorski@kaggle-cpu-maciej:~/docker-debug$ singularity --version
singularity-ce version 3.11.0+277-gcd6fa5c0d

(base) maciej.skorski@kaggle-cpu-maciej:~/docker-debug$ sudo docker build . --tag test-sing:latest
Successfully tagged test-sing:latest

(base) maciej.skorski@kaggle-cpu-maciej:~/docker-debug$ docker save test-sing:latest --output test-sing.tgz

(base) maciej.skorski@kaggle-cpu-maciej:~/docker-debug$ sudo singularity build test-sing.sif docker-archive:test-sing.tgz
...
INFO:    Build complete: test-sing.sif
(base) maciej.skorski@kaggle-cpu-maciej:~/docker-debug$ sudo singularity run test-sing.sif
Welcome to my Docker!

在相同的软件环境下,我重现了您的错误。所以我认为它已经解决了!如果您需要进一步的帮助,我们可以设置一个共享的虚拟机,使用GitHub Codespace来使其完全可复制。

英文:

Note that you put your file under the very root. singularity removed your file during conversion and cleaning. Similar issues have been reported and are, in general, expected when working with containers.

This slightly modified image

FROM python:3.11-slim
COPY ./basicprint.py ./
CMD ["ls"]

demonstrates that the file lands under the linux root:

(base) maciej.skorski@kaggle-cpu-maciej:~/docker-debug$ docker build . --tag test-sing
(base) maciej.skorski@kaggle-cpu-maciej:~/docker-debug$ docker run -it test-sing
root@ed8e3545a00b:/# ls
basicprint.py  bin  boot  dev  etc  home  lib  lib64  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var

Now, let me demonstrate the working version. Importantly, follow best Docker practices and use the working dir

# Dockerfile
FROM python:3.11-slim-bullseye

RUN mkdir -p /usr/src
WORKDIR /usr/src
COPY ./basicprint.py /usr/src/

CMD ["python", "/usr/src/basicprint.py"]

where the sample Python script is

# basicprint.py
print('Welcome to my Docker!')

Then I build and test on Debian GNU/Linux 10 with Docker 20.10.17 and Singularity 3.11:

(base) maciej.skorski@kaggle-cpu-maciej:~/docker-debug$ singularity --version
singularity-ce version 3.11.0+277-gcd6fa5c0d

(base) maciej.skorski@kaggle-cpu-maciej:~/docker-debug$ sudo docker build . --tag test-sing:latest
Successfully tagged test-sing:latest

(base) maciej.skorski@kaggle-cpu-maciej:~/docker-debug$ docker save test-sing:latest --output test-sing.tgz

(base) maciej.skorski@kaggle-cpu-maciej:~/docker-debug$ sudo singularity build test-sing.sif docker-archive:test-sing.tgz
...
INFO:    Build complete: test-sing.sif
(base) maciej.skorski@kaggle-cpu-maciej:~/docker-debug$ sudo singularity run test-sing.sif
Welcome to my Docker!

Under the same software, I reproduced your error. So I think it has been solved! If you need any further assistance, we can set up a shared Virtual Machine with GitHub Codespace to make it fully reproducible.

答案2

得分: 1

以下是翻译好的部分:

用于测试,您可以使用 def 文件,使用 %file 代替 Dockerfile:

> %files 部分允许您以比使用 %setup 部分更安全的方式将文件复制到容器中。

在您的情况下,创建一个名为 Singularity.def 的文件,其中包含:

Bootstrap: docker
Registry: docker.io
Namespace: library
From: python

%files
    basicprint.py /opt/

%runscript
    exec python /opt/basicprint.py

检查该方法是否有效:

singularity build sing_image.sif Singularity.def
singularity run sing_image.sif

关于您的 Dockerfile,请尝试使用不同的文件夹,再次用于测试:

FROM python

COPY ./basicprint.py /app/
CMD ["python", "/app/basicprint.py"]

然后:

docker build -t my-python-image .
docker save my-python-image:latest | gzip > my-python-image.tgz
singularity build sing_image.sif docker-archive://my-python-image.tgz
singularity run sing_image.sif

还要检查 .sif 文件是否包含您的文件:singularity exec sing_image.sif ls /app

英文:

For testing, you could use a def file, using %file instead of the Dockerfile:

> The %files section allows you to copy files into the container with greater safety than using the %setup section.

In your case, create a Singularity.def file with:

Bootstrap: docker
Registry: docker.io
Namespace: library
From: python

%files
    basicprint.py /opt/

%runscript
    exec python /opt/basicprint.py

Check if that approach is working with:

singularity build sing_image.sif Singularity.def
singularity run sing_image.sif

Regarding your Dockerfile, try and use a different folder, again for testing:

FROM python

COPY ./basicprint.py /app/
CMD ["python", "/app/basicprint.py"]

Then:

docker build -t my-python-image .
docker save my-python-image:latest | gzip > my-python-image.tgz
singularity build sing_image.sif docker-archive://my-python-image.tgz
singularity run sing_image.sif

Check also the .sif file includes your file: singularity exec sing_image.sif ls /app.

huangapple
  • 本文由 发表于 2023年5月10日 15:03:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/76215725.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定