Docker + R:为什么“保存”方法不起作用?

时间:2018-09-26 23:28:47

标签: r docker save

我有一个docker文件:

FROM rocker/verse:latest
RUN apt-get update 
RUN R -e "install.packages('TraMineR', repos = 'http://cran.us.r-project.org')"
RUN R -e "install.packages('ggthemes', repos = 'http://cran.us.r-project.org')"
RUN R -e "install.packages('ggplot2', repos = 'http://cran.us.r-project.org')"
RUN R -e "install.packages('Rcpp'            , repos = 'http://cran.us.r-project.org')"
RUN R -e "install.packages('data.table', repos = 'http://cran.us.r-project.org')"
RUN R -e "install.packages('randomForest', repos = 'http://cran.us.r-project.org')"
RUN R -e "install.packages('pROC'            , repos = 'http://cran.us.r-project.org')"
RUN R -e "install.packages('stringr', repos = 'http://cran.us.r-project.org')"
RUN R -e "install.packages('ggplot2', repos = 'http://cran.us.r-project.org')"
RUN R -e "install.packages('ggthemes', repos = 'http://cran.us.r-project.org')"
RUN R -e "install.packages('TraMineR', repos = 'http://cran.us.r-project.org')"
RUN R -e "install.packages('bipartite', repos = 'http://cran.us.r-project.org')"
RUN R -e "install.packages('tm'            , repos = 'http://cran.us.r-project.org')"
RUN R -e "install.packages('dummies', repos = 'http://cran.us.r-project.org')"
RUN R -e "install.packages('xgboost', repos = 'http://cran.us.r-project.org')"

RUN R -e "install.packages('SnowballC', repos = 'http://cran.us.r-project.org')"

我构建它,然后运行它并获得一个容器。在此容器中,我运行一个R文件并尝试保存一个对象:

NdelivIDexcessif<-15 ; 
save(NdelivIDexcessif, file="sauvegardeTest.rda")

这不会产生任何输出,因为我期望在当前目录上创建一个“ sauvegardeTest.rda”。

我想念什么?

1 个答案:

答案 0 :(得分:0)

可能是几种可能的事情之一,(在我看来)头两个是:

  1. Docker在容器停止时默认会破坏环境(例如,对文件系统的更改)。这可以提高安全性和代码清洁度,但它要求容器完成后,如果必须具有文件级持久性,则必须显式设置它。

    没有任何内容,找不到文件。

    root@myhost:/tmp/ltest# ll
    total 76
    drwxr-xr-x  2 root root  4096 Sep 26 19:08 ./
    drwxrwxrwt 35 root root 69632 Sep 26 19:08 ../
    
    
    root@myhost:/tmp/ltest# docker run \
      --rm -it rocker/tidyverse:3.3.3 \
      R -e "save(mtcars, file='mt.rda')"
    
    R version 3.3.3 (2017-03-06) -- "Another Canoe"
    Copyright (C) 2017 The R Foundation for Statistical Computing
    Platform: x86_64-pc-linux-gnu (64-bit)
    
    R is free software and comes with ABSOLUTELY NO WARRANTY.
    You are welcome to redistribute it under certain conditions.
    Type 'license()' or 'licence()' for distribution details.
    
    R is a collaborative project with many contributors.
    Type 'contributors()' for more information and
    'citation()' on how to cite R or R packages in publications.
    
    Type 'demo()' for some demos, 'help()' for on-line help, or
    'help.start()' for an HTML browser interface to help.
    Type 'q()' to quit R.
    
    > save(mtcars, file='mt.rda')
    >
    >
    
    root@myhost:/tmp/ltest# ll
    total 76
    drwxr-xr-x  2 root root  4096 Sep 26 19:08 ./
    drwxrwxrwt 35 root root 69632 Sep 26 19:08 ../
    

    但是,如果您明确设置卷或挂载点(https://docs.docker.com/storage/volumes/#choose-the--v-or---mount-flag),则可以实现持久性。

    root@myhost:/tmp/ltest# docker run \
      -v /tmp/ltest:/tmp/rtest/ \
      --rm -it rocker/tidyverse:3.3.3 \
      R -e "save(mtcars, file='/tmp/rtest/mt.rda')"
    
    R version 3.3.3 (2017-03-06) -- "Another Canoe"
    Copyright (C) 2017 The R Foundation for Statistical Computing
    Platform: x86_64-pc-linux-gnu (64-bit)
    
    R is free software and comes with ABSOLUTELY NO WARRANTY.
    You are welcome to redistribute it under certain conditions.
    Type 'license()' or 'licence()' for distribution details.
    
    R is a collaborative project with many contributors.
    Type 'contributors()' for more information and
    'citation()' on how to cite R or R packages in publications.
    
    Type 'demo()' for some demos, 'help()' for on-line help, or
    'help.start()' for an HTML browser interface to help.
    Type 'q()' to quit R.
    
    > save(mtcars, file='/tmp/rtest/mt.rda')
    >
    >
    
    root@franz:/tmp/ltest# ls -l
    total 4
    -rw-r--r-- 1 root root 1234 Sep 26 19:04 mt.rda
    
  2. 如果您已经通过某种机制安装它,请确保在save命令中设置路径,或者通过预先设置R进程的工作目录来设置路径:

    docker run \
      -v /tmp/rtest:/tmp/rtest/ \
      -w /tmp/rtest/ --rm -it \
      rocker/tidyverse:3.3.3 R -e "save(mtcars, file='mt.rda')"
    docker run \
      -v /tmp/rtest:/tmp/rtest/ \
      --rm -ti \
      rocker/tidyverse:3.3.3 R -e "save(mtcars, file='/tmp/rtest/mt.rda')"
    

请注意,目录在容器外部和内部的位置是唯一的;尽管您可以使它们相同,但是并不需要这样做。在此示例中,容器外部的 real 目录为/tmp/ltest/,内部目录的名称为/tmp/rtest/