Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Passing an ARG into CMD for the Dockerfile

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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?

>Solution :

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading