The following Dockerfile does work for me. All iris.R does is output a PNG file upon execution of the R code (if you wish to see it, it's at Generating a png file using ggsave() and Docker).
Dockerfile
FROM r-base
# Set an environment variable at runtime
ENV MAINDIR /home
# Define the date (YYYY-MM-DD) at build time
ARG WHEN
# Define the files at build time
ARG INPUTDIR
ARG INPUTFILENAME
# Execute R from the terminal
RUN R -e "options(repos = \
list(CRAN = 'http:/mran.revolutionanalytics.com/snapshot/${WHEN}')); \
install.packages('ggplot2')"
# Create a path in the container
RUN mkdir -p ${MAINDIR}
# Copy the file to a path in the container
COPY ./${INPUTDIR}/iris.R ${MAINDIR}/iris.R
# Set working directory
WORKDIR ${MAINDIR}
# Run iris.R in the container
CMD ["Rscript", "iris.R"]
with the following commands executed:
$ docker build --build-arg WHEN=2022-12-10 --build-arg INPUTDIR=Code --build-arg INPUTFILENAME=iris.R -t plot .
$ docker run -v [output directory]:/home/ plot
Saving 7 x 7 in image
`geom_smooth()` using formula = 'y ~ x'
However, one will notice that INPUTFILENAME
is not being used; ideally, I would like it to just take in what is being brought into docker build
.
The following DOES NOT work and is what I would like fixed:
Dockerfile
FROM r-base
# Set an environment variable at runtime
ENV MAINDIR /home
# Define the date (YYYY-MM-DD) at build time
ARG WHEN
# Define the files at build time
ARG INPUTDIR
ARG INPUTFILENAME
# Execute R from the terminal
RUN R -e "options(repos = \
list(CRAN = 'http:/mran.revolutionanalytics.com/snapshot/${WHEN}')); \
install.packages('ggplot2')"
# Create a path in the container
RUN mkdir -p ${MAINDIR}
# Copy the file to a path in the container
COPY ./${INPUTDIR}/${INPUTFILENAME} ${MAINDIR}/${INPUTFILENAME}
# Set working directory
WORKDIR ${MAINDIR}
# Run iris.R in the container
CMD ["sh", "-c", "Rscript $INPUTFILENAME"]
Docker CLI Commands Executed
$ docker build --build-arg WHEN=2022-12-10 --build-arg INPUTDIR=Code --build-arg INPUTFILENAME=iris.R -t plot .
$ docker run -v [output directory]:/home/ plot
This just prints a bunch of documentation on how RScript should be used, which suggests that this code is wrong.
How do I correct the Dockerfile to allow for use of INPUTFILENAME
?
CodePudding user response:
Build-time args set an environment variable during build time. By default, they are not kept after building. If you want that built-time argument to carry over into run-time, then you need to use the ENV
command. Example:
FROM r-base
ARG INPUTFILENAME
ENV INPUTFILENAME=$INPUTFILENAME
CMD ["sh", "-c", "echo $INPUTFILENAME"]
You can build this with docker build --build-arg INPUTFILENAME=iris.R -t plot .
, and run it with docker run -it plot