Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should I Compile My Application Inside of a Docker Image

Although most of the time I am developing Java apps and am simply using Maven so my builds should be reproducible (at least that's what Maven says).

But say you are compiling a C++ program or something a little more involved, should you build inside of docker?
Or ideally use vagrant or another technology to produce reproduce able builds.

How do you manage reproducible build with docker?

like image 673
Derrops Avatar asked Jan 17 '17 03:01

Derrops


People also ask

Should I build inside Docker?

Building inside a Dockerfile allows you to have all the tools and environment you need inside independently of your platform and ready to use. In a development perspective is easier to have all you need inside the container.

Should you Dockerize your app?

Docker may speed up your development process significantly, but not necessarily your app itself. Although it helps with making your application scalable, so more users will be able to use it, the single instance of your app will usually be just a hint slower than without Docker.

What goes inside a Docker image?

A Docker image contains application code, libraries, tools, dependencies and other files needed to make an application run. When a user runs an image, it can become one or many instances of a container. Docker images have multiple layers, each one originates from the previous layer but is different from it.

How do I run an image in a docker container?

To do so, we will use the docker run command (remember that from earlier?). Start your container using the docker run command and specify the name of the image we just created: Remember the -d and -p flags?

What is a dockerfile and how to create one?

A Dockerfile is a text document that contains the instructions to assemble a Docker image. When we tell Docker to build our image by executing the docker build command, Docker reads these instructions, executes them, and creates a Docker image as a result. Let’s walk through the process of creating a Dockerfile for our application.

Can I build a docker image without build tools?

You can, but not in your final image, as that would mean a much larger image than necessary: it would include all the compilation tool, instead of limiting to only what you need to execute the resulting binary. You can see an alternative in "How do I build a Docker image for a Ruby project without build tools?"

What does the Docker build command do?

The . at the end of the docker build command tells Docker that it should look for the Dockerfile in the current directory. Now that we have an image, let’s run the application. To do so, we will use the docker run command (remember that from earlier?).


2 Answers

You can, but not in your final image, as that would mean a much larger image than necessary: it would include all the compilation tool, instead of limiting to only what you need to execute the resulting binary.

You can see an alternative in "How do I build a Docker image for a Ruby project without build tools?"

  • I use an image to build,
  • I commit the resulting stopped container as a new image (with a volume including the resulting binary)
  • I use an execution image (one which only contain what you need to run), and copy the binary from the other image. I commit again the resulting container.

The final image includes the compiled binary and the execution environment.

like image 97
VonC Avatar answered Oct 13 '22 18:10

VonC


I wanted to post an answer to this as well actually because to build on VonC's answer. Actually I just had Redhat Openshift training and they use a tool called Source to Image s2i, which uses docker to create docker images. And actually this strategy is great for managing a private (or public) cloud, where your build may be compiled on different machines, but you need to keep the build environment consistent.

like image 27
Derrops Avatar answered Oct 13 '22 18:10

Derrops