containerit icon indicating copy to clipboard operation
containerit copied to clipboard

File not found using CMD_RScript to run the R script on start-up

Open natemiller opened this issue 4 years ago • 1 comments

I am trying set up a container based on a script and then have that script run on start-up using the CMD_RScript command. I can build the container, but when I try to run it I get the error No such file or directory referencing the .R script, which I thought was supposed to be copied to the working directory in the container. I've gone back and tried using the simple example in the containRit help, where I seem to get the same error. (at this point don't care about the script output, I'm just trying to see if the script is found).

# create simple script file
scriptFile <- tempfile(pattern = "containerit_", fileext = ".R")
writeLines(c('library(rgdal)',
             'nc <- rgdal::readOGR(system.file("shapes/", package="maptools"), "sids", verbose = FALSE)',
             'proj4string(nc) <- CRS("+proj=longlat +datum=NAD27")',
             'plot(nc)'), scriptFile)

# use a custom startup command
scriptCmd <- CMD_Rscript(basename(scriptFile))

# create Dockerfile for the script
dockerfile_object <- dockerfile(from = scriptFile, silent = TRUE, cmd = scriptCmd)
containerit::write(dockerfile_object, file = './Dockerfile')

Then at the terminal I set up a 'virtualbox'

docker-machine create  "virtualbox"
docker-machine env virtualbox
eval $(docker-machine env virtualbox)

And run
build -t testing:v1 .
which works... generates an image which I can see using docker image ls.
And then I run
run --rm testing:v1
which gives the error cannot open file 'containerit_ad95cd542a1.R': No such file or directory which references the image id.
Is the .R script not being copied to the container properly so that cmd = containerit_ad95cd542a1.R can be run at start up? Is it missing a component of the path to the script that isn't mentioned in the help?

natemiller avatar Aug 30 '19 19:08 natemiller

Thanks for reporting this issue! When I run the code above, I get a warning message:

> library("containerit")
> scriptFile <- tempfile(pattern = "containerit_", fileext = ".R")
> writeLines(c('library(rgdal)',
+              'nc <- rgdal::readOGR(system.file("shapes/", package="maptools"), "sids", verbose = FALSE)',
+              'proj4string(nc) <- CRS("+proj=longlat +datum=NAD27")',
+              'plot(nc)'), scriptFile)
> 
> scriptCmd <- CMD_Rscript(basename(scriptFile))
> dockerfile_object <- dockerfile(from = scriptFile, silent = TRUE, cmd = scriptCmd)
Warning message:
In dockerfileFromFile(file = from, dockerfile = the_dockerfile,  :
  The given file is not inside the working directory! This may lead to incorrect COPY instructions.

Do you not get that?

I think the devil might lie in you using a temporary directory and not defining the context of the build. In which directory do you execute docker build ?

If I move the working directory to where the file is, the warning disappears, but the file is still not copied because copy must be defined. Maybe this should change! Would you expect the script file you use to always be copied into the container? :thinking:

> setwd(dirname(scriptFile))
> dockerfile_object <- dockerfile(from = scriptFile, silent = TRUE, cmd = scriptCmd)
> print(dockerfile_object)
FROM rocker/r-ver:3.6.1
LABEL maintainer="daniel"
RUN export DEBIAN_FRONTEND=noninteractive; apt-get -y update \
  && apt-get install -y gdal-bin \
	libgdal-dev \
	libproj-dev
RUN ["install2.r", "lattice", "rgdal", "sp"]
WORKDIR /payload/
CMD ["R", "--vanilla", "-f", "containerit_7ff348b98936.R"]

At the moment you must explicitly set copy:

> dockerfile_object <- dockerfile(from = scriptFile, silent = TRUE, cmd = scriptCmd, copy = "script")
> containerit::write(dockerfile_object, file = './Dockerfile')
> print(dockerfile_object)
FROM rocker/r-ver:3.6.1
LABEL maintainer="daniel"
RUN export DEBIAN_FRONTEND=noninteractive; apt-get -y update \
  && apt-get install -y gdal-bin \
	libgdal-dev \
	libproj-dev
RUN ["install2.r", "lattice", "rgdal", "sp"]
WORKDIR /payload/
COPY ["containerit_7ff347aed43d.R", "containerit_7ff347aed43d.R"]
CMD ["R", "--vanilla", "-f", "containerit_7ff347aed43d.R"]
> getwd()
[1] "/tmp/RtmpmUFEE9"

Then I can build and run the container (defining the build context) both in the command line:

daniel@nuest:/tmp/RtmpmUFEE9$ docker build --tag testing:v1 .
Sending build context to Docker daemon  80.38kB
Step 1/7 : FROM rocker/r-ver:3.6.1
 ---> 47a23bad22d6
Step 2/7 : LABEL maintainer="daniel"
 ---> Using cache
 ---> ad975cc38ca3
Step 3/7 : RUN export DEBIAN_FRONTEND=noninteractive; apt-get -y update   && apt-get install -y gdal-bin libgdal-dev 	libproj-dev
 ---> Using cache
 ---> af0e87cf4f66
Step 4/7 : RUN ["install2.r", "lattice", "rgdal", "sp"]
 ---> Using cache
 ---> 4b7e2e73e073
Step 5/7 : WORKDIR /payload/
 ---> Using cache
 ---> b5dbfba240c8
Step 6/7 : COPY ["containerit_7ff347aed43d.R", "containerit_7ff347aed43d.R"]
 ---> 3e8b8c83bcf0
Step 7/7 : CMD ["R", "--vanilla", "-f", "containerit_7ff347aed43d.R"]
 ---> Running in f2da6c8a9f1a
Removing intermediate container f2da6c8a9f1a
 ---> 62fcfaa25e00
Successfully built 62fcfaa25e00
Successfully tagged testing:v1
daniel@nuest:/tmp/RtmpmUFEE9$ docker run --rm -it testing:v1

R version 3.6.1 (2019-07-05) -- "Action of the Toes"
Copyright (C) 2019 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.

> library(rgdal)
Loading required package: sp
rgdal: version: 1.4-4, (SVN revision 833)
 Geospatial Data Abstraction Library extensions to R successfully loaded
 Loaded GDAL runtime: GDAL 2.1.2, released 2016/10/24
 Path to GDAL shared files: /usr/share/gdal/2.1
 GDAL binary built with GEOS: TRUE 
 Loaded PROJ.4 runtime: Rel. 4.9.3, 15 August 2016, [PJ_VERSION: 493]
 Path to PROJ.4 shared files: (autodetected)
 Linking to sp version: 1.3-1 
> nc <- rgdal::readOGR(system.file("shapes/", package="maptools"), "sids", verbose = FALSE)
Error in rgdal::readOGR(system.file("shapes/", package = "maptools"),  : 
  empty name
Execution halted

And also with stevedore:

> client <- stevedore::docker_client()
Detected API version '1.40' is above max version '1.39'; downgrading
> client$image$build(context = getwd(), tag = "testing:stevedore")
Step 1/7 : FROM rocker/r-ver:3.6.1
 ---> 47a23bad22d6
Step 2/7 : LABEL maintainer="daniel"
 ---> Using cache
 ---> ad975cc38ca3
Step 3/7 : RUN export DEBIAN_FRONTEND=noninteractive; apt-get -y update   && apt-get install -y gdal-bin 	libgdal-dev 	libproj-dev
 ---> Using cache
 ---> af0e87cf4f66
Step 4/7 : RUN ["install2.r", "lattice", "rgdal", "sp"]
 ---> Using cache
 ---> 4b7e2e73e073
Step 5/7 : WORKDIR /payload/
 ---> Using cache
 ---> b5dbfba240c8
Step 6/7 : COPY ["containerit_7ff347aed43d.R", "containerit_7ff347aed43d.R"]
 ---> 372c303a5baf
Step 7/7 : CMD ["R", "--vanilla", "-f", "containerit_7ff347aed43d.R"]
 ---> Running in afa33b1a69bc
Removing intermediate container afa33b1a69bc
 ---> 1fef89152a70
Successfully built 1fef89152a70
Successfully tagged testing:stevedore
<docker_image>
  export()
  help(help_type = getOption("help_type"))
  history()
  id()
  inspect(reload = TRUE)
  labels(reload = TRUE)
  name()
  reload()
  remove(force = NULL, noprune = NULL)
  short_id()
  tag(repo, tag = NULL)
  tags(reload = TRUE)
  untag(repo_tag)
> client$container$run(image = "testing:stevedore", rm = TRUE)
O> 
O> R version 3.6.1 (2019-07-05) -- "Action of the Toes"
O> Copyright (C) 2019 The R Foundation for Statistical Computing
O> Platform: x86_64-pc-linux-gnu (64-bit)
O> 
O> R is free software and comes with ABSOLUTELY NO WARRANTY.
O> You are welcome to redistribute it under certain conditions.
O> Type 'license()' or 'licence()' for distribution details.
O> 
O> R is a collaborative project with many contributors.
O> Type 'contributors()' for more information and
O> 'citation()' on how to cite R or R packages in publications.
O> 
O> Type 'demo()' for some demos, 'help()' for on-line help, or
O> 'help.start()' for an HTML browser interface to help.
O> Type 'q()' to quit R.
O> 
O> > library(rgdal)
E> Loading required package: sp
E> rgdal: version: 1.4-4, (SVN revision 833)
E>  Geospatial Data Abstraction Library extensions to R successfully loaded
E>  Loaded GDAL runtime: GDAL 2.1.2, released 2016/10/24
E>  Path to GDAL shared files: /usr/share/gdal/2.1
E>  GDAL binary built with GEOS: TRUE 
E>  Loaded PROJ.4 runtime: Rel. 4.9.3, 15 August 2016, [PJ_VERSION: 493]
E>  Path to PROJ.4 shared files: (autodetected)
E>  Linking to sp version: 1.3-1 
O> > nc <- rgdal::readOGR(system.file("shapes/", package="maptools"), "sids", verbose = FALSE)
E> Error in rgdal::readOGR(system.file("shapes/", package = "maptools"),  : 
E>   empty name
E> Execution halted
Error: Command '' in image 'testing:stevedore' returned non-zero exit status 1
Loading required package: sp

rgdal: version: 1.4-4, (SVN revision 833)

 Geospatial Data Abstraction Library extensions to R successfully loaded

 Loaded GDAL runtime: GDAL 2.1.2, released 2016/10/24

 Path to GDAL shared files: /usr/share/gdal/2.1

 GDAL binary built with GEOS: TRUE 

 Loaded PROJ.4 runtime: Rel. 4.9.3, 15 August 2016, [PJ_VERSION: 493]

 Path to PROJ.4 shared files: (autodetected)

 Linking to sp version: 1.3-1 

Error in rgdal::readOGR(system.file("shapes/", package = "maptools"),  : 

  empty name

Execution halted

HTH.

nuest avatar Sep 10 '19 09:09 nuest