Reputation: 849
The idea is to run a R script in a docker container. The R script works fine. Here is a piece from this R script. The R sript create the file alpha.csv. The script doesn*t start. If I run the script by hand from the root dir Rscript /home/script/master.R I get an error message.
The error message
Error in file(file, ifelse(append, "a", "w")) :
cannot open the connection
Calls: write.csv -> eval.parent -> eval -> eval -> write.table -> file
In addition: Warning message:
In file(file, ifelse(append, "a", "w")) :
cannot open file '../output/alpha.csv': No such file or directory
Execution halted
I copyy the script to /home/master.r in the container
Here is my dockerfile
From rocker/r-base:latest
# Create directories
RUN mkdir -p home/data home/output home/script
# Copy files
COPY /src/data/test.csv /home/data/test.csv
COPY /src/master.R /home/script/master.R
COPY /src/install_packages.R /home/script/install_packages.R
# Install R-packages
RUN Rscript /home/script/install_packages.R
# Run the script
CMD Rscript /home/script/master.R
The second problem is, that I need groff. So I tried this:
install.packages('groff', dependencies = TRUE, repos='http://cran.us.r-project.org')
Te error message
Installing package into ‘/usr/local/lib/R/site-library’
(as ‘lib’ is unspecified)
Warning message:
package ‘groff’ is not available (for R version 3.6.1)
Installing package into ‘/usr/local/lib/R/site-library’
(as ‘lib’ is unspecified)
Warning message:
package ‘pandoc’ is not available (for R version 3.6.1)
How do I run the container.? I tried this:
docker run -it --rm test
Upvotes: 1
Views: 1649
Reputation: 5076
Concerning the first issue, you should set the WORKDIR directive to /home/script, concerning groff, I'm not aware of any R package with that name but I'm familiar with the command itself and I think you want to have that installed.
This should be the resulting Dockerfile:
FROM rocker/r-base:latest
RUN apt-get update \
&& apt-get install -yq --no-install-recommends groff \
&& rm -rf /var/lib/apt/lists/*
# Create directories
RUN mkdir -p /home/data /home/output /home/script
WORKDIR /home/script
# Install R-packages
COPY ./src/install_packages.R /home/script/install_packages.R
RUN Rscript /home/script/install_packages.R
# Copy data
COPY ./src/data/test.csv /home/data/test.csv
COPY /src/master.R /home/script/master.R
# Run the script
CMD Rscript /home/script/master.R
Concerning the Dockerfile writing, I'd suggest you check https://docs.docker.com/develop/develop-images/dockerfile_best-practices/
If your data.csv file changes often, I suggest to avoid copying it in the docker image but to mount the folder when the container starts. I suppose you want to access the output files once you're done with the execution, then I suppose you should mount the output folder as well. You can take this commands as examples:
docker build --tag newtest .
docker run \
-it --rm \
-v "$(pwd)/src/data/:/home/data/" \
-v "$(pwd)/src/output/:/home/output/" \
newtest
Upvotes: 2