Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use docker multistage builds for R development and deployment setting

Problem

  • For development, I want to use a Docker image with the RStudio IDE, which relatively heavy. I also need many packages for my project, so I create my own docker file that has the above referenced image in the FROM statement. Let's call this new Dockerfile Dokerfile.development.
  • For deployment, I want to use a base R image without IDE and as few dependencies as required for deployment, with the identical setup-up as in the development, but without an IDE, other development tools and dependencies required to run tests. Let us call the Dockerfile for deplyoment Dockerfile.deployment.

Candidate solutions

So now I see the following options to create these two images:

  • One inelegant way to do that is to c/p everything from Dokerfile.development into my Dockerfile.deployment, but use the r-base image in the FROM statement. Drawback: I always need to keep multiple Dockerfiles up to date. If I add another image for testing, I have 3 Dockerfiles with 99% overlap.
  • Another way is to first create a Dockerfile.deployment with all requirements for the deployment. Then, the development image is built on top of the deployment image. Dockerfile.development is more or less c/p the installation instructions in the Dockerfile for the RStudio image to add RStudio to the deployment image, but with the deplyoment image as base image. Drawback: I would not understand the code in my own Dockerfiles anymore.

  • Use multistage builds that make it easy to extract the built executables (or actually anything) from one image and use it into another one, without having to copy all the dependencies that were needed to build the executable. So my idea would be to extract the relevant files from the RStudio image into a new image that would be my deployment image.

I think the last option is preferred because it's the most modular solution and has the least duplication in the Dockerfiles and the lowest maintenance burden.

Question

  • my narrow question is: Is there a (single) executable I could extract from the build RStudio image and put it on top of my deployment image?

  • my more open question is: How are people handling the situation where the development image is the deployment image plus some other tools that are available as standalone images and they want to avoid duplication as shown in the two first solutions under Candidate solutions.

like image 888
Lorenz Walthert Avatar asked Oct 07 '19 12:10

Lorenz Walthert


People also ask

What is multistage build in Docker?

A multistage build allows you to use multiple images to build a final product. In a multistage build, you have a single Dockerfile, but can define multiple images inside it to help build the final image.

What benefits multi-stage builds enable?

Multistage builds let the developer automate the creation process of applications that require some amount of compilation. Developers can create versions that target different OS versions or any other process dependency, which is a big benefit of the approach.

What is the purpose of multi-stage builds Mcq?

To provide the network configuration for an application's network stack.


1 Answers

tl;dr: You cannot. In the way you ask, methinks. Maybe you can do it differently.

Longer version:

  • Don't take this as a glib answer.
  • I think it gets to the core of how we package source code
    • from taking source code from source repos
    • or taking binary aggreations, for example as .deb files
    • or taking binary meta-aggreations, here Docker layers
  • You realize that there are Docker layers containing components
  • If I read your request correctly, you desire "reversing" one layer
  • i.e. get the RStudio part out of the RStudio docker
  • I think you can't
  • Fundamentally, the RStudio Docker container just unpacks the .deb they ship
  • So you should reverse your whole stack
  • Start fron r-ver or r-base, add your particular development needs
  • Then add the RStudio .deb as we do in the RStudio container
  • (Legalese: We have explicit permission to redistribute this, I doubt this is transitive)

Hope this helps. Fill in any blanks where I got my assumptions wrong.

like image 95
Dirk Eddelbuettel Avatar answered Nov 15 '22 21:11

Dirk Eddelbuettel