<p>I am trying to create a custom Docker image based on Rocker using Dockerfile. In the Dockerfile I am pulling my own R package from a custom GitLab server using:</p>

<p><code>RUN R -e  "devtools::install_git('[custom gitlab server]', quiet = FALSE)"</code></p>

<p>Everything usually works, but I have noticed that when the GitLab server is down, or the machine running Docker is low on RAM memory, the package does not install correctly and returns an error message in the R console. This behavior is to be expected. However, Docker does not notice the error produced by R and continues evaluating the rest of the Dockerfile. I would like Docker to fail building the image when this occurs. In that way, I could ultimately prevent automatic deployment of the incomplete Docker container by Kubernetes.</p>

<p>So far I have thought of two potential solutions, but I am struggling with the execution:</p>

<ol>
<li><strong>R level</strong>: Wrap <code>tryCatch()</code> around <code>devtools::install_git</code> to catch the error. But then what? Use <code>stop</code>? Will this cause the Docker building process to stop as well? Could <code>withCallingHandlers()</code> be used?</li>
<li><strong>Dockerfile level</strong>: Use a shell command to check for errors? I cannot find the contents of <code>R --help</code> as I do not have a Linux machine at the moment. So I am not sure of what <code>R -e</code> actually does (execute I presume) and which other commands could be passed along with <code>R</code>.</li>
</ol>

<p>It seems that a similar issue is discussed <a href="https://github.com/bcgov/simple-R-shiny/issues/7" rel="noreferrer">here</a> and <a href="https://github.com/rocker-org/rocker/issues/24" rel="noreferrer">here</a>, but the I do not understand how they have solved it.</p>

<p>Thus how to make sure no Docker image ends up running on the Kubernetes cluster without the custom package?</p>

<p>The Docker build process should stop once one of the commands in the <code>Dockerfile</code> returns a non zero status.</p>

<p><code>install_git</code> doesn't seem to throw an error when the package wasn't installed successfully, so the execution keeps on.</p>

<p>An obvious way to go would be to wrap the installation inside a dedicated <code>R</code> script and throw an error if it didn't finish successfully, which would then stop the build.</p>

<p>So I would suggest something like this ...</p>

<p>Create installation script <code>install_gitlab.R</code>:</p>

<pre><code>### file install_gitlab.R

## change repo- and package name!!

repo &lt;- '[custom gitlab server]'

pkgname &lt;- 'testpackage'

devtools::install_git(repo, quiet = FALSE)

stopifnot(pkgname %in% installed.packages()[,'Package'])
</code></pre>

<p>Modify your <code>Dockerfile</code> accordingly (replace the <code>install_git</code> line):</p>

<pre><code>...

Add install_gitlab.R /runscripts/install_gitlab.R

RUN Rscript /runscripts/install_gitlab.R

...
</code></pre>

<p>One thing to keep in mind is, this approach assumes the package you're trying to install is <strong>NOT</strong> installed prior to calling the command.</p>
