skip to Main Content

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.

2

Answers


  1. 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.

    Login or Signup to reply.
  2. 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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search