首页 > 解决方案 > 为 R 脚本运行 Singularity 容器时出错

问题描述

我正在构建一个 Singularity 容器,以使用 LidR 软件包运行自定义 R 脚本以进行树分割。

我已经这样编写了奇点定义文件:

Bootstrap: docker
From: ubuntu:20.04

%setup
    touch test.R
    touch treeSeg_dalponte2016.R
    touch /home/ljeasson/R/x86_64-pc-linux-gnu-library/3.6/rgdal/libs/rgdal.so
    
%files
    test.R
    treeSeg_dalponte2016.R
    /home/ljeasson/R/x86_64-pc-linux-gnu-library/3.6/rgdal/libs/rgdal.so

%post
    # Disable interactivity, including region and time zone
    export DEBIAN_FRONTEND="noninteractive"
    export DEBCONF_NONINTERACTIVE_SEEN=true
        
    # Update apt and install necessary libraries and repositories
    apt update
    
    apt install -y build-essential r-base-core software-properties-common dirmngr apt-transport-https lsb-release ca-certificates
    
    add-apt-repository ppa:ubuntugis/ubuntugis-unstable
    
    apt install -y libgdal-dev libgeos++-dev libudunits2-dev libproj-dev libx11-dev libgl1-mesa-dev libglu1-mesa-dev libfreetype6-dev libnode-dev libxt-dev libfftw3-dev
    
    apt clean
    
    
    # Install necessary R packages and dependencies
    R -e "install.packages('lidR', dependencies = TRUE)"
    R -e "install.packages('raster', dependencies = TRUE)"
    R -e "install.packages('sf', dependencies = TRUE)"
    R -e "install.packages('dplyr', dependencies = TRUE)"
    
    R -e "install.packages('rgdal', dependencies = TRUE, repos='https://cran.rstudio.com', configure.args=c('--with-gdal-config=/opt/conda/bin/gdal-config', '--with-proj-include=/opt/conda/include', '--with-proj-lib=/opt/conda/lib', '--with-proj-share=/opt/conda/share/proj/'))"
            
    R -e "install.packages('gdalUtils', dependencies = TRUE, repos='https://cran.rstudio.com')"
    
    
%test
    #!/bin/bash
    R --version
    Rscript test.R
    

%runscript
    #!/bin/sh
    echo "Arguments received: $*"
    Rscript treeSeg_dalponte2016.R $*

并使用构建容器singularity build ga_container.sif ga_container.def

容器构建没有错误,但是当容器使用运行时./ga_container <arguments>,总是会出现这个错误:

Error: package or namespace load failed for 'rgdal' in dyn.load(file, DLLpath = DLLpath, ...):
 unable to load shared object '/home/ljeasson/R/x86_64-pc-linux-gnu-library/3.6/rgdal/libs/rgdal.so':
  libgdal.so.26: cannot open shared object file: No such file or directory
Execution halted

我知道发生错误是因为它找不到 Rgdal 的图像,即使我似乎已在%setupand%files部分中附加到容器:

%setup
    touch test.R
    touch treeSeg_dalponte2016.R
    touch /home/ljeasson/R/x86_64-pc-linux-gnu-library/3.6/rgdal/libs/rgdal.so

%files
    test.R
    treeSeg_dalponte2016.R
    /home/ljeasson/R/x86_64-pc-linux-gnu-library/3.6/rgdal/libs/rgdal.so

如果错误来自不正确的文件附件,我如何确保将 Rgdal(和其他类似库)正确附加到 Singularity 容器中?

提前致谢

标签: rrgdalsingularity-container

解决方案


This looks like an environmental issue causing the image to look at your locally installed R modules instead of using the ones installed in the image. Perhaps in your .Rprofile or R_LIBS/R_LIBS_USER. Try running with singularity run --cleanenv ..., or temporarily moving your .Rprofile if you have one, and see if that fixes it. If not, I have a few other observations.


First, the %setup block is creating root owned, empty files on the host OS if they don't exist already. An empty .so file would certainly cause problems. For the majority of cases you don't want to use %setup, as it directly modifies the host as root during sudo singularity build.

In the %files block you are copying the (potentially root owned/empty) to a path in the image that matches your home directory. Your $HOME is automatically mounted when you run/exec/shell an image, which will hide any files in the image at that location. When adding files to an image, you should always put them in a place they are unlikely to get clobbered by a mount. /opt/myapp or something similar usually works well. Additionally, test.R and treeSeg_dalponte2016.R are copied to /test.R and /treeSeg_dalponte2016.R inside the container, but relative paths are used in %runscript and %test. Singularity run/exec will attempt to run from the first path that exists in the container: $PWD (implicitly mounted, but this can fail silently), then $HOME (also implicitly mounted and can fail silently), then /. You can use singularity --verbose run ... to see if anything isn't being mounted correctly and add echo $PWD to %runscript to see where it's running from.

In %post when you install the rgdal package, you specify several paths with /opt/conda/... but conda is not installed or configured in the image. I'm not familiar with rgdal, so don't know if that would cause problems or not though.


推荐阅读